Skip to content

Commit a7573b1

Browse files
committed
wip
1 parent 22e2686 commit a7573b1

File tree

10 files changed

+128
-34
lines changed

10 files changed

+128
-34
lines changed

packages/database/src/Config/SeederConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ public function __construct(
88
/** @var array<array-key, class-string<\Tempest\Database\DatabaseSeeder>> */
99
public array $seeders = [],
1010
) {}
11-
}
11+
}

packages/database/src/Config/seeder.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
use Tempest\Database\Config\SeederConfig;
44

5-
return new SeederConfig();
5+
return new SeederConfig();

packages/database/src/DatabaseSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
interface DatabaseSeeder
88
{
99
public function run(null|string|UnitEnum $database): void;
10-
}
10+
}

packages/database/src/SeederDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ public function apply(): void
2929
$this->seederConfig->seeders[] = $discoveryItem;
3030
}
3131
}
32-
}
32+
}

src/Tempest/Framework/Commands/DatabaseSeedCommand.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,38 @@ public function __construct(
2626
public function __invoke(
2727
#[ConsoleArgument(description: 'Use a specific database.')]
2828
?string $database = null,
29-
): void
30-
{
31-
foreach ($this->seederConfig->seeders as $seederClass) {
32-
/** @var \Tempest\Database\DatabaseSeeder $seeder */
33-
$seeder = $this->container->get($seederClass);
34-
$seeder->run($database);
35-
36-
$this->console->keyValue(
37-
key: "<style='fg-gray'>{$seederClass}</style>",
38-
value: "<style='fg-green'>SEEDED</style>",
29+
#[ConsoleArgument(description: 'Run all database seeders')]
30+
bool $all = false,
31+
): void {
32+
if (count($this->seederConfig->seeders) === 1) {
33+
$this->runSeeder($this->seederConfig->seeders[0], $database);
34+
return;
35+
}
36+
37+
if ($all) {
38+
$seedersToRun = $this->seederConfig->seeders;
39+
} else {
40+
$seedersToRun = $this->ask(
41+
question: 'Which seeders do you want to run?',
42+
options: $this->seederConfig->seeders,
43+
multiple: true,
3944
);
4045
}
46+
47+
foreach ($seedersToRun as $seederClass) {
48+
$this->runSeeder($seederClass, $database);
49+
}
50+
}
51+
52+
private function runSeeder(string $seederClass, ?string $database): void
53+
{
54+
/** @var \Tempest\Database\DatabaseSeeder $seeder */
55+
$seeder = $this->container->get($seederClass);
56+
$seeder->run($database);
57+
58+
$this->console->keyValue(
59+
key: "<style='fg-gray'>{$seederClass}</style>",
60+
value: "<style='fg-green'>SEEDED</style>",
61+
);
4162
}
42-
}
63+
}

src/Tempest/Framework/Commands/MigrateFreshCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ public function __invoke(
3636
bool $validate = true,
3737
#[ConsoleArgument(description: 'Use a specific database.')]
3838
?string $database = null,
39-
#[ConsoleArgument(description: 'Run all database seeders after the database has been migrated')]
39+
#[ConsoleArgument(description: 'Run database seeders after the database has been migrated')]
4040
bool $seed = false,
41+
#[ConsoleArgument(description: 'Run all database seeders after the database has been migrated')]
42+
bool $all = false,
4143
): ExitCode {
4244
if ($validate) {
4345
$validationSuccess = $this->console->call(MigrateValidateCommand::class);
@@ -58,7 +60,7 @@ public function __invoke(
5860

5961
if ($seed) {
6062
$this->console->header('Seeding database');
61-
$this->console->call(DatabaseSeedCommand::class, ['--database' => $database]);
63+
$this->console->call(DatabaseSeedCommand::class, ['database' => $database, 'all' => $all]);
6264
}
6365

6466
return ExitCode::SUCCESS;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Fixtures;
4+
5+
use Tempest\Database\DatabaseSeeder;
6+
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
7+
use UnitEnum;
8+
9+
use function Tempest\Database\query;
10+
11+
final class SecondTestDatabaseSeeder implements DatabaseSeeder
12+
{
13+
public function run(null|string|UnitEnum $database): void
14+
{
15+
query(Book::class)
16+
->insert(
17+
title: 'Timeline Taxi 2',
18+
)
19+
->onDatabase($database)
20+
->execute();
21+
}
22+
}

tests/Fixtures/TestDatabaseSeeder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tempest\Database\DatabaseSeeder;
66
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
77
use UnitEnum;
8+
89
use function Tempest\Database\query;
910

1011
final class TestDatabaseSeeder implements DatabaseSeeder
@@ -18,4 +19,4 @@ public function run(null|string|UnitEnum $database): void
1819
->onDatabase($database)
1920
->execute();
2021
}
21-
}
22+
}

tests/Integration/Database/MultiDatabaseTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ public function test_migrate_up_command(): void
188188
public function test_migrate_fresh_command(): void
189189
{
190190
$this->console
191-
->call('migrate:fresh --database=main')
191+
->call('migrate:fresh --database=main --all')
192192
->assertSuccess();
193193

194194
$this->assertTableExists(Migration::class, 'main');
195195
$this->assertTableDoesNotExist(Migration::class, 'backup');
196196

197197
$this->console
198-
->call('migrate:fresh --database=backup')
198+
->call('migrate:fresh --database=backup --all')
199199
->assertSuccess();
200200

201201
$this->assertTableExists(Migration::class, 'backup');
@@ -284,50 +284,50 @@ public function test_database_seed_on_selected_database(): void
284284
$migrationManager->onDatabase('backup')->executeUp(new CreateBookTable());
285285

286286
$this->console
287-
->call('db:seed --database=main')
287+
->call('db:seed --database=main --all')
288288
->assertSuccess();
289289

290290
$this->assertSame(
291291
'Timeline Taxi',
292-
query(Book::class)->select()->onDatabase('main')->first()->title
292+
query(Book::class)->select()->onDatabase('main')->first()->title,
293293
);
294294

295295
$this->assertNull(
296-
query(Book::class)->select()->onDatabase('backup')->first()
296+
query(Book::class)->select()->onDatabase('backup')->first(),
297297
);
298298

299299
$this->console
300-
->call('db:seed --database=backup')
300+
->call('db:seed --database=backup --all')
301301
->assertSuccess();
302302

303303
$this->assertSame(
304304
'Timeline Taxi',
305-
query(Book::class)->select()->onDatabase('backup')->first()->title
305+
query(Book::class)->select()->onDatabase('backup')->first()->title,
306306
);
307307
}
308308

309309
public function test_migrate_fresh_seed_on_selected_database(): void
310310
{
311311
$this->console
312-
->call('migrate:fresh --seed --database=main')
312+
->call('migrate:fresh --seed --database=main --all')
313313
->assertSuccess();
314314

315315
$this->assertSame(
316316
'Timeline Taxi',
317-
query(Book::class)->select()->onDatabase('main')->first()->title
317+
query(Book::class)->select()->onDatabase('main')->first()->title,
318318
);
319319

320-
$this->assertException(PDOException::class, function () {
320+
$this->assertException(PDOException::class, function (): void {
321321
query(Book::class)->select()->onDatabase('backup')->first();
322322
});
323323

324324
$this->console
325-
->call('migrate:fresh --seed --database=backup')
325+
->call('migrate:fresh --seed --database=backup --all')
326326
->assertSuccess();
327327

328328
$this->assertSame(
329329
'Timeline Taxi',
330-
query(Book::class)->select()->onDatabase('backup')->first()->title
330+
query(Book::class)->select()->onDatabase('backup')->first()->title,
331331
);
332332
}
333333

tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
use Tempest\Core\AppConfig;
66
use Tempest\Core\Environment;
7+
use Tempest\Database\Config\SeederConfig;
78
use Tempest\Database\Migrations\CreateMigrationsTable;
89
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
910
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
11+
use Tests\Tempest\Fixtures\SecondTestDatabaseSeeder;
12+
use Tests\Tempest\Fixtures\TestDatabaseSeeder;
1013
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1114

15+
use function Tempest\Database\query;
16+
1217
final class DatabaseSeedCommandTest extends FrameworkIntegrationTestCase
1318
{
14-
public function test_seed(): void
19+
public function test_seed_with_selected_seeder(): void
1520
{
1621
$this->migrate(
1722
CreateMigrationsTable::class,
@@ -20,17 +25,60 @@ public function test_seed(): void
2025

2126
$this->console
2227
->call('db:seed')
28+
->assertSee(TestDatabaseSeeder::class)
29+
->assertSee(SecondTestDatabaseSeeder::class)
30+
->submit('1')
31+
->submit()
32+
->assertSuccess();
33+
34+
$book = Book::get(1);
35+
$this->assertSame('Timeline Taxi 2', $book->title);
36+
$this->assertSame(1, query(Book::class)->count()->execute());
37+
}
38+
39+
public function test_seed_all(): void
40+
{
41+
$this->migrate(
42+
CreateMigrationsTable::class,
43+
CreateBookTable::class,
44+
);
45+
46+
$this->console
47+
->call('db:seed --all')
2348
->assertSuccess();
2449

2550
$book = Book::get(1);
51+
$this->assertSame('Timeline Taxi', $book->title);
2652

53+
$book = Book::get(2);
54+
$this->assertSame('Timeline Taxi 2', $book->title);
55+
56+
$this->assertSame(2, query(Book::class)->count()->execute());
57+
}
58+
59+
public function test_seed_when_only_one_seeder_is_available(): void
60+
{
61+
$seederConfig = $this->container->get(SeederConfig::class);
62+
unset($seederConfig->seeders[1]);
63+
64+
$this->migrate(
65+
CreateMigrationsTable::class,
66+
CreateBookTable::class,
67+
);
68+
69+
$this->console
70+
->call('db:seed')
71+
->print();
72+
73+
$book = Book::get(1);
2774
$this->assertSame('Timeline Taxi', $book->title);
75+
$this->assertSame(1, query(Book::class)->count()->execute());
2876
}
2977

3078
public function test_seed_via_migrate_fresh(): void
3179
{
3280
$this->console
33-
->call('migrate:fresh --seed')
81+
->call('migrate:fresh --seed --all')
3482
->assertSuccess();
3583

3684
$book = Book::get(1);
@@ -44,7 +92,7 @@ public function test_db_seed_caution(): void
4492
$appConfig->environment = Environment::PRODUCTION;
4593

4694
$this->console
47-
->call('migrate:fresh --seed')
95+
->call('migrate:fresh --seed --all')
4896
->assertSee('Do you wish to continue');
4997
}
50-
}
98+
}

0 commit comments

Comments
 (0)