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