13
13
14
14
namespace CodeIgniter \Commands \Utilities ;
15
15
16
+ use Closure ;
16
17
use CodeIgniter \Test \CIUnitTestCase ;
17
18
use CodeIgniter \Test \StreamFilterTrait ;
18
19
use Config \App ;
20
+ use Kint \Kint ;
21
+ use Kint \Renderer \CliRenderer ;
19
22
use PHPUnit \Framework \Attributes \Group ;
20
23
21
24
/**
@@ -26,6 +29,26 @@ final class ConfigCheckTest extends CIUnitTestCase
26
29
{
27
30
use StreamFilterTrait;
28
31
32
+ public static function setUpBeforeClass (): void
33
+ {
34
+ App::$ override = false ;
35
+
36
+ putenv ('NO_COLOR=1 ' );
37
+ CliRenderer::$ cli_colors = false ;
38
+
39
+ parent ::setUpBeforeClass ();
40
+ }
41
+
42
+ public static function tearDownAfterClass (): void
43
+ {
44
+ App::$ override = true ;
45
+
46
+ putenv ('NO_COLOR ' );
47
+ CliRenderer::$ cli_colors = true ;
48
+
49
+ parent ::tearDownAfterClass ();
50
+ }
51
+
29
52
protected function setUp (): void
30
53
{
31
54
$ this ->resetServices ();
@@ -38,188 +61,71 @@ protected function tearDown(): void
38
61
parent ::tearDown ();
39
62
}
40
63
41
- protected function getBuffer ()
42
- {
43
- return $ this ->getStreamFilterBuffer ();
44
- }
45
-
46
- public function testCommandConfigCheckNoArg (): void
64
+ public function testCommandConfigCheckWithNoArgumentPassed (): void
47
65
{
48
66
command ('config:check ' );
49
67
50
- $ this ->assertStringContainsString (
51
- 'You must specify a Config classname. ' ,
52
- $ this ->getBuffer ()
53
- );
54
- }
68
+ $ this ->assertSame (
69
+ <<<'EOF'
70
+ You must specify a Config classname.
71
+ Usage: config:check <classname>
72
+ Example: config:check App
73
+ config:check 'CodeIgniter\Shield\Config\Auth'
55
74
56
- public function testCommandConfigCheckApp (): void
57
- {
58
- command ('config:check App ' );
59
-
60
- $ this ->assertStringContainsString (App::class, $ this ->getBuffer ());
61
- $ this ->assertStringContainsString ("public 'baseURL " , $ this ->getBuffer ());
75
+ EOF,
76
+ str_replace ("\n\n" , "\n" , $ this ->getStreamFilterBuffer ())
77
+ );
62
78
}
63
79
64
80
public function testCommandConfigCheckNonexistentClass (): void
65
81
{
66
82
command ('config:check Nonexistent ' );
67
83
68
- $ this ->assertStringContainsString (
69
- ' No such Config class: Nonexistent ' ,
70
- $ this ->getBuffer ()
84
+ $ this ->assertSame (
85
+ " No such Config class: Nonexistent \n" ,
86
+ $ this ->getStreamFilterBuffer ()
71
87
);
72
88
}
73
89
74
- public function testGetKintD (): void
90
+ public function testConfigCheckWithKintEnabledUsesKintD (): void
75
91
{
76
- $ command = new ConfigCheck (service ('logger ' ), service ('commands ' ));
77
- $ getKintD = $ this ->getPrivateMethodInvoker ($ command , 'getKintD ' );
92
+ /** @var Closure(object): string $command */
93
+ $ command = $ this ->getPrivateMethodInvoker (
94
+ new ConfigCheck (service ('logger ' ), service ('commands ' )),
95
+ 'getKintD '
96
+ );
78
97
79
- $ output = $ getKintD ( new App () );
98
+ command ( ' config:check App' );
80
99
81
- $ output = preg_replace (
82
- '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u ' ,
83
- '' ,
84
- $ output
100
+ $ this ->assertSame (
101
+ $ command (config ('App ' )) . "\n" ,
102
+ preg_replace ('/\s+Config Caching: \S+/ ' , '' , $ this ->getStreamFilterBuffer ())
85
103
);
104
+ }
86
105
87
- $ this ->assertStringContainsString (
88
- 'Config\App# ' ,
89
- $ output
106
+ public function testConfigCheckWithKintDisabledUsesVarDump (): void
107
+ {
108
+ /** @var Closure(object): string $command */
109
+ $ command = $ this ->getPrivateMethodInvoker (
110
+ new ConfigCheck (service ('logger ' ), service ('commands ' )),
111
+ 'getVarDump '
90
112
);
91
- $ this ->assertStringContainsString (
92
- <<<'EOL'
93
- (
94
- public 'baseURL' -> string (19) "http://example.com/"
95
- public 'allowedHostnames' -> array (0) []
96
- public 'indexPage' -> string (9) "index.php"
97
- public 'uriProtocol' -> string (11) "REQUEST_URI"
98
- public 'permittedURIChars' -> string (14) "a-z 0-9~%.:_\-"
99
- public 'defaultLocale' -> string (2) "en"
100
- public 'negotiateLocale' -> boolean false
101
- public 'supportedLocales' -> array (1) [
102
- 0 => string (2) "en"
103
- ]
104
- public 'appTimezone' -> string (3) "UTC"
105
- public 'charset' -> string (5) "UTF-8"
106
- public 'forceGlobalSecureRequests' -> boolean false
107
- public 'proxyIPs' -> array (0) []
108
- public 'CSPEnabled' -> boolean false
109
- EOL,
110
- $ output
113
+ $ clean = static fn (string $ input ): string => preg_replace (
114
+ '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u ' ,
115
+ '' ,
116
+ $ input
111
117
);
112
- }
113
118
114
- public function testGetVarDump (): void
115
- {
116
- $ command = new ConfigCheck (service ('logger ' ), service ('commands ' ));
117
- $ getVarDump = $ this ->getPrivateMethodInvoker ($ command , 'getVarDump ' );
118
-
119
- $ output = $ getVarDump (new App ());
120
-
121
- if (
122
- ini_get ('xdebug.mode ' )
123
- && in_array (
124
- 'develop ' ,
125
- explode (', ' , ini_get ('xdebug.mode ' )),
126
- true
127
- )
128
- ) {
129
- // Xdebug force adds colors on xdebug.cli_color=2
130
- $ output = preg_replace (
131
- '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u ' ,
132
- '' ,
133
- $ output
134
- );
119
+ try {
120
+ Kint::$ enabled_mode = false ;
121
+ command ('config:check App ' );
135
122
136
- // Xdebug overloads var_dump().
137
- $ this ->assertStringContainsString (
138
- 'class Config\App# ' ,
139
- $ output
140
- );
141
- $ this ->assertStringContainsString (
142
- <<<'EOL'
143
- {
144
- public string $baseURL =>
145
- string(19) "http://example.com/"
146
- public array $allowedHostnames =>
147
- array(0) {
148
- }
149
- public string $indexPage =>
150
- string(9) "index.php"
151
- public string $uriProtocol =>
152
- string(11) "REQUEST_URI"
153
- public string $permittedURIChars =>
154
- string(14) "a-z 0-9~%.:_\-"
155
- public string $defaultLocale =>
156
- string(2) "en"
157
- public bool $negotiateLocale =>
158
- bool(false)
159
- public array $supportedLocales =>
160
- array(1) {
161
- [0] =>
162
- string(2) "en"
163
- }
164
- public string $appTimezone =>
165
- string(3) "UTC"
166
- public string $charset =>
167
- string(5) "UTF-8"
168
- public bool $forceGlobalSecureRequests =>
169
- bool(false)
170
- public array $proxyIPs =>
171
- array(0) {
172
- }
173
- public bool $CSPEnabled =>
174
- bool(false)
175
- }
176
- EOL,
177
- $ output
178
- );
179
- } else {
180
- // PHP's var_dump().
181
- $ this ->assertStringContainsString (
182
- 'object(Config\App)# ' ,
183
- $ output
184
- );
185
- $ this ->assertStringContainsString (
186
- <<<'EOL'
187
- {
188
- ["baseURL"]=>
189
- string(19) "http://example.com/"
190
- ["allowedHostnames"]=>
191
- array(0) {
192
- }
193
- ["indexPage"]=>
194
- string(9) "index.php"
195
- ["uriProtocol"]=>
196
- string(11) "REQUEST_URI"
197
- ["permittedURIChars"]=>
198
- string(14) "a-z 0-9~%.:_\-"
199
- ["defaultLocale"]=>
200
- string(2) "en"
201
- ["negotiateLocale"]=>
202
- bool(false)
203
- ["supportedLocales"]=>
204
- array(1) {
205
- [0]=>
206
- string(2) "en"
207
- }
208
- ["appTimezone"]=>
209
- string(3) "UTC"
210
- ["charset"]=>
211
- string(5) "UTF-8"
212
- ["forceGlobalSecureRequests"]=>
213
- bool(false)
214
- ["proxyIPs"]=>
215
- array(0) {
216
- }
217
- ["CSPEnabled"]=>
218
- bool(false)
219
- }
220
- EOL,
221
- $ output
123
+ $ this ->assertSame (
124
+ $ clean ($ command (config ('App ' ))),
125
+ $ clean (preg_replace ('/\s+Config Caching: \S+/ ' , '' , $ this ->getStreamFilterBuffer ()))
222
126
);
127
+ } finally {
128
+ Kint::$ enabled_mode = true ;
223
129
}
224
130
}
225
131
}
0 commit comments