1313
1414namespace CodeIgniter \Commands \Generators ;
1515
16+ use CodeIgniter \CLI \CLI ;
1617use CodeIgniter \Config \Factories ;
1718use CodeIgniter \Test \CIUnitTestCase ;
1819use CodeIgniter \Test \StreamFilterTrait ;
@@ -27,61 +28,137 @@ final class MigrationGeneratorTest extends CIUnitTestCase
2728{
2829 use StreamFilterTrait;
2930
31+ protected function setUp (): void
32+ {
33+ parent ::setUp ();
34+
35+ CLI ::reset ();
36+ }
37+
3038 protected function tearDown (): void
3139 {
40+ parent ::tearDown ();
41+
42+ CLI ::reset ();
3243 Factories ::reset ('config ' );
3344
34- $ result = str_replace (["\033[0;32m " , "\033[0m " , "\n" ], '' , $ this ->getStreamFilterBuffer ());
35- $ file = str_replace ('APPPATH ' . DIRECTORY_SEPARATOR , APPPATH , trim (substr ($ result , 14 )));
36- if (is_file ($ file )) {
45+ foreach (glob (APPPATH . 'Database/Migrations/*_*.php ' ) as $ file ) {
3746 unlink ($ file );
3847 }
3948 }
4049
50+ private function getUndecoratedBuffer (): string
51+ {
52+ return preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()) ?? '' ;
53+ }
54+
55+ private function getContents (string $ basename ): string
56+ {
57+ $ files = glob (APPPATH . 'Database/Migrations/*_ ' . $ basename . '.php ' );
58+ $ this ->assertCount (1 , $ files );
59+
60+ $ contents = file_get_contents ($ files [0 ]);
61+ $ this ->assertIsString ($ contents );
62+
63+ return $ contents ;
64+ }
65+
66+ private function injectSessionsGroup (string $ driver ): void
67+ {
68+ $ config = new class () extends Database {
69+ /**
70+ * @var array<string, string>
71+ */
72+ public array $ sessions = [];
73+ };
74+ $ config ->sessions ['DBDriver ' ] = $ driver ;
75+
76+ Factories ::injectMock ('config ' , 'Database ' , $ config );
77+ }
78+
4179 public function testGenerateMigration (): void
4280 {
4381 command ('make:migration database ' );
44- $ this ->assertStringContainsString ('_Database.php ' , $ this ->getStreamFilterBuffer ());
82+
83+ $ this ->assertMatchesRegularExpression (
84+ '#^\nFile created: APPPATH/Database/Migrations/\d{4}-\d{2}-\d{2}-\d{6}_Database\.php\n$# ' ,
85+ $ this ->getUndecoratedBuffer (),
86+ );
87+
88+ $ contents = $ this ->getContents ('Database ' );
89+ $ this ->assertStringContainsString ('namespace App\Database\Migrations; ' , $ contents );
90+ $ this ->assertStringContainsString ('class Database extends Migration ' , $ contents );
91+ $ this ->assertStringNotContainsString ('$DBGroup ' , $ contents );
4592 }
4693
4794 public function testGenerateMigrationWithOptionSession (): void
4895 {
49- command ('make:migration -session ' );
50- $ this ->assertStringContainsString ('_CreateCiSessionsTable.php ' , $ this ->getStreamFilterBuffer ());
96+ command ('make:migration --session ' );
97+
98+ $ contents = $ this ->getContents ('CreateCiSessionsTable ' );
99+ $ this ->assertStringContainsString ('class CreateCiSessionsTable extends Migration ' , $ contents );
100+ $ this ->assertStringContainsString ("protected \$DBGroup = 'default'; " , $ contents );
101+ $ this ->assertStringContainsString ("\$this->forge->addKey('id', true); " , $ contents );
102+ $ this ->assertStringContainsString ("\$this->forge->createTable('ci_sessions', true); " , $ contents );
51103 }
52104
53- public function testGenerateMigrationWithOptionTable (): void
105+ public function testSessionIgnoresNameArgument (): void
54106 {
55- command ('make:migration -session -table logger ' );
56- $ this ->assertStringContainsString ('_CreateLoggerTable.php ' , $ this ->getStreamFilterBuffer ());
107+ command ('make:migration database --session ' );
108+
109+ $ this ->assertStringContainsString ('_CreateCiSessionsTable.php ' , $ this ->getUndecoratedBuffer ());
110+ $ this ->assertFileDoesNotExist (APPPATH . 'Database/Migrations/Database.php ' );
111+ }
112+
113+ public function testGenerateMigrationWithOptionTableAndDbGroup (): void
114+ {
115+ $ this ->injectSessionsGroup ('Postgre ' );
116+
117+ command ('make:migration --session --table logger --dbgroup sessions ' );
118+
119+ $ contents = $ this ->getContents ('CreateLoggerTable ' );
120+ $ this ->assertStringContainsString ("protected \$DBGroup = 'sessions'; " , $ contents );
121+ $ this ->assertStringContainsString ("'ip_address inet NOT NULL', " , $ contents );
122+ $ this ->assertStringContainsString ("\$this->forge->createTable('logger', true); " , $ contents );
123+ $ this ->assertStringContainsString ("\$this->forge->dropTable('logger', true); " , $ contents );
124+ }
125+
126+ public function testGenerateMigrationWithShortcuts (): void
127+ {
128+ $ this ->injectSessionsGroup ('MySQLi ' );
129+
130+ command ('make:migration --session -t logger -g sessions ' );
131+
132+ $ contents = $ this ->getContents ('CreateLoggerTable ' );
133+ $ this ->assertStringContainsString ("protected \$DBGroup = 'sessions'; " , $ contents );
134+ $ this ->assertStringContainsString ("\$this->forge->createTable('logger', true); " , $ contents );
57135 }
58136
59137 public function testSessionRejectsUnsupportedDriver (): void
60138 {
61- $ config = new Database ();
62- $ config ->default ['DBDriver ' ] = 'SQLite3 ' ;
63- Factories ::injectMock ('config ' , 'Database ' , $ config );
139+ $ this ->injectSessionsGroup ('SQLite3 ' );
64140
65- command ('make:migration -session ' );
141+ command ('make:migration -- session --dbgroup sessions ' );
66142
67- $ this ->assertStringContainsString (
68- ' Database sessions are only supported on MySQLi and Postgre. The "default " database group uses the "SQLite3" driver.' ,
69- $ this ->getStreamFilterBuffer (),
143+ $ this ->assertSame (
144+ "\n Database sessions are only supported on MySQLi and Postgre. The \" sessions \ " database group uses the \ "SQLite3 \ " driver. \n" ,
145+ $ this ->getUndecoratedBuffer (),
70146 );
71147 $ this ->assertSame ([], glob (APPPATH . 'Database/Migrations/*_CreateCiSessionsTable.php ' ));
72148 }
73149
74150 public function testSessionRejectsUndefinedGroup (): void
75151 {
76- command ('make:migration -session -dbgroup bogus ' );
152+ command ('make:migration -- session - -dbgroup bogus ' );
77153
78- $ this ->assertStringContainsString ( ' The "bogus" database group is not defined.' , $ this ->getStreamFilterBuffer ());
154+ $ this ->assertSame ( "\n The \ "bogus \ " database group is not defined. \n" , $ this ->getUndecoratedBuffer ());
79155 $ this ->assertSame ([], glob (APPPATH . 'Database/Migrations/*_CreateCiSessionsTable.php ' ));
80156 }
81157
82158 public function testGenerateMigrationWithOptionSuffix (): void
83159 {
84- command ('make:migration database -suffix ' );
85- $ this ->assertStringContainsString ('_DatabaseMigration.php ' , $ this ->getStreamFilterBuffer ());
160+ command ('make:migration database --suffix ' );
161+
162+ $ this ->assertStringContainsString ('class DatabaseMigration extends Migration ' , $ this ->getContents ('DatabaseMigration ' ));
86163 }
87164}
0 commit comments