1414namespace CodeIgniter \Commands ;
1515
1616use CodeIgniter \Test \CIUnitTestCase ;
17- use CodeIgniter \Test \DatabaseTestTrait ;
1817use CodeIgniter \Test \StreamFilterTrait ;
1918use Config \Database ;
2019use PHPUnit \Framework \Attributes \Group ;
2726#[Group('DatabaseLive ' )]
2827final class MigrationIntegrationTest extends CIUnitTestCase
2928{
30- use DatabaseTestTrait;
3129 use StreamFilterTrait;
3230
33- private string $ migrationVersion = '20990101000000 ' ;
34- private string $ migrationFileFrom = SUPPORTPATH . 'Database/Migrations/20160428212500_Create_test_tables.php ' ;
35- private string $ migrationFileTo = APPPATH . 'Database/Migrations/20990101000000_Create_test_tables.php ' ;
36-
3731 protected function setUp (): void
3832 {
39- if (is_file ($ this ->migrationFileTo )) {
40- @unlink ($ this ->migrationFileTo );
41- }
42-
4333 $ this ->resetServices ();
4434
4535 parent ::setUp ();
4636
47- if (! is_file ($ this ->migrationFileFrom )) {
48- $ this ->fail (clean_path ($ this ->migrationFileFrom ) . ' is not found. ' );
49- }
50-
51- copy ($ this ->migrationFileFrom , $ this ->migrationFileTo );
52-
53- $ contents = file_get_contents ($ this ->migrationFileTo );
54- $ contents = str_replace ('namespace Tests\Support\Database\Migrations; ' , 'namespace App\Database\Migrations; ' , $ contents );
55- file_put_contents ($ this ->migrationFileTo , $ contents );
56-
57- $ this ->resetServices ();
37+ service ('migrations ' )->clearHistory ();
38+ $ this ->dropTestTables ();
5839 }
5940
6041 protected function tearDown (): void
6142 {
62- Database::connect ()
63- ->table ('migrations ' )
64- ->where ('version ' , $ this ->migrationVersion )
65- ->where ('namespace ' , 'App ' )
66- ->delete ();
67-
68- if (is_file ($ this ->migrationFileTo )) {
69- @unlink ($ this ->migrationFileTo );
70- }
43+ service ('migrations ' )->clearHistory ();
44+ $ this ->dropTestTables ();
7145
7246 $ this ->resetServices ();
7347
@@ -76,19 +50,40 @@ protected function tearDown(): void
7650
7751 public function testMigrationWithRollbackHasSameNameFormat (): void
7852 {
79- command ('migrate -n App ' );
53+ command ('migrate -n Tests \\\\ Support ' );
8054 $ this ->assertStringContainsString (
81- '(App) 20990101000000_App \Database\Migrations\Migration_Create_test_tables ' ,
55+ '(Tests\Support) 20160428212500_Tests\Support \Database\Migrations\Migration_Create_test_tables ' ,
8256 $ this ->getStreamFilterBuffer (),
8357 );
8458
8559 $ this ->resetStreamFilterBuffer ();
8660 $ this ->resetServices ();
8761
88- command ('migrate:rollback -n App ' );
62+ command ('migrate:rollback ' );
8963 $ this ->assertStringContainsString (
90- '(App) 20990101000000_App \Database\Migrations\Migration_Create_test_tables ' ,
64+ '(Tests\Support) 20160428212500_Tests\Support \Database\Migrations\Migration_Create_test_tables ' ,
9165 $ this ->getStreamFilterBuffer (),
9266 );
9367 }
68+
69+ private function dropTestTables (): void
70+ {
71+ $ forge = Database::forge ();
72+
73+ foreach ([
74+ 'user ' ,
75+ 'job ' ,
76+ 'misc ' ,
77+ 'type_test ' ,
78+ 'empty ' ,
79+ 'secondary ' ,
80+ 'stringifypkey ' ,
81+ 'without_auto_increment ' ,
82+ 'ip_table ' ,
83+ 'ci_sessions ' ,
84+ 'migrations_lock ' ,
85+ ] as $ table ) {
86+ $ forge ->dropTable ($ table , true );
87+ }
88+ }
9489}
0 commit comments