diff --git a/composer.json b/composer.json index 73b3162c2e..913f25889e 100644 --- a/composer.json +++ b/composer.json @@ -112,7 +112,8 @@ }, "autoload-dev": { "psr-4": { - "App\\Tests\\": "tests/" + "App\\Test\\Fixtures\\": "tests/fixtures/", + "App\\Test\\": "tests/" } }, "replace": { diff --git a/config/services.yaml b/config/services.yaml index 17684b5530..8e06e3b9c7 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -15,6 +15,9 @@ services: resource: '../src/*' exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php,Routes}' + App\Test\Fixtures\: + resource: '../tests/fixtures/*' + # controllers are imported separately to make sure services can be injected # as action arguments even if you don't extend any base controller class App\Controller\: diff --git a/src/Core/ModuleManager.php b/src/Core/ModuleManager.php index 875f69692d..7d02a2a5e2 100644 --- a/src/Core/ModuleManager.php +++ b/src/Core/ModuleManager.php @@ -44,6 +44,7 @@ use Functional as F; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\Config\Resource\GlobResource; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -101,29 +102,48 @@ class ModuleManager $module_paths = array_merge(glob(INSTALLDIR . '/components/*/*.php'), glob(INSTALLDIR . '/plugins/*/*.php')); $module_manager = new self(); $entity_paths = []; + $fixtures = []; foreach ($module_paths as $path) { $type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path)); $dir = \dirname($path); $module = basename($dir); // component or plugin $fqcn = "\\{$type}\\{$module}\\{$module}"; $module_manager->add($fqcn, $path); - if (!\is_null($container) && file_exists($dir = $dir . '/Entity') && is_dir($dir)) { + + // Register Entities + if (!\is_null($container) && file_exists($entity_dir = $dir . '/Entity') && is_dir($entity_dir)) { // Happens at compile time, so it's hard to do integration testing. However, // everything would break if this did :') // @codeCoverageIgnoreStart - $entity_paths[] = $dir; + $entity_paths[] = $entity_dir; $container->findDefinition('doctrine.orm.default_metadata_driver')->addMethodCall( 'addDriver', [new Reference('app.schemadef_driver'), "{$type}\\{$module}\\Entity"], ); // @codeCoverageIgnoreEnd } + + // Register Test Fixtures + if (!\is_null($container) && file_exists($fixtures_dir = $dir . '/tests/fixtures') && is_dir($fixtures_dir)) { + $fixtures_files = glob("{$fixtures_dir}/*.php"); + self::$loader->addPsr4("{$type}\\{$module}\\Test\\Fixtures\\", $fixtures_dir); + $container->addResource(new GlobResource($dir, '/tests/fixtures/*', false)); + + foreach ($fixtures_files as $fixture) { + $class = Formatting::removeSuffix(Formatting::removePrefix($fixture, $fixtures_dir . '/'), '.php'); + $id = Formatting::removeSuffix(Formatting::camelCaseToSnakeCase($class), '_fixtures'); + $fqcn = "{$type}\\{$module}\\Test\\Fixtures\\{$class}"; + $container->autowire($fqcn, $fqcn); + $fixtures[] = ['fixture' => new Reference($fqcn), 'groups' => [$type]]; + } + } } if (!\is_null($container)) { // @codeCoverageIgnoreStart $container->findDefinition('app.schemadef_driver') ->addMethodCall('addPaths', ['$paths' => $entity_paths]); + $container->findDefinition('doctrine.fixtures.loader')->addMethodCall('addFixtures', [$fixtures]); // @codeCoverageIgnoreEnd } @@ -156,7 +176,7 @@ class ModuleManager } else { $rdi = new AppendIterator(); $rdi->append(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(INSTALLDIR . '/components', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS))); - $rdi->append(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(INSTALLDIR . '/plugins', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS))); + $rdi->append(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(INSTALLDIR . '/plugins', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS))); $time = file_exists(MODULE_CACHE_FILE) ? filemtime(MODULE_CACHE_FILE) : 0; if ($_ENV['APP_ENV'] === 'test' || F\some($rdi, fn ($e) => $e->getMTime() > $time)) { diff --git a/src/DataFixtures/CoreFixtures.php b/tests/fixtures/CoreFixtures.php similarity index 99% rename from src/DataFixtures/CoreFixtures.php rename to tests/fixtures/CoreFixtures.php index 117cf6d6f7..56ba0f4893 100644 --- a/src/DataFixtures/CoreFixtures.php +++ b/tests/fixtures/CoreFixtures.php @@ -2,7 +2,7 @@ declare(strict_types = 1); -namespace App\DataFixtures; +namespace App\Test\Fixtures; use App\Core\ActorLocalRoles; use App\Core\VisibilityScope; diff --git a/src/DataFixtures/MediaFixtures.php b/tests/fixtures/MediaFixtures.php similarity index 98% rename from src/DataFixtures/MediaFixtures.php rename to tests/fixtures/MediaFixtures.php index 45002709d2..fcf7e383f2 100644 --- a/src/DataFixtures/MediaFixtures.php +++ b/tests/fixtures/MediaFixtures.php @@ -2,7 +2,7 @@ declare(strict_types = 1); -namespace App\DataFixtures; +namespace App\Test\Fixtures; use App\Core\ActorLocalRoles; use App\Core\DB\DB;