View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Preprocess;
use Drupal\hook_event_dispatcher\Event\Preprocess\CommentPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\EckEntityPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\NodePreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\ParagraphPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\TaxonomyTermPreprocessEvent;
use Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEventVariables;
use Drupal\hook_event_dispatcher\Service\PreprocessEventService;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\EntityMock;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\SpyEventDispatcher;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\YamlDefinitionsLoader;
use Drupal\Tests\UnitTestCase;
final class EntityEventTest extends UnitTestCase {
private $service;
private $dispatcher;
private $variables;
public function setUp() {
$loader = YamlDefinitionsLoader::getInstance();
$this->dispatcher = new SpyEventDispatcher();
$this->service = new PreprocessEventService($this->dispatcher, $loader
->getMapper());
$this->variables = [];
}
public function testCommentEvent() {
$this->variables = [
'comment' => new EntityMock('comment', 'bundle', 'view_mode'),
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(CommentPreprocessEvent::class);
}
public function testEckEntityEvent() {
$this->variables = [
'elements' => [
'#view_mode' => 'view_mode',
],
'eck_entity' => new EntityMock('eck_entity', 'bundle', 'view_mode'),
'theme_hook_original' => 'eck_entity',
'bundle' => 'bundle',
];
$this
->createAndAssertEntityEvent(EckEntityPreprocessEvent::class);
}
public function testNodeEvent() {
$this->variables = [
'node' => new EntityMock('node', 'bundle', 'view_mode'),
'theme_hook_original' => 'node',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(NodePreprocessEvent::class);
}
public function testParagraphEvent() {
$this->variables = [
'paragraph' => new EntityMock('paragraph', 'bundle', 'view_mode'),
'theme_hook_original' => 'paragraph',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(ParagraphPreprocessEvent::class);
}
public function testTaxonomyTermEvent() {
$this->variables = [
'term' => new EntityMock('taxonomy_term', 'bundle', 'view_mode'),
'theme_hook_original' => 'taxonomy_term',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(TaxonomyTermPreprocessEvent::class);
}
private function createAndAssertEntityEvent($class) {
$this->dispatcher
->setExpectedEventCount(3);
$this->service
->createAndDispatchKnownEvents($class::getHook(), $this->variables);
$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::assertInstanceOf(AbstractEventVariables::class, $firstEvent
->getVariables());
$secondEvent = \next($events);
$secondName = \key($events);
$bundle = $secondEvent
->getVariables()
->getEntityBundle();
self::assertNotNull($bundle);
self::assertInternalType('string', $bundle);
$expectedName .= '.' . $bundle;
self::assertSame($expectedName, $secondName);
self::assertInstanceOf($class, $secondEvent);
self::assertInstanceOf(AbstractEventVariables::class, $secondEvent
->getVariables());
$thirdEvent = \next($events);
$thirdName = \key($events);
$viewMode = $thirdEvent
->getVariables()
->getViewMode();
self::assertNotNull($viewMode);
self::assertInternalType('string', $viewMode);
$expectedName .= '.' . $viewMode;
self::assertSame($expectedName, $thirdName);
self::assertInstanceOf($class, $thirdEvent);
self::assertInstanceOf(AbstractEventVariables::class, $thirdEvent
->getVariables());
}
}