From 22e2686711d6cde52756c9c9c398b081195b022a Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 14:14:42 +0200 Subject: [PATCH 01/10] wip --- packages/database/src/Config/SeederConfig.php | 11 ++++ .../database/src/Config/seeder.config.php | 5 ++ packages/database/src/DatabaseSeeder.php | 10 +++ packages/database/src/SeederDiscovery.php | 32 ++++++++++ .../Commands/DatabaseSeedCommand.php | 42 +++++++++++++ .../Commands/MigrateFreshCommand.php | 11 +++- .../Framework/Commands/MigrateUpCommand.php | 1 - tests/Fixtures/TestDatabaseSeeder.php | 21 +++++++ .../Database/MultiDatabaseTest.php | 62 ++++++++++++++++++- .../Commands/DatabaseSeedCommandTest.php | 50 +++++++++++++++ 10 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 packages/database/src/Config/SeederConfig.php create mode 100644 packages/database/src/Config/seeder.config.php create mode 100644 packages/database/src/DatabaseSeeder.php create mode 100644 packages/database/src/SeederDiscovery.php create mode 100644 src/Tempest/Framework/Commands/DatabaseSeedCommand.php create mode 100644 tests/Fixtures/TestDatabaseSeeder.php create mode 100644 tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php diff --git a/packages/database/src/Config/SeederConfig.php b/packages/database/src/Config/SeederConfig.php new file mode 100644 index 000000000..8896a8cf5 --- /dev/null +++ b/packages/database/src/Config/SeederConfig.php @@ -0,0 +1,11 @@ +> */ + public array $seeders = [], + ) {} +} \ No newline at end of file diff --git a/packages/database/src/Config/seeder.config.php b/packages/database/src/Config/seeder.config.php new file mode 100644 index 000000000..de459ee91 --- /dev/null +++ b/packages/database/src/Config/seeder.config.php @@ -0,0 +1,5 @@ +implements(DatabaseSeeder::class)) { + $this->discoveryItems->add($location, $class->getName()); + } + } + + public function apply(): void + { + foreach ($this->discoveryItems as $discoveryItem) { + $this->seederConfig->seeders[] = $discoveryItem; + } + } +} \ No newline at end of file diff --git a/src/Tempest/Framework/Commands/DatabaseSeedCommand.php b/src/Tempest/Framework/Commands/DatabaseSeedCommand.php new file mode 100644 index 000000000..ebb3f5e3e --- /dev/null +++ b/src/Tempest/Framework/Commands/DatabaseSeedCommand.php @@ -0,0 +1,42 @@ +seederConfig->seeders as $seederClass) { + /** @var \Tempest\Database\DatabaseSeeder $seeder */ + $seeder = $this->container->get($seederClass); + $seeder->run($database); + + $this->console->keyValue( + key: "{$seederClass}", + value: "SEEDED", + ); + } + } +} \ No newline at end of file diff --git a/src/Tempest/Framework/Commands/MigrateFreshCommand.php b/src/Tempest/Framework/Commands/MigrateFreshCommand.php index 9d7c32021..0968925b0 100644 --- a/src/Tempest/Framework/Commands/MigrateFreshCommand.php +++ b/src/Tempest/Framework/Commands/MigrateFreshCommand.php @@ -36,6 +36,8 @@ public function __invoke( bool $validate = true, #[ConsoleArgument(description: 'Use a specific database.')] ?string $database = null, + #[ConsoleArgument(description: 'Run all database seeders after the database has been migrated')] + bool $seed = false, ): ExitCode { if ($validate) { $validationSuccess = $this->console->call(MigrateValidateCommand::class); @@ -52,7 +54,14 @@ public function __invoke( $this->console->info('There is no migration to drop.'); } - return $this->console->call(MigrateUpCommand::class, ['fresh' => false, 'validate' => false, 'database' => $database]); + $this->console->call(MigrateUpCommand::class, ['fresh' => false, 'validate' => false, 'database' => $database]); + + if ($seed) { + $this->console->header('Seeding database'); + $this->console->call(DatabaseSeedCommand::class, ['--database' => $database]); + } + + return ExitCode::SUCCESS; } #[EventHandler] diff --git a/src/Tempest/Framework/Commands/MigrateUpCommand.php b/src/Tempest/Framework/Commands/MigrateUpCommand.php index 68ad321cd..2a3806204 100644 --- a/src/Tempest/Framework/Commands/MigrateUpCommand.php +++ b/src/Tempest/Framework/Commands/MigrateUpCommand.php @@ -8,7 +8,6 @@ use Tempest\Console\ConsoleArgument; use Tempest\Console\ConsoleCommand; use Tempest\Console\ExitCode; -use Tempest\Console\Input\ConsoleArgumentBag; use Tempest\Console\Middleware\CautionMiddleware; use Tempest\Console\Middleware\ForceMiddleware; use Tempest\Container\Singleton; diff --git a/tests/Fixtures/TestDatabaseSeeder.php b/tests/Fixtures/TestDatabaseSeeder.php new file mode 100644 index 000000000..c465f462c --- /dev/null +++ b/tests/Fixtures/TestDatabaseSeeder.php @@ -0,0 +1,21 @@ +insert( + title: 'Timeline Taxi', + ) + ->onDatabase($database) + ->execute(); + } +} \ No newline at end of file diff --git a/tests/Integration/Database/MultiDatabaseTest.php b/tests/Integration/Database/MultiDatabaseTest.php index b187093f2..546e26055 100644 --- a/tests/Integration/Database/MultiDatabaseTest.php +++ b/tests/Integration/Database/MultiDatabaseTest.php @@ -1,6 +1,6 @@ assertTableDoesNotExist('main_table', 'backup'); } + public function test_database_seed_on_selected_database(): void + { + /** @var MigrationManager $migrationManager */ + $migrationManager = $this->container->get(MigrationManager::class); + + $migrationManager->onDatabase('main')->executeUp(new CreateMigrationsTable()); + $migrationManager->onDatabase('main')->executeUp(new CreateBookTable()); + $migrationManager->onDatabase('backup')->executeUp(new CreateMigrationsTable()); + $migrationManager->onDatabase('backup')->executeUp(new CreateBookTable()); + + $this->console + ->call('db:seed --database=main') + ->assertSuccess(); + + $this->assertSame( + 'Timeline Taxi', + query(Book::class)->select()->onDatabase('main')->first()->title + ); + + $this->assertNull( + query(Book::class)->select()->onDatabase('backup')->first() + ); + + $this->console + ->call('db:seed --database=backup') + ->assertSuccess(); + + $this->assertSame( + 'Timeline Taxi', + query(Book::class)->select()->onDatabase('backup')->first()->title + ); + } + + public function test_migrate_fresh_seed_on_selected_database(): void + { + $this->console + ->call('migrate:fresh --seed --database=main') + ->assertSuccess(); + + $this->assertSame( + 'Timeline Taxi', + query(Book::class)->select()->onDatabase('main')->first()->title + ); + + $this->assertException(PDOException::class, function () { + query(Book::class)->select()->onDatabase('backup')->first(); + }); + + $this->console + ->call('migrate:fresh --seed --database=backup') + ->assertSuccess(); + + $this->assertSame( + 'Timeline Taxi', + query(Book::class)->select()->onDatabase('backup')->first()->title + ); + } + private function assertTableExists(string $tableName, string $onDatabase): void { $this->assertTrue(query($tableName)->count()->onDatabase($onDatabase)->execute() >= 0); diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php new file mode 100644 index 000000000..9d1bacf57 --- /dev/null +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -0,0 +1,50 @@ +migrate( + CreateMigrationsTable::class, + CreateBookTable::class, + ); + + $this->console + ->call('db:seed') + ->assertSuccess(); + + $book = Book::get(1); + + $this->assertSame('Timeline Taxi', $book->title); + } + + public function test_seed_via_migrate_fresh(): void + { + $this->console + ->call('migrate:fresh --seed') + ->assertSuccess(); + + $book = Book::get(1); + + $this->assertSame('Timeline Taxi', $book->title); + } + + public function test_db_seed_caution(): void + { + $appConfig = $this->container->get(AppConfig::class); + $appConfig->environment = Environment::PRODUCTION; + + $this->console + ->call('migrate:fresh --seed') + ->assertSee('Do you wish to continue'); + } +} \ No newline at end of file From a7573b1ceb3b368e652425b733cfa5947288b449 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 14:32:12 +0200 Subject: [PATCH 02/10] wip --- packages/database/src/Config/SeederConfig.php | 2 +- .../database/src/Config/seeder.config.php | 2 +- packages/database/src/DatabaseSeeder.php | 2 +- packages/database/src/SeederDiscovery.php | 2 +- .../Commands/DatabaseSeedCommand.php | 43 ++++++++++---- .../Commands/MigrateFreshCommand.php | 6 +- tests/Fixtures/SecondTestDatabaseSeeder.php | 22 ++++++++ tests/Fixtures/TestDatabaseSeeder.php | 3 +- .../Database/MultiDatabaseTest.php | 24 ++++---- .../Commands/DatabaseSeedCommandTest.php | 56 +++++++++++++++++-- 10 files changed, 128 insertions(+), 34 deletions(-) create mode 100644 tests/Fixtures/SecondTestDatabaseSeeder.php diff --git a/packages/database/src/Config/SeederConfig.php b/packages/database/src/Config/SeederConfig.php index 8896a8cf5..5b64c09de 100644 --- a/packages/database/src/Config/SeederConfig.php +++ b/packages/database/src/Config/SeederConfig.php @@ -8,4 +8,4 @@ public function __construct( /** @var array> */ public array $seeders = [], ) {} -} \ No newline at end of file +} diff --git a/packages/database/src/Config/seeder.config.php b/packages/database/src/Config/seeder.config.php index de459ee91..eb487b4d0 100644 --- a/packages/database/src/Config/seeder.config.php +++ b/packages/database/src/Config/seeder.config.php @@ -2,4 +2,4 @@ use Tempest\Database\Config\SeederConfig; -return new SeederConfig(); \ No newline at end of file +return new SeederConfig(); diff --git a/packages/database/src/DatabaseSeeder.php b/packages/database/src/DatabaseSeeder.php index c85aadff3..3f6c754fa 100644 --- a/packages/database/src/DatabaseSeeder.php +++ b/packages/database/src/DatabaseSeeder.php @@ -7,4 +7,4 @@ interface DatabaseSeeder { public function run(null|string|UnitEnum $database): void; -} \ No newline at end of file +} diff --git a/packages/database/src/SeederDiscovery.php b/packages/database/src/SeederDiscovery.php index ee6e63af6..030321c98 100644 --- a/packages/database/src/SeederDiscovery.php +++ b/packages/database/src/SeederDiscovery.php @@ -29,4 +29,4 @@ public function apply(): void $this->seederConfig->seeders[] = $discoveryItem; } } -} \ No newline at end of file +} diff --git a/src/Tempest/Framework/Commands/DatabaseSeedCommand.php b/src/Tempest/Framework/Commands/DatabaseSeedCommand.php index ebb3f5e3e..3e15eb1b8 100644 --- a/src/Tempest/Framework/Commands/DatabaseSeedCommand.php +++ b/src/Tempest/Framework/Commands/DatabaseSeedCommand.php @@ -26,17 +26,38 @@ public function __construct( public function __invoke( #[ConsoleArgument(description: 'Use a specific database.')] ?string $database = null, - ): void - { - foreach ($this->seederConfig->seeders as $seederClass) { - /** @var \Tempest\Database\DatabaseSeeder $seeder */ - $seeder = $this->container->get($seederClass); - $seeder->run($database); - - $this->console->keyValue( - key: "{$seederClass}", - value: "SEEDED", + #[ConsoleArgument(description: 'Run all database seeders')] + bool $all = false, + ): void { + if (count($this->seederConfig->seeders) === 1) { + $this->runSeeder($this->seederConfig->seeders[0], $database); + return; + } + + if ($all) { + $seedersToRun = $this->seederConfig->seeders; + } else { + $seedersToRun = $this->ask( + question: 'Which seeders do you want to run?', + options: $this->seederConfig->seeders, + multiple: true, ); } + + foreach ($seedersToRun as $seederClass) { + $this->runSeeder($seederClass, $database); + } + } + + private function runSeeder(string $seederClass, ?string $database): void + { + /** @var \Tempest\Database\DatabaseSeeder $seeder */ + $seeder = $this->container->get($seederClass); + $seeder->run($database); + + $this->console->keyValue( + key: "{$seederClass}", + value: "SEEDED", + ); } -} \ No newline at end of file +} diff --git a/src/Tempest/Framework/Commands/MigrateFreshCommand.php b/src/Tempest/Framework/Commands/MigrateFreshCommand.php index 0968925b0..e5079f914 100644 --- a/src/Tempest/Framework/Commands/MigrateFreshCommand.php +++ b/src/Tempest/Framework/Commands/MigrateFreshCommand.php @@ -36,8 +36,10 @@ public function __invoke( bool $validate = true, #[ConsoleArgument(description: 'Use a specific database.')] ?string $database = null, - #[ConsoleArgument(description: 'Run all database seeders after the database has been migrated')] + #[ConsoleArgument(description: 'Run database seeders after the database has been migrated')] bool $seed = false, + #[ConsoleArgument(description: 'Run all database seeders after the database has been migrated')] + bool $all = false, ): ExitCode { if ($validate) { $validationSuccess = $this->console->call(MigrateValidateCommand::class); @@ -58,7 +60,7 @@ public function __invoke( if ($seed) { $this->console->header('Seeding database'); - $this->console->call(DatabaseSeedCommand::class, ['--database' => $database]); + $this->console->call(DatabaseSeedCommand::class, ['database' => $database, 'all' => $all]); } return ExitCode::SUCCESS; diff --git a/tests/Fixtures/SecondTestDatabaseSeeder.php b/tests/Fixtures/SecondTestDatabaseSeeder.php new file mode 100644 index 000000000..6015a40ed --- /dev/null +++ b/tests/Fixtures/SecondTestDatabaseSeeder.php @@ -0,0 +1,22 @@ +insert( + title: 'Timeline Taxi 2', + ) + ->onDatabase($database) + ->execute(); + } +} diff --git a/tests/Fixtures/TestDatabaseSeeder.php b/tests/Fixtures/TestDatabaseSeeder.php index c465f462c..946c751f8 100644 --- a/tests/Fixtures/TestDatabaseSeeder.php +++ b/tests/Fixtures/TestDatabaseSeeder.php @@ -5,6 +5,7 @@ use Tempest\Database\DatabaseSeeder; use Tests\Tempest\Fixtures\Modules\Books\Models\Book; use UnitEnum; + use function Tempest\Database\query; final class TestDatabaseSeeder implements DatabaseSeeder @@ -18,4 +19,4 @@ public function run(null|string|UnitEnum $database): void ->onDatabase($database) ->execute(); } -} \ No newline at end of file +} diff --git a/tests/Integration/Database/MultiDatabaseTest.php b/tests/Integration/Database/MultiDatabaseTest.php index 546e26055..957b82f27 100644 --- a/tests/Integration/Database/MultiDatabaseTest.php +++ b/tests/Integration/Database/MultiDatabaseTest.php @@ -188,14 +188,14 @@ public function test_migrate_up_command(): void public function test_migrate_fresh_command(): void { $this->console - ->call('migrate:fresh --database=main') + ->call('migrate:fresh --database=main --all') ->assertSuccess(); $this->assertTableExists(Migration::class, 'main'); $this->assertTableDoesNotExist(Migration::class, 'backup'); $this->console - ->call('migrate:fresh --database=backup') + ->call('migrate:fresh --database=backup --all') ->assertSuccess(); $this->assertTableExists(Migration::class, 'backup'); @@ -284,50 +284,50 @@ public function test_database_seed_on_selected_database(): void $migrationManager->onDatabase('backup')->executeUp(new CreateBookTable()); $this->console - ->call('db:seed --database=main') + ->call('db:seed --database=main --all') ->assertSuccess(); $this->assertSame( 'Timeline Taxi', - query(Book::class)->select()->onDatabase('main')->first()->title + query(Book::class)->select()->onDatabase('main')->first()->title, ); $this->assertNull( - query(Book::class)->select()->onDatabase('backup')->first() + query(Book::class)->select()->onDatabase('backup')->first(), ); $this->console - ->call('db:seed --database=backup') + ->call('db:seed --database=backup --all') ->assertSuccess(); $this->assertSame( 'Timeline Taxi', - query(Book::class)->select()->onDatabase('backup')->first()->title + query(Book::class)->select()->onDatabase('backup')->first()->title, ); } public function test_migrate_fresh_seed_on_selected_database(): void { $this->console - ->call('migrate:fresh --seed --database=main') + ->call('migrate:fresh --seed --database=main --all') ->assertSuccess(); $this->assertSame( 'Timeline Taxi', - query(Book::class)->select()->onDatabase('main')->first()->title + query(Book::class)->select()->onDatabase('main')->first()->title, ); - $this->assertException(PDOException::class, function () { + $this->assertException(PDOException::class, function (): void { query(Book::class)->select()->onDatabase('backup')->first(); }); $this->console - ->call('migrate:fresh --seed --database=backup') + ->call('migrate:fresh --seed --database=backup --all') ->assertSuccess(); $this->assertSame( 'Timeline Taxi', - query(Book::class)->select()->onDatabase('backup')->first()->title + query(Book::class)->select()->onDatabase('backup')->first()->title, ); } diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index 9d1bacf57..2422f8dad 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -4,14 +4,19 @@ use Tempest\Core\AppConfig; use Tempest\Core\Environment; +use Tempest\Database\Config\SeederConfig; use Tempest\Database\Migrations\CreateMigrationsTable; use Tests\Tempest\Fixtures\Migrations\CreateBookTable; use Tests\Tempest\Fixtures\Modules\Books\Models\Book; +use Tests\Tempest\Fixtures\SecondTestDatabaseSeeder; +use Tests\Tempest\Fixtures\TestDatabaseSeeder; use Tests\Tempest\Integration\FrameworkIntegrationTestCase; +use function Tempest\Database\query; + final class DatabaseSeedCommandTest extends FrameworkIntegrationTestCase { - public function test_seed(): void + public function test_seed_with_selected_seeder(): void { $this->migrate( CreateMigrationsTable::class, @@ -20,17 +25,60 @@ public function test_seed(): void $this->console ->call('db:seed') + ->assertSee(TestDatabaseSeeder::class) + ->assertSee(SecondTestDatabaseSeeder::class) + ->submit('1') + ->submit() + ->assertSuccess(); + + $book = Book::get(1); + $this->assertSame('Timeline Taxi 2', $book->title); + $this->assertSame(1, query(Book::class)->count()->execute()); + } + + public function test_seed_all(): void + { + $this->migrate( + CreateMigrationsTable::class, + CreateBookTable::class, + ); + + $this->console + ->call('db:seed --all') ->assertSuccess(); $book = Book::get(1); + $this->assertSame('Timeline Taxi', $book->title); + $book = Book::get(2); + $this->assertSame('Timeline Taxi 2', $book->title); + + $this->assertSame(2, query(Book::class)->count()->execute()); + } + + public function test_seed_when_only_one_seeder_is_available(): void + { + $seederConfig = $this->container->get(SeederConfig::class); + unset($seederConfig->seeders[1]); + + $this->migrate( + CreateMigrationsTable::class, + CreateBookTable::class, + ); + + $this->console + ->call('db:seed') + ->print(); + + $book = Book::get(1); $this->assertSame('Timeline Taxi', $book->title); + $this->assertSame(1, query(Book::class)->count()->execute()); } public function test_seed_via_migrate_fresh(): void { $this->console - ->call('migrate:fresh --seed') + ->call('migrate:fresh --seed --all') ->assertSuccess(); $book = Book::get(1); @@ -44,7 +92,7 @@ public function test_db_seed_caution(): void $appConfig->environment = Environment::PRODUCTION; $this->console - ->call('migrate:fresh --seed') + ->call('migrate:fresh --seed --all') ->assertSee('Do you wish to continue'); } -} \ No newline at end of file +} From baddcb10be2d247827aeba68d440f64b90ea9d0e Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 14:32:33 +0200 Subject: [PATCH 03/10] wip --- .../Integration/Framework/Commands/DatabaseSeedCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index 2422f8dad..09f695710 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -68,7 +68,7 @@ public function test_seed_when_only_one_seeder_is_available(): void $this->console ->call('db:seed') - ->print(); + ->assertSuccess(); $book = Book::get(1); $this->assertSame('Timeline Taxi', $book->title); From 87214e0ef6c6882d139c757fdf0d22b995254118 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 14:38:06 +0200 Subject: [PATCH 04/10] wip --- .../Commands/DatabaseSeedCommand.php | 7 +++++ .../Commands/MigrateFreshCommand.php | 16 +++++++++-- tests/Fixtures/TestDatabaseSeeder.php | 1 + .../Commands/DatabaseSeedCommandTest.php | 27 +++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Tempest/Framework/Commands/DatabaseSeedCommand.php b/src/Tempest/Framework/Commands/DatabaseSeedCommand.php index 3e15eb1b8..033a83492 100644 --- a/src/Tempest/Framework/Commands/DatabaseSeedCommand.php +++ b/src/Tempest/Framework/Commands/DatabaseSeedCommand.php @@ -28,7 +28,14 @@ public function __invoke( ?string $database = null, #[ConsoleArgument(description: 'Run all database seeders')] bool $all = false, + #[ConsoleArgument(description: 'Select one specific seeder to run')] + ?string $seeder = null, ): void { + if ($seeder !== null) { + $this->runSeeder($seeder, $database); + return; + } + if (count($this->seederConfig->seeders) === 1) { $this->runSeeder($this->seederConfig->seeders[0], $database); return; diff --git a/src/Tempest/Framework/Commands/MigrateFreshCommand.php b/src/Tempest/Framework/Commands/MigrateFreshCommand.php index e5079f914..753949860 100644 --- a/src/Tempest/Framework/Commands/MigrateFreshCommand.php +++ b/src/Tempest/Framework/Commands/MigrateFreshCommand.php @@ -40,6 +40,8 @@ public function __invoke( bool $seed = false, #[ConsoleArgument(description: 'Run all database seeders after the database has been migrated')] bool $all = false, + #[ConsoleArgument(description: 'Select one specific seeder to run')] + ?string $seeder = null, ): ExitCode { if ($validate) { $validationSuccess = $this->console->call(MigrateValidateCommand::class); @@ -56,11 +58,21 @@ public function __invoke( $this->console->info('There is no migration to drop.'); } - $this->console->call(MigrateUpCommand::class, ['fresh' => false, 'validate' => false, 'database' => $database]); + $this->console->call(MigrateUpCommand::class, [ + 'fresh' => false, + 'validate' => false, + 'database' => $database + ]); + + $seed = $seed || $seeder !== null; if ($seed) { $this->console->header('Seeding database'); - $this->console->call(DatabaseSeedCommand::class, ['database' => $database, 'all' => $all]); + $this->console->call(DatabaseSeedCommand::class, [ + 'database' => $database, + 'all' => $all, + 'seeder' => $seeder, + ]); } return ExitCode::SUCCESS; diff --git a/tests/Fixtures/TestDatabaseSeeder.php b/tests/Fixtures/TestDatabaseSeeder.php index 946c751f8..b00a6af65 100644 --- a/tests/Fixtures/TestDatabaseSeeder.php +++ b/tests/Fixtures/TestDatabaseSeeder.php @@ -3,6 +3,7 @@ namespace Tests\Tempest\Fixtures; use Tempest\Database\DatabaseSeeder; +use Tempest\Discovery\SkipDiscovery; use Tests\Tempest\Fixtures\Modules\Books\Models\Book; use UnitEnum; diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index 09f695710..0da24d02b 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -36,6 +36,33 @@ public function test_seed_with_selected_seeder(): void $this->assertSame(1, query(Book::class)->count()->execute()); } + public function test_seed_with_manually_selected_seeder(): void + { + $this->migrate( + CreateMigrationsTable::class, + CreateBookTable::class, + ); + + $this->console + ->call(sprintf('db:seed --seeder=%s', SecondTestDatabaseSeeder::class)) + ->assertSuccess(); + + $book = Book::get(1); + $this->assertSame('Timeline Taxi 2', $book->title); + $this->assertSame(1, query(Book::class)->count()->execute()); + } + + public function test_migrate_fresh_seed_with_manually_selected_seeder(): void + { + $this->console + ->call(sprintf('migrate:fresh --seeder=%s', SecondTestDatabaseSeeder::class)) + ->assertSuccess(); + + $book = Book::get(1); + $this->assertSame('Timeline Taxi 2', $book->title); + $this->assertSame(1, query(Book::class)->count()->execute()); + } + public function test_seed_all(): void { $this->migrate( From 79c93249c4567a818ff77c4d4cfbc0bdbc2c9757 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 15:08:55 +0200 Subject: [PATCH 05/10] wip --- src/Tempest/Framework/Commands/MigrateFreshCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tempest/Framework/Commands/MigrateFreshCommand.php b/src/Tempest/Framework/Commands/MigrateFreshCommand.php index 753949860..66eb87df4 100644 --- a/src/Tempest/Framework/Commands/MigrateFreshCommand.php +++ b/src/Tempest/Framework/Commands/MigrateFreshCommand.php @@ -61,7 +61,7 @@ public function __invoke( $this->console->call(MigrateUpCommand::class, [ 'fresh' => false, 'validate' => false, - 'database' => $database + 'database' => $database, ]); $seed = $seed || $seeder !== null; From 6dc931c2d72c0b94d7669dac65a6c978d25b582a Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 15:13:43 +0200 Subject: [PATCH 06/10] wip --- tests/Integration/Database/MultiDatabaseTest.php | 6 +++++- .../Framework/Commands/DatabaseSeedCommandTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/Integration/Database/MultiDatabaseTest.php b/tests/Integration/Database/MultiDatabaseTest.php index 957b82f27..32edc3ec5 100644 --- a/tests/Integration/Database/MultiDatabaseTest.php +++ b/tests/Integration/Database/MultiDatabaseTest.php @@ -300,9 +300,13 @@ public function test_database_seed_on_selected_database(): void ->call('db:seed --database=backup --all') ->assertSuccess(); + /** @var Book $book */ + /** @phpstan-ignore-next-line */ + $book = query(Book::class)->select()->onDatabase('backup')->first(); + $this->assertSame( 'Timeline Taxi', - query(Book::class)->select()->onDatabase('backup')->first()->title, + $book->title, ); } diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index 0da24d02b..ded60371b 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -6,7 +6,9 @@ use Tempest\Core\Environment; use Tempest\Database\Config\SeederConfig; use Tempest\Database\Migrations\CreateMigrationsTable; +use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable; use Tests\Tempest\Fixtures\Migrations\CreateBookTable; +use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable; use Tests\Tempest\Fixtures\Modules\Books\Models\Book; use Tests\Tempest\Fixtures\SecondTestDatabaseSeeder; use Tests\Tempest\Fixtures\TestDatabaseSeeder; @@ -20,6 +22,8 @@ public function test_seed_with_selected_seeder(): void { $this->migrate( CreateMigrationsTable::class, + CreatePublishersTable::class, + CreateAuthorTable::class, CreateBookTable::class, ); @@ -40,6 +44,8 @@ public function test_seed_with_manually_selected_seeder(): void { $this->migrate( CreateMigrationsTable::class, + CreatePublishersTable::class, + CreateAuthorTable::class, CreateBookTable::class, ); @@ -67,6 +73,8 @@ public function test_seed_all(): void { $this->migrate( CreateMigrationsTable::class, + CreatePublishersTable::class, + CreateAuthorTable::class, CreateBookTable::class, ); @@ -90,6 +98,8 @@ public function test_seed_when_only_one_seeder_is_available(): void $this->migrate( CreateMigrationsTable::class, + CreatePublishersTable::class, + CreateAuthorTable::class, CreateBookTable::class, ); From 3225ffcd5e48dbe87a8f8f8c8ce6bdd7eb4fe1c7 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 15:19:06 +0200 Subject: [PATCH 07/10] wip --- .../Commands/DatabaseSeedCommandTest.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index ded60371b..89588039b 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -53,9 +53,9 @@ public function test_seed_with_manually_selected_seeder(): void ->call(sprintf('db:seed --seeder=%s', SecondTestDatabaseSeeder::class)) ->assertSuccess(); + $this->assertSame(1, query(Book::class)->count()->execute()); $book = Book::get(1); $this->assertSame('Timeline Taxi 2', $book->title); - $this->assertSame(1, query(Book::class)->count()->execute()); } public function test_migrate_fresh_seed_with_manually_selected_seeder(): void @@ -64,9 +64,9 @@ public function test_migrate_fresh_seed_with_manually_selected_seeder(): void ->call(sprintf('migrate:fresh --seeder=%s', SecondTestDatabaseSeeder::class)) ->assertSuccess(); + $this->assertSame(1, query(Book::class)->count()->execute()); $book = Book::get(1); $this->assertSame('Timeline Taxi 2', $book->title); - $this->assertSame(1, query(Book::class)->count()->execute()); } public function test_seed_all(): void @@ -82,11 +82,11 @@ public function test_seed_all(): void ->call('db:seed --all') ->assertSuccess(); - $book = Book::get(1); - $this->assertSame('Timeline Taxi', $book->title); + $book = Book::select()->whereField('title', 'Timeline Taxi')->first(); + $this->assertNotNull($book); - $book = Book::get(2); - $this->assertSame('Timeline Taxi 2', $book->title); + $book = Book::select()->whereField('title', 'Timeline Taxi 2')->first(); + $this->assertNotNull($book); $this->assertSame(2, query(Book::class)->count()->execute()); } @@ -107,9 +107,9 @@ public function test_seed_when_only_one_seeder_is_available(): void ->call('db:seed') ->assertSuccess(); + $this->assertSame(1, query(Book::class)->count()->execute()); $book = Book::get(1); $this->assertSame('Timeline Taxi', $book->title); - $this->assertSame(1, query(Book::class)->count()->execute()); } public function test_seed_via_migrate_fresh(): void @@ -118,9 +118,13 @@ public function test_seed_via_migrate_fresh(): void ->call('migrate:fresh --seed --all') ->assertSuccess(); - $book = Book::get(1); + $this->assertSame(2, query(Book::class)->count()->execute()); - $this->assertSame('Timeline Taxi', $book->title); + $book = Book::select()->whereField('title', 'Timeline Taxi')->first(); + $this->assertNotNull($book); + + $book = Book::select()->whereField('title', 'Timeline Taxi 2')->first(); + $this->assertNotNull($book); } public function test_db_seed_caution(): void From b88e52e64e39a15dd23b18991638b693d197fe6a Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 15:19:51 +0200 Subject: [PATCH 08/10] wip --- .../Framework/Commands/DatabaseSeedCommandTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index 89588039b..d3f9a649e 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -94,7 +94,9 @@ public function test_seed_all(): void public function test_seed_when_only_one_seeder_is_available(): void { $seederConfig = $this->container->get(SeederConfig::class); - unset($seederConfig->seeders[1]); + $seederConfig->seeders = [ + TestDatabaseSeeder::class, + ]; $this->migrate( CreateMigrationsTable::class, From d8367687bf23128ba08ae89f271456536251acf1 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 15:20:43 +0200 Subject: [PATCH 09/10] wip --- .../Framework/Commands/DatabaseSeedCommandTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php index d3f9a649e..54630403b 100644 --- a/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php +++ b/tests/Integration/Framework/Commands/DatabaseSeedCommandTest.php @@ -35,8 +35,6 @@ public function test_seed_with_selected_seeder(): void ->submit() ->assertSuccess(); - $book = Book::get(1); - $this->assertSame('Timeline Taxi 2', $book->title); $this->assertSame(1, query(Book::class)->count()->execute()); } @@ -82,13 +80,13 @@ public function test_seed_all(): void ->call('db:seed --all') ->assertSuccess(); + $this->assertSame(2, query(Book::class)->count()->execute()); + $book = Book::select()->whereField('title', 'Timeline Taxi')->first(); $this->assertNotNull($book); $book = Book::select()->whereField('title', 'Timeline Taxi 2')->first(); $this->assertNotNull($book); - - $this->assertSame(2, query(Book::class)->count()->execute()); } public function test_seed_when_only_one_seeder_is_available(): void From cffe78b724291131bbc15036cc12024b79d5cde6 Mon Sep 17 00:00:00 2001 From: brendt Date: Fri, 4 Jul 2025 19:37:30 +0200 Subject: [PATCH 10/10] wip --- tests/Integration/Database/MultiDatabaseTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/MultiDatabaseTest.php b/tests/Integration/Database/MultiDatabaseTest.php index 32edc3ec5..ea0e223a5 100644 --- a/tests/Integration/Database/MultiDatabaseTest.php +++ b/tests/Integration/Database/MultiDatabaseTest.php @@ -188,14 +188,14 @@ public function test_migrate_up_command(): void public function test_migrate_fresh_command(): void { $this->console - ->call('migrate:fresh --database=main --all') + ->call('migrate:fresh --database=main') ->assertSuccess(); $this->assertTableExists(Migration::class, 'main'); $this->assertTableDoesNotExist(Migration::class, 'backup'); $this->console - ->call('migrate:fresh --database=backup --all') + ->call('migrate:fresh --database=backup') ->assertSuccess(); $this->assertTableExists(Migration::class, 'backup');