|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Integration\Database; |
| 3 | +namespace Tests\Tempest\Integration\Database; |
4 | 4 |
|
5 | 5 | use PDOException; |
6 | 6 | use Tempest\Container\Exceptions\TaggedDependencyCouldNotBeResolved; |
|
13 | 13 | use Tempest\Database\Migrations\CreateMigrationsTable; |
14 | 14 | use Tempest\Database\Migrations\Migration; |
15 | 15 | use Tempest\Database\Migrations\MigrationManager; |
| 16 | +use Tests\Tempest\Fixtures\Migrations\CreateBookTable; |
16 | 17 | use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable; |
| 18 | +use Tests\Tempest\Fixtures\Modules\Books\Models\Book; |
17 | 19 | use Tests\Tempest\Fixtures\Modules\Books\Models\Publisher; |
18 | 20 | use Tests\Tempest\Integration\Database\Fixtures\MigrationForBackup; |
19 | 21 | use Tests\Tempest\Integration\Database\Fixtures\MigrationForMain; |
@@ -271,6 +273,68 @@ public function test_should_migrate(): void |
271 | 273 | $this->assertTableDoesNotExist('main_table', 'backup'); |
272 | 274 | } |
273 | 275 |
|
| 276 | + public function test_database_seed_on_selected_database(): void |
| 277 | + { |
| 278 | + /** @var MigrationManager $migrationManager */ |
| 279 | + $migrationManager = $this->container->get(MigrationManager::class); |
| 280 | + |
| 281 | + $migrationManager->onDatabase('main')->executeUp(new CreateMigrationsTable()); |
| 282 | + $migrationManager->onDatabase('main')->executeUp(new CreateBookTable()); |
| 283 | + $migrationManager->onDatabase('backup')->executeUp(new CreateMigrationsTable()); |
| 284 | + $migrationManager->onDatabase('backup')->executeUp(new CreateBookTable()); |
| 285 | + |
| 286 | + $this->console |
| 287 | + ->call('db:seed --database=main --all') |
| 288 | + ->assertSuccess(); |
| 289 | + |
| 290 | + $this->assertSame( |
| 291 | + 'Timeline Taxi', |
| 292 | + query(Book::class)->select()->onDatabase('main')->first()->title, |
| 293 | + ); |
| 294 | + |
| 295 | + $this->assertNull( |
| 296 | + query(Book::class)->select()->onDatabase('backup')->first(), |
| 297 | + ); |
| 298 | + |
| 299 | + $this->console |
| 300 | + ->call('db:seed --database=backup --all') |
| 301 | + ->assertSuccess(); |
| 302 | + |
| 303 | + /** @var Book $book */ |
| 304 | + /** @phpstan-ignore-next-line */ |
| 305 | + $book = query(Book::class)->select()->onDatabase('backup')->first(); |
| 306 | + |
| 307 | + $this->assertSame( |
| 308 | + 'Timeline Taxi', |
| 309 | + $book->title, |
| 310 | + ); |
| 311 | + } |
| 312 | + |
| 313 | + public function test_migrate_fresh_seed_on_selected_database(): void |
| 314 | + { |
| 315 | + $this->console |
| 316 | + ->call('migrate:fresh --seed --database=main --all') |
| 317 | + ->assertSuccess(); |
| 318 | + |
| 319 | + $this->assertSame( |
| 320 | + 'Timeline Taxi', |
| 321 | + query(Book::class)->select()->onDatabase('main')->first()->title, |
| 322 | + ); |
| 323 | + |
| 324 | + $this->assertException(PDOException::class, function (): void { |
| 325 | + query(Book::class)->select()->onDatabase('backup')->first(); |
| 326 | + }); |
| 327 | + |
| 328 | + $this->console |
| 329 | + ->call('migrate:fresh --seed --database=backup --all') |
| 330 | + ->assertSuccess(); |
| 331 | + |
| 332 | + $this->assertSame( |
| 333 | + 'Timeline Taxi', |
| 334 | + query(Book::class)->select()->onDatabase('backup')->first()->title, |
| 335 | + ); |
| 336 | + } |
| 337 | + |
274 | 338 | private function assertTableExists(string $tableName, string $onDatabase): void |
275 | 339 | { |
276 | 340 | $this->assertTrue(query($tableName)->count()->onDatabase($onDatabase)->execute() >= 0); |
|
0 commit comments