View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit;
use Drupal\comment\CommentInterface;
use Drupal\eck\EckEntityInterface;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\preprocess_event_dispatcher\Event\CommentPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\EckEntityPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\NodePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ParagraphPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\TaxonomyTermPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Service\PreprocessEventService;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\EntityMockFactory;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\SpyEventDispatcher;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader;
use PHPUnit\Framework\TestCase;
use function key;
use function next;
use function reset;
final class EntityEventTest extends TestCase {
private $service;
private $dispatcher;
public function setUp() : void {
$loader = YamlDefinitionsLoader::getInstance();
$this->dispatcher = new SpyEventDispatcher();
$this->service = new PreprocessEventService($this->dispatcher, $loader
->getMapper());
}
public function testCommentEvent() : void {
$variables = [
'comment' => EntityMockFactory::getMock(CommentInterface::class, 'comment', 'bundle', 'view_mode'),
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(CommentPreprocessEvent::class, $variables);
}
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);
}
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);
}
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);
}
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);
}
private function createAndAssertEntityEvent(string $class, array $variables) : void {
$this->dispatcher
->setExpectedEventCount(3);
$this->service
->createAndDispatchKnownEvents($class::getHook(), $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::assertNotNull($firstEvent
->getVariables());
$secondEvent = next($events);
$secondName = key($events);
$secondVariables = $secondEvent
->getVariables();
$bundle = $secondVariables
->getEntityBundle();
$expectedName .= '.' . $bundle;
self::assertSame($expectedName, $secondName);
self::assertInstanceOf($class, $secondEvent);
$thirdEvent = next($events);
$thirdName = key($events);
$thirdVariables = $thirdEvent
->getVariables();
$viewMode = $thirdVariables
->getViewMode();
$expectedName .= '.' . $viewMode;
self::assertSame($expectedName, $thirdName);
self::assertInstanceOf($class, $thirdEvent);
}
}