You are here

class PreprocessEventPassTest in Hook Event Dispatcher 8

Class PreprocessEventPassTest.

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of PreprocessEventPassTest

File

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

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess
View source
class PreprocessEventPassTest extends UnitTestCase {

  /**
   * The Drupal ContainerBuilder.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  private $builder;

  /**
   * The PreprocessEventPass.
   *
   * @var \Drupal\hook_event_dispatcher\Service\PreprocessEventPass
   */
  private $pass;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->builder = new ContainerBuilder();
    $locator = new FileLocator([
      dirname(dirname(dirname(dirname(__DIR__)))),
    ]);
    $loader = new YamlFileLoader($this->builder, $locator);
    $loader
      ->load('hook_event_dispatcher.services.yml');
    $this->builder
      ->set('event_dispatcher', new SpyEventDispatcher());
    $this->pass = new PreprocessEventPass();
  }

  /**
   * Test if the ContainerBuilder has all services from the YAML file.
   */
  public function testBuilderHasAllServices() {
    $this->pass
      ->process($this->builder);
    $this->builder
      ->compile();
    $services = YamlDefinitionsLoader::getInstance()
      ->getServices();
    foreach (array_keys($services) as $id) {
      self::assertTrue($this->builder
        ->has($id));
    }
  }

  /**
   * Test if we can overwrite a default factory.
   *
   * Using the hook_event_dispatcher_factory tag.
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
PreprocessEventPassTest::$builder private property The Drupal ContainerBuilder.
PreprocessEventPassTest::$pass private property The PreprocessEventPass.
PreprocessEventPassTest::setUp public function Overrides UnitTestCase::setUp
PreprocessEventPassTest::testBuilderHasAllServices public function Test if the ContainerBuilder has all services from the YAML file.
PreprocessEventPassTest::testOverwritingDefaultFactory public function Test if we can overwrite a default factory.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.