You are here

final class FactoryMapperTest in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/preprocess_event_dispatcher/tests/src/Unit/FactoryMapperTest.php \Drupal\Tests\preprocess_event_dispatcher\Unit\FactoryMapperTest

Class FactoryMapperTest.

@group preprocess_event_dispatcher

Hierarchy

  • class \Drupal\Tests\preprocess_event_dispatcher\Unit\FactoryMapperTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of FactoryMapperTest

File

modules/preprocess_event_dispatcher/tests/src/Unit/FactoryMapperTest.php, line 19

Namespace

Drupal\Tests\preprocess_event_dispatcher\Unit
View source
final class FactoryMapperTest extends TestCase {

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

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

  /**
   * Test if all factories are in the YAML file.
   */
  public function testYamlHasAllFactories() : void {
    $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() : array {
    $files = scandir(dirname(__DIR__, 3) . '/src/Factory', SCANDIR_SORT_NONE);
    $invalidFactories = [
      '.',
      '..',
      'PreprocessEventFactoryInterface.php',
    ];
    $files = array_filter($files, static function ($file) use ($invalidFactories) {
      return !in_array($file, $invalidFactories, TRUE);
    });
    $classNames = [];
    foreach ($files as $file) {
      $classNames[] = 'Drupal\\preprocess_event_dispatcher\\Factory\\' . substr($file, 0, -4);
    }
    return $classNames;
  }

  /**
   * Test a none existing hook.
   */
  public function testNoneExistingHook() : void {
    $factory = $this->mapper
      ->getFactory('NoneExistingHook');
    self::assertNull($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
FactoryMapperTest::testNoneExistingHook public function Test a none existing hook.
FactoryMapperTest::testYamlHasAllFactories public function Test if all factories are in the YAML file.