feature/cierres (#25)
Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #25
This commit is contained in:
70
app/tests/extension/TestSeeder.php
Normal file
70
app/tests/extension/TestSeeder.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use FilesystemIterator;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class TestSeeder
|
||||
{
|
||||
public function __construct(protected PDO $connection) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$seedClasses = $this->getSeedClasses();
|
||||
$orderedSeeds = $this->orderedSeeds($seedClasses);
|
||||
foreach ($orderedSeeds as $seed) {
|
||||
$seed->run();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getSeedClasses(): array
|
||||
{
|
||||
$seedsFolder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Seeds']);
|
||||
$files = new FilesystemIterator($seedsFolder, FilesystemIterator::SKIP_DOTS);
|
||||
$seeds = [];
|
||||
foreach ($files as $file) {
|
||||
$seeds []= $this->buildClassName($file->getBasename('.php'));
|
||||
}
|
||||
return $seeds;
|
||||
}
|
||||
protected function getSeed(string $seedClassName): SeedInterface
|
||||
{
|
||||
return new $seedClassName($this->connection);
|
||||
}
|
||||
|
||||
protected function buildClassName(string $fileBaseName): string
|
||||
{
|
||||
$namespace = implode('\\', [__NAMESPACE__, 'Seeds']);
|
||||
return implode('\\', [$namespace, $fileBaseName]);
|
||||
}
|
||||
|
||||
protected function orderedSeeds(array $seedClasses): array
|
||||
{
|
||||
$orderedSeeds = [];
|
||||
foreach ($seedClasses as $seedClassName) {
|
||||
$seed = $this->getSeed($seedClassName);
|
||||
if ($seed->getDependencies() === []) {
|
||||
$orderedSeeds[$seedClassName] = $seed;
|
||||
continue;
|
||||
}
|
||||
$orderedSeeds = array_merge($orderedSeeds, $this->orderedDependencies($orderedSeeds, $seedClasses, $seedClassName));
|
||||
}
|
||||
return $orderedSeeds;
|
||||
}
|
||||
|
||||
protected function orderedDependencies(array $orderedSeeds, array $seedClasses, string $seedClassName): array
|
||||
{
|
||||
$seed = $this->getSeed($seedClassName);
|
||||
$dependencies = $seed->getDependencies();
|
||||
foreach ($dependencies as $dependencyClass) {
|
||||
if (!array_key_exists($dependencyClass, $orderedSeeds)) {
|
||||
$orderedSeeds = array_merge($orderedSeeds, $this->orderedDependencies($orderedSeeds, $seedClasses, $dependencyClass));
|
||||
}
|
||||
}
|
||||
if (!array_key_exists($seedClassName, $orderedSeeds)) {
|
||||
$orderedSeeds[$seedClassName] = $seed;
|
||||
}
|
||||
return $orderedSeeds;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user