f45016982e291f20f9e6b6b3dc0b69722132de55
[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\ProgressBar;
16 use Symfony\Component\Console\Helper\Helper;
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 = $this->getMockBuilder('Symfony\Component\Console\Helper\ProgressBar')->setMethods(array('display'))->setConstructorArgs(array($this->getOutputStream(), 6))->getMock();
358         $bar->expects($this->exactly(4))->method('display');
359
360         $bar->setRedrawFrequency(2);
361         $bar->start();
362         $bar->setProgress(1);
363         $bar->advance(2);
364         $bar->advance(2);
365         $bar->advance(1);
366     }
367
368     public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
369     {
370         $bar = $this->getMockBuilder('Symfony\Component\Console\Helper\ProgressBar')->setMethods(array('display'))->setConstructorArgs(array($this->getOutputStream()))->getMock();
371
372         $bar->expects($this->exactly(2))->method('display');
373         $bar->setRedrawFrequency(0);
374         $bar->start();
375         $bar->advance();
376     }
377
378     public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
379     {
380         $bar = $this->getMockBuilder('Symfony\Component\Console\Helper\ProgressBar')->setMethods(array('display'))->setConstructorArgs(array($this->getOutputStream()))->getMock();
381
382         $bar->expects($this->exactly(2))->method('display');
383         $bar->setRedrawFrequency(0.9);
384         $bar->start();
385         $bar->advance();
386     }
387
388     public function testMultiByteSupport()
389     {
390         $bar = new ProgressBar($output = $this->getOutputStream());
391         $bar->start();
392         $bar->setBarCharacter('■');
393         $bar->advance(3);
394
395         rewind($output->getStream());
396         $this->assertEquals(
397             '    0 [>---------------------------]'.
398             $this->generateOutput('    3 [■■■>------------------------]'),
399             stream_get_contents($output->getStream())
400         );
401     }
402
403     public function testClear()
404     {
405         $bar = new ProgressBar($output = $this->getOutputStream(), 50);
406         $bar->start();
407         $bar->setProgress(25);
408         $bar->clear();
409
410         rewind($output->getStream());
411         $this->assertEquals(
412             '  0/50 [>---------------------------]   0%'.
413             $this->generateOutput(' 25/50 [==============>-------------]  50%').
414             $this->generateOutput(''),
415             stream_get_contents($output->getStream())
416         );
417     }
418
419     public function testPercentNotHundredBeforeComplete()
420     {
421         $bar = new ProgressBar($output = $this->getOutputStream(), 200);
422         $bar->start();
423         $bar->display();
424         $bar->advance(199);
425         $bar->advance();
426
427         rewind($output->getStream());
428         $this->assertEquals(
429             '   0/200 [>---------------------------]   0%'.
430             $this->generateOutput('   0/200 [>---------------------------]   0%').
431             $this->generateOutput(' 199/200 [===========================>]  99%').
432             $this->generateOutput(' 200/200 [============================] 100%'),
433             stream_get_contents($output->getStream())
434         );
435     }
436
437     public function testNonDecoratedOutput()
438     {
439         $bar = new ProgressBar($output = $this->getOutputStream(false), 200);
440         $bar->start();
441
442         for ($i = 0; $i < 200; ++$i) {
443             $bar->advance();
444         }
445
446         $bar->finish();
447
448         rewind($output->getStream());
449         $this->assertEquals(
450             '   0/200 [>---------------------------]   0%'.PHP_EOL.
451             '  20/200 [==>-------------------------]  10%'.PHP_EOL.
452             '  40/200 [=====>----------------------]  20%'.PHP_EOL.
453             '  60/200 [========>-------------------]  30%'.PHP_EOL.
454             '  80/200 [===========>----------------]  40%'.PHP_EOL.
455             ' 100/200 [==============>-------------]  50%'.PHP_EOL.
456             ' 120/200 [================>-----------]  60%'.PHP_EOL.
457             ' 140/200 [===================>--------]  70%'.PHP_EOL.
458             ' 160/200 [======================>-----]  80%'.PHP_EOL.
459             ' 180/200 [=========================>--]  90%'.PHP_EOL.
460             ' 200/200 [============================] 100%',
461             stream_get_contents($output->getStream())
462         );
463     }
464
465     public function testNonDecoratedOutputWithClear()
466     {
467         $bar = new ProgressBar($output = $this->getOutputStream(false), 50);
468         $bar->start();
469         $bar->setProgress(25);
470         $bar->clear();
471         $bar->setProgress(50);
472         $bar->finish();
473
474         rewind($output->getStream());
475         $this->assertEquals(
476             '  0/50 [>---------------------------]   0%'.PHP_EOL.
477             ' 25/50 [==============>-------------]  50%'.PHP_EOL.
478             ' 50/50 [============================] 100%',
479             stream_get_contents($output->getStream())
480         );
481     }
482
483     public function testNonDecoratedOutputWithoutMax()
484     {
485         $bar = new ProgressBar($output = $this->getOutputStream(false));
486         $bar->start();
487         $bar->advance();
488
489         rewind($output->getStream());
490         $this->assertEquals(
491             '    0 [>---------------------------]'.PHP_EOL.
492             '    1 [->--------------------------]',
493             stream_get_contents($output->getStream())
494         );
495     }
496
497     public function testParallelBars()
498     {
499         $output = $this->getOutputStream();
500         $bar1 = new ProgressBar($output, 2);
501         $bar2 = new ProgressBar($output, 3);
502         $bar2->setProgressCharacter('#');
503         $bar3 = new ProgressBar($output);
504
505         $bar1->start();
506         $output->write("\n");
507         $bar2->start();
508         $output->write("\n");
509         $bar3->start();
510
511         for ($i = 1; $i <= 3; ++$i) {
512             // up two lines
513             $output->write("\033[2A");
514             if ($i <= 2) {
515                 $bar1->advance();
516             }
517             $output->write("\n");
518             $bar2->advance();
519             $output->write("\n");
520             $bar3->advance();
521         }
522         $output->write("\033[2A");
523         $output->write("\n");
524         $output->write("\n");
525         $bar3->finish();
526
527         rewind($output->getStream());
528         $this->assertEquals(
529             ' 0/2 [>---------------------------]   0%'."\n".
530             ' 0/3 [#---------------------------]   0%'."\n".
531             rtrim('    0 [>---------------------------]').
532
533             "\033[2A".
534             $this->generateOutput(' 1/2 [==============>-------------]  50%')."\n".
535             $this->generateOutput(' 1/3 [=========#------------------]  33%')."\n".
536             rtrim($this->generateOutput('    1 [->--------------------------]')).
537
538             "\033[2A".
539             $this->generateOutput(' 2/2 [============================] 100%')."\n".
540             $this->generateOutput(' 2/3 [==================#---------]  66%')."\n".
541             rtrim($this->generateOutput('    2 [-->-------------------------]')).
542
543             "\033[2A".
544             "\n".
545             $this->generateOutput(' 3/3 [============================] 100%')."\n".
546             rtrim($this->generateOutput('    3 [--->------------------------]')).
547
548             "\033[2A".
549             "\n".
550             "\n".
551             rtrim($this->generateOutput('    3 [============================]')),
552             stream_get_contents($output->getStream())
553         );
554     }
555
556     public function testWithoutMax()
557     {
558         $output = $this->getOutputStream();
559
560         $bar = new ProgressBar($output);
561         $bar->start();
562         $bar->advance();
563         $bar->advance();
564         $bar->advance();
565         $bar->finish();
566
567         rewind($output->getStream());
568         $this->assertEquals(
569             rtrim('    0 [>---------------------------]').
570             rtrim($this->generateOutput('    1 [->--------------------------]')).
571             rtrim($this->generateOutput('    2 [-->-------------------------]')).
572             rtrim($this->generateOutput('    3 [--->------------------------]')).
573             rtrim($this->generateOutput('    3 [============================]')),
574             stream_get_contents($output->getStream())
575         );
576     }
577
578     public function testWithSmallScreen()
579     {
580         $output = $this->getOutputStream();
581
582         $bar = new ProgressBar($output);
583         putenv('COLUMNS=12');
584         $bar->start();
585         $bar->advance();
586         putenv('COLUMNS=120');
587
588         rewind($output->getStream());
589         $this->assertEquals(
590             '    0 [>---]'.
591             $this->generateOutput('    1 [->--]'),
592             stream_get_contents($output->getStream())
593         );
594     }
595
596     public function testAddingPlaceholderFormatter()
597     {
598         ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
599             return $bar->getMaxSteps() - $bar->getProgress();
600         });
601         $bar = new ProgressBar($output = $this->getOutputStream(), 3);
602         $bar->setFormat(' %remaining_steps% [%bar%]');
603
604         $bar->start();
605         $bar->advance();
606         $bar->finish();
607
608         rewind($output->getStream());
609         $this->assertEquals(
610             ' 3 [>---------------------------]'.
611             $this->generateOutput(' 2 [=========>------------------]').
612             $this->generateOutput(' 0 [============================]'),
613             stream_get_contents($output->getStream())
614         );
615     }
616
617     public function testMultilineFormat()
618     {
619         $bar = new ProgressBar($output = $this->getOutputStream(), 3);
620         $bar->setFormat("%bar%\nfoobar");
621
622         $bar->start();
623         $bar->advance();
624         $bar->clear();
625         $bar->finish();
626
627         rewind($output->getStream());
628         $this->assertEquals(
629             ">---------------------------\nfoobar".
630             $this->generateOutput("=========>------------------\nfoobar").
631             "\x0D\x1B[2K\x1B[1A\x1B[2K".
632             $this->generateOutput("============================\nfoobar"),
633             stream_get_contents($output->getStream())
634         );
635     }
636
637     public function testAnsiColorsAndEmojis()
638     {
639         putenv('COLUMNS=156');
640
641         $bar = new ProgressBar($output = $this->getOutputStream(), 15);
642         ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
643             static $i = 0;
644             $mem = 100000 * $i;
645             $colors = $i++ ? '41;37' : '44;37';
646
647             return "\033[".$colors.'m '.Helper::formatMemory($mem)." \033[0m";
648         });
649         $bar->setFormat(" \033[44;37m %title:-37s% \033[0m\n %current%/%max% %bar% %percent:3s%%\n 🏁  %remaining:-10s% %memory:37s%");
650         $bar->setBarCharacter($done = "\033[32m●\033[0m");
651         $bar->setEmptyBarCharacter($empty = "\033[31m●\033[0m");
652         $bar->setProgressCharacter($progress = "\033[32m➤ \033[0m");
653
654         $bar->setMessage('Starting the demo... fingers crossed', 'title');
655         $bar->start();
656
657         rewind($output->getStream());
658         $this->assertEquals(
659             " \033[44;37m Starting the demo... fingers crossed  \033[0m\n".
660             '  0/15 '.$progress.str_repeat($empty, 26)."   0%\n".
661             " \xf0\x9f\x8f\x81  < 1 sec                        \033[44;37m 0 B \033[0m",
662             stream_get_contents($output->getStream())
663         );
664         ftruncate($output->getStream(), 0);
665         rewind($output->getStream());
666
667         $bar->setMessage('Looks good to me...', 'title');
668         $bar->advance(4);
669
670         rewind($output->getStream());
671         $this->assertEquals(
672             $this->generateOutput(
673                 " \033[44;37m Looks good to me...                   \033[0m\n".
674                 '  4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)."  26%\n".
675                 " \xf0\x9f\x8f\x81  < 1 sec                     \033[41;37m 97 KiB \033[0m"
676             ),
677             stream_get_contents($output->getStream())
678         );
679         ftruncate($output->getStream(), 0);
680         rewind($output->getStream());
681
682         $bar->setMessage('Thanks, bye', 'title');
683         $bar->finish();
684
685         rewind($output->getStream());
686         $this->assertEquals(
687             $this->generateOutput(
688                 " \033[44;37m Thanks, bye                           \033[0m\n".
689                 ' 15/15 '.str_repeat($done, 28)." 100%\n".
690                 " \xf0\x9f\x8f\x81  < 1 sec                    \033[41;37m 195 KiB \033[0m"
691             ),
692             stream_get_contents($output->getStream())
693         );
694         putenv('COLUMNS=120');
695     }
696
697     public function testSetFormat()
698     {
699         $bar = new ProgressBar($output = $this->getOutputStream());
700         $bar->setFormat('normal');
701         $bar->start();
702         rewind($output->getStream());
703         $this->assertEquals(
704             '    0 [>---------------------------]',
705             stream_get_contents($output->getStream())
706         );
707
708         $bar = new ProgressBar($output = $this->getOutputStream(), 10);
709         $bar->setFormat('normal');
710         $bar->start();
711         rewind($output->getStream());
712         $this->assertEquals(
713             '  0/10 [>---------------------------]   0%',
714             stream_get_contents($output->getStream())
715         );
716     }
717
718     /**
719      * @dataProvider provideFormat
720      */
721     public function testFormatsWithoutMax($format)
722     {
723         $bar = new ProgressBar($output = $this->getOutputStream());
724         $bar->setFormat($format);
725         $bar->start();
726
727         rewind($output->getStream());
728         $this->assertNotEmpty(stream_get_contents($output->getStream()));
729     }
730
731     /**
732      * Provides each defined format.
733      *
734      * @return array
735      */
736     public function provideFormat()
737     {
738         return array(
739             array('normal'),
740             array('verbose'),
741             array('very_verbose'),
742             array('debug'),
743         );
744     }
745
746     protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
747     {
748         return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
749     }
750
751     protected function generateOutput($expected)
752     {
753         $count = substr_count($expected, "\n");
754
755         return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
756     }
757
758     public function testBarWidthWithMultilineFormat()
759     {
760         putenv('COLUMNS=10');
761
762         $bar = new ProgressBar($output = $this->getOutputStream());
763         $bar->setFormat("%bar%\n0123456789");
764
765         // before starting
766         $bar->setBarWidth(5);
767         $this->assertEquals(5, $bar->getBarWidth());
768
769         // after starting
770         $bar->start();
771         rewind($output->getStream());
772         $this->assertEquals(5, $bar->getBarWidth(), stream_get_contents($output->getStream()));
773         putenv('COLUMNS=120');
774     }
775 }