FactoryMapperTest.php in Hook Event Dispatcher 8
File
tests/src/Unit/Preprocess/FactoryMapperTest.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Preprocess;
use Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\YamlDefinitionsLoader;
use Drupal\Tests\UnitTestCase;
final class FactoryMapperTest extends UnitTestCase {
private $mapper;
public function setUp() {
$this->mapper = YamlDefinitionsLoader::getInstance()
->getMapper();
}
public function testYamlHasAllFactories() {
$factoriesYaml = YamlDefinitionsLoader::getInstance()
->getFactories();
$classNames = $this
->getFactoryClassNamesFromFilesystem();
self::assertCount(\count($factoriesYaml), $classNames);
foreach ($factoriesYaml as $entry) {
self::assertContains($entry['class'], $classNames);
}
}
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;
}
public function testNoneExistingHook() {
$factory = $this->mapper
->getFactory('NoneExistingHook');
self::assertEquals(NULL, $factory);
}
}