final class EntityEventVariablesTest in Hook Event Dispatcher 8
Class EntityEventVariablesTest.
@group hook_event_dispatcher
Testing all variables gives expected PHPMD warnings.
Plugin annotation
@SuppressWarnings(PHPMD . CouplingBetweenObjects);
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\EntityEventVariablesTest
Expanded class hierarchy of EntityEventVariablesTest
File
- tests/
src/ Unit/ Preprocess/ EntityEventVariablesTest.php, line 30
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\PreprocessView source
final class EntityEventVariablesTest extends UnitTestCase {
/**
* Factory mapper.
*
* @var \Drupal\hook_event_dispatcher\Service\PreprocessEventFactoryMapper
*/
private $mapper;
/**
* {@inheritdoc}
*/
public function setUp() {
$this->mapper = YamlDefinitionsLoader::getInstance()
->getMapper();
}
/**
* Test a CommentPreprocessEvent.
*/
public function testCommentEvent() {
$comment = new EntityMock('comment', 'comment_bundle', 'comment_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['comment'] = $comment;
$variablesArray['commented_entity'] = 'node';
$variablesArray['view_mode'] = $comment
->getViewMode();
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\CommentEventVariables $variables */
$variables = $this
->getVariablesFromCreatedEvent(CommentPreprocessEvent::class, $variablesArray);
self::assertInstanceOf(CommentEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $comment);
$this
->assertSame($comment, $variables
->getComment());
$this
->assertSame('node', $variables
->getCommentedEntity());
}
/**
* Test a EckEntityPreprocessEvent.
*/
public function testEckEntityEvent() {
$eckEntity = new EntityMock('eck_entity', 'eck_entity_bundle', 'eck_entity_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['eck_entity'] = $eckEntity;
$variablesArray['elements'] = [
'#view_mode' => $eckEntity
->getViewMode(),
];
$variablesArray['theme_hook_original'] = $eckEntity
->getEntityType();
$variablesArray['bundle'] = $eckEntity
->bundle();
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\EckEntityEventVariables $variables */
$variables = $this
->getVariablesFromCreatedEvent(EckEntityPreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(EckEntityEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $eckEntity);
$this
->assertSame($eckEntity, $variables
->getEckEntity());
}
/**
* Test a NodePreprocessEvent.
*/
public function testNodeEvent() {
$node = new EntityMock('node', 'node_bundle', 'node_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['node'] = $node;
$variablesArray['theme_hook_original'] = $node
->getEntityType();
$variablesArray['view_mode'] = $node
->getViewMode();
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\NodeEventVariables $variables */
$variables = $this
->getVariablesFromCreatedEvent(NodePreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(NodeEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $node);
$this
->assertSame($node, $variables
->getNode());
}
/**
* Test a ParagraphPreprocessEvent.
*/
public function testParagraphEvent() {
$paragraph = new EntityMock('paragraph', 'paragraph_bundle', 'paragraph_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['paragraph'] = $paragraph;
$variablesArray['theme_hook_original'] = $paragraph
->getEntityType();
$variablesArray['view_mode'] = $paragraph
->getViewMode();
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\ParagraphEventVariables $variables */
$variables = $this
->getVariablesFromCreatedEvent(ParagraphPreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(ParagraphEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $paragraph);
$this
->assertSame($paragraph, $variables
->getParagraph());
}
/**
* Test a TaxonomyTermPreprocessEvent.
*/
public function testTaxonomyTermEvent() {
$term = new EntityMock('taxonomy_term', 'term_bundle', 'term_view_mode');
$variablesArray = $this
->createVariablesArray();
$variablesArray['term'] = $term;
$variablesArray['theme_hook_original'] = $term
->getEntityType();
$variablesArray['view_mode'] = $term
->getViewMode();
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\TaxonomyTermEventVariables $variables */
$variables = $this
->getVariablesFromCreatedEvent(TaxonomyTermPreprocessEvent::class, $variablesArray);
$this
->assertInstanceOf(TaxonomyTermEventVariables::class, $variables);
$this
->assertAbstractEntityEventVariables($variables, $term);
$this
->assertSame($term, $variables
->getTerm());
}
/**
* Test the default entity event variable methods.
*
* @param \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEntityEventVariables $variables
* Variables object.
* @param \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\EntityMock $entity
* Entity mock.
*/
private function assertAbstractEntityEventVariables(AbstractEntityEventVariables $variables, EntityMock $entity) {
$this
->assertAbstractEventVariables($variables);
$this
->assertSame($entity, $variables
->getEntity());
$this
->assertSame($entity
->getEntityType(), $variables
->getEntityType());
$this
->assertSame($entity
->bundle(), $variables
->getEntityBundle());
$this
->assertSame($entity
->getViewMode(), $variables
->getViewMode());
}
/**
* Test the default event variable methods.
*
* @param \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEntityEventVariables $variables
* Variables object.
*/
private function assertAbstractEventVariables(AbstractEntityEventVariables $variables) {
$this
->assertEquals('success', $variables
->get('test'));
$this
->assertEquals('default', $variables
->get('test2', 'default'));
$reference =& $variables
->getByReference('reference');
$this
->assertEquals('first', $reference);
$reference = 'second';
$this
->assertEquals('second', $variables
->get('reference'));
$variables
->set('test3', 'new set');
$this
->assertEquals('new set', $variables
->get('test3'));
$variables
->remove('test');
$this
->assertNull($variables
->get('test'));
}
/**
* Get the variables from the created event.
*
* @param string $class
* Event class name.
* @param array $variablesArray
* Variables array.
*
* @return \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEntityEventVariables
* Variables object.
*/
private function getVariablesFromCreatedEvent($class, array $variablesArray) {
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEventInterface $class */
$hook = $class::getHook();
$this
->assertEquals(AbstractPreprocessEvent::DISPATCH_NAME_PREFIX . $hook, $class::name());
$factory = $this->mapper
->getFactory($hook);
$this
->assertEquals($hook, $factory
->getEventHook());
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEntityEventInterface $event*/
$event = $factory
->createEvent($variablesArray);
$this
->assertInstanceOf(PreprocessEntityEventInterface::class, $event);
$this
->assertInstanceOf($class, $event);
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEntityEventVariables $variables */
$variables = $event
->getVariables();
$this
->assertInstanceOf(AbstractEntityEventVariables::class, $variables);
return $variables;
}
/**
* Create the variables array.
*
* @return array
* Variables array.
*/
private function createVariablesArray() {
return [
'test' => 'success',
'reference' => 'first',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityEventVariablesTest:: |
private | property | Factory mapper. | |
EntityEventVariablesTest:: |
private | function | Test the default entity event variable methods. | |
EntityEventVariablesTest:: |
private | function | Test the default event variable methods. | |
EntityEventVariablesTest:: |
private | function | Create the variables array. | |
EntityEventVariablesTest:: |
private | function | Get the variables from the created event. | |
EntityEventVariablesTest:: |
public | function |
Overrides UnitTestCase:: |
|
EntityEventVariablesTest:: |
public | function | Test a CommentPreprocessEvent. | |
EntityEventVariablesTest:: |
public | function | Test a EckEntityPreprocessEvent. | |
EntityEventVariablesTest:: |
public | function | Test a NodePreprocessEvent. | |
EntityEventVariablesTest:: |
public | function | Test a ParagraphPreprocessEvent. | |
EntityEventVariablesTest:: |
public | function | Test a TaxonomyTermPreprocessEvent. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |