Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Helper / ProgressIndicatorTest.php
1 <?php
2
3 namespace Symfony\Component\Console\Tests\Helper;
4
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Console\Helper\ProgressIndicator;
7 use Symfony\Component\Console\Output\StreamOutput;
8
9 /**
10  * @group time-sensitive
11  */
12 class ProgressIndicatorTest extends TestCase
13 {
14     public function testDefaultIndicator()
15     {
16         $bar = new ProgressIndicator($output = $this->getOutputStream());
17         $bar->start('Starting...');
18         usleep(101000);
19         $bar->advance();
20         usleep(101000);
21         $bar->advance();
22         usleep(101000);
23         $bar->advance();
24         usleep(101000);
25         $bar->advance();
26         usleep(101000);
27         $bar->advance();
28         usleep(101000);
29         $bar->setMessage('Advancing...');
30         $bar->advance();
31         $bar->finish('Done...');
32         $bar->start('Starting Again...');
33         usleep(101000);
34         $bar->advance();
35         $bar->finish('Done Again...');
36
37         rewind($output->getStream());
38
39         $this->assertEquals(
40             $this->generateOutput(' - Starting...').
41             $this->generateOutput(' \\ Starting...').
42             $this->generateOutput(' | Starting...').
43             $this->generateOutput(' / Starting...').
44             $this->generateOutput(' - Starting...').
45             $this->generateOutput(' \\ Starting...').
46             $this->generateOutput(' \\ Advancing...').
47             $this->generateOutput(' | Advancing...').
48             $this->generateOutput(' | Done...').
49             PHP_EOL.
50             $this->generateOutput(' - Starting Again...').
51             $this->generateOutput(' \\ Starting Again...').
52             $this->generateOutput(' \\ Done Again...').
53             PHP_EOL,
54             stream_get_contents($output->getStream())
55         );
56     }
57
58     public function testNonDecoratedOutput()
59     {
60         $bar = new ProgressIndicator($output = $this->getOutputStream(false));
61
62         $bar->start('Starting...');
63         $bar->advance();
64         $bar->advance();
65         $bar->setMessage('Midway...');
66         $bar->advance();
67         $bar->advance();
68         $bar->finish('Done...');
69
70         rewind($output->getStream());
71
72         $this->assertEquals(
73             ' Starting...'.PHP_EOL.
74             ' Midway...'.PHP_EOL.
75             ' Done...'.PHP_EOL.PHP_EOL,
76             stream_get_contents($output->getStream())
77         );
78     }
79
80     public function testCustomIndicatorValues()
81     {
82         $bar = new ProgressIndicator($output = $this->getOutputStream(), null, 100, array('a', 'b', 'c'));
83
84         $bar->start('Starting...');
85         usleep(101000);
86         $bar->advance();
87         usleep(101000);
88         $bar->advance();
89         usleep(101000);
90         $bar->advance();
91
92         rewind($output->getStream());
93
94         $this->assertEquals(
95             $this->generateOutput(' a Starting...').
96             $this->generateOutput(' b Starting...').
97             $this->generateOutput(' c Starting...').
98             $this->generateOutput(' a Starting...'),
99             stream_get_contents($output->getStream())
100         );
101     }
102
103     /**
104      * @expectedException \InvalidArgumentException
105      * @expectedExceptionMessage Must have at least 2 indicator value characters.
106      */
107     public function testCannotSetInvalidIndicatorCharacters()
108     {
109         $bar = new ProgressIndicator($this->getOutputStream(), null, 100, array('1'));
110     }
111
112     /**
113      * @expectedException \LogicException
114      * @expectedExceptionMessage Progress indicator already started.
115      */
116     public function testCannotStartAlreadyStartedIndicator()
117     {
118         $bar = new ProgressIndicator($this->getOutputStream());
119         $bar->start('Starting...');
120         $bar->start('Starting Again.');
121     }
122
123     /**
124      * @expectedException \LogicException
125      * @expectedExceptionMessage Progress indicator has not yet been started.
126      */
127     public function testCannotAdvanceUnstartedIndicator()
128     {
129         $bar = new ProgressIndicator($this->getOutputStream());
130         $bar->advance();
131     }
132
133     /**
134      * @expectedException \LogicException
135      * @expectedExceptionMessage Progress indicator has not yet been started.
136      */
137     public function testCannotFinishUnstartedIndicator()
138     {
139         $bar = new ProgressIndicator($this->getOutputStream());
140         $bar->finish('Finished');
141     }
142
143     /**
144      * @dataProvider provideFormat
145      */
146     public function testFormats($format)
147     {
148         $bar = new ProgressIndicator($output = $this->getOutputStream(), $format);
149         $bar->start('Starting...');
150         $bar->advance();
151
152         rewind($output->getStream());
153
154         $this->assertNotEmpty(stream_get_contents($output->getStream()));
155     }
156
157     /**
158      * Provides each defined format.
159      *
160      * @return array
161      */
162     public function provideFormat()
163     {
164         return array(
165             array('normal'),
166             array('verbose'),
167             array('very_verbose'),
168             array('debug'),
169         );
170     }
171
172     protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
173     {
174         return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
175     }
176
177     protected function generateOutput($expected)
178     {
179         $count = substr_count($expected, "\n");
180
181         return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;
182     }
183 }