1313
1414namespace CodeIgniter \Commands \Generators ;
1515
16+ use CodeIgniter \CLI \CLI ;
1617use CodeIgniter \Test \CIUnitTestCase ;
1718use CodeIgniter \Test \StreamFilterTrait ;
1819use PHPUnit \Framework \Attributes \Group ;
@@ -25,24 +26,78 @@ final class ConfigGeneratorTest extends CIUnitTestCase
2526{
2627 use StreamFilterTrait;
2728
29+ protected function setUp (): void
30+ {
31+ parent ::setUp ();
32+
33+ CLI ::reset ();
34+ }
35+
2836 protected function tearDown (): void
2937 {
30- $ result = str_replace (["\033[0;32m " , "\033[0m " , "\n" ], '' , $ this ->getStreamFilterBuffer ());
31- $ file = str_replace ('APPPATH ' . DIRECTORY_SEPARATOR , APPPATH , trim (substr ($ result , 14 )));
32- if (is_file ($ file )) {
33- unlink ($ file );
38+ parent ::tearDown ();
39+
40+ CLI ::reset ();
41+
42+ foreach (['Auth.php ' , 'AuthConfig.php ' ] as $ file ) {
43+ if (is_file (APPPATH . 'Config/ ' . $ file )) {
44+ unlink (APPPATH . 'Config/ ' . $ file );
45+ }
3446 }
47+
48+ if (is_dir (APPPATH . 'Config/Sub ' )) {
49+ helper ('filesystem ' );
50+ delete_files (APPPATH . 'Config/Sub ' , true );
51+ rmdir (APPPATH . 'Config/Sub ' );
52+ }
53+
54+ if (is_file (SUPPORTPATH . 'Config/Auth.php ' )) {
55+ unlink (SUPPORTPATH . 'Config/Auth.php ' );
56+ }
57+ }
58+
59+ private function getUndecoratedBuffer (): string
60+ {
61+ return preg_replace ('/\e\[[^m]+m/ ' , '' , $ this ->getStreamFilterBuffer ()) ?? '' ;
3562 }
3663
3764 public function testGenerateConfig (): void
3865 {
3966 command ('make:config auth ' );
40- $ this ->assertFileExists (APPPATH . 'Config/Auth.php ' );
67+
68+ $ this ->assertSame (
69+ PHP_EOL . 'File created: ' . clean_path (APPPATH . 'Config/Auth.php ' ) . PHP_EOL ,
70+ $ this ->getUndecoratedBuffer (),
71+ );
72+
73+ $ content = file_get_contents (APPPATH . 'Config/Auth.php ' );
74+ $ this ->assertIsString ($ content );
75+ $ this ->assertStringContainsString ('namespace Config; ' , $ content );
76+ $ this ->assertStringContainsString ('class Auth extends BaseConfig ' , $ content );
77+ }
78+
79+ public function testGenerateConfigInSubdirectory (): void
80+ {
81+ command ('make:config sub/auth ' );
82+
83+ $ content = file_get_contents (APPPATH . 'Config/Sub/Auth.php ' );
84+ $ this ->assertIsString ($ content );
85+ $ this ->assertStringContainsString ('namespace Config \\Sub; ' , $ content );
86+ }
87+
88+ public function testGenerateConfigWithOtherNamespaceKeepsFullNamespace (): void
89+ {
90+ command ('make:config auth --namespace Tests \\\\Support ' );
91+
92+ $ content = file_get_contents (SUPPORTPATH . 'Config/Auth.php ' );
93+ $ this ->assertIsString ($ content );
94+ $ this ->assertStringContainsString ('namespace Tests \\Support \\Config; ' , $ content );
4195 }
4296
43- public function testGenerateConfigWithOptionSuffix (): void
97+ public function testGenerateConfigWithSuffix (): void
4498 {
45- command ('make:config auth -suffix ' );
99+ command ('make:config auth --suffix ' );
100+
46101 $ this ->assertFileExists (APPPATH . 'Config/AuthConfig.php ' );
47102 }
48103}
0 commit comments