summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Tests/Command/CommandTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/console/Tests/Command/CommandTest.php')
-rw-r--r--vendor/symfony/console/Tests/Command/CommandTest.php117
1 files changed, 78 insertions, 39 deletions
diff --git a/vendor/symfony/console/Tests/Command/CommandTest.php b/vendor/symfony/console/Tests/Command/CommandTest.php
index e8836d8c..4fcbf957 100644
--- a/vendor/symfony/console/Tests/Command/CommandTest.php
+++ b/vendor/symfony/console/Tests/Command/CommandTest.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Console\Tests\Command;
+use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Application;
@@ -23,7 +24,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\CommandTester;
-class CommandTest extends \PHPUnit_Framework_TestCase
+class CommandTest extends TestCase
{
protected static $fixturesPath;
@@ -45,7 +46,7 @@ class CommandTest extends \PHPUnit_Framework_TestCase
*/
public function testCommandNameCannotBeEmpty()
{
- new Command();
+ (new Application())->add(new Command());
}
public function testSetApplication()
@@ -54,6 +55,14 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$command = new \TestCommand();
$command->setApplication($application);
$this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
+ $this->assertEquals($application->getHelperSet(), $command->getHelperSet());
+ }
+
+ public function testSetApplicationNull()
+ {
+ $command = new \TestCommand();
+ $command->setApplication(null);
+ $this->assertNull($command->getHelperSet());
}
public function testSetGetDefinition()
@@ -84,6 +93,13 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
}
+ public function testSetHidden()
+ {
+ $command = new \TestCommand();
+ $command->setHidden(true);
+ $this->assertTrue($command->isHidden());
+ }
+
public function testGetNamespaceGetNameSetName()
{
$command = new \TestCommand();
@@ -101,7 +117,12 @@ class CommandTest extends \PHPUnit_Framework_TestCase
*/
public function testInvalidCommandNames($name)
{
- $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
+ if (method_exists($this, 'expectException')) {
+ $this->expectException('InvalidArgumentException');
+ $this->expectExceptionMessage(sprintf('Command name "%s" is invalid.', $name));
+ } else {
+ $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
+ }
$command = new \TestCommand();
$command->setName($name);
@@ -156,6 +177,13 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
}
+ public function testSetAliasesNull()
+ {
+ $command = new \TestCommand();
+ $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
+ $command->setAliases(null);
+ }
+
public function testGetSynopsis()
{
$command = new \TestCommand();
@@ -164,6 +192,15 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
}
+ public function testAddGetUsages()
+ {
+ $command = new \TestCommand();
+ $command->addUsage('foo1');
+ $command->addUsage('foo2');
+ $this->assertContains('namespace:name foo1', $command->getUsages());
+ $this->assertContains('namespace:name foo2', $command->getUsages());
+ }
+
public function testGetHelper()
{
$application = new Application();
@@ -257,7 +294,7 @@ class CommandTest extends \PHPUnit_Framework_TestCase
}
/**
- * @expectedException Symfony\Component\Console\Exception\InvalidOptionException
+ * @expectedException \Symfony\Component\Console\Exception\InvalidOptionException
* @expectedExceptionMessage The "--bar" option does not exist.
*/
public function testRunWithInvalidOption()
@@ -273,10 +310,10 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$exitCode = $command->run(new StringInput(''), new NullOutput());
$this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
- $command = $this->getMock('TestCommand', array('execute'));
+ $command = $this->getMockBuilder('TestCommand')->setMethods(array('execute'))->getMock();
$command->expects($this->once())
- ->method('execute')
- ->will($this->returnValue('2.3'));
+ ->method('execute')
+ ->will($this->returnValue('2.3'));
$exitCode = $command->run(new StringInput(''), new NullOutput());
$this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
}
@@ -297,6 +334,20 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
}
+ public function testRunWithProcessTitle()
+ {
+ $command = new \TestCommand();
+ $command->setApplication(new Application());
+ $command->setProcessTitle('foo');
+ $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
+ if (function_exists('cli_set_process_title')) {
+ if (null === @cli_get_process_title() && 'Darwin' === PHP_OS) {
+ $this->markTestSkipped('Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.');
+ }
+ $this->assertEquals('foo', cli_get_process_title());
+ }
+ }
+
public function testSetCode()
{
$command = new \TestCommand();
@@ -319,7 +370,6 @@ class CommandTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider getSetCodeBindToClosureTests
- * @requires PHP 5.4
*/
public function testSetCodeBindToClosure($previouslyBound, $expected)
{
@@ -335,53 +385,42 @@ class CommandTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('interact called'.PHP_EOL.$expected.PHP_EOL, $tester->getDisplay());
}
- public function testSetCodeWithNonClosureCallable()
+ public function testSetCodeWithStaticClosure()
{
$command = new \TestCommand();
- $ret = $command->setCode(array($this, 'callableMethodCommand'));
- $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
+ $command->setCode(self::createClosure());
$tester = new CommandTester($command);
$tester->execute(array());
- $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid callable provided to Command::setCode.
- */
- public function testSetCodeWithNonCallable()
- {
- $command = new \TestCommand();
- $command->setCode(array($this, 'nonExistentMethod'));
+ if (\PHP_VERSION_ID < 70000) {
+ // Cannot bind static closures in PHP 5
+ $this->assertEquals('interact called'.PHP_EOL.'not bound'.PHP_EOL, $tester->getDisplay());
+ } else {
+ // Can bind static closures in PHP 7
+ $this->assertEquals('interact called'.PHP_EOL.'bound'.PHP_EOL, $tester->getDisplay());
+ }
}
- public function callableMethodCommand(InputInterface $input, OutputInterface $output)
+ private static function createClosure()
{
- $output->writeln('from the code...');
+ return function (InputInterface $input, OutputInterface $output) {
+ $output->writeln(isset($this) ? 'bound' : 'not bound');
+ };
}
- /**
- * @group legacy
- */
- public function testLegacyAsText()
+ public function testSetCodeWithNonClosureCallable()
{
$command = new \TestCommand();
- $command->setApplication(new Application());
+ $ret = $command->setCode(array($this, 'callableMethodCommand'));
+ $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
$tester = new CommandTester($command);
- $tester->execute(array('command' => $command->getName()));
- $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
+ $tester->execute(array());
+ $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
}
- /**
- * @group legacy
- */
- public function testLegacyAsXml()
+ public function callableMethodCommand(InputInterface $input, OutputInterface $output)
{
- $command = new \TestCommand();
- $command->setApplication(new Application());
- $tester = new CommandTester($command);
- $tester->execute(array('command' => $command->getName()));
- $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');
+ $output->writeln('from the code...');
}
}