FactoryMapperTest.php in Hook Event Dispatcher 3.x
File
modules/preprocess_event_dispatcher/tests/src/Unit/FactoryMapperTest.php
View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit;
use Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader;
use PHPUnit\Framework\TestCase;
use function array_filter;
use function count;
use function dirname;
use function in_array;
use function scandir;
use function substr;
final class FactoryMapperTest extends TestCase {
private $mapper;
public function setUp() : void {
$this->mapper = YamlDefinitionsLoader::getInstance()
->getMapper();
}
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);
}
}
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;
}
public function testNoneExistingHook() : void {
$factory = $this->mapper
->getFactory('NoneExistingHook');
self::assertNull($factory);
}
}