final class EntityEventTest in Hook Event Dispatcher 3.x
Same name in this branch
- 3.x modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php \Drupal\Tests\preprocess_event_dispatcher\Unit\EntityEventTest
- 3.x modules/core_event_dispatcher/tests/src/Unit/Entity/EntityEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityEventTest
Same name and namespace in other branches
- 8.2 modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php \Drupal\Tests\preprocess_event_dispatcher\Unit\EntityEventTest
Class EntityEventTest.
@group preprocess_event_dispatcher
Testing all lots of classes gives expected coupling warnings.
Plugin annotation
@SuppressWarnings(PHPMD . CouplingBetweenObjects);
Hierarchy
- class \Drupal\Tests\preprocess_event_dispatcher\Unit\EntityEventTest extends \PHPUnit\Framework\TestCase
Expanded class hierarchy of EntityEventTest
File
- modules/
preprocess_event_dispatcher/ tests/ src/ Unit/ EntityEventTest.php, line 32
Namespace
Drupal\Tests\preprocess_event_dispatcher\UnitView source
final class EntityEventTest extends TestCase {
/**
* PreprocessEventService.
*
* @var \Drupal\preprocess_event_dispatcher\Service\PreprocessEventService
* PreprocessEventService.
*/
private $service;
/**
* SpyEventDispatcher.
*
* @var \Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\SpyEventDispatcher
* SpyEventDispatcher
*/
private $dispatcher;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$loader = YamlDefinitionsLoader::getInstance();
$this->dispatcher = new SpyEventDispatcher();
$this->service = new PreprocessEventService($this->dispatcher, $loader
->getMapper());
}
/**
* Test a BlockPreprocessEvent.
*/
public function testCommentEvent() : void {
$variables = [
'comment' => EntityMockFactory::getMock(CommentInterface::class, 'comment', 'bundle', 'view_mode'),
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(CommentPreprocessEvent::class, $variables);
}
/**
* Test a EckEntityPreprocessEvent.
*/
public function testEckEntityEvent() : void {
$variables = [
'elements' => [
'#view_mode' => 'view_mode',
],
'eck_entity' => EntityMockFactory::getMock(EckEntityInterface::class, 'eck_entity', 'bundle', 'view_mode'),
'theme_hook_original' => 'eck_entity',
'bundle' => 'bundle',
];
$this
->createAndAssertEntityEvent(EckEntityPreprocessEvent::class, $variables);
}
/**
* Test a NodePreprocessEvent.
*/
public function testNodeEvent() : void {
$variables = [
'node' => EntityMockFactory::getMock(NodeInterface::class, 'node', 'bundle', 'view_mode'),
'theme_hook_original' => 'node',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(NodePreprocessEvent::class, $variables);
}
/**
* Test a ParagraphPreprocessEvent.
*/
public function testParagraphEvent() : void {
$variables = [
'paragraph' => EntityMockFactory::getMock(ParagraphInterface::class, 'paragraph', 'bundle', 'view_mode'),
'theme_hook_original' => 'paragraph',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(ParagraphPreprocessEvent::class, $variables);
}
/**
* Test a TaxonomyTermPreprocessEvent.
*/
public function testTaxonomyTermEvent() : void {
$variables = [
'term' => EntityMockFactory::getMock(TermInterface::class, 'taxonomy_term', 'bundle', 'view_mode'),
'theme_hook_original' => 'taxonomy_term',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(TaxonomyTermPreprocessEvent::class, $variables);
}
/**
* Create and assert the given entity event class.
*
* @param string $class
* Event class name.
* @param array $variables
* Variables.
*/
private function createAndAssertEntityEvent(string $class, array $variables) : void {
$this->dispatcher
->setExpectedEventCount(3);
/** @var \Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEntityEvent $class */
$this->service
->createAndDispatchKnownEvents($class::getHook(), $variables);
/** @var \Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEntityEvent[] $events */
$events = $this->dispatcher
->getEvents();
$expectedName = $class::DISPATCH_NAME_PREFIX . $class::getHook();
$firstEvent = reset($events);
$firstName = key($events);
self::assertSame($expectedName, $firstName);
self::assertInstanceOf($class, $firstEvent);
self::assertNotNull($firstEvent
->getVariables());
$secondEvent = next($events);
$secondName = key($events);
/** @var \Drupal\preprocess_event_dispatcher\Variables\AbstractEntityEventVariables $secondVariables */
$secondVariables = $secondEvent
->getVariables();
$bundle = $secondVariables
->getEntityBundle();
$expectedName .= '.' . $bundle;
self::assertSame($expectedName, $secondName);
self::assertInstanceOf($class, $secondEvent);
$thirdEvent = next($events);
$thirdName = key($events);
/** @var \Drupal\preprocess_event_dispatcher\Variables\AbstractEntityEventVariables $thirdVariables */
$thirdVariables = $thirdEvent
->getVariables();
$viewMode = $thirdVariables
->getViewMode();
$expectedName .= '.' . $viewMode;
self::assertSame($expectedName, $thirdName);
self::assertInstanceOf($class, $thirdEvent);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityEventTest:: |
private | property | SpyEventDispatcher. | |
EntityEventTest:: |
private | property | PreprocessEventService. | |
EntityEventTest:: |
private | function | Create and assert the given entity event class. | |
EntityEventTest:: |
public | function | ||
EntityEventTest:: |
public | function | Test a BlockPreprocessEvent. | |
EntityEventTest:: |
public | function | Test a EckEntityPreprocessEvent. | |
EntityEventTest:: |
public | function | Test a NodePreprocessEvent. | |
EntityEventTest:: |
public | function | Test a ParagraphPreprocessEvent. | |
EntityEventTest:: |
public | function | Test a TaxonomyTermPreprocessEvent. |