Kanboard requires at least PHP 5.6 now
This commit is contained in:
34
vendor/symfony/console/Tests/Helper/AbstractQuestionHelperTest.php
vendored
Normal file
34
vendor/symfony/console/Tests/Helper/AbstractQuestionHelperTest.php
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Input\StreamableInputInterface;
|
||||
|
||||
abstract class AbstractQuestionHelperTest extends TestCase
|
||||
{
|
||||
protected function createStreamableInputInterfaceMock($stream = null, $interactive = true)
|
||||
{
|
||||
$mock = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
|
||||
$mock->expects($this->any())
|
||||
->method('isInteractive')
|
||||
->will($this->returnValue($interactive));
|
||||
|
||||
if ($stream) {
|
||||
$mock->expects($this->any())
|
||||
->method('getStream')
|
||||
->willReturn($stream);
|
||||
}
|
||||
|
||||
return $mock;
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\FormatterHelper;
|
||||
|
||||
class FormatterHelperTest extends \PHPUnit_Framework_TestCase
|
||||
class FormatterHelperTest extends TestCase
|
||||
{
|
||||
public function testFormatSection()
|
||||
{
|
||||
@@ -89,4 +90,40 @@ class FormatterHelperTest extends \PHPUnit_Framework_TestCase
|
||||
'::formatBlock() escapes \'<\' chars'
|
||||
);
|
||||
}
|
||||
|
||||
public function testTruncatingWithShorterLengthThanMessageWithSuffix()
|
||||
{
|
||||
$formatter = new FormatterHelper();
|
||||
$message = 'testing truncate';
|
||||
|
||||
$this->assertSame('test...', $formatter->truncate($message, 4));
|
||||
$this->assertSame('testing truncat...', $formatter->truncate($message, 15));
|
||||
$this->assertSame('testing truncate...', $formatter->truncate($message, 16));
|
||||
$this->assertSame('zażółć gęślą...', $formatter->truncate('zażółć gęślą jaźń', 12));
|
||||
}
|
||||
|
||||
public function testTruncatingMessageWithCustomSuffix()
|
||||
{
|
||||
$formatter = new FormatterHelper();
|
||||
$message = 'testing truncate';
|
||||
|
||||
$this->assertSame('test!', $formatter->truncate($message, 4, '!'));
|
||||
}
|
||||
|
||||
public function testTruncatingWithLongerLengthThanMessageWithSuffix()
|
||||
{
|
||||
$formatter = new FormatterHelper();
|
||||
$message = 'test';
|
||||
|
||||
$this->assertSame($message, $formatter->truncate($message, 10));
|
||||
}
|
||||
|
||||
public function testTruncatingWithNegativeLength()
|
||||
{
|
||||
$formatter = new FormatterHelper();
|
||||
$message = 'testing truncate';
|
||||
|
||||
$this->assertSame('testing tru...', $formatter->truncate($message, -5));
|
||||
$this->assertSame('...', $formatter->truncate($message, -100));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
class HelperSetTest extends \PHPUnit_Framework_TestCase
|
||||
class HelperSetTest extends TestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
@@ -108,16 +109,9 @@ class HelperSetTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a generic mock for the helper interface. Optionally check for a call to setHelperSet with a specific
|
||||
* helperset instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @param HelperSet $helperset allows a mock to verify a particular helperset set is being added to the Helper
|
||||
*/
|
||||
private function getGenericMockHelper($name, HelperSet $helperset = null)
|
||||
{
|
||||
$mock_helper = $this->getMock('\Symfony\Component\Console\Helper\HelperInterface');
|
||||
$mock_helper = $this->getMockBuilder('\Symfony\Component\Console\Helper\HelperInterface')->getMock();
|
||||
$mock_helper->expects($this->any())
|
||||
->method('getName')
|
||||
->will($this->returnValue($name));
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
|
||||
class HelperTest extends \PHPUnit_Framework_TestCase
|
||||
class HelperTest extends TestCase
|
||||
{
|
||||
public function formatTimeProvider()
|
||||
{
|
||||
|
||||
@@ -1,263 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Helper\DialogHelper;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Helper\FormatterHelper;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSelect()
|
||||
{
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
|
||||
$this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2'));
|
||||
$this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
|
||||
$this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
|
||||
$this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream()));
|
||||
|
||||
try {
|
||||
$this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1));
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
|
||||
}
|
||||
|
||||
$this->assertEquals(array('1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
|
||||
$this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
|
||||
$this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
|
||||
$this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '0,1', false, 'Input "%s" is not a superhero!', true));
|
||||
$this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, ' 0 , 1 ', false, 'Input "%s" is not a superhero!', true));
|
||||
}
|
||||
|
||||
public function testSelectOnErrorOutput()
|
||||
{
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("Stdout\n1\n"));
|
||||
$this->assertEquals('1', $dialog->select($output = $this->getConsoleOutput($this->getOutputStream()), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
|
||||
|
||||
rewind($output->getErrorOutput()->getStream());
|
||||
$this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||
}
|
||||
|
||||
public function testAsk()
|
||||
{
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("\n8AM\n"));
|
||||
|
||||
$this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
|
||||
$this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testAskOnErrorOutput()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stderr` is required to test stderr output functionality');
|
||||
}
|
||||
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("not stdout\n"));
|
||||
|
||||
$this->assertEquals('not stdout', $dialog->ask($output = $this->getConsoleOutput($this->getOutputStream()), 'Where should output go?', 'stderr'));
|
||||
|
||||
rewind($output->getErrorOutput()->getStream());
|
||||
$this->assertEquals('Where should output go?', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||
}
|
||||
|
||||
public function testAskWithAutocomplete()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
}
|
||||
|
||||
// Acm<NEWLINE>
|
||||
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
|
||||
// <NEWLINE>
|
||||
// <UP ARROW><UP ARROW><NEWLINE>
|
||||
// <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
|
||||
// <DOWN ARROW><NEWLINE>
|
||||
// S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
|
||||
// F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
|
||||
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n\033[A\033[A\n\033[A\033[A\033[A\033[A\033[A\tTest\n\033[B\nS\177\177\033[B\033[B\nF00\177\177oo\t\n");
|
||||
|
||||
$dialog = new DialogHelper();
|
||||
$dialog->setInputStream($inputStream);
|
||||
|
||||
$bundles = array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle');
|
||||
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('AsseticBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('FrameworkBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('SecurityBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('FooBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('AsseticBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
$this->assertEquals('FooBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tty
|
||||
*/
|
||||
public function testAskHiddenResponse()
|
||||
{
|
||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||
$this->markTestSkipped('This test is not supported on Windows');
|
||||
}
|
||||
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("8AM\n"));
|
||||
|
||||
$this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tty
|
||||
*/
|
||||
public function testAskHiddenResponseOnErrorOutput()
|
||||
{
|
||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||
$this->markTestSkipped('This test is not supported on Windows');
|
||||
}
|
||||
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("8AM\n"));
|
||||
|
||||
$this->assertEquals('8AM', $dialog->askHiddenResponse($output = $this->getConsoleOutput($this->getOutputStream()), 'What time is it?'));
|
||||
|
||||
rewind($output->getErrorOutput()->getStream());
|
||||
$this->assertContains('What time is it?', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||
}
|
||||
|
||||
public function testAskConfirmation()
|
||||
{
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("\n\n"));
|
||||
$this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
|
||||
$this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("y\nyes\n"));
|
||||
$this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
|
||||
$this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("n\nno\n"));
|
||||
$this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
|
||||
$this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
|
||||
}
|
||||
|
||||
public function testAskAndValidate()
|
||||
{
|
||||
$dialog = new DialogHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = 'What color was the white horse of Henry IV?';
|
||||
$error = 'This is not a color!';
|
||||
$validator = function ($color) use ($error) {
|
||||
if (!in_array($color, array('white', 'black'))) {
|
||||
throw new InvalidArgumentException($error);
|
||||
}
|
||||
|
||||
return $color;
|
||||
};
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("\nblack\n"));
|
||||
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
|
||||
$this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
|
||||
|
||||
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
|
||||
try {
|
||||
$this->assertEquals('white', $dialog->askAndValidate($output = $this->getConsoleOutput($this->getOutputStream()), $question, $validator, 2, 'white'));
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertEquals($error, $e->getMessage());
|
||||
rewind($output->getErrorOutput()->getStream());
|
||||
$this->assertContains('What color was the white horse of Henry IV?', stream_get_contents($output->getErrorOutput()->getStream()));
|
||||
}
|
||||
}
|
||||
|
||||
public function testNoInteraction()
|
||||
{
|
||||
$dialog = new DialogHelper();
|
||||
|
||||
$input = new ArrayInput(array());
|
||||
$input->setInteractive(false);
|
||||
|
||||
$dialog->setInput($input);
|
||||
|
||||
$this->assertEquals('not yet', $dialog->ask($this->getOutputStream(), 'Do you have a job?', 'not yet'));
|
||||
}
|
||||
|
||||
protected function getInputStream($input)
|
||||
{
|
||||
$stream = fopen('php://memory', 'r+', false);
|
||||
fwrite($stream, $input);
|
||||
rewind($stream);
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
protected function getOutputStream()
|
||||
{
|
||||
return new StreamOutput(fopen('php://memory', 'r+', false));
|
||||
}
|
||||
|
||||
protected function getConsoleOutput($stderr)
|
||||
{
|
||||
$output = new ConsoleOutput();
|
||||
$output->setErrorOutput($stderr);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
private function hasStderrSupport()
|
||||
{
|
||||
return false === $this->isRunningOS400();
|
||||
}
|
||||
|
||||
private function hasSttyAvailable()
|
||||
{
|
||||
exec('stty 2>&1', $output, $exitcode);
|
||||
|
||||
return $exitcode === 0;
|
||||
}
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use Symfony\Component\Console\Helper\ProgressHelper;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @group time-sensitive
|
||||
*/
|
||||
class LegacyProgressHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testAdvance()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream());
|
||||
$progress->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 1 [->--------------------------]'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testAdvanceWithStep()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream());
|
||||
$progress->advance(5);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testAdvanceMultipleTimes()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream());
|
||||
$progress->advance(3);
|
||||
$progress->advance(2);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 3 [--->------------------------]').$this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testCustomizations()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->setBarWidth(10);
|
||||
$progress->setBarCharacter('_');
|
||||
$progress->setEmptyBarCharacter(' ');
|
||||
$progress->setProgressCharacter('/');
|
||||
$progress->setFormat(' %current%/%max% [%bar%] %percent%%');
|
||||
$progress->start($output = $this->getOutputStream(), 10);
|
||||
$progress->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 1/10 [_/ ] 10%'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testPercent()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream(), 50);
|
||||
$progress->display();
|
||||
$progress->advance();
|
||||
$progress->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 0/50 [>---------------------------] 0%').$this->generateOutput(' 1/50 [>---------------------------] 2%').$this->generateOutput(' 2/50 [=>--------------------------] 4%'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testOverwriteWithShorterLine()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->setFormat(' %current%/%max% [%bar%] %percent%%');
|
||||
$progress->start($output = $this->getOutputStream(), 50);
|
||||
$progress->display();
|
||||
$progress->advance();
|
||||
|
||||
// set shorter format
|
||||
$progress->setFormat(' %current%/%max% [%bar%]');
|
||||
$progress->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
$this->generateOutput(' 1/50 [>---------------------------] 2%').
|
||||
$this->generateOutput(' 2/50 [=>--------------------------] '),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetCurrentProgress()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream(), 50);
|
||||
$progress->display();
|
||||
$progress->advance();
|
||||
$progress->setCurrent(15);
|
||||
$progress->setCurrent(25);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
$this->generateOutput(' 1/50 [>---------------------------] 2%').
|
||||
$this->generateOutput(' 15/50 [========>-------------------] 30%').
|
||||
$this->generateOutput(' 25/50 [==============>-------------] 50%'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage You must start the progress bar
|
||||
*/
|
||||
public function testSetCurrentBeforeStarting()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->setCurrent(15);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage You can't regress the progress bar
|
||||
*/
|
||||
public function testRegressProgress()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream(), 50);
|
||||
$progress->setCurrent(15);
|
||||
$progress->setCurrent(10);
|
||||
}
|
||||
|
||||
public function testRedrawFrequency()
|
||||
{
|
||||
$progress = $this->getMock('Symfony\Component\Console\Helper\ProgressHelper', array('display'));
|
||||
$progress->expects($this->exactly(4))
|
||||
->method('display');
|
||||
|
||||
$progress->setRedrawFrequency(2);
|
||||
|
||||
$progress->start($output = $this->getOutputStream(), 6);
|
||||
$progress->setCurrent(1);
|
||||
$progress->advance(2);
|
||||
$progress->advance(2);
|
||||
$progress->advance(1);
|
||||
}
|
||||
|
||||
public function testMultiByteSupport()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream());
|
||||
$progress->setBarCharacter('■');
|
||||
$progress->advance(3);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 3 [■■■>------------------------]'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testClear()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream(), 50);
|
||||
$progress->setCurrent(25);
|
||||
$progress->clear();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 25/50 [==============>-------------] 50%').$this->generateOutput(''),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testPercentNotHundredBeforeComplete()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream(), 200);
|
||||
$progress->display();
|
||||
$progress->advance(199);
|
||||
$progress->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals($this->generateOutput(' 0/200 [>---------------------------] 0%').$this->generateOutput(' 199/200 [===========================>] 99%').$this->generateOutput(' 200/200 [============================] 100%'), stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testNonDecoratedOutput()
|
||||
{
|
||||
$progress = new ProgressHelper();
|
||||
$progress->start($output = $this->getOutputStream(false));
|
||||
$progress->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals('', stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
protected function getOutputStream($decorated = true)
|
||||
{
|
||||
return new StreamOutput(fopen('php://memory', 'r+', false), StreamOutput::VERBOSITY_NORMAL, $decorated);
|
||||
}
|
||||
|
||||
protected $lastMessagesLength;
|
||||
|
||||
protected function generateOutput($expected)
|
||||
{
|
||||
$expectedout = $expected;
|
||||
|
||||
if ($this->lastMessagesLength !== null) {
|
||||
$expectedout = str_pad($expected, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
|
||||
}
|
||||
|
||||
$this->lastMessagesLength = strlen($expectedout);
|
||||
|
||||
return "\x0D".$expectedout;
|
||||
}
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use Symfony\Component\Console\Helper\TableHelper;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
class LegacyTableHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $stream;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->stream = fopen('php://memory', 'r+');
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
fclose($this->stream);
|
||||
$this->stream = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testRenderProvider
|
||||
*/
|
||||
public function testRender($headers, $rows, $layout, $expected)
|
||||
{
|
||||
$table = new TableHelper();
|
||||
$table
|
||||
->setHeaders($headers)
|
||||
->setRows($rows)
|
||||
->setLayout($layout)
|
||||
;
|
||||
$table->render($output = $this->getOutputStream());
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testRenderProvider
|
||||
*/
|
||||
public function testRenderAddRows($headers, $rows, $layout, $expected)
|
||||
{
|
||||
$table = new TableHelper();
|
||||
$table
|
||||
->setHeaders($headers)
|
||||
->addRows($rows)
|
||||
->setLayout($layout)
|
||||
;
|
||||
$table->render($output = $this->getOutputStream());
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testRenderProvider
|
||||
*/
|
||||
public function testRenderAddRowsOneByOne($headers, $rows, $layout, $expected)
|
||||
{
|
||||
$table = new TableHelper();
|
||||
$table
|
||||
->setHeaders($headers)
|
||||
->setLayout($layout)
|
||||
;
|
||||
foreach ($rows as $row) {
|
||||
$table->addRow($row);
|
||||
}
|
||||
$table->render($output = $this->getOutputStream());
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
public function testRenderProvider()
|
||||
{
|
||||
$books = array(
|
||||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
|
||||
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
|
||||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
|
||||
);
|
||||
|
||||
return array(
|
||||
array(
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
$books,
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+---------------+--------------------------+------------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+--------------------------+------------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
|
||||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
|
||||
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
|
||||
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
|
||||
+---------------+--------------------------+------------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
$books,
|
||||
TableHelper::LAYOUT_COMPACT,
|
||||
<<<TABLE
|
||||
ISBN Title Author
|
||||
99921-58-10-7 Divine Comedy Dante Alighieri
|
||||
9971-5-0210-0 A Tale of Two Cities Charles Dickens
|
||||
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
|
||||
80-902734-1-6 And Then There Were None Agatha Christie
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
$books,
|
||||
TableHelper::LAYOUT_BORDERLESS,
|
||||
<<<TABLE
|
||||
=============== ========================== ==================
|
||||
ISBN Title Author
|
||||
=============== ========================== ==================
|
||||
99921-58-10-7 Divine Comedy Dante Alighieri
|
||||
9971-5-0210-0 A Tale of Two Cities Charles Dickens
|
||||
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
|
||||
80-902734-1-6 And Then There Were None Agatha Christie
|
||||
=============== ========================== ==================
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array('ISBN', 'Title'),
|
||||
array(
|
||||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
|
||||
array('9971-5-0210-0'),
|
||||
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
|
||||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
|
||||
),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+---------------+--------------------------+------------------+
|
||||
| ISBN | Title | |
|
||||
+---------------+--------------------------+------------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
|
||||
| 9971-5-0210-0 | | |
|
||||
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
|
||||
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
|
||||
+---------------+--------------------------+------------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
array(
|
||||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
|
||||
array('9971-5-0210-0'),
|
||||
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
|
||||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
|
||||
),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+---------------+--------------------------+------------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
|
||||
| 9971-5-0210-0 | | |
|
||||
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
|
||||
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
|
||||
+---------------+--------------------------+------------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
array(
|
||||
array('99921-58-10-7', "Divine\nComedy", 'Dante Alighieri'),
|
||||
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
|
||||
array('9971-5-0210-2', "Harry Potter\nand the Chamber of Secrets", "Rowling\nJoanne K."),
|
||||
array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
|
||||
),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+---------------+----------------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+----------------------------+-----------------+
|
||||
| 99921-58-10-7 | Divine | Dante Alighieri |
|
||||
| | Comedy | |
|
||||
| 9971-5-0210-2 | Harry Potter | Rowling |
|
||||
| | and the Chamber of Secrets | Joanne K. |
|
||||
| 9971-5-0210-2 | Harry Potter | Rowling |
|
||||
| | and the Chamber of Secrets | Joanne K. |
|
||||
| 960-425-059-0 | The Lord of the Rings | J. R. R. |
|
||||
| | | Tolkien |
|
||||
+---------------+----------------------------+-----------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array('ISBN', 'Title'),
|
||||
array(),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+------+-------+
|
||||
| ISBN | Title |
|
||||
+------+-------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
array(),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
'',
|
||||
),
|
||||
'Cell text with tags used for Output styling' => array(
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
array(
|
||||
array('<info>99921-58-10-7</info>', '<error>Divine Comedy</error>', '<fg=blue;bg=white>Dante Alighieri</fg=blue;bg=white>'),
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
|
||||
),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+---------------+----------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+----------------------+-----------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
|
||||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
|
||||
+---------------+----------------------+-----------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
'Cell text with tags not used for Output styling' => array(
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
array(
|
||||
array('<strong>99921-58-10-700</strong>', '<f>Divine Com</f>', 'Dante Alighieri'),
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
|
||||
),
|
||||
TableHelper::LAYOUT_DEFAULT,
|
||||
<<<'TABLE'
|
||||
+----------------------------------+----------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+----------------------------------+----------------------+-----------------+
|
||||
| <strong>99921-58-10-700</strong> | <f>Divine Com</f> | Dante Alighieri |
|
||||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
|
||||
+----------------------------------+----------------------+-----------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderMultiByte()
|
||||
{
|
||||
$table = new TableHelper();
|
||||
$table
|
||||
->setHeaders(array('■■'))
|
||||
->setRows(array(array(1234)))
|
||||
->setLayout(TableHelper::LAYOUT_DEFAULT)
|
||||
;
|
||||
$table->render($output = $this->getOutputStream());
|
||||
|
||||
$expected =
|
||||
<<<'TABLE'
|
||||
+------+
|
||||
| ■■ |
|
||||
+------+
|
||||
| 1234 |
|
||||
+------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
public function testRenderFullWidthCharacters()
|
||||
{
|
||||
$table = new TableHelper();
|
||||
$table
|
||||
->setHeaders(array('あいうえお'))
|
||||
->setRows(array(array(1234567890)))
|
||||
->setLayout(TableHelper::LAYOUT_DEFAULT)
|
||||
;
|
||||
$table->render($output = $this->getOutputStream());
|
||||
|
||||
$expected =
|
||||
<<<'TABLE'
|
||||
+------------+
|
||||
| あいうえお |
|
||||
+------------+
|
||||
| 1234567890 |
|
||||
+------------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
protected function getOutputStream()
|
||||
{
|
||||
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
|
||||
}
|
||||
|
||||
protected function getOutputContent(StreamOutput $output)
|
||||
{
|
||||
rewind($output->getStream());
|
||||
|
||||
return str_replace(PHP_EOL, "\n", stream_get_contents($output->getStream()));
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,14 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\DebugFormatterHelper;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
use Symfony\Component\Console\Helper\ProcessHelper;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class ProcessHelperTest extends \PHPUnit_Framework_TestCase
|
||||
class ProcessHelperTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideCommandsAndOutput
|
||||
@@ -46,35 +47,35 @@ class ProcessHelperTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function provideCommandsAndOutput()
|
||||
{
|
||||
$successOutputVerbose = <<<EOT
|
||||
$successOutputVerbose = <<<'EOT'
|
||||
RUN php -r "echo 42;"
|
||||
RES Command ran successfully
|
||||
|
||||
EOT;
|
||||
$successOutputDebug = <<<EOT
|
||||
$successOutputDebug = <<<'EOT'
|
||||
RUN php -r "echo 42;"
|
||||
OUT 42
|
||||
RES Command ran successfully
|
||||
|
||||
EOT;
|
||||
$successOutputDebugWithTags = <<<EOT
|
||||
$successOutputDebugWithTags = <<<'EOT'
|
||||
RUN php -r "echo '<info>42</info>';"
|
||||
OUT <info>42</info>
|
||||
RES Command ran successfully
|
||||
|
||||
EOT;
|
||||
$successOutputProcessDebug = <<<EOT
|
||||
$successOutputProcessDebug = <<<'EOT'
|
||||
RUN 'php' '-r' 'echo 42;'
|
||||
OUT 42
|
||||
RES Command ran successfully
|
||||
|
||||
EOT;
|
||||
$syntaxErrorOutputVerbose = <<<EOT
|
||||
$syntaxErrorOutputVerbose = <<<'EOT'
|
||||
RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);"
|
||||
RES 252 Command did not run successfully
|
||||
|
||||
EOT;
|
||||
$syntaxErrorOutputDebug = <<<EOT
|
||||
$syntaxErrorOutputDebug = <<<'EOT'
|
||||
RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
|
||||
ERR error message
|
||||
OUT out message
|
||||
@@ -83,9 +84,9 @@ EOT;
|
||||
EOT;
|
||||
|
||||
$errorMessage = 'An error occurred';
|
||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||
$successOutputProcessDebug = str_replace("'", '"', $successOutputProcessDebug);
|
||||
}
|
||||
$args = new Process(array('php', '-r', 'echo 42;'));
|
||||
$args = $args->getCommandLine();
|
||||
$successOutputProcessDebug = str_replace("'php' '-r' 'echo 42;'", $args, $successOutputProcessDebug);
|
||||
|
||||
return array(
|
||||
array('', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null),
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Helper\Helper;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
@@ -18,7 +19,7 @@ use Symfony\Component\Console\Output\StreamOutput;
|
||||
/**
|
||||
* @group time-sensitive
|
||||
*/
|
||||
class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
class ProgressBarTest extends TestCase
|
||||
{
|
||||
public function testMultipleStart()
|
||||
{
|
||||
@@ -29,7 +30,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0 [>---------------------------]').
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 1 [->--------------------------]').
|
||||
$this->generateOutput(' 0 [>---------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
@@ -44,7 +45,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0 [>---------------------------]').
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 1 [->--------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
@@ -58,7 +59,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0 [>---------------------------]').
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 5 [----->----------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
@@ -73,7 +74,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0 [>---------------------------]').
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 3 [--->------------------------]').
|
||||
$this->generateOutput(' 5 [----->----------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
@@ -89,17 +90,88 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 9/10 [=========================>--] 90%').
|
||||
' 9/10 [=========================>--] 90%'.
|
||||
$this->generateOutput(' 10/10 [============================] 100%').
|
||||
$this->generateOutput(' 11/11 [============================] 100%'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testRegress()
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar->start();
|
||||
$bar->advance();
|
||||
$bar->advance();
|
||||
$bar->advance(-1);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 1 [->--------------------------]').
|
||||
$this->generateOutput(' 2 [-->-------------------------]').
|
||||
$this->generateOutput(' 1 [->--------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testRegressWithStep()
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar->start();
|
||||
$bar->advance(4);
|
||||
$bar->advance(4);
|
||||
$bar->advance(-2);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 4 [---->-----------------------]').
|
||||
$this->generateOutput(' 8 [-------->-------------------]').
|
||||
$this->generateOutput(' 6 [------>---------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testRegressMultipleTimes()
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar->start();
|
||||
$bar->advance(3);
|
||||
$bar->advance(3);
|
||||
$bar->advance(-1);
|
||||
$bar->advance(-2);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 3 [--->------------------------]').
|
||||
$this->generateOutput(' 6 [------>---------------------]').
|
||||
$this->generateOutput(' 5 [----->----------------------]').
|
||||
$this->generateOutput(' 3 [--->------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testRegressBelowMin()
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
|
||||
$bar->setProgress(1);
|
||||
$bar->advance(-1);
|
||||
$bar->advance(-1);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 1/10 [==>-------------------------] 10%'.
|
||||
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testFormat()
|
||||
{
|
||||
$expected =
|
||||
$this->generateOutput(' 0/10 [>---------------------------] 0%').
|
||||
' 0/10 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 10/10 [============================] 100%').
|
||||
$this->generateOutput(' 10/10 [============================] 100%')
|
||||
;
|
||||
@@ -156,7 +228,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/10 [/ ] 0%').
|
||||
' 0/10 [/ ] 0%'.
|
||||
$this->generateOutput(' 1/10 [_/ ] 10%'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
@@ -169,7 +241,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%'),
|
||||
' 0/50 [>---------------------------] 0%',
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
@@ -193,7 +265,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 50/50 [============================] 100%'),
|
||||
' 50/50 [============================] 100%',
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
@@ -208,7 +280,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
' 0/50 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
$this->generateOutput(' 1/50 [>---------------------------] 2%').
|
||||
$this->generateOutput(' 2/50 [=>--------------------------] 4%'),
|
||||
@@ -230,7 +302,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
' 0/50 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
$this->generateOutput(' 1/50 [>---------------------------] 2%').
|
||||
$this->generateOutput(' 2/50 [=>--------------------------]'),
|
||||
@@ -247,7 +319,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------]').
|
||||
' 0/50 [>---------------------------]'.
|
||||
$this->generateOutput(' 1/50 [>---------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
@@ -264,7 +336,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
' 0/50 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
$this->generateOutput(' 1/50 [>---------------------------] 2%').
|
||||
$this->generateOutput(' 15/50 [========>-------------------] 30%').
|
||||
@@ -273,8 +345,6 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testSetCurrentBeforeStarting()
|
||||
{
|
||||
$bar = new ProgressBar($this->getOutputStream());
|
||||
@@ -282,49 +352,54 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertNotNull($bar->getStartTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage You can't regress the progress bar
|
||||
*/
|
||||
public function testRegressProgress()
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
||||
$bar->start();
|
||||
$bar->setProgress(15);
|
||||
$bar->setProgress(10);
|
||||
}
|
||||
|
||||
public function testRedrawFrequency()
|
||||
{
|
||||
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream(), 6));
|
||||
$bar->expects($this->exactly(4))->method('display');
|
||||
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(), 6);
|
||||
$bar->setRedrawFrequency(2);
|
||||
$bar->start();
|
||||
$bar->setProgress(1);
|
||||
$bar->advance(2);
|
||||
$bar->advance(2);
|
||||
$bar->advance(1);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0/6 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 3/6 [==============>-------------] 50%').
|
||||
$this->generateOutput(' 5/6 [=======================>----] 83%').
|
||||
$this->generateOutput(' 6/6 [============================] 100%'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
|
||||
{
|
||||
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream()));
|
||||
|
||||
$bar->expects($this->exactly(2))->method('display');
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar->setRedrawFrequency(0);
|
||||
$bar->start();
|
||||
$bar->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 1 [->--------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
|
||||
{
|
||||
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream()));
|
||||
|
||||
$bar->expects($this->exactly(2))->method('display');
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar->setRedrawFrequency(0.9);
|
||||
$bar->start();
|
||||
$bar->advance();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 1 [->--------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testMultiByteSupport()
|
||||
@@ -336,7 +411,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0 [>---------------------------]').
|
||||
' 0 [>---------------------------]'.
|
||||
$this->generateOutput(' 3 [■■■>------------------------]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
@@ -351,7 +426,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/50 [>---------------------------] 0%').
|
||||
' 0/50 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 25/50 [==============>-------------] 50%').
|
||||
$this->generateOutput(''),
|
||||
stream_get_contents($output->getStream())
|
||||
@@ -368,7 +443,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/200 [>---------------------------] 0%').
|
||||
' 0/200 [>---------------------------] 0%'.
|
||||
$this->generateOutput(' 0/200 [>---------------------------] 0%').
|
||||
$this->generateOutput(' 199/200 [===========================>] 99%').
|
||||
$this->generateOutput(' 200/200 [============================] 100%'),
|
||||
@@ -468,9 +543,9 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/2 [>---------------------------] 0%')."\n".
|
||||
$this->generateOutput(' 0/3 [#---------------------------] 0%')."\n".
|
||||
rtrim($this->generateOutput(' 0 [>---------------------------]')).
|
||||
' 0/2 [>---------------------------] 0%'."\n".
|
||||
' 0/3 [#---------------------------] 0%'."\n".
|
||||
rtrim(' 0 [>---------------------------]').
|
||||
|
||||
"\033[2A".
|
||||
$this->generateOutput(' 1/2 [==============>-------------] 50%')."\n".
|
||||
@@ -508,7 +583,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
rtrim($this->generateOutput(' 0 [>---------------------------]')).
|
||||
rtrim(' 0 [>---------------------------]').
|
||||
rtrim($this->generateOutput(' 1 [->--------------------------]')).
|
||||
rtrim($this->generateOutput(' 2 [-->-------------------------]')).
|
||||
rtrim($this->generateOutput(' 3 [--->------------------------]')).
|
||||
@@ -517,6 +592,24 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testWithSmallScreen()
|
||||
{
|
||||
$output = $this->getOutputStream();
|
||||
|
||||
$bar = new ProgressBar($output);
|
||||
putenv('COLUMNS=12');
|
||||
$bar->start();
|
||||
$bar->advance();
|
||||
putenv('COLUMNS=120');
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
' 0 [>---]'.
|
||||
$this->generateOutput(' 1 [->--]'),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
|
||||
public function testAddingPlaceholderFormatter()
|
||||
{
|
||||
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
|
||||
@@ -531,7 +624,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 3 [>---------------------------]').
|
||||
' 3 [>---------------------------]'.
|
||||
$this->generateOutput(' 2 [=========>------------------]').
|
||||
$this->generateOutput(' 0 [============================]'),
|
||||
stream_get_contents($output->getStream())
|
||||
@@ -550,7 +643,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(">---------------------------\nfoobar").
|
||||
">---------------------------\nfoobar".
|
||||
$this->generateOutput("=========>------------------\nfoobar").
|
||||
"\x0D\x1B[2K\x1B[1A\x1B[2K".
|
||||
$this->generateOutput("============================\nfoobar"),
|
||||
@@ -560,6 +653,8 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testAnsiColorsAndEmojis()
|
||||
{
|
||||
putenv('COLUMNS=156');
|
||||
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(), 15);
|
||||
ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
|
||||
static $i = 0;
|
||||
@@ -575,23 +670,37 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$bar->setMessage('Starting the demo... fingers crossed', 'title');
|
||||
$bar->start();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
|
||||
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
|
||||
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m",
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
ftruncate($output->getStream(), 0);
|
||||
rewind($output->getStream());
|
||||
|
||||
$bar->setMessage('Looks good to me...', 'title');
|
||||
$bar->advance(4);
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(
|
||||
" \033[44;37m Looks good to me... \033[0m\n".
|
||||
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
|
||||
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 97 KiB \033[0m"
|
||||
),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
ftruncate($output->getStream(), 0);
|
||||
rewind($output->getStream());
|
||||
|
||||
$bar->setMessage('Thanks, bye', 'title');
|
||||
$bar->finish();
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(
|
||||
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
|
||||
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
|
||||
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m"
|
||||
).
|
||||
$this->generateOutput(
|
||||
" \033[44;37m Looks good to me... \033[0m\n".
|
||||
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
|
||||
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 97 KiB \033[0m"
|
||||
).
|
||||
$this->generateOutput(
|
||||
" \033[44;37m Thanks, bye \033[0m\n".
|
||||
' 15/15 '.str_repeat($done, 28)." 100%\n".
|
||||
@@ -599,6 +708,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
),
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
putenv('COLUMNS=120');
|
||||
}
|
||||
|
||||
public function testSetFormat()
|
||||
@@ -608,7 +718,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
$bar->start();
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0 [>---------------------------]'),
|
||||
' 0 [>---------------------------]',
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
|
||||
@@ -617,7 +727,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
$bar->start();
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(
|
||||
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
|
||||
' 0/10 [>---------------------------] 0%',
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
@@ -661,4 +771,22 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
|
||||
}
|
||||
|
||||
public function testBarWidthWithMultilineFormat()
|
||||
{
|
||||
putenv('COLUMNS=10');
|
||||
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar->setFormat("%bar%\n0123456789");
|
||||
|
||||
// before starting
|
||||
$bar->setBarWidth(5);
|
||||
$this->assertEquals(5, $bar->getBarWidth());
|
||||
|
||||
// after starting
|
||||
$bar->start();
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals(5, $bar->getBarWidth(), stream_get_contents($output->getStream()));
|
||||
putenv('COLUMNS=120');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\ProgressIndicator;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
|
||||
/**
|
||||
* @group time-sensitive
|
||||
*/
|
||||
class ProgressIndicatorTest extends \PHPUnit_Framework_TestCase
|
||||
class ProgressIndicatorTest extends TestCase
|
||||
{
|
||||
public function testDefaultIndicator()
|
||||
{
|
||||
@@ -44,11 +45,11 @@ class ProgressIndicatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->generateOutput(' \\ Starting...').
|
||||
$this->generateOutput(' \\ Advancing...').
|
||||
$this->generateOutput(' | Advancing...').
|
||||
$this->generateOutput(' | Done... ').
|
||||
$this->generateOutput(' | Done...').
|
||||
PHP_EOL.
|
||||
$this->generateOutput(' - Starting Again...').
|
||||
$this->generateOutput(' \\ Starting Again...').
|
||||
$this->generateOutput(' \\ Done Again... ').
|
||||
$this->generateOutput(' \\ Done Again...').
|
||||
PHP_EOL,
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
@@ -70,8 +71,8 @@ class ProgressIndicatorTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals(
|
||||
' Starting...'.PHP_EOL.
|
||||
' Midway... '.PHP_EOL.
|
||||
' Done... '.PHP_EOL.PHP_EOL,
|
||||
' Midway...'.PHP_EOL.
|
||||
' Done...'.PHP_EOL.PHP_EOL,
|
||||
stream_get_contents($output->getStream())
|
||||
);
|
||||
}
|
||||
@@ -177,6 +178,6 @@ class ProgressIndicatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$count = substr_count($expected, "\n");
|
||||
|
||||
return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected;
|
||||
return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ use Symfony\Component\Console\Question\Question;
|
||||
/**
|
||||
* @group tty
|
||||
*/
|
||||
class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
class QuestionHelperTest extends AbstractQuestionHelperTest
|
||||
{
|
||||
public function testAskChoice()
|
||||
{
|
||||
@@ -34,6 +34,447 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||
|
||||
$inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
|
||||
$question->setMaxAttempts(1);
|
||||
// first answer is an empty answer, we're supposed to receive the default value
|
||||
$this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
|
||||
$question->setMaxAttempts(1);
|
||||
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
|
||||
$question->setErrorMessage('Input "%s" is not a superhero!');
|
||||
$question->setMaxAttempts(2);
|
||||
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
|
||||
|
||||
rewind($output->getStream());
|
||||
$stream = stream_get_contents($output->getStream());
|
||||
$this->assertContains('Input "Fabien" is not a superhero!', $stream);
|
||||
|
||||
try {
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
|
||||
$question->setMaxAttempts(1);
|
||||
$questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
|
||||
}
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
|
||||
$this->assertEquals(array('Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
|
||||
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
|
||||
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAsk()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
$inputStream = $this->getInputStream("\n8AM\n");
|
||||
|
||||
$question = new Question('What time is it?', '2PM');
|
||||
$this->assertEquals('2PM', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new Question('What time is it?', '2PM');
|
||||
$this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
|
||||
|
||||
rewind($output->getStream());
|
||||
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testAskWithAutocomplete()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
}
|
||||
|
||||
// Acm<NEWLINE>
|
||||
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
|
||||
// <NEWLINE>
|
||||
// <UP ARROW><UP ARROW><NEWLINE>
|
||||
// <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
|
||||
// <DOWN ARROW><NEWLINE>
|
||||
// S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
|
||||
// F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
|
||||
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n\033[A\033[A\n\033[A\033[A\033[A\033[A\033[A\tTest\n\033[B\nS\177\177\033[B\033[B\nF00\177\177oo\t\n");
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new Question('Please select a bundle', 'FrameworkBundle');
|
||||
$question->setAutocompleterValues(array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'));
|
||||
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AsseticBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('FrameworkBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('SecurityBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('FooBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AsseticBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAskWithAutocompleteWithNonSequentialKeys()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
}
|
||||
|
||||
// <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
|
||||
$inputStream = $this->getInputStream("\033[A\033[A\n\033[B\033[B\n");
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$dialog->setHelperSet(new HelperSet(array(new FormatterHelper())));
|
||||
|
||||
$question = new ChoiceQuestion('Please select a bundle', array(1 => 'AcmeDemoBundle', 4 => 'AsseticBundle'));
|
||||
$question->setMaxAttempts(1);
|
||||
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AsseticBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAutocompleteWithTrailingBackslash()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
}
|
||||
|
||||
$inputStream = $this->getInputStream('E');
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new Question('');
|
||||
$expectedCompletion = 'ExampleNamespace\\';
|
||||
$question->setAutocompleterValues(array($expectedCompletion));
|
||||
|
||||
$output = $this->createOutputInterface();
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $output, $question);
|
||||
|
||||
$outputStream = $output->getStream();
|
||||
rewind($outputStream);
|
||||
$actualOutput = stream_get_contents($outputStream);
|
||||
|
||||
// Shell control (esc) sequences are not so important: we only care that
|
||||
// <hl> tag is interpreted correctly and replaced
|
||||
$irrelevantEscSequences = array(
|
||||
"\0337" => '', // Save cursor position
|
||||
"\0338" => '', // Restore cursor position
|
||||
"\033[K" => '', // Clear line from cursor till the end
|
||||
);
|
||||
|
||||
$importantActualOutput = strtr($actualOutput, $irrelevantEscSequences);
|
||||
|
||||
// Remove colors (e.g. "\033[30m", "\033[31;41m")
|
||||
$importantActualOutput = preg_replace('/\033\[\d+(;\d+)?m/', '', $importantActualOutput);
|
||||
|
||||
$this->assertEquals($expectedCompletion, $importantActualOutput);
|
||||
}
|
||||
|
||||
public function testAskHiddenResponse()
|
||||
{
|
||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||
$this->markTestSkipped('This test is not supported on Windows');
|
||||
}
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
$question = new Question('What time is it?');
|
||||
$question->setHidden(true);
|
||||
|
||||
$this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("8AM\n")), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getAskConfirmationData
|
||||
*/
|
||||
public function testAskConfirmation($question, $expected, $default = true)
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
$inputStream = $this->getInputStream($question."\n");
|
||||
$question = new ConfirmationQuestion('Do you like French fries?', $default);
|
||||
$this->assertEquals($expected, $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
|
||||
}
|
||||
|
||||
public function getAskConfirmationData()
|
||||
{
|
||||
return array(
|
||||
array('', true),
|
||||
array('', false, false),
|
||||
array('y', true),
|
||||
array('yes', true),
|
||||
array('n', false),
|
||||
array('no', false),
|
||||
);
|
||||
}
|
||||
|
||||
public function testAskConfirmationWithCustomTrueAnswer()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
$inputStream = $this->getInputStream("j\ny\n");
|
||||
$question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
|
||||
$this->assertTrue($dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$question = new ConfirmationQuestion('Do you like French fries?', false, '/^(j|y)/i');
|
||||
$this->assertTrue($dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAskAndValidate()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$error = 'This is not a color!';
|
||||
$validator = function ($color) use ($error) {
|
||||
if (!in_array($color, array('white', 'black'))) {
|
||||
throw new \InvalidArgumentException($error);
|
||||
}
|
||||
|
||||
return $color;
|
||||
};
|
||||
|
||||
$question = new Question('What color was the white horse of Henry IV?', 'white');
|
||||
$question->setValidator($validator);
|
||||
$question->setMaxAttempts(2);
|
||||
|
||||
$inputStream = $this->getInputStream("\nblack\n");
|
||||
$this->assertEquals('white', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('black', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
try {
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("green\nyellow\norange\n")), $this->createOutputInterface(), $question);
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertEquals($error, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider simpleAnswerProvider
|
||||
*/
|
||||
public function testSelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'My environment 1',
|
||||
'My environment 2',
|
||||
'My environment 3',
|
||||
);
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
|
||||
$question->setMaxAttempts(1);
|
||||
$answer = $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream($providedAnswer."\n")), $this->createOutputInterface(), $question);
|
||||
|
||||
$this->assertSame($expectedValue, $answer);
|
||||
}
|
||||
|
||||
public function simpleAnswerProvider()
|
||||
{
|
||||
return array(
|
||||
array(0, 'My environment 1'),
|
||||
array(1, 'My environment 2'),
|
||||
array(2, 'My environment 3'),
|
||||
array('My environment 1', 'My environment 1'),
|
||||
array('My environment 2', 'My environment 2'),
|
||||
array('My environment 3', 'My environment 3'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider specialCharacterInMultipleChoice
|
||||
*/
|
||||
public function testSpecialCharacterChoiceFromMultipleChoiceList($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'.',
|
||||
'src',
|
||||
);
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$inputStream = $this->getInputStream($providedAnswer."\n");
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new ChoiceQuestion('Please select the directory', $possibleChoices);
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
$answer = $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question);
|
||||
|
||||
$this->assertSame($expectedValue, $answer);
|
||||
}
|
||||
|
||||
public function specialCharacterInMultipleChoice()
|
||||
{
|
||||
return array(
|
||||
array('.', array('.')),
|
||||
array('., src', array('.', 'src')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider mixedKeysChoiceListAnswerProvider
|
||||
*/
|
||||
public function testChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'0' => 'No environment',
|
||||
'1' => 'My environment 1',
|
||||
'env_2' => 'My environment 2',
|
||||
3 => 'My environment 3',
|
||||
);
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
|
||||
$question->setMaxAttempts(1);
|
||||
$answer = $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream($providedAnswer."\n")), $this->createOutputInterface(), $question);
|
||||
|
||||
$this->assertSame($expectedValue, $answer);
|
||||
}
|
||||
|
||||
public function mixedKeysChoiceListAnswerProvider()
|
||||
{
|
||||
return array(
|
||||
array('0', '0'),
|
||||
array('No environment', '0'),
|
||||
array('1', '1'),
|
||||
array('env_2', 'env_2'),
|
||||
array(3, '3'),
|
||||
array('My environment 1', '1'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider answerProvider
|
||||
*/
|
||||
public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'env_1' => 'My environment 1',
|
||||
'env_2' => 'My environment',
|
||||
'env_3' => 'My environment',
|
||||
);
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
|
||||
$question->setMaxAttempts(1);
|
||||
$answer = $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream($providedAnswer."\n")), $this->createOutputInterface(), $question);
|
||||
|
||||
$this->assertSame($expectedValue, $answer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3.
|
||||
*/
|
||||
public function testAmbiguousChoiceFromChoicelist()
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'env_1' => 'My first environment',
|
||||
'env_2' => 'My environment',
|
||||
'env_3' => 'My environment',
|
||||
);
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new ChoiceQuestion('Please select the environment to load', $possibleChoices);
|
||||
$question->setMaxAttempts(1);
|
||||
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("My environment\n")), $this->createOutputInterface(), $question);
|
||||
}
|
||||
|
||||
public function answerProvider()
|
||||
{
|
||||
return array(
|
||||
array('env_1', 'env_1'),
|
||||
array('env_2', 'env_2'),
|
||||
array('env_3', 'env_3'),
|
||||
array('My environment 1', 'env_1'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoInteraction()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
$question = new Question('Do you have a job?', 'not yet');
|
||||
$this->assertEquals('not yet', $dialog->ask($this->createStreamableInputInterfaceMock(null, false), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires function mb_strwidth
|
||||
*/
|
||||
public function testChoiceOutputFormattingQuestionForUtf8Keys()
|
||||
{
|
||||
$question = 'Lorem ipsum?';
|
||||
$possibleChoices = array(
|
||||
'foo' => 'foo',
|
||||
'żółw' => 'bar',
|
||||
'łabądź' => 'baz',
|
||||
);
|
||||
$outputShown = array(
|
||||
$question,
|
||||
' [<info>foo </info>] foo',
|
||||
' [<info>żółw </info>] bar',
|
||||
' [<info>łabądź</info>] baz',
|
||||
);
|
||||
$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
|
||||
$output->method('getFormatter')->willReturn(new OutputFormatter());
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$output->expects($this->once())->method('writeln')->with($this->equalTo($outputShown));
|
||||
|
||||
$question = new ChoiceQuestion($question, $possibleChoices, 'foo');
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("\n")), $output, $question);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAskChoice()
|
||||
{
|
||||
$questionHelper = new QuestionHelper();
|
||||
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$questionHelper->setHelperSet($helperSet);
|
||||
|
||||
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||
|
||||
$questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
|
||||
@@ -85,7 +526,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAsk()
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAsk()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
@@ -101,7 +545,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
|
||||
}
|
||||
|
||||
public function testAskWithAutocomplete()
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAskWithAutocomplete()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
@@ -135,7 +582,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('FooBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAskWithAutocompleteWithNonSequentialKeys()
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAskWithAutocompleteWithNonSequentialKeys()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
@@ -155,7 +605,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('AsseticBundle', $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAskHiddenResponse()
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAskHiddenResponse()
|
||||
{
|
||||
if ('\\' === DIRECTORY_SEPARATOR) {
|
||||
$this->markTestSkipped('This test is not supported on Windows');
|
||||
@@ -171,9 +624,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @dataProvider getAskConfirmationData
|
||||
*/
|
||||
public function testAskConfirmation($question, $expected, $default = true)
|
||||
public function testLegacyAskConfirmation($question, $expected, $default = true)
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
@@ -182,19 +636,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
|
||||
}
|
||||
|
||||
public function getAskConfirmationData()
|
||||
{
|
||||
return array(
|
||||
array('', true),
|
||||
array('', false, false),
|
||||
array('y', true),
|
||||
array('yes', true),
|
||||
array('n', false),
|
||||
array('no', false),
|
||||
);
|
||||
}
|
||||
|
||||
public function testAskConfirmationWithCustomTrueAnswer()
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAskConfirmationWithCustomTrueAnswer()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
@@ -205,7 +650,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAskAndValidate()
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyAskAndValidate()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
@@ -238,9 +686,10 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @dataProvider simpleAnswerProvider
|
||||
*/
|
||||
public function testSelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
|
||||
public function testLegacySelectChoiceFromSimpleChoices($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'My environment 1',
|
||||
@@ -260,22 +709,11 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSame($expectedValue, $answer);
|
||||
}
|
||||
|
||||
public function simpleAnswerProvider()
|
||||
{
|
||||
return array(
|
||||
array(0, 'My environment 1'),
|
||||
array(1, 'My environment 2'),
|
||||
array(2, 'My environment 3'),
|
||||
array('My environment 1', 'My environment 1'),
|
||||
array('My environment 2', 'My environment 2'),
|
||||
array('My environment 3', 'My environment 3'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @dataProvider mixedKeysChoiceListAnswerProvider
|
||||
*/
|
||||
public function testChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
|
||||
public function testLegacyChoiceFromChoicelistWithMixedKeys($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'0' => 'No environment',
|
||||
@@ -296,22 +734,11 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSame($expectedValue, $answer);
|
||||
}
|
||||
|
||||
public function mixedKeysChoiceListAnswerProvider()
|
||||
{
|
||||
return array(
|
||||
array('0', '0'),
|
||||
array('No environment', '0'),
|
||||
array('1', '1'),
|
||||
array('env_2', 'env_2'),
|
||||
array(3, '3'),
|
||||
array('My environment 1', '1'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @dataProvider answerProvider
|
||||
*/
|
||||
public function testSelectChoiceFromChoiceList($providedAnswer, $expectedValue)
|
||||
public function testLegacySelectChoiceFromChoiceList($providedAnswer, $expectedValue)
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'env_1' => 'My environment 1',
|
||||
@@ -332,10 +759,11 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3.
|
||||
*/
|
||||
public function testAmbiguousChoiceFromChoicelist()
|
||||
public function testLegacyAmbiguousChoiceFromChoicelist()
|
||||
{
|
||||
$possibleChoices = array(
|
||||
'env_1' => 'My first environment',
|
||||
@@ -354,27 +782,11 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
|
||||
}
|
||||
|
||||
public function answerProvider()
|
||||
{
|
||||
return array(
|
||||
array('env_1', 'env_1'),
|
||||
array('env_2', 'env_2'),
|
||||
array('env_3', 'env_3'),
|
||||
array('My environment 1', 'env_1'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoInteraction()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
$question = new Question('Do you have a job?', 'not yet');
|
||||
$this->assertEquals('not yet', $dialog->ask($this->createInputInterfaceMock(false), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires function mb_strwidth
|
||||
* @group legacy
|
||||
*/
|
||||
public function testChoiceOutputFormattingQuestionForUtf8Keys()
|
||||
public function testLegacyChoiceOutputFormattingQuestionForUtf8Keys()
|
||||
{
|
||||
$question = 'Lorem ipsum?';
|
||||
$possibleChoices = array(
|
||||
@@ -388,7 +800,7 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
' [<info>żółw </info>] bar',
|
||||
' [<info>łabądź</info>] baz',
|
||||
);
|
||||
$output = $this->getMock('\Symfony\Component\Console\Output\OutputInterface');
|
||||
$output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock();
|
||||
$output->method('getFormatter')->willReturn(new OutputFormatter());
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
@@ -402,6 +814,76 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$dialog->ask($this->createInputInterfaceMock(), $output, $question);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
|
||||
* @expectedExceptionMessage Aborted
|
||||
*/
|
||||
public function testAskThrowsExceptionOnMissingInput()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
|
||||
* @expectedExceptionMessage Aborted
|
||||
*/
|
||||
public function testAskThrowsExceptionOnMissingInputWithValidator()
|
||||
{
|
||||
$dialog = new QuestionHelper();
|
||||
|
||||
$question = new Question('What\'s your name?');
|
||||
$question->setValidator(function () {
|
||||
if (!$value) {
|
||||
throw new \Exception('A value is required.');
|
||||
}
|
||||
});
|
||||
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Choice question must have at least 1 choice available.
|
||||
*/
|
||||
public function testEmptyChoices()
|
||||
{
|
||||
new ChoiceQuestion('Question', array(), 'irrelevant');
|
||||
}
|
||||
|
||||
public function testTraversableAutocomplete()
|
||||
{
|
||||
if (!$this->hasSttyAvailable()) {
|
||||
$this->markTestSkipped('`stty` is required to test autocomplete functionality');
|
||||
}
|
||||
|
||||
// Acm<NEWLINE>
|
||||
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
|
||||
// <NEWLINE>
|
||||
// <UP ARROW><UP ARROW><NEWLINE>
|
||||
// <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
|
||||
// <DOWN ARROW><NEWLINE>
|
||||
// S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
|
||||
// F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
|
||||
$inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n\033[A\033[A\n\033[A\033[A\033[A\033[A\033[A\tTest\n\033[B\nS\177\177\033[B\033[B\nF00\177\177oo\t\n");
|
||||
|
||||
$dialog = new QuestionHelper();
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$dialog->setHelperSet($helperSet);
|
||||
|
||||
$question = new Question('Please select a bundle', 'FrameworkBundle');
|
||||
$question->setAutocompleterValues(new AutocompleteValues(array('irrelevant' => 'AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle')));
|
||||
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AsseticBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('FrameworkBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('SecurityBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('FooBundleTest', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AcmeDemoBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('AsseticBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('FooBundle', $dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
protected function getInputStream($input)
|
||||
{
|
||||
$stream = fopen('php://memory', 'r+', false);
|
||||
@@ -418,7 +900,7 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected function createInputInterfaceMock($interactive = true)
|
||||
{
|
||||
$mock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
|
||||
$mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
||||
$mock->expects($this->any())
|
||||
->method('isInteractive')
|
||||
->will($this->returnValue($interactive));
|
||||
@@ -430,6 +912,21 @@ class QuestionHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
exec('stty 2>&1', $output, $exitcode);
|
||||
|
||||
return $exitcode === 0;
|
||||
return 0 === $exitcode;
|
||||
}
|
||||
}
|
||||
|
||||
class AutocompleteValues implements \IteratorAggregate
|
||||
{
|
||||
private $values;
|
||||
|
||||
public function __construct(array $values)
|
||||
{
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->values);
|
||||
}
|
||||
}
|
||||
|
||||
156
vendor/symfony/console/Tests/Helper/SymfonyQuestionHelperTest.php
vendored
Normal file
156
vendor/symfony/console/Tests/Helper/SymfonyQuestionHelperTest.php
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use Symfony\Component\Console\Helper\FormatterHelper;
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
|
||||
/**
|
||||
* @group tty
|
||||
*/
|
||||
class SymfonyQuestionHelperTest extends AbstractQuestionHelperTest
|
||||
{
|
||||
public function testAskChoice()
|
||||
{
|
||||
$questionHelper = new SymfonyQuestionHelper();
|
||||
|
||||
$helperSet = new HelperSet(array(new FormatterHelper()));
|
||||
$questionHelper->setHelperSet($helperSet);
|
||||
|
||||
$heroes = array('Superman', 'Batman', 'Spiderman');
|
||||
|
||||
$inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
|
||||
$question->setMaxAttempts(1);
|
||||
// first answer is an empty answer, we're supposed to receive the default value
|
||||
$this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
|
||||
$this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
|
||||
$question->setMaxAttempts(1);
|
||||
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
|
||||
$question->setErrorMessage('Input "%s" is not a superhero!');
|
||||
$question->setMaxAttempts(2);
|
||||
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
|
||||
$this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
|
||||
|
||||
try {
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
|
||||
$question->setMaxAttempts(1);
|
||||
$questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
|
||||
$this->fail();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
|
||||
}
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
|
||||
$this->assertEquals(array('Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
|
||||
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
|
||||
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
|
||||
|
||||
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
|
||||
$question->setMaxAttempts(1);
|
||||
$question->setMultiselect(true);
|
||||
|
||||
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
|
||||
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
|
||||
}
|
||||
|
||||
public function testAskReturnsNullIfValidatorAllowsIt()
|
||||
{
|
||||
$questionHelper = new SymfonyQuestionHelper();
|
||||
$question = new Question('What is your favorite superhero?');
|
||||
$question->setValidator(function ($value) { return $value; });
|
||||
$input = $this->createStreamableInputInterfaceMock($this->getInputStream("\n"));
|
||||
$this->assertNull($questionHelper->ask($input, $this->createOutputInterface(), $question));
|
||||
}
|
||||
|
||||
public function testAskEscapeDefaultValue()
|
||||
{
|
||||
$helper = new SymfonyQuestionHelper();
|
||||
$input = $this->createStreamableInputInterfaceMock($this->getInputStream('\\'));
|
||||
$helper->ask($input, $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
|
||||
|
||||
$this->assertOutputContains('Can I have a backslash? [\]', $output);
|
||||
}
|
||||
|
||||
public function testAskEscapeAndFormatLabel()
|
||||
{
|
||||
$helper = new SymfonyQuestionHelper();
|
||||
$input = $this->createStreamableInputInterfaceMock($this->getInputStream('Foo\\Bar'));
|
||||
$helper->ask($input, $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
|
||||
|
||||
$this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
|
||||
}
|
||||
|
||||
public function testLabelTrailingBackslash()
|
||||
{
|
||||
$helper = new SymfonyQuestionHelper();
|
||||
$input = $this->createStreamableInputInterfaceMock($this->getInputStream('sure'));
|
||||
$helper->ask($input, $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
|
||||
|
||||
$this->assertOutputContains('Question with a trailing \\', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
|
||||
* @expectedExceptionMessage Aborted
|
||||
*/
|
||||
public function testAskThrowsExceptionOnMissingInput()
|
||||
{
|
||||
$dialog = new SymfonyQuestionHelper();
|
||||
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
|
||||
}
|
||||
|
||||
protected function getInputStream($input)
|
||||
{
|
||||
$stream = fopen('php://memory', 'r+', false);
|
||||
fwrite($stream, $input);
|
||||
rewind($stream);
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
protected function createOutputInterface()
|
||||
{
|
||||
$output = new StreamOutput(fopen('php://memory', 'r+', false));
|
||||
$output->setDecorated(false);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
protected function createInputInterfaceMock($interactive = true)
|
||||
{
|
||||
$mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
|
||||
$mock->expects($this->any())
|
||||
->method('isInteractive')
|
||||
->will($this->returnValue($interactive));
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
private function assertOutputContains($expected, StreamOutput $output)
|
||||
{
|
||||
rewind($output->getStream());
|
||||
$stream = stream_get_contents($output->getStream());
|
||||
$this->assertContains($expected, $stream);
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,10 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\TableStyle;
|
||||
|
||||
class TableStyleTest extends \PHPUnit_Framework_TestCase
|
||||
class TableStyleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
|
||||
271
vendor/symfony/console/Tests/Helper/TableTest.php
vendored
271
vendor/symfony/console/Tests/Helper/TableTest.php
vendored
@@ -11,13 +11,14 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Helper\TableStyle;
|
||||
use Symfony\Component\Console\Helper\TableSeparator;
|
||||
use Symfony\Component\Console\Helper\TableCell;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
|
||||
class TableTest extends \PHPUnit_Framework_TestCase
|
||||
class TableTest extends TestCase
|
||||
{
|
||||
protected $stream;
|
||||
|
||||
@@ -33,11 +34,11 @@ class TableTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testRenderProvider
|
||||
* @dataProvider renderProvider
|
||||
*/
|
||||
public function testRender($headers, $rows, $style, $expected)
|
||||
public function testRender($headers, $rows, $style, $expected, $decorated = false)
|
||||
{
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
$table = new Table($output = $this->getOutputStream($decorated));
|
||||
$table
|
||||
->setHeaders($headers)
|
||||
->setRows($rows)
|
||||
@@ -49,11 +50,11 @@ class TableTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testRenderProvider
|
||||
* @dataProvider renderProvider
|
||||
*/
|
||||
public function testRenderAddRows($headers, $rows, $style, $expected)
|
||||
public function testRenderAddRows($headers, $rows, $style, $expected, $decorated = false)
|
||||
{
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
$table = new Table($output = $this->getOutputStream($decorated));
|
||||
$table
|
||||
->setHeaders($headers)
|
||||
->addRows($rows)
|
||||
@@ -65,11 +66,11 @@ class TableTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider testRenderProvider
|
||||
* @dataProvider renderProvider
|
||||
*/
|
||||
public function testRenderAddRowsOneByOne($headers, $rows, $style, $expected)
|
||||
public function testRenderAddRowsOneByOne($headers, $rows, $style, $expected, $decorated = false)
|
||||
{
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
$table = new Table($output = $this->getOutputStream($decorated));
|
||||
$table
|
||||
->setHeaders($headers)
|
||||
->setStyle($style)
|
||||
@@ -82,7 +83,7 @@ class TableTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
public function testRenderProvider()
|
||||
public function renderProvider()
|
||||
{
|
||||
$books = array(
|
||||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
|
||||
@@ -96,7 +97,7 @@ class TableTest extends \PHPUnit_Framework_TestCase
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
$books,
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---------------+--------------------------+------------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+--------------------------+------------------+
|
||||
@@ -112,7 +113,7 @@ TABLE
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
$books,
|
||||
'compact',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
ISBN Title Author
|
||||
99921-58-10-7 Divine Comedy Dante Alighieri
|
||||
9971-5-0210-0 A Tale of Two Cities Charles Dickens
|
||||
@@ -125,7 +126,7 @@ TABLE
|
||||
array('ISBN', 'Title', 'Author'),
|
||||
$books,
|
||||
'borderless',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
=============== ========================== ==================
|
||||
ISBN Title Author
|
||||
=============== ========================== ==================
|
||||
@@ -146,7 +147,7 @@ TABLE
|
||||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---------------+--------------------------+------------------+
|
||||
| ISBN | Title | |
|
||||
+---------------+--------------------------+------------------+
|
||||
@@ -167,7 +168,7 @@ TABLE
|
||||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---------------+--------------------------+------------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
|
||||
| 9971-5-0210-0 | | |
|
||||
@@ -186,7 +187,7 @@ TABLE
|
||||
array('960-425-059-0', 'The Lord of the Rings', "J. R. R.\nTolkien"),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---------------+----------------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+----------------------------+-----------------+
|
||||
@@ -206,7 +207,7 @@ TABLE
|
||||
array('ISBN', 'Title'),
|
||||
array(),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+------+-------+
|
||||
| ISBN | Title |
|
||||
+------+-------+
|
||||
@@ -226,7 +227,7 @@ TABLE
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', '<info>Charles Dickens</>'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---------------+----------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+----------------------+-----------------+
|
||||
@@ -243,7 +244,7 @@ TABLE
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+----------------------------------+----------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+----------------------------------+----------------------+-----------------+
|
||||
@@ -275,7 +276,7 @@ TABLE
|
||||
),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+-------------------------------+-------------------------------+-----------------------------+
|
||||
| ISBN | Title | Author |
|
||||
+-------------------------------+-------------------------------+-----------------------------+
|
||||
@@ -298,29 +299,29 @@ TABLE
|
||||
array(
|
||||
array(
|
||||
new TableCell('9971-5-0210-0', array('rowspan' => 3)),
|
||||
'Divine Comedy',
|
||||
new TableCell('Divine Comedy', array('rowspan' => 2)),
|
||||
'Dante Alighieri',
|
||||
),
|
||||
array('A Tale of Two Cities', 'Charles Dickens'),
|
||||
array(),
|
||||
array("The Lord of \nthe Rings", "J. R. \nR. Tolkien"),
|
||||
new TableSeparator(),
|
||||
array('80-902734-1-6', new TableCell("And Then \nThere \nWere None", array('rowspan' => 3)), 'Agatha Christie'),
|
||||
array('80-902734-1-7', 'Test'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
+---------------+----------------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+----------------------+-----------------+
|
||||
| 9971-5-0210-0 | Divine Comedy | Dante Alighieri |
|
||||
| | A Tale of Two Cities | Charles Dickens |
|
||||
| | The Lord of | J. R. |
|
||||
| | the Rings | R. Tolkien |
|
||||
+---------------+----------------------+-----------------+
|
||||
| 80-902734-1-6 | And Then | Agatha Christie |
|
||||
| 80-902734-1-7 | There | Test |
|
||||
| | Were None | |
|
||||
+---------------+----------------------+-----------------+
|
||||
<<<'TABLE'
|
||||
+---------------+---------------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+---------------+---------------+-----------------+
|
||||
| 9971-5-0210-0 | Divine Comedy | Dante Alighieri |
|
||||
| | | |
|
||||
| | The Lord of | J. R. |
|
||||
| | the Rings | R. Tolkien |
|
||||
+---------------+---------------+-----------------+
|
||||
| 80-902734-1-6 | And Then | Agatha Christie |
|
||||
| 80-902734-1-7 | There | Test |
|
||||
| | Were None | |
|
||||
+---------------+---------------+-----------------+
|
||||
|
||||
TABLE
|
||||
),
|
||||
@@ -341,7 +342,7 @@ TABLE
|
||||
array('J. R. R'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+------------------+---------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+------------------+---------+-----------------+
|
||||
@@ -376,7 +377,7 @@ TABLE
|
||||
),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+-----------------+-------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+-----------------+-------+-----------------+
|
||||
@@ -413,7 +414,7 @@ TABLE
|
||||
array('Charles Dickens'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+-----------------+-------+-----------------+
|
||||
| ISBN | Title | Author |
|
||||
+-----------------+-------+-----------------+
|
||||
@@ -440,7 +441,7 @@ TABLE
|
||||
array('Charles Dickens'),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---------------+-----------------+
|
||||
| ISBN | Author |
|
||||
+---------------+-----------------+
|
||||
@@ -458,7 +459,7 @@ TABLE
|
||||
),
|
||||
array(),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+------+-------+--------+
|
||||
| Main title |
|
||||
+------+-------+--------+
|
||||
@@ -478,13 +479,71 @@ TABLE
|
||||
),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+---+--+--+---+--+---+--+---+--+
|
||||
| 1 | 2 | 3 | 4 |
|
||||
+---+--+--+---+--+---+--+---+--+
|
||||
|
||||
TABLE
|
||||
),
|
||||
'Coslpan and table cells with comment style' => array(
|
||||
array(
|
||||
new TableCell('<comment>Long Title</comment>', array('colspan' => 3)),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
new TableCell('9971-5-0210-0', array('colspan' => 3)),
|
||||
),
|
||||
new TableSeparator(),
|
||||
array(
|
||||
'Dante Alighieri',
|
||||
'J. R. R. Tolkien',
|
||||
'J. R. R',
|
||||
),
|
||||
),
|
||||
'default',
|
||||
<<<TABLE
|
||||
+-----------------+------------------+---------+
|
||||
|\033[32m \033[39m\033[33mLong Title\033[39m\033[32m \033[39m|
|
||||
+-----------------+------------------+---------+
|
||||
| 9971-5-0210-0 |
|
||||
+-----------------+------------------+---------+
|
||||
| Dante Alighieri | J. R. R. Tolkien | J. R. R |
|
||||
+-----------------+------------------+---------+
|
||||
|
||||
TABLE
|
||||
,
|
||||
true,
|
||||
),
|
||||
'Row with formatted cells containing a newline' => array(
|
||||
array(),
|
||||
array(
|
||||
array(
|
||||
new TableCell('<error>Dont break'."\n".'here</error>', array('colspan' => 2)),
|
||||
),
|
||||
new TableSeparator(),
|
||||
array(
|
||||
'foo',
|
||||
new TableCell('<error>Dont break'."\n".'here</error>', array('rowspan' => 2)),
|
||||
),
|
||||
array(
|
||||
'bar',
|
||||
),
|
||||
),
|
||||
'default',
|
||||
<<<'TABLE'
|
||||
+-------+------------+
|
||||
[39;49m| [39;49m[37;41mDont break[39;49m[39;49m |[39;49m
|
||||
[39;49m| [39;49m[37;41mhere[39;49m |
|
||||
+-------+------------+
|
||||
[39;49m| foo | [39;49m[37;41mDont break[39;49m[39;49m |[39;49m
|
||||
[39;49m| bar | [39;49m[37;41mhere[39;49m |
|
||||
+-------+------------+
|
||||
|
||||
TABLE
|
||||
,
|
||||
true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -499,13 +558,49 @@ TABLE
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+------+
|
||||
| ■■ |
|
||||
+------+
|
||||
| 1234 |
|
||||
+------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
public function testTableCellWithNumericIntValue()
|
||||
{
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
|
||||
$table->setRows(array(array(new TableCell(12345))));
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<'TABLE'
|
||||
+-------+
|
||||
| 12345 |
|
||||
+-------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
public function testTableCellWithNumericFloatValue()
|
||||
{
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
|
||||
$table->setRows(array(array(new TableCell(12345.01))));
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<'TABLE'
|
||||
+----------+
|
||||
| 12345.01 |
|
||||
+----------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
@@ -529,7 +624,7 @@ TABLE;
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
.......
|
||||
. Foo .
|
||||
.......
|
||||
@@ -556,7 +651,7 @@ TABLE;
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<TABLE
|
||||
<<<'TABLE'
|
||||
+------+
|
||||
| Foo |
|
||||
+------+
|
||||
@@ -631,9 +726,91 @@ TABLE;
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
protected function getOutputStream()
|
||||
public function testColumnWith()
|
||||
{
|
||||
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
$table
|
||||
->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
|
||||
->setRows(array(
|
||||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
|
||||
))
|
||||
->setColumnWidth(0, 15)
|
||||
->setColumnWidth(3, 10);
|
||||
|
||||
$style = new TableStyle();
|
||||
$style->setPadType(STR_PAD_LEFT);
|
||||
$table->setColumnStyle(3, $style);
|
||||
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<TABLE
|
||||
+-----------------+----------------------+-----------------+------------+
|
||||
| ISBN | Title | Author | Price |
|
||||
+-----------------+----------------------+-----------------+------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
|
||||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
|
||||
+-----------------+----------------------+-----------------+------------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
public function testColumnWiths()
|
||||
{
|
||||
$table = new Table($output = $this->getOutputStream());
|
||||
$table
|
||||
->setHeaders(array('ISBN', 'Title', 'Author', 'Price'))
|
||||
->setRows(array(
|
||||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', '9.95'),
|
||||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', '139.25'),
|
||||
))
|
||||
->setColumnWidths(array(15, 0, -1, 10));
|
||||
|
||||
$style = new TableStyle();
|
||||
$style->setPadType(STR_PAD_LEFT);
|
||||
$table->setColumnStyle(3, $style);
|
||||
|
||||
$table->render();
|
||||
|
||||
$expected =
|
||||
<<<TABLE
|
||||
+-----------------+----------------------+-----------------+------------+
|
||||
| ISBN | Title | Author | Price |
|
||||
+-----------------+----------------------+-----------------+------------+
|
||||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | 9.95 |
|
||||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | 139.25 |
|
||||
+-----------------+----------------------+-----------------+------------+
|
||||
|
||||
TABLE;
|
||||
|
||||
$this->assertEquals($expected, $this->getOutputContent($output));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Style "absent" is not defined.
|
||||
*/
|
||||
public function testIsNotDefinedStyleException()
|
||||
{
|
||||
$table = new Table($this->getOutputStream());
|
||||
$table->setStyle('absent');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Style "absent" is not defined.
|
||||
*/
|
||||
public function testGetStyleDefinition()
|
||||
{
|
||||
Table::getStyleDefinition('absent');
|
||||
}
|
||||
|
||||
protected function getOutputStream($decorated = false)
|
||||
{
|
||||
return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, $decorated);
|
||||
}
|
||||
|
||||
protected function getOutputContent(StreamOutput $output)
|
||||
|
||||
Reference in New Issue
Block a user