@@ -39,18 +39,21 @@ protected function tearDown(): void
3939 public function testNew ()
4040 {
4141 $ actual = new CLI ();
42+
4243 $ this ->assertInstanceOf (CLI ::class, $ actual );
4344 }
4445
4546 public function testBeep ()
4647 {
4748 $ this ->expectOutputString ("\x07" );
49+
4850 CLI ::beep ();
4951 }
5052
5153 public function testBeep4 ()
5254 {
5355 $ this ->expectOutputString ("\x07\x07\x07\x07" );
56+
5457 CLI ::beep (4 );
5558 }
5659
@@ -96,6 +99,7 @@ public function testIsWindows()
9699 public function testNewLine ()
97100 {
98101 $ this ->expectOutputString ('' );
102+
99103 CLI ::newLine ();
100104 }
101105
@@ -119,19 +123,21 @@ public function testColorSupportOnNoColor()
119123 {
120124 $ nocolor = getenv ('NO_COLOR ' );
121125 putenv ('NO_COLOR=1 ' );
122-
123126 CLI ::init (); // force re-check on env
127+
124128 $ this ->assertSame ('test ' , CLI ::color ('test ' , 'white ' , 'green ' ));
129+
125130 putenv ($ nocolor ? "NO_COLOR= {$ nocolor }" : 'NO_COLOR ' );
126131 }
127132
128133 public function testColorSupportOnHyperTerminals ()
129134 {
130135 $ termProgram = getenv ('TERM_PROGRAM ' );
131136 putenv ('TERM_PROGRAM=Hyper ' );
132-
133137 CLI ::init (); // force re-check on env
138+
134139 $ this ->assertSame ("\033[1;37m \033[42m \033[4mtest \033[0m " , CLI ::color ('test ' , 'white ' , 'green ' , 'underline ' ));
140+
135141 putenv ($ termProgram ? "TERM_PROGRAM= {$ termProgram }" : 'TERM_PROGRAM ' );
136142 }
137143
@@ -146,72 +152,105 @@ public function testColor()
146152 // After the tests on NO_COLOR and TERM_PROGRAM above,
147153 // the $isColored variable is rigged. So we reset this.
148154 CLI ::init ();
149- $ this ->assertSame ("\033[1;37m \033[42m \033[4mtest \033[0m " , CLI ::color ('test ' , 'white ' , 'green ' , 'underline ' ));
155+
156+ $ this ->assertSame (
157+ "\033[1;37m \033[42m \033[4mtest \033[0m " ,
158+ CLI ::color ('test ' , 'white ' , 'green ' , 'underline ' )
159+ );
160+ }
161+
162+ public function testColorEmtpyString ()
163+ {
164+ $ this ->assertSame (
165+ '' ,
166+ CLI ::color ('' , 'white ' , 'green ' , 'underline ' )
167+ );
150168 }
151169
152170 public function testPrint ()
153171 {
154172 CLI ::print ('test ' );
155- $ expected = 'test ' ;
156173
174+ $ expected = 'test ' ;
157175 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
158176 }
159177
160178 public function testPrintForeground ()
161179 {
162180 CLI ::print ('test ' , 'red ' );
163- $ expected = "\033[0;31mtest \033[0m " ;
164181
182+ $ expected = "\033[0;31mtest \033[0m " ;
165183 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
166184 }
167185
168186 public function testPrintBackground ()
169187 {
170188 CLI ::print ('test ' , 'red ' , 'green ' );
171- $ expected = "\033[0;31m \033[42mtest \033[0m " ;
172189
190+ $ expected = "\033[0;31m \033[42mtest \033[0m " ;
173191 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
174192 }
175193
176194 public function testWrite ()
177195 {
178196 CLI ::write ('test ' );
197+
179198 $ expected = PHP_EOL . 'test ' . PHP_EOL ;
180199 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
181200 }
182201
183202 public function testWriteForeground ()
184203 {
185204 CLI ::write ('test ' , 'red ' );
205+
186206 $ expected = "\033[0;31mtest \033[0m " . PHP_EOL ;
187207 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
188208 }
189209
190210 public function testWriteForegroundWithColorBefore ()
191211 {
192212 CLI ::write (CLI ::color ('green ' , 'green ' ) . ' red ' , 'red ' );
193- $ expected = "\033[0;31m \033[0;32mgreen \033[0m \033[0;31m red \033[0m " . PHP_EOL ;
213+
214+ $ expected = "\033[0;32mgreen \033[0m \033[0;31m red \033[0m " . PHP_EOL ;
194215 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
195216 }
196217
197218 public function testWriteForegroundWithColorAfter ()
198219 {
199220 CLI ::write ('red ' . CLI ::color ('green ' , 'green ' ), 'red ' );
200- $ expected = "\033[0;31mred \033[0;32mgreen \033[0m " . PHP_EOL ;
221+
222+ $ expected = "\033[0;31mred \033[0m \033[0;32mgreen \033[0m " . PHP_EOL ;
223+ $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
224+ }
225+
226+ /**
227+ * @see https://github.com/codeigniter4/CodeIgniter4/issues/5892
228+ */
229+ public function testWriteForegroundWithColorTwice ()
230+ {
231+ CLI ::write (
232+ CLI ::color ('green ' , 'green ' ) . ' red ' . CLI ::color ('green ' , 'green ' ),
233+ 'red '
234+ );
235+
236+ $ expected = "\033[0;32mgreen \033[0m \033[0;31m red \033[0m \033[0;32mgreen \033[0m " . PHP_EOL ;
201237 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
202238 }
203239
204240 public function testWriteBackground ()
205241 {
206242 CLI ::write ('test ' , 'red ' , 'green ' );
243+
207244 $ expected = "\033[0;31m \033[42mtest \033[0m " . PHP_EOL ;
208245 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
209246 }
210247
211248 public function testError ()
212249 {
213250 $ this ->stream_filter = stream_filter_append (STDERR , 'CITestStreamFilter ' );
251+
214252 CLI ::error ('test ' );
253+
215254 // red expected cuz stderr
216255 $ expected = "\033[1;31mtest \033[0m " . PHP_EOL ;
217256 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
@@ -220,15 +259,19 @@ public function testError()
220259 public function testErrorForeground ()
221260 {
222261 $ this ->stream_filter = stream_filter_append (STDERR , 'CITestStreamFilter ' );
262+
223263 CLI ::error ('test ' , 'purple ' );
264+
224265 $ expected = "\033[0;35mtest \033[0m " . PHP_EOL ;
225266 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
226267 }
227268
228269 public function testErrorBackground ()
229270 {
230271 $ this ->stream_filter = stream_filter_append (STDERR , 'CITestStreamFilter ' );
272+
231273 CLI ::error ('test ' , 'purple ' , 'green ' );
274+
232275 $ expected = "\033[0;35m \033[42mtest \033[0m " . PHP_EOL ;
233276 $ this ->assertSame ($ expected , CITestStreamFilter::$ buffer );
234277 }
@@ -273,9 +316,18 @@ public function testShowProgressWithoutBar()
273316 public function testWrap ()
274317 {
275318 $ this ->assertSame ('' , CLI ::wrap ('' ));
276- $ this ->assertSame ('1234 ' . PHP_EOL . ' 5678 ' . PHP_EOL . ' 90 ' . PHP_EOL . ' abc ' . PHP_EOL . ' de ' . PHP_EOL . ' fghij ' . PHP_EOL . ' 0987654321 ' , CLI ::wrap ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' , 5 , 1 ));
277- $ this ->assertSame ('1234 5678 90 ' . PHP_EOL . ' abc de fghij ' . PHP_EOL . ' 0987654321 ' , CLI ::wrap ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' , 999 , 2 ));
278- $ this ->assertSame ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' , CLI ::wrap ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' ));
319+ $ this ->assertSame (
320+ '1234 ' . PHP_EOL . ' 5678 ' . PHP_EOL . ' 90 ' . PHP_EOL . ' abc ' . PHP_EOL . ' de ' . PHP_EOL . ' fghij ' . PHP_EOL . ' 0987654321 ' ,
321+ CLI ::wrap ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' , 5 , 1 )
322+ );
323+ $ this ->assertSame (
324+ '1234 5678 90 ' . PHP_EOL . ' abc de fghij ' . PHP_EOL . ' 0987654321 ' ,
325+ CLI ::wrap ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' , 999 , 2 )
326+ );
327+ $ this ->assertSame (
328+ '1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' ,
329+ CLI ::wrap ('1234 5678 90 ' . PHP_EOL . 'abc de fghij ' . PHP_EOL . '0987654321 ' )
330+ );
279331 }
280332
281333 public function testParseCommand ()
@@ -287,6 +339,7 @@ public function testParseCommand()
287339 ];
288340 $ _SERVER ['argc ' ] = 3 ;
289341 CLI ::init ();
342+
290343 $ this ->assertNull (CLI ::getSegment (3 ));
291344 $ this ->assertSame ('b ' , CLI ::getSegment (1 ));
292345 $ this ->assertSame ('c ' , CLI ::getSegment (2 ));
@@ -312,6 +365,7 @@ public function testParseCommandMixed()
312365 'sure ' ,
313366 ];
314367 CLI ::init ();
368+
315369 $ this ->assertNull (CLI ::getSegment (7 ));
316370 $ this ->assertSame ('b ' , CLI ::getSegment (1 ));
317371 $ this ->assertSame ('c ' , CLI ::getSegment (2 ));
@@ -335,6 +389,7 @@ public function testParseCommandOption()
335389 'd ' ,
336390 ];
337391 CLI ::init ();
392+
338393 $ this ->assertSame (['parm ' => 'pvalue ' ], CLI ::getOptions ());
339394 $ this ->assertSame ('pvalue ' , CLI ::getOption ('parm ' ));
340395 $ this ->assertSame ('-parm pvalue ' , CLI ::getOptionString ());
@@ -359,6 +414,7 @@ public function testParseCommandMultipleOptions()
359414 'value 3 ' ,
360415 ];
361416 CLI ::init ();
417+
362418 $ this ->assertSame (['parm ' => 'pvalue ' , 'p2 ' => null , 'p3 ' => 'value 3 ' ], CLI ::getOptions ());
363419 $ this ->assertSame ('pvalue ' , CLI ::getOption ('parm ' ));
364420 $ this ->assertSame ('-parm pvalue -p2 -p3 "value 3" ' , CLI ::getOptionString ());
@@ -373,11 +429,13 @@ public function testWindow()
373429 $ height = new ReflectionProperty (CLI ::class, 'height ' );
374430 $ height ->setAccessible (true );
375431 $ height ->setValue (null );
432+
376433 $ this ->assertIsInt (CLI ::getHeight ());
377434
378435 $ width = new ReflectionProperty (CLI ::class, 'width ' );
379436 $ width ->setAccessible (true );
380437 $ width ->setValue (null );
438+
381439 $ this ->assertIsInt (CLI ::getWidth ());
382440 }
383441
@@ -391,6 +449,7 @@ public function testWindow()
391449 public function testTable ($ tbody , $ thead , $ expected )
392450 {
393451 CLI ::table ($ tbody , $ thead );
452+
394453 $ this ->assertSame (CITestStreamFilter::$ buffer , $ expected );
395454 }
396455
0 commit comments