Skip to content

Commit

Permalink
Merge branch '9.7.x' into 9.x
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
2 parents 299559e + 7eb65df commit 3a3af6f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG-8.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG-9.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This changelog references the relevant changes (bug and security fixes) done to `orchestra/testbench-core`.

## 9.7.3

Released: 2024-12-14

### Fixes

* Fixes database seeding using `Orchestra\Testbench\Concerns\WithWorkbench` and `Illuminate\Foundation\Testing\RefreshDatabase`.

## 9.7.2

Released: 2024-12-06
Expand Down
2 changes: 1 addition & 1 deletion laravel/config/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
|
*/

'lifetime' => env('SESSION_LIFETIME', 120),
'lifetime' => (int) env('SESSION_LIFETIME', 120),

'expire_on_close' => env('SESSION_EXPIRE_ON_CLOSE', false),

Expand Down
36 changes: 33 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 @@ -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,
Expand All @@ -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<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;

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

return $seeders->isEmpty() ? false : $seeders->all();
}
}
49 changes: 49 additions & 0 deletions tests/WithWorkbenchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\Contracts\Config as ConfigContract;
use Orchestra\Testbench\Foundation\Config;
use Orchestra\Testbench\Foundation\Env;
use Orchestra\Testbench\TestCase;
use Orchestra\Testbench\Workbench\Workbench;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -52,4 +54,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;
}
}

0 comments on commit 3a3af6f

Please sign in to comment.