|
| 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 Symfony\Component\Console\Helper\ProgressHelper; |
| 15 | +use Symfony\Component\Console\Helper\HelperSet; |
| 16 | +use Symfony\Component\Console\Output\StreamOutput; |
| 17 | + |
| 18 | +class ProgressHelperTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + public function testAdvance() |
| 21 | + { |
| 22 | + $progress = new ProgressHelper(); |
| 23 | + $progress->start($output = $this->getOutputStream()); |
| 24 | + $progress->advance(); |
| 25 | + |
| 26 | + rewind($output->getStream()); |
| 27 | + $this->assertEquals($this->generateOutput(' 1 [->--------------------------]'), stream_get_contents($output->getStream())); |
| 28 | + } |
| 29 | + |
| 30 | + public function testAdvanceWithStep() |
| 31 | + { |
| 32 | + $progress = new ProgressHelper(); |
| 33 | + $progress->start($output = $this->getOutputStream()); |
| 34 | + $progress->advance(5); |
| 35 | + |
| 36 | + rewind($output->getStream()); |
| 37 | + $this->assertEquals($this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output
9E12
->getStream())); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAdvanceMultipleTimes() |
| 41 | + { |
| 42 | + $progress = new ProgressHelper(); |
| 43 | + $progress->start($output = $this->getOutputStream()); |
| 44 | + $progress->advance(3); |
| 45 | + $progress->advance(2); |
| 46 | + |
| 47 | + rewind($output->getStream()); |
| 48 | + $this->assertEquals($this->generateOutput(' 3 [--->------------------------]').$this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream())); |
| 49 | + } |
| 50 | + |
| 51 | + public function testCustomizations() |
| 52 | + { |
| 53 | + $progress = new ProgressHelper(); |
| 54 | + $progress->setBarWidth(10); |
| 55 | + $progress->setBarCharacter('_'); |
| 56 | + $progress->setEmptyBarCharacter(' '); |
| 57 | + $progress->setProgressCharacter('/'); |
| 58 | + $progress->setFormat(' %current%/%max% [%bar%] %percent%%'); |
| 59 | + $progress->start($output = $this->getOutputStream(), 10); |
| 60 | + $progress->advance(); |
| 61 | + |
| 62 | + rewind($output->getStream()); |
| 63 | + $this->assertEquals($this->generateOutput(' 1/10 [_/ ] 10%'), stream_get_contents($output->getStream())); |
| 64 | + } |
| 65 | + |
| 66 | + protected function getOutputStream() |
| 67 | + { |
| 68 | + return new StreamOutput(fopen('php://memory', 'r+', false)); |
| 69 | + } |
| 70 | + |
| 71 | + protected function generateOutput($expected) |
| 72 | + { |
| 73 | + return str_repeat("\x08", 80).$expected.str_repeat(' ', 80 - strlen($expected)).str_repeat("\x08", 80 - strlen($expected)); |
| 74 | + } |
| 75 | +} |
0 commit comments