[TESTS][MODULES] Move Test Fixtures to tests/fixtures folder and add support for loading fixtures from components and plugins

This commit is contained in:
Hugo Sales 2022-03-13 18:00:21 +00:00
parent 18864ca9fa
commit 1d8bba3949
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 30 additions and 6 deletions

View File

@ -112,7 +112,8 @@
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
"App\\Test\\Fixtures\\": "tests/fixtures/",
"App\\Test\\": "tests/"
}
},
"replace": {

View File

@ -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\:

View File

@ -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)) {

View File

@ -2,7 +2,7 @@
declare(strict_types = 1);
namespace App\DataFixtures;
namespace App\Test\Fixtures;
use App\Core\ActorLocalRoles;
use App\Core\VisibilityScope;

View File

@ -2,7 +2,7 @@
declare(strict_types = 1);
namespace App\DataFixtures;
namespace App\Test\Fixtures;
use App\Core\ActorLocalRoles;
use App\Core\DB\DB;