1313
1414namespace CodeIgniter \Commands ;
1515
16+ use CodeIgniter \CLI \CLI ;
1617use CodeIgniter \Test \CIUnitTestCase ;
18+ use CodeIgniter \Test \Mock \MockInputOutput ;
1719use CodeIgniter \Test \StreamFilterTrait ;
1820use PHPUnit \Framework \Attributes \DataProvider ;
1921use PHPUnit \Framework \Attributes \Group ;
@@ -31,6 +33,9 @@ protected function setUp(): void
3133 parent ::setUp ();
3234
3335 $ this ->resetStreamFilterBuffer ();
36+
37+ putenv ('NO_COLOR=1 ' );
38+ CLI ::init ();
3439 }
3540
3641 protected function tearDown (): void
@@ -39,19 +44,22 @@ protected function tearDown(): void
3944
4045 $ this ->clearTestFiles ();
4146 $ this ->resetStreamFilterBuffer ();
47+
48+ putenv ('NO_COLOR ' );
49+ CLI ::init ();
4250 }
4351
4452 private function clearTestFiles (): void
4553 {
46- $ result = str_replace ([ "\033 [0;32m " , "\033 [0m " , "\n" ], '' , $ this ->getStreamFilterBuffer ());
54+ preg_match ( ' /File created: (.*)/ ' , $ this ->getStreamFilterBuffer (), $ result );
4755
48- $ file = str_replace ('ROOTPATH ' . DIRECTORY_SEPARATOR , ROOTPATH , trim ( substr ( $ result, strlen ( ' File created: ' ))) );
56+ $ file = str_replace ('ROOTPATH ' . DIRECTORY_SEPARATOR , ROOTPATH , $ result[ 1 ] ?? '' );
4957 if (is_file ($ file )) {
5058 unlink ($ file );
5159 }
5260
5361 $ dir = dirname ($ file ) . DIRECTORY_SEPARATOR ;
54- if (is_dir ($ dir ) && ! in_array ($ dir , [TESTPATH , TESTPATH . 'system/ ' , TESTPATH . '_support/ ' ], true )) {
62+ if (is_dir ($ dir ) && ! in_array ($ dir , [' / ' , TESTPATH , TESTPATH . 'system/ ' , TESTPATH . '_support/ ' ], true )) {
5563 rmdir ($ dir );
5664 }
5765 }
@@ -81,4 +89,31 @@ public static function provideGenerateTestFiles(): iterable
8189 // the 4 slashes are needed to escape here and in the command
8290 yield 'namespace style class name ' => ['Foo \\\\Bar ' , 'Foo/BarTest ' ];
8391 }
92+
93+ public function testGenerateTestWithEmptyClassName (): void
94+ {
95+ try {
96+ $ io = new MockInputOutput ();
97+ CLI ::setInputOutput ($ io );
98+
99+ // Simulate running `make:test` with no input followed by entering `Foo`
100+ $ io ->setInputs (['' , 'Foo ' ]);
101+ command ('make:test ' );
102+
103+ $ expectedFile = ROOTPATH . 'tests/FooTest.php ' ;
104+
105+ $ expectedOutput = 'Test class name : ' . PHP_EOL ;
106+ $ expectedOutput .= 'The "Test class name" field is required. ' . PHP_EOL ;
107+ $ expectedOutput .= 'Test class name : Foo ' . PHP_EOL . PHP_EOL ;
108+ $ expectedOutput .= 'File created: ROOTPATH/tests/FooTest.php ' . PHP_EOL . PHP_EOL ;
109+ $ this ->assertSame ($ expectedOutput , $ io ->getOutput ());
110+ $ this ->assertFileExists ($ expectedFile );
111+ } finally {
112+ if (is_file ($ expectedFile )) {
113+ unlink ($ expectedFile );
114+ }
115+
116+ CLI ::resetInputOutput ();
117+ }
118+ }
84119}
0 commit comments