diff --git a/CHANGELOG-8.x.md b/CHANGELOG-8.x.md index 506fdfae..b3859da5 100644 --- a/CHANGELOG-8.x.md +++ b/CHANGELOG-8.x.md @@ -2,6 +2,14 @@ This changelog references the relevant changes (bug and security fixes) done to `orchestra/testbench-core`. +## 8.30.1 + +Released: 2024-12-14 + +### Fixes + +* Fixes database seeding using `Orchestra\Testbench\Concerns\WithWorkbench` and `Illuminate\Foundation\Testing\RefreshDatabase`. + ## 8.30.0 Released: 2024-12-01 diff --git a/src/Concerns/WithWorkbench.php b/src/Concerns/WithWorkbench.php index 33ecb39e..73f31900 100644 --- a/src/Concerns/WithWorkbench.php +++ b/src/Concerns/WithWorkbench.php @@ -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; @@ -30,9 +32,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, @@ -54,4 +56,32 @@ protected function bootDiscoverRoutesForWorkbench($app): void Workbench::discoverRoutes($app, $config); } + + /** + * Merge seeders for Workbench. + * + * @param \Orchestra\Testbench\Contracts\Config $config + * @return array|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; + + $seeders = Collection::make($seeders) + ->reject(static fn ($seeder) => $seeder === $testCaseSeeder) + ->values(); + + return $seeders->isEmpty() ? false : $seeders->all(); + } } diff --git a/tests/WithWorkbenchTest.php b/tests/WithWorkbenchTest.php index d68b3782..479cc90f 100644 --- a/tests/WithWorkbenchTest.php +++ b/tests/WithWorkbenchTest.php @@ -5,8 +5,10 @@ use Orchestra\Testbench\Concerns\WithWorkbench; use Orchestra\Testbench\Contracts\Config as ConfigContract; use Orchestra\Testbench\Foundation\Env; +use Orchestra\Testbench\Foundation\Config; use Orchestra\Testbench\TestCase; use Orchestra\Testbench\Workbench\Workbench; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; class WithWorkbenchTest extends TestCase @@ -50,4 +52,51 @@ public function it_can_resolve_user_model_from_workbench() $this->assertFalse(Env::has('AUTH_MODEL')); $this->assertSame('Workbench\App\Models\User', config('auth.providers.users.model')); } + + #[Test] + #[DataProvider('seedersDataProvider')] + public function it_can_merge_seeders_with_illuminate_database_refresh( + bool $seed, + string|false $seeder, + array|false $workbenchSeeders, + array|false $expected + ) { + $stub = new MergeSeedersTestStub($seed, $seeder); + + $config = new Config(['seeders' => $workbenchSeeders]); + + $this->assertSame($expected, $stub($config)); + } + + public static function seedersDataProvider() + { + yield [false, false, ['Workbench\Database\Seeders\DatabaseSeeder'], false]; + yield [true, false, ['Workbench\Database\Seeders\DatabaseSeeder'], ['Workbench\Database\Seeders\DatabaseSeeder']]; + yield [true, 'Database\Seeders\DatabaseSeeder', ['Workbench\Database\Seeders\DatabaseSeeder'], ['Workbench\Database\Seeders\DatabaseSeeder']]; + yield [false, 'Database\Seeders\DatabaseSeeder', ['Workbench\Database\Seeders\DatabaseSeeder'], false]; + yield [true, 'Database\Seeders\DatabaseSeeder', ['Database\Seeders\DatabaseSeeder', 'Workbench\Database\Seeders\DatabaseSeeder'], ['Workbench\Database\Seeders\DatabaseSeeder']]; + yield [true, 'Workbench\Database\Seeders\DatabaseSeeder', ['Workbench\Database\Seeders\DatabaseSeeder'], false]; + } +} + +class MergeSeedersTestStub +{ + use WithWorkbench; + + public function __construct(protected bool $seed, protected string|false $seeders) {} + + public function __invoke(ConfigContract $config) + { + return $this->mergeSeedersForWorkbench($config); + } + + public function shouldSeed(): bool + { + return $this->seed; + } + + public function seeder(): string|false + { + return $this->seeders; + } }