You are here

final class FactoryMapperTest in Hook Event Dispatcher 8

Class FactoryMapperTest.

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of FactoryMapperTest

File

tests/src/Unit/Preprocess/FactoryMapperTest.php, line 13

Namespace

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

  /**
   * Factory mapper.
   *
   * @var \Drupal\hook_event_dispatcher\Service\PreprocessEventFactoryMapper
   */
  private $mapper;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->mapper = YamlDefinitionsLoader::getInstance()
      ->getMapper();
  }

  /**
   * Test if all factories are in the YAML file.
   */
  public function testYamlHasAllFactories() {
    $factoriesYaml = YamlDefinitionsLoader::getInstance()
      ->getFactories();
    $classNames = $this
      ->getFactoryClassNamesFromFilesystem();
    self::assertCount(\count($factoriesYaml), $classNames);
    foreach ($factoriesYaml as $entry) {
      self::assertContains($entry['class'], $classNames);
    }
  }

  /**
   * Get the Factory class names from the filesystems directory.
   *
   * @return array
   *   Class names array.
   */
  private function getFactoryClassNamesFromFilesystem() {
    $files = \scandir(\dirname(\dirname(\dirname(\dirname(__DIR__)))) . '/src/Event/Preprocess/Factory', SCANDIR_SORT_NONE);
    $invalidFactories = [
      '.',
      '..',
      'PreprocessEventFactoryInterface.php',
    ];
    $files = \array_filter($files, function ($file) use ($invalidFactories) {
      return !\in_array($file, $invalidFactories, TRUE);
    });
    $classNames = [];
    foreach ($files as $file) {
      $classNames[] = 'Drupal\\hook_event_dispatcher\\Event\\Preprocess\\Factory\\' . \substr($file, 0, -4);
    }
    return $classNames;
  }

  /**
   * Test a none existing hook.
   */
  public function testNoneExistingHook() {
    $factory = $this->mapper
      ->getFactory('NoneExistingHook');
    self::assertEquals(NULL, $factory);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FactoryMapperTest::$mapper private property Factory mapper.
FactoryMapperTest::getFactoryClassNamesFromFilesystem private function Get the Factory class names from the filesystems directory.
FactoryMapperTest::setUp public function Overrides UnitTestCase::setUp
FactoryMapperTest::testNoneExistingHook public function Test a none existing hook.
FactoryMapperTest::testYamlHasAllFactories public function Test if all factories are in the YAML file.
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.
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.