You are here

public function PreprocessEventPassTest::testOverwritingDefaultFactory in Hook Event Dispatcher 8

Test if we can overwrite a default factory.

Using the hook_event_dispatcher_factory tag.

File

tests/src/Unit/Preprocess/PreprocessEventPassTest.php, line 69

Class

PreprocessEventPassTest
Class PreprocessEventPassTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess

Code

public function testOverwritingDefaultFactory() {
  $fakeEckEntityFactory = new Definition(FakePreprocessEventFactory::class, [
    EckEntityPreprocessEvent::getHook(),
  ]);
  $fakeEckEntityFactory
    ->addTag('preprocess_event_factory');
  $this->builder
    ->setDefinition('preprocess_event.fake_factory.eck_entity', $fakeEckEntityFactory);
  $fakeHtmlFactory = new Definition(FakePreprocessEventFactory::class, [
    HtmlPreprocessEvent::getHook(),
  ]);
  $fakeHtmlFactory
    ->addTag('preprocess_event_factory');
  $this->builder
    ->setDefinition('preprocess_event.fake_factory.html', $fakeHtmlFactory);
  $this->pass
    ->process($this->builder);
  $this->builder
    ->compile();

  /** @var \Drupal\hook_event_dispatcher\Service\PreprocessEventFactoryMapper $mapper */
  $mapper = $this->builder
    ->get('preprocess_event.factory_mapper');
  $variables = [];
  $eckMappedFactory = $mapper
    ->getFactory(EckEntityPreprocessEvent::getHook());
  self::assertInstanceOf(FakePreprocessEventFactory::class, $eckMappedFactory);
  self::assertEquals(EckEntityPreprocessEvent::getHook(), $eckMappedFactory
    ->getEventHook());
  self::assertInstanceOf(FakePreprocessEvent::class, $eckMappedFactory
    ->createEvent($variables));
  $htmlMappedFactory = $mapper
    ->getFactory(HtmlPreprocessEvent::getHook());
  self::assertInstanceOf(FakePreprocessEventFactory::class, $htmlMappedFactory);
  self::assertEquals(HtmlPreprocessEvent::getHook(), $htmlMappedFactory
    ->getEventHook());
  self::assertInstanceOf(FakePreprocessEvent::class, $htmlMappedFactory
    ->createEvent($variables));
}