Skip to content

Commit

Permalink
[7.x] Fixes database seeding using WithWorkbench and RefreshDatabase
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Dec 14, 2024
1 parent c76bf8e commit a9d45ec
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/Concerns/WithWorkbench.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Orchestra\Testbench\Concerns;

use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
use Illuminate\Support\Collection;
use Orchestra\Testbench\Contracts\Config as ConfigContract;
use Orchestra\Testbench\Foundation\Bootstrap\LoadMigrationsFromArray;
use Orchestra\Testbench\Workbench\Workbench;

Expand Down Expand Up @@ -31,9 +33,9 @@ protected function setUpWithWorkbench(): void

$seeders = $config['seeders'] ?? false;

if (static::usesTestingConcern(CanConfigureMigrationCommands::class) && $this->shouldSeed() === false) {
$seeders = false;
}
$seeders = static::usesTestingConcern(CanConfigureMigrationCommands::class)
? $this->mergeSeedersForWorkbench($config)
: ($config['seeders'] ?? false);

(new LoadMigrationsFromArray(
$config['migrations'] ?? [], $seeders,
Expand All @@ -53,4 +55,31 @@ protected function bootDiscoverRoutesForWorkbench($app): void

Workbench::discoverRoutes($app, $config);
}

/**
* Merge seeders for Workbench.
*
* @param \Orchestra\Testbench\Contracts\Config $config
* @return array<int, class-string>|false
*/
protected function mergeSeedersForWorkbench(ConfigContract $config): array|false
{
$seeders = $config['seeders'] ?? false;

if ($this->shouldSeed() === false || $seeders === false) {
return false;
}

$testCaseSeeder = $this->seeder();

/** @var class-string $testCaseSeeder */
$testCaseSeeder = $testCaseSeeder !== false
? $testCaseSeeder
: \Database\Seeders\DatabaseSeeder::class;

return Collection::make($seeders)
->reject(static fn ($seeder) => $seeder === $testCaseSeeder)
->values()
->all();
}
}

0 comments on commit a9d45ec

Please sign in to comment.