Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Helper / LegacyProgressHelperTest.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\ProgressHelper;
16 use Symfony\Component\Console\Output\StreamOutput;
17
18 /**
19  * @group legacy
20  * @group time-sensitive
21  */
22 class LegacyProgressHelperTest extends TestCase
23 {
24     public function testAdvance()
25     {
26         $progress = new ProgressHelper();
27         $progress->start($output = $this->getOutputStream());
28         $progress->advance();
29
30         rewind($output->getStream());
31         $this->assertEquals($this->generateOutput('    1 [->--------------------------]'), stream_get_contents($output->getStream()));
32     }
33
34     public function testAdvanceWithStep()
35     {
36         $progress = new ProgressHelper();
37         $progress->start($output = $this->getOutputStream());
38         $progress->advance(5);
39
40         rewind($output->getStream());
41         $this->assertEquals($this->generateOutput('    5 [----->----------------------]'), stream_get_contents($output->getStream()));
42     }
43
44     public function testAdvanceMultipleTimes()
45     {
46         $progress = new ProgressHelper();
47         $progress->start($output = $this->getOutputStream());
48         $progress->advance(3);
49         $progress->advance(2);
50
51         rewind($output->getStream());
52         $this->assertEquals($this->generateOutput('    3 [--->------------------------]').$this->generateOutput('    5 [----->----------------------]'), stream_get_contents($output->getStream()));
53     }
54
55     public function testCustomizations()
56     {
57         $progress = new ProgressHelper();
58         $progress->setBarWidth(10);
59         $progress->setBarCharacter('_');
60         $progress->setEmptyBarCharacter(' ');
61         $progress->setProgressCharacter('/');
62         $progress->setFormat(' %current%/%max% [%bar%] %percent%%');
63         $progress->start($output = $this->getOutputStream(), 10);
64         $progress->advance();
65
66         rewind($output->getStream());
67         $this->assertEquals($this->generateOutput('  1/10 [_/        ]  10%'), stream_get_contents($output->getStream()));
68     }
69
70     public function testPercent()
71     {
72         $progress = new ProgressHelper();
73         $progress->start($output = $this->getOutputStream(), 50);
74         $progress->display();
75         $progress->advance();
76         $progress->advance();
77
78         rewind($output->getStream());
79         $this->assertEquals($this->generateOutput('  0/50 [>---------------------------]   0%').$this->generateOutput('  1/50 [>---------------------------]   2%').$this->generateOutput('  2/50 [=>--------------------------]   4%'), stream_get_contents($output->getStream()));
80     }
81
82     public function testOverwriteWithShorterLine()
83     {
84         $progress = new ProgressHelper();
85         $progress->setFormat(' %current%/%max% [%bar%] %percent%%');
86         $progress->start($output = $this->getOutputStream(), 50);
87         $progress->display();
88         $progress->advance();
89
90         // set shorter format
91         $progress->setFormat(' %current%/%max% [%bar%]');
92         $progress->advance();
93
94         rewind($output->getStream());
95         $this->assertEquals(
96             $this->generateOutput('  0/50 [>---------------------------]   0%').
97             $this->generateOutput('  1/50 [>---------------------------]   2%').
98             $this->generateOutput('  2/50 [=>--------------------------]     '),
99             stream_get_contents($output->getStream())
100         );
101     }
102
103     public function testSetCurrentProgress()
104     {
105         $progress = new ProgressHelper();
106         $progress->start($output = $this->getOutputStream(), 50);
107         $progress->display();
108         $progress->advance();
109         $progress->setCurrent(15);
110         $progress->setCurrent(25);
111
112         rewind($output->getStream());
113         $this->assertEquals(
114             $this->generateOutput('  0/50 [>---------------------------]   0%').
115             $this->generateOutput('  1/50 [>---------------------------]   2%').
116             $this->generateOutput(' 15/50 [========>-------------------]  30%').
117             $this->generateOutput(' 25/50 [==============>-------------]  50%'),
118             stream_get_contents($output->getStream())
119         );
120     }
121
122     /**
123      * @expectedException        \LogicException
124      * @expectedExceptionMessage You must start the progress bar
125      */
126     public function testSetCurrentBeforeStarting()
127     {
128         $progress = new ProgressHelper();
129         $progress->setCurrent(15);
130     }
131
132     /**
133      * @expectedException        \LogicException
134      * @expectedExceptionMessage You can't regress the progress bar
135      */
136     public function testRegressProgress()
137     {
138         $progress = new ProgressHelper();
139         $progress->start($output = $this->getOutputStream(), 50);
140         $progress->setCurrent(15);
141         $progress->setCurrent(10);
142     }
143
144     public function testRedrawFrequency()
145     {
146         $progress = $this->getMockBuilder('Symfony\Component\Console\Helper\ProgressHelper')->setMethods(array('display'))->getMock();
147         $progress->expects($this->exactly(4))
148                  ->method('display');
149
150         $progress->setRedrawFrequency(2);
151
152         $progress->start($output = $this->getOutputStream(), 6);
153         $progress->setCurrent(1);
154         $progress->advance(2);
155         $progress->advance(2);
156         $progress->advance(1);
157     }
158
159     public function testMultiByteSupport()
160     {
161         $progress = new ProgressHelper();
162         $progress->start($output = $this->getOutputStream());
163         $progress->setBarCharacter('■');
164         $progress->advance(3);
165
166         rewind($output->getStream());
167         $this->assertEquals($this->generateOutput('    3 [■■■>------------------------]'), stream_get_contents($output->getStream()));
168     }
169
170     public function testClear()
171     {
172         $progress = new ProgressHelper();
173         $progress->start($output = $this->getOutputStream(), 50);
174         $progress->setCurrent(25);
175         $progress->clear();
176
177         rewind($output->getStream());
178         $this->assertEquals(
179             $this->generateOutput(' 25/50 [==============>-------------]  50%').$this->generateOutput(''),
180             stream_get_contents($output->getStream())
181         );
182     }
183
184     public function testPercentNotHundredBeforeComplete()
185     {
186         $progress = new ProgressHelper();
187         $progress->start($output = $this->getOutputStream(), 200);
188         $progress->display();
189         $progress->advance(199);
190         $progress->advance();
191
192         rewind($output->getStream());
193         $this->assertEquals($this->generateOutput('   0/200 [>---------------------------]   0%').$this->generateOutput(' 199/200 [===========================>]  99%').$this->generateOutput(' 200/200 [============================] 100%'), stream_get_contents($output->getStream()));
194     }
195
196     public function testNonDecoratedOutput()
197     {
198         $progress = new ProgressHelper();
199         $progress->start($output = $this->getOutputStream(false));
200         $progress->advance();
201
202         rewind($output->getStream());
203         $this->assertEquals('', stream_get_contents($output->getStream()));
204     }
205
206     protected function getOutputStream($decorated = true)
207     {
208         return new StreamOutput(fopen('php://memory', 'r+', false), StreamOutput::VERBOSITY_NORMAL, $decorated);
209     }
210
211     protected $lastMessagesLength;
212
213     protected function generateOutput($expected)
214     {
215         $expectedout = $expected;
216
217         if ($this->lastMessagesLength !== null) {
218             $expectedout = str_pad($expected, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
219         }
220
221         $this->lastMessagesLength = strlen($expectedout);
222
223         return "\x0D".$expectedout;
224     }
225 }