From e9f7c2f5e5a679ea3367c286e895b429e41d17f0 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 14 Dec 2024 09:09:14 +0800 Subject: [PATCH 1/2] [7.x] Fixes database seeding using `WithWorkbench` and `RefreshDatabase` (#279) * [7.x] Fixes database seeding using `WithWorkbench` and `RefreshDatabase` Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- CHANGELOG-7.x.md | 8 ++++++ src/Concerns/WithWorkbench.php | 36 ++++++++++++++++++++++-- tests/WithWorkbenchTest.php | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/CHANGELOG-7.x.md b/CHANGELOG-7.x.md index 64579e6d..cc20d9b3 100644 --- a/CHANGELOG-7.x.md +++ b/CHANGELOG-7.x.md @@ -2,6 +2,14 @@ This changelog references the relevant changes (bug and security fixes) done to `orchestra/testbench-core`. +## 7.49.1 + +Released: 2024-12-14 + +### Fixes + +* Fixes database seeding using `Orchestra\Testbench\Concerns\WithWorkbench` and `Illuminate\Foundation\Testing\RefreshDatabase`. + ## 7.49.0 Released: 2024-12-01 diff --git a/src/Concerns/WithWorkbench.php b/src/Concerns/WithWorkbench.php index bb499681..ec39467e 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; @@ -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, @@ -53,4 +55,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 564eb4dc..90cc2c1c 100644 --- a/tests/WithWorkbenchTest.php +++ b/tests/WithWorkbenchTest.php @@ -4,6 +4,7 @@ use Orchestra\Testbench\Concerns\WithWorkbench; use Orchestra\Testbench\Contracts\Config as ConfigContract; +use Orchestra\Testbench\Foundation\Config; use Orchestra\Testbench\TestCase; use Orchestra\Testbench\Workbench\Workbench; @@ -47,4 +48,54 @@ public function it_can_resolve_user_model_from_workbench() { $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; + } } From d0d0badafa2bd2fc3fa7e4e763a9a667ea2c30e2 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 14 Dec 2024 09:11:45 +0800 Subject: [PATCH 2/2] Release 8.30.1 Signed-off-by: Mior Muhammad Zaki --- CHANGELOG-8.x.md | 8 ++++++++ 1 file changed, 8 insertions(+) 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