You are here

public function PreprocessEventPassTest::testOverwritingDefaultFactory in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/preprocess_event_dispatcher/tests/src/Unit/PreprocessEventPassTest.php \Drupal\Tests\preprocess_event_dispatcher\Unit\PreprocessEventPassTest::testOverwritingDefaultFactory()

Test if we can overwrite a default factory.

Using the preprocess_event_dispatcher_factory tag.

File

modules/preprocess_event_dispatcher/tests/src/Unit/PreprocessEventPassTest.php, line 71

Class

PreprocessEventPassTest
Class PreprocessEventPassTest.

Namespace

Drupal\Tests\preprocess_event_dispatcher\Unit

Code

public function testOverwritingDefaultFactory() : void {
  $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\preprocess_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::assertSame(EckEntityPreprocessEvent::getHook(), $eckMappedFactory
    ->getEventHook());
  self::assertInstanceOf(FakePreprocessEvent::class, $eckMappedFactory
    ->createEvent($variables));
  $htmlMappedFactory = $mapper
    ->getFactory(HtmlPreprocessEvent::getHook());
  self::assertInstanceOf(FakePreprocessEventFactory::class, $htmlMappedFactory);
  self::assertSame(HtmlPreprocessEvent::getHook(), $htmlMappedFactory
    ->getEventHook());
  self::assertInstanceOf(FakePreprocessEvent::class, $htmlMappedFactory
    ->createEvent($variables));
}