1515use Tempest \Database \Migrations \MigrationManager ;
1616use Tests \Tempest \Fixtures \Migrations \CreatePublishersTable ;
1717use Tests \Tempest \Fixtures \Modules \Books \Models \Publisher ;
18+ use Tests \Tempest \Integration \Database \Fixtures \MigrationForBackup ;
19+ use Tests \Tempest \Integration \Database \Fixtures \MigrationForMain ;
1820use Tests \Tempest \Integration \FrameworkIntegrationTestCase ;
1921use Tests \Tempest \Integration \TestingDatabaseInitializer ;
2022
@@ -168,18 +170,14 @@ public function test_migrate_up_command(): void
168170 ->call ('migrate:up --database=main ' )
169171 ->assertSuccess ();
170172
171- $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('main ' )->execute () > 0 );
172-
173- $ this ->assertException (
174- PDOException::class,
175- fn () => $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('backup ' )->execute () > 0 ),
176- );
173+ $ this ->assertTableExists (Migration::class, 'main ' );
174+ $ this ->assertTableDoesNotExist (Migration::class, 'backup ' );
177175
178176 $ this ->console
179177 ->call ('migrate:up --database=backup ' )
180178 ->assertSuccess ();
181179
182- $ this ->assertTrue ( query ( Migration::class)-> count ()-> onDatabase ( 'backup ' )-> execute () > 0 );
180+ $ this ->assertTableExists ( Migration::class, 'backup ' );
183181 }
184182
185183 public function test_migrate_fresh_command (): void
@@ -188,18 +186,14 @@ public function test_migrate_fresh_command(): void
188186 ->call ('migrate:fresh --database=main ' )
189187 ->assertSuccess ();
190188
191- $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('main ' )->execute () > 0 );
192-
193- $ this ->assertException (
194- PDOException::class,
195- fn () => $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('backup ' )->execute () > 0 ),
196- );
189+ $ this ->assertTableExists (Migration::class, 'main ' );
190+ $ this ->assertTableDoesNotExist (Migration::class, 'backup ' );
197191
198192 $ this ->console
199193 ->call ('migrate:fresh --database=backup ' )
200194 ->assertSuccess ();
201195
202- $ this ->assertTrue ( query ( Migration::class)-> count ()-> onDatabase ( 'backup ' )-> execute () > 0 );
196+ $ this ->assertTableExists ( Migration::class, 'backup ' );
203197 }
204198
205199 public function test_migrate_up_fresh_command (): void
@@ -208,18 +202,14 @@ public function test_migrate_up_fresh_command(): void
208202 ->call ('migrate:up --fresh --database=main ' )
209203 ->assertSuccess ();
210204
211- $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('main ' )->execute () > 0 );
212-
213- $ this ->assertException (
214- PDOException::class,
215- fn () => $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('backup ' )->execute () > 0 ),
216- );
205+ $ this ->assertTableExists (Migration::class, 'main ' );
206+ $ this ->assertTableDoesNotExist (Migration::class, 'backup ' );
217207
218208 $ this ->console
219209 ->call ('migrate:up --fresh --database=backup ' )
220210 ->assertSuccess ();
221211
222- $ this ->assertTrue ( query ( Migration::class)-> count ()-> onDatabase ( 'backup ' )-> execute () > 0 );
212+ $ this ->assertTableExists ( Migration::class, 'backup ' );
223213 }
224214
225215 public function test_migrate_down_command (): void
@@ -236,12 +226,8 @@ public function test_migrate_down_command(): void
236226 ->call ('migrate:down --database=backup ' )
237227 ->assertSuccess ();
238228
239- $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('main ' )->execute () > 0 );
240-
241- $ this ->assertException (
242- PDOException::class,
243- fn () => $ this ->assertTrue (query (Migration::class)->count ()->onDatabase ('backup ' )->execute () > 0 ),
244- );
229+ $ this ->assertTableExists (Migration::class, 'main ' );
230+ $ this ->assertTableDoesNotExist (Migration::class, 'backup ' );
245231 }
246232
247233 public function test_migrate_validate_command (): void
@@ -250,4 +236,48 @@ public function test_migrate_validate_command(): void
250236 ->call ('migrate:validate --database=main ' )
251237 ->assertSuccess ();
252238 }
239+
240+ public function test_should_migrate (): void
241+ {
242+ /** @var MigrationManager $migrationManager */
243+ $ migrationManager = $ this ->container ->get (MigrationManager::class);
244+
245+ $ migrationManager ->onDatabase ('main ' )->executeUp (new CreateMigrationsTable ());
246+ $ migrationManager ->onDatabase ('backup ' )->executeUp (new CreateMigrationsTable ());
247+
248+ $ migrationManager ->onDatabase ('main ' )->executeUp (new MigrationForMain ());
249+ $ migrationManager ->onDatabase ('main ' )->executeUp (new MigrationForBackup ());
250+
251+ $ this ->assertTableExists ('main_table ' , 'main ' );
252+ $ this ->assertTableDoesNotExist ('backup_table ' , 'main ' );
253+
254+ $ migrationManager ->onDatabase ('backup ' )->executeUp (new MigrationForMain ());
255+ $ migrationManager ->onDatabase ('backup ' )->executeUp (new MigrationForBackup ());
256+
257+ $ this ->assertTableExists ('backup_table ' , 'backup ' );
258+ $ this ->assertTableDoesNotExist ('main_table ' , 'backup ' );
259+
260+ $ migrationManager ->onDatabase ('main ' )->executeDown (new MigrationForMain ());
261+ $ migrationManager ->onDatabase ('main ' )->executeDown (new MigrationForBackup ());
262+ $ this ->assertTableDoesNotExist ('main_table ' , 'main ' );
263+ $ this ->assertTableDoesNotExist ('backup_table ' , 'main ' );
264+
265+ $ migrationManager ->onDatabase ('backup ' )->executeDown (new MigrationForBackup ());
266+ $ migrationManager ->onDatabase ('backup ' )->executeDown (new MigrationForMain ());
267+ $ this ->assertTableDoesNotExist ('backup_table ' , 'backup ' );
268+ $ this ->assertTableDoesNotExist ('main_table ' , 'backup ' );
269+ }
270+
271+ private function assertTableExists (string $ tableName , string $ onDatabase ): void
272+ {
273+ $ this ->assertTrue (query ($ tableName )->count ()->onDatabase ($ onDatabase )->execute () >= 0 );
274+ }
275+
276+ private function assertTableDoesNotExist (string $ tableName , string $ onDatabase ): void
277+ {
278+ $ this ->assertException (
279+ PDOException::class,
280+ fn () => query ($ tableName )->count ()->onDatabase ($ onDatabase )->execute (),
281+ );
282+ }
253283}
0 commit comments