4c2de26a8638fcaca61c111c37c303a3861fccf6
[yaffs-website] / vendor / symfony / console / Tests / Helper / ProgressBarTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Helper;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Helper\Helper;
16 use Symfony\Component\Console\Helper\ProgressBar;
17 use Symfony\Component\Console\Output\StreamOutput;
18
19 /**
20  * @group time-sensitive
21  */
22 class ProgressBarTest extends TestCase
23 {
24     public function testMultipleStart()
25     {
26         $bar = new ProgressBar($output = $this->getOutputStream());
27         $bar->start();
28         $bar->advance();
29         $bar->start();
30
31         rewind($output->getStream());
32         $this->assertEquals(
33             '    0 [>---------------------------]'.
34             $this->generateOutput('    1 [->--------------------------]').
35             $this->generateOutput('    0 [>---------------------------]'),
36             stream_get_contents($output->getStream())
37         );
38     }
39
40     public function testAdvance()
41     {
42         $bar = new ProgressBar($output = $this->getOutputStream());
43         $bar->start();
44         $bar->advance();
45
46         rewind($output->getStream());
47         $this->assertEquals(
48             '    0 [>---------------------------]'.
49             $this->generateOutput('    1 [->--------------------------]'),
50             stream_get_contents($output->getStream())
51         );
52     }
53
54     public function testAdvanceWithStep()
55     {
56         $bar = new ProgressBar($output = $this->getOutputStream());
57         $bar->start();
58         $bar->advance(5);
59
60         rewind($output->getStream());
61         $this->assertEquals(
62             '    0 [>---------------------------]'.
63             $this->generateOutput('    5 [----->----------------------]'),
64             stream_get_contents($output->getStream())
65         );
66     }
67
68     public function testAdvanceMultipleTimes()
69     {
70         $bar = new ProgressBar($output = $this->getOutputStream());
71         $bar->start();
72         $bar->advance(3);
73         $bar->advance(2);
74
75         rewind($output->getStream());
76         $this->assertEquals(
77             '    0 [>---------------------------]'.
78             $this->generateOutput('    3 [--->------------------------]').
79             $this->generateOutput('    5 [----->----------------------]'),
80             stream_get_contents($output->getStream())
81         );
82     }
83
84     public function testAdvanceOverMax()
85     {
86         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
87         $bar->setProgress(9);
88         $bar->advance();
89         $bar->advance();
90
91         rewind($output->getStream());
92         $this->assertEquals(
93             '  9/10 [=========================>--]  90%'.
94             $this->generateOutput(' 10/10 [============================] 100%').
95             $this->generateOutput(' 11/11 [============================] 100%'),
96             stream_get_contents($output->getStream())
97         );
98     }
99
100     public function testRegress()
101     {
102         $bar = new ProgressBar($output = $this->getOutputStream());
103         $bar->start();
104         $bar->advance();
105         $bar->advance();
106         $bar->advance(-1);
107
108         rewind($output->getStream());
109         $this->assertEquals(
110             '    0 [>---------------------------]'.
111             $this->generateOutput('    1 [->--------------------------]').
112             $this->generateOutput('    2 [-->-------------------------]').
113             $this->generateOutput('    1 [->--------------------------]'),
114             stream_get_contents($output->getStream())
115         );
116     }
117
118     public function testRegressWithStep()
119     {
120         $bar = new ProgressBar($output = $this->getOutputStream());
121         $bar->start();
122         $bar->advance(4);
123         $bar->advance(4);
124         $bar->advance(-2);
125
126         rewind($output->getStream());
127         $this->assertEquals(
128             '    0 [>---------------------------]'.
129             $this->generateOutput('    4 [---->-----------------------]').
130             $this->generateOutput('    8 [-------->-------------------]').
131             $this->generateOutput('    6 [------>---------------------]'),
132             stream_get_contents($output->getStream())
133         );
134     }
135
136     public function testRegressMultipleTimes()
137     {
138         $bar = new ProgressBar($output = $this->getOutputStream());
139         $bar->start();
140         $bar->advance(3);
141         $bar->advance(3);
142         $bar->advance(-1);
143         $bar->advance(-2);
144
145         rewind($output->getStream());
146         $this->assertEquals(
147             '    0 [>---------------------------]'.
148             $this->generateOutput('    3 [--->------------------------]').
149             $this->generateOutput('    6 [------>---------------------]').
150             $this->generateOutput('    5 [----->----------------------]').
151             $this->generateOutput('    3 [--->------------------------]'),
152             stream_get_contents($output->getStream())
153         );
154     }
155
156     public function testRegressBelowMin()
157     {
158         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
159         $bar->setProgress(1);
160         $bar->advance(-1);
161         $bar->advance(-1);
162
163         rewind($output->getStream());
164         $this->assertEquals(
165             '  1/10 [==>-------------------------]  10%'.
166             $this->generateOutput('  0/10 [>---------------------------]   0%'),
167             stream_get_contents($output->getStream())
168         );
169     }
170
171     public function testFormat()
172     {
173         $expected =
174             '  0/10 [>---------------------------]   0%'.
175             $this->generateOutput(' 10/10 [============================] 100%').
176             $this->generateOutput(' 10/10 [============================] 100%')
177         ;
178
179         // max in construct, no format
180         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
181         $bar->start();
182         $bar->advance(10);
183         $bar->finish();
184
185         rewind($output->getStream());
186         $this->assertEquals($expected, stream_get_contents($output->getStream()));
187
188         // max in start, no format
189         $bar = new ProgressBar($output = $this->getOutputStream());
190         $bar->start(10);
191         $bar->advance(10);
192         $bar->finish();
193
194         rewind($output->getStream());
195         $this->assertEquals($expected, stream_get_contents($output->getStream()));
196
197         // max in construct, explicit format before
198         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
199         $bar->setFormat('normal');
200         $bar->start();
201         $bar->advance(10);
202         $bar->finish();
203
204         rewind($output->getStream());
205         $this->assertEquals($expected, stream_get_contents($output->getStream()));
206
207         // max in start, explicit format before
208         $bar = new ProgressBar($output = $this->getOutputStream());
209         $bar->setFormat('normal');
210         $bar->start(10);
211         $bar->advance(10);
212         $bar->finish();
213
214         rewind($output->getStream());
215         $this->assertEquals($expected, stream_get_contents($output->getStream()));
216     }
217
218     public function testCustomizations()
219     {
220         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
221         $bar->setBarWidth(10);
222         $bar->setBarCharacter('_');
223         $bar->setEmptyBarCharacter(' ');
224         $bar->setProgressCharacter('/');
225         $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
226         $bar->start();
227         $bar->advance();
228
229         rewind($output->getStream());
230         $this->assertEquals(
231             '  0/10 [/         ]   0%'.
232             $this->generateOutput('  1/10 [_/        ]  10%'),
233             stream_get_contents($output->getStream())
234         );
235     }
236
237     public function testDisplayWithoutStart()
238     {
239         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
240         $bar->display();
241
242         rewind($output->getStream());
243         $this->assertEquals(
244             '  0/50 [>---------------------------]   0%',
245             stream_get_contents($output->getStream())
246         );
247     }
248
249     public function testDisplayWithQuietVerbosity()
250     {
251         $bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50);
252         $bar->display();
253
254         rewind($output->getStream());
255         $this->assertEquals(
256             '',
257             stream_get_contents($output->getStream())
258         );
259     }
260
261     public function testFinishWithoutStart()
262     {
263         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
264         $bar->finish();
265
266         rewind($output->getStream());
267         $this->assertEquals(
268             ' 50/50 [============================] 100%',
269             stream_get_contents($output->getStream())
270         );
271     }
272
273     public function testPercent()
274     {
275         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
276         $bar->start();
277         $bar->display();
278         $bar->advance();
279         $bar->advance();
280
281         rewind($output->getStream());
282         $this->assertEquals(
283             '  0/50 [>---------------------------]   0%'.
284             $this->generateOutput('  0/50 [>---------------------------]   0%').
285             $this->generateOutput('  1/50 [>---------------------------]   2%').
286             $this->generateOutput('  2/50 [=>--------------------------]   4%'),
287             stream_get_contents($output->getStream())
288         );
289     }
290
291     public function testOverwriteWithShorterLine()
292     {
293         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
294         $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
295         $bar->start();
296         $bar->display();
297         $bar->advance();
298
299         // set shorter format
300         $bar->setFormat(' %current%/%max% [%bar%]');
301         $bar->advance();
302
303         rewind($output->getStream());
304         $this->assertEquals(
305             '  0/50 [>---------------------------]   0%'.
306             $this->generateOutput('  0/50 [>---------------------------]   0%').
307             $this->generateOutput('  1/50 [>---------------------------]   2%').
308             $this->generateOutput('  2/50 [=>--------------------------]'),
309             stream_get_contents($output->getStream())
310         );
311     }
312
313     public function testStartWithMax()
314     {
315         $bar = new ProgressBar($output = $this->getOutputStream());
316         $bar->setFormat('%current%/%max% [%bar%]');
317         $bar->start(50);
318         $bar->advance();
319
320         rewind($output->getStream());
321         $this->assertEquals(
322             ' 0/50 [>---------------------------]'.
323             $this->generateOutput(' 1/50 [>---------------------------]'),
324             stream_get_contents($output->getStream())
325         );
326     }
327
328     public function testSetCurrentProgress()
329     {
330         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
331         $bar->start();
332         $bar->display();
333         $bar->advance();
334         $bar->setProgress(15);
335         $bar->setProgress(25);
336
337         rewind($output->getStream());
338         $this->assertEquals(
339             '  0/50 [>---------------------------]   0%'.
340             $this->generateOutput('  0/50 [>---------------------------]   0%').
341             $this->generateOutput('  1/50 [>---------------------------]   2%').
342             $this->generateOutput(' 15/50 [========>-------------------]  30%').
343             $this->generateOutput(' 25/50 [==============>-------------]  50%'),
344             stream_get_contents($output->getStream())
345         );
346     }
347
348     public function testSetCurrentBeforeStarting()
349     {
350         $bar = new ProgressBar($this->getOutputStream());
351         $bar->setProgress(15);
352         $this->assertNotNull($bar->getStartTime());
353     }
354
355     public function testRedrawFrequency()
356     {
357         $bar = new ProgressBar($output = $this->getOutputStream(), 6);
358         $bar->setRedrawFrequency(2);
359         $bar->start();
360         $bar->setProgress(1);
361         $bar->advance(2);
362         $bar->advance(2);
363         $bar->advance(1);
364
365         rewind($output->getStream());
366         $this->assertEquals(
367             ' 0/6 [>---------------------------]   0%'.
368             $this->generateOutput(' 3/6 [==============>-------------]  50%').
369             $this->generateOutput(' 5/6 [=======================>----]  83%').
370             $this->generateOutput(' 6/6 [============================] 100%'),
371             stream_get_contents($output->getStream())
372         );
373     }
374
375     public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
376     {
377         $bar = new ProgressBar($output = $this->getOutputStream());
378         $bar->setRedrawFrequency(0);
379         $bar->start();
380         $bar->advance();
381
382         rewind($output->getStream());
383         $this->assertEquals(
384             '    0 [>---------------------------]'.
385             $this->generateOutput('    1 [->--------------------------]'),
386             stream_get_contents($output->getStream())
387         );
388     }
389
390     public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
391     {
392         $bar = new ProgressBar($output = $this->getOutputStream());
393         $bar->setRedrawFrequency(0.9);
394         $bar->start();
395         $bar->advance();
396
397         rewind($output->getStream());
398         $this->assertEquals(
399             '    0 [>---------------------------]'.
400             $this->generateOutput('    1 [->--------------------------]'),
401             stream_get_contents($output->getStream())
402         );
403     }
404
405     public function testMultiByteSupport()
406     {
407         $bar = new ProgressBar($output = $this->getOutputStream());
408         $bar->start();
409         $bar->setBarCharacter('■');
410         $bar->advance(3);
411
412         rewind($output->getStream());
413         $this->assertEquals(
414             '    0 [>---------------------------]'.
415             $this->generateOutput('    3 [■■■>------------------------]'),
416             stream_get_contents($output->getStream())
417         );
418     }
419
420     public function testClear()
421     {
422         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
423         $bar->start();
424         $bar->setProgress(25);
425         $bar->clear();
426
427         rewind($output->getStream());
428         $this->assertEquals(
429             '  0/50 [>---------------------------]   0%'.
430             $this->generateOutput(' 25/50 [==============>-------------]  50%').
431             $this->generateOutput(''),
432             stream_get_contents($output->getStream())
433         );
434     }
435
436     public function testPercentNotHundredBeforeComplete()
437     {
438         $bar = new ProgressBar($output = $this->getOutputStream(), 200);
439         $bar->start();
440         $bar->display();
441         $bar->advance(199);
442         $bar->advance();
443
444         rewind($output->getStream());
445         $this->assertEquals(
446             '   0/200 [>---------------------------]   0%'.
447             $this->generateOutput('   0/200 [>---------------------------]   0%').
448             $this->generateOutput(' 199/200 [===========================>]  99%').
449             $this->generateOutput(' 200/200 [============================] 100%'),
450             stream_get_contents($output->getStream())
451         );
452     }
453
454     public function testNonDecoratedOutput()
455     {
456         $bar = new ProgressBar($output = $this->getOutputStream(false), 200);
457         $bar->start();
458
459         for ($i = 0; $i < 200; ++$i) {
460             $bar->advance();
461         }
462
463         $bar->finish();
464
465         rewind($output->getStream());
466         $this->assertEquals(
467             '   0/200 [>---------------------------]   0%'.PHP_EOL.
468             '  20/200 [==>-------------------------]  10%'.PHP_EOL.
469             '  40/200 [=====>----------------------]  20%'.PHP_EOL.
470             '  60/200 [========>-------------------]  30%'.PHP_EOL.
471             '  80/200 [===========>----------------]  40%'.PHP_EOL.
472             ' 100/200 [==============>-------------]  50%'.PHP_EOL.
473             ' 120/200 [================>-----------]  60%'.PHP_EOL.
474             ' 140/200 [===================>--------]  70%'.PHP_EOL.
475             ' 160/200 [======================>-----]  80%'.PHP_EOL.
476             ' 180/200 [=========================>--]  90%'.PHP_EOL.
477             ' 200/200 [============================] 100%',
478             stream_get_contents($output->getStream())
479         );
480     }
481
482     public function testNonDecoratedOutputWithClear()
483     {
484         $bar = new ProgressBar($output = $this->getOutputStream(false), 50);
485         $bar->start();
486         $bar->setProgress(25);
487         $bar->clear();
488         $bar->setProgress(50);
489         $bar->finish();
490
491         rewind($output->getStream());
492         $this->assertEquals(
493             '  0/50 [>---------------------------]   0%'.PHP_EOL.
494             ' 25/50 [==============>-------------]  50%'.PHP_EOL.
495             ' 50/50 [============================] 100%',
496             stream_get_contents($output->getStream())
497         );
498     }
499
500     public function testNonDecoratedOutputWithoutMax()
501     {
502         $bar = new ProgressBar($output = $this->getOutputStream(false));
503         $bar->start();
504         $bar->advance();
505
506         rewind($output->getStream());
507         $this->assertEquals(
508             '    0 [>---------------------------]'.PHP_EOL.
509             '    1 [->--------------------------]',
510             stream_get_contents($output->getStream())
511         );
512     }
513
514     public function testParallelBars()
515     {
516         $output = $this->getOutputStream();
517         $bar1 = new ProgressBar($output, 2);
518         $bar2 = new ProgressBar($output, 3);
519         $bar2->setProgressCharacter('#');
520         $bar3 = new ProgressBar($output);
521
522         $bar1->start();
523         $output->write("\n");
524         $bar2->start();
525         $output->write("\n");
526         $bar3->start();
527
528         for ($i = 1; $i <= 3; ++$i) {
529             // up two lines
530             $output->write("\033[2A");
531             if ($i <= 2) {
532                 $bar1->advance();
533             }
534             $output->write("\n");
535             $bar2->advance();
536             $output->write("\n");
537             $bar3->advance();
538         }
539         $output->write("\033[2A");
540         $output->write("\n");
541         $output->write("\n");
542         $bar3->finish();
543
544         rewind($output->getStream());
545         $this->assertEquals(
546             ' 0/2 [>---------------------------]   0%'."\n".
547             ' 0/3 [#---------------------------]   0%'."\n".
548             rtrim('    0 [>---------------------------]').
549
550             "\033[2A".
551             $this->generateOutput(' 1/2 [==============>-------------]  50%')."\n".
552             $this->generateOutput(' 1/3 [=========#------------------]  33%')."\n".
553             rtrim($this->generateOutput('    1 [->--------------------------]')).
554
555             "\033[2A".
556             $this->generateOutput(' 2/2 [============================] 100%')."\n".
557             $this->generateOutput(' 2/3 [==================#---------]  66%')."\n".
558             rtrim($this->generateOutput('    2 [-->-------------------------]')).
559
560             "\033[2A".
561             "\n".
562             $this->generateOutput(' 3/3 [============================] 100%')."\n".
563             rtrim($this->generateOutput('    3 [--->------------------------]')).
564
565             "\033[2A".
566             "\n".
567             "\n".
568             rtrim($this->generateOutput('    3 [============================]')),
569             stream_get_contents($output->getStream())
570         );
571     }
572
573     public function testWithoutMax()
574     {
575         $output = $this->getOutputStream();
576
577         $bar = new ProgressBar($output);
578         $bar->start();
579         $bar->advance();
580         $bar->advance();
581         $bar->advance();
582         $bar->finish();
583
584         rewind($output->getStream());
585         $this->assertEquals(
586             rtrim('    0 [>---------------------------]').
587             rtrim($this->generateOutput('    1 [->--------------------------]')).
588             rtrim($this->generateOutput('    2 [-->-------------------------]')).
589             rtrim($this->generateOutput('    3 [--->------------------------]')).
590             rtrim($this->generateOutput('    3 [============================]')),
591             stream_get_contents($output->getStream())
592         );
593     }
594
595     public function testWithSmallScreen()
596     {
597         $output = $this->getOutputStream();
598
599         $bar = new ProgressBar($output);
600         putenv('COLUMNS=12');
601         $bar->start();
602         $bar->advance();
603         putenv('COLUMNS=120');
604
605         rewind($output->getStream());
606         $this->assertEquals(
607             '    0 [>---]'.
608             $this->generateOutput('    1 [->--]'),
609             stream_get_contents($output->getStream())
610         );
611     }
612
613     public function testAddingPlaceholderFormatter()
614     {
615         ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
616             return $bar->getMaxSteps() - $bar->getProgress();
617         });
618         $bar = new ProgressBar($output = $this->getOutputStream(), 3);
619         $bar->setFormat(' %remaining_steps% [%bar%]');
620
621         $bar->start();
622         $bar->advance();
623         $bar->finish();
624
625         rewind($output->getStream());
626         $this->assertEquals(
627             ' 3 [>---------------------------]'.
628             $this->generateOutput(' 2 [=========>------------------]').
629             $this->generateOutput(' 0 [============================]'),
630             stream_get_contents($output->getStream())
631         );
632     }
633
634     public function testMultilineFormat()
635     {
636         $bar = new ProgressBar($output = $this->getOutputStream(), 3);
637         $bar->setFormat("%bar%\nfoobar");
638
639         $bar->start();
640         $bar->advance();
641         $bar->clear();
642         $bar->finish();
643
644         rewind($output->getStream());
645         $this->assertEquals(
646             ">---------------------------\nfoobar".
647             $this->generateOutput("=========>------------------\nfoobar").
648             "\x0D\x1B[2K\x1B[1A\x1B[2K".
649             $this->generateOutput("============================\nfoobar"),
650             stream_get_contents($output->getStream())
651         );
652     }
653
654     public function testAnsiColorsAndEmojis()
655     {
656         putenv('COLUMNS=156');
657
658         $bar = new ProgressBar($output = $this->getOutputStream(), 15);
659         ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
660             static $i = 0;
661             $mem = 100000 * $i;
662             $colors = $i++ ? '41;37' : '44;37';
663
664             return "\033[".$colors.'m '.Helper::formatMemory($mem)." \033[0m";
665         });
666         $bar->setFormat(" \033[44;37m %title:-37s% \033[0m\n %current%/%max% %bar% %percent:3s%%\n 🏁  %remaining:-10s% %memory:37s%");
667         $bar->setBarCharacter($done = "\033[32m●\033[0m");
668         $bar->setEmptyBarCharacter($empty = "\033[31m●\033[0m");
669         $bar->setProgressCharacter($progress = "\033[32m➤ \033[0m");
670
671         $bar->setMessage('Starting the demo... fingers crossed', 'title');
672         $bar->start();
673
674         rewind($output->getStream());
675         $this->assertEquals(
676             " \033[44;37m Starting the demo... fingers crossed  \033[0m\n".
677             '  0/15 '.$progress.str_repeat($empty, 26)."   0%\n".
678             " \xf0\x9f\x8f\x81  < 1 sec                        \033[44;37m 0 B \033[0m",
679             stream_get_contents($output->getStream())
680         );
681         ftruncate($output->getStream(), 0);
682         rewind($output->getStream());
683
684         $bar->setMessage('Looks good to me...', 'title');
685         $bar->advance(4);
686
687         rewind($output->getStream());
688         $this->assertEquals(
689             $this->generateOutput(
690                 " \033[44;37m Looks good to me...                   \033[0m\n".
691                 '  4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)."  26%\n".
692                 " \xf0\x9f\x8f\x81  < 1 sec                     \033[41;37m 97 KiB \033[0m"
693             ),
694             stream_get_contents($output->getStream())
695         );
696         ftruncate($output->getStream(), 0);
697         rewind($output->getStream());
698
699         $bar->setMessage('Thanks, bye', 'title');
700         $bar->finish();
701
702         rewind($output->getStream());
703         $this->assertEquals(
704             $this->generateOutput(
705                 " \033[44;37m Thanks, bye                           \033[0m\n".
706                 ' 15/15 '.str_repeat($done, 28)." 100%\n".
707                 " \xf0\x9f\x8f\x81  < 1 sec                    \033[41;37m 195 KiB \033[0m"
708             ),
709             stream_get_contents($output->getStream())
710         );
711         putenv('COLUMNS=120');
712     }
713
714     public function testSetFormat()
715     {
716         $bar = new ProgressBar($output = $this->getOutputStream());
717         $bar->setFormat('normal');
718         $bar->start();
719         rewind($output->getStream());
720         $this->assertEquals(
721             '    0 [>---------------------------]',
722             stream_get_contents($output->getStream())
723         );
724
725         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
726         $bar->setFormat('normal');
727         $bar->start();
728         rewind($output->getStream());
729         $this->assertEquals(
730             '  0/10 [>---------------------------]   0%',
731             stream_get_contents($output->getStream())
732         );
733     }
734
735     /**
736      * @dataProvider provideFormat
737      */
738     public function testFormatsWithoutMax($format)
739     {
740         $bar = new ProgressBar($output = $this->getOutputStream());
741         $bar->setFormat($format);
742         $bar->start();
743
744         rewind($output->getStream());
745         $this->assertNotEmpty(stream_get_contents($output->getStream()));
746     }
747
748     /**
749      * Provides each defined format.
750      *
751      * @return array
752      */
753     public function provideFormat()
754     {
755         return array(
756             array('normal'),
757             array('verbose'),
758             array('very_verbose'),
759             array('debug'),
760         );
761     }
762
763     protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
764     {
765         return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
766     }
767
768     protected function generateOutput($expected)
769     {
770         $count = substr_count($expected, "\n");
771
772         return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
773     }
774
775     public function testBarWidthWithMultilineFormat()
776     {
777         putenv('COLUMNS=10');
778
779         $bar = new ProgressBar($output = $this->getOutputStream());
780         $bar->setFormat("%bar%\n0123456789");
781
782         // before starting
783         $bar->setBarWidth(5);
784         $this->assertEquals(5, $bar->getBarWidth());
785
786         // after starting
787         $bar->start();
788         rewind($output->getStream());
789         $this->assertEquals(5, $bar->getBarWidth(), stream_get_contents($output->getStream()));
790         putenv('COLUMNS=120');
791     }
792 }