OtherEventTest.php in Hook Event Dispatcher 8.2
File
modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventTest.php
View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit;
use Drupal\preprocess_event_dispatcher\Event\BlockPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\FieldPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\FormPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\HtmlPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ImagePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\PagePreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\StatusMessagesPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ViewFieldPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Event\ViewPreprocessEvent;
use Drupal\preprocess_event_dispatcher\Service\PreprocessEventService;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\SpyEventDispatcher;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader;
use PHPUnit\Framework\TestCase;
use function str_replace;
final class OtherEventTest extends TestCase {
private $service;
private $dispatcher;
private $variables;
public function setUp() : void {
$loader = YamlDefinitionsLoader::getInstance();
$this->dispatcher = new SpyEventDispatcher();
$this->service = new PreprocessEventService($this->dispatcher, $loader
->getMapper());
$this->variables = [];
}
public function testBlockEvent() : void {
$this
->createAndAssertEvent(BlockPreprocessEvent::class);
}
public function testFieldEvent() : void {
$this
->createAndAssertEvent(FieldPreprocessEvent::class);
}
public function testFormEvent() : void {
$this
->createAndAssertEvent(FormPreprocessEvent::class);
}
public function testHtmlEvent() : void {
$this
->createAndAssertEvent(HtmlPreprocessEvent::class);
}
public function testImageEvent() : void {
$this
->createAndAssertEvent(ImagePreprocessEvent::class);
}
public function testPageEvent() : void {
$this
->createAndAssertEvent(PagePreprocessEvent::class);
}
public function testViewFieldEvent() : void {
$this
->createAndAssertEvent(ViewFieldPreprocessEvent::class);
}
public function testViewEvent() : void {
$this
->createAndAssertEvent(ViewPreprocessEvent::class);
}
public function testStatusMessagesEvent() : void {
$this
->createAndAssertEvent(StatusMessagesPreprocessEvent::class);
}
public function testNotMappingEvent() : void {
$this->service
->createAndDispatchKnownEvents('NoneExistingHook', $this->variables);
self::assertSame([], $this->dispatcher
->getEvents());
}
private function createAndAssertEvent(string $class) : void {
$this->service
->createAndDispatchKnownEvents($class::getHook(), $this->variables);
self::assertSame($class::name(), $this->dispatcher
->getLastEventName());
$event = $this->dispatcher
->getLastEvent();
self::assertInstanceOf($class, $event);
$variablesClass = str_replace([
'\\Event\\',
'PreprocessEvent',
], [
'\\Variables\\',
'EventVariables',
], $class);
self::assertInstanceOf($variablesClass, $event
->getVariables());
}
}