4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Command;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Helper\FormatterHelper;
16 use Symfony\Component\Console\Application;
17 use Symfony\Component\Console\Input\InputDefinition;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\StringInput;
22 use Symfony\Component\Console\Output\OutputInterface;
23 use Symfony\Component\Console\Output\NullOutput;
24 use Symfony\Component\Console\Tester\CommandTester;
26 class CommandTest extends \PHPUnit_Framework_TestCase
28 protected static $fixturesPath;
30 public static function setUpBeforeClass()
32 self::$fixturesPath = __DIR__.'/../Fixtures/';
33 require_once self::$fixturesPath.'/TestCommand.php';
36 public function testConstructor()
39 $command = new Command();
40 $this->fail('__construct() throws a \LogicException if the name is null');
41 } catch (\Exception $e) {
42 $this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null');
43 $this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null');
45 $command = new Command('foo:bar');
46 $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
49 public function testSetApplication()
51 $application = new Application();
52 $command = new \TestCommand();
53 $command->setApplication($application);
54 $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
57 public function testSetGetDefinition()
59 $command = new \TestCommand();
60 $ret = $command->setDefinition($definition = new InputDefinition());
61 $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
62 $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
63 $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
64 $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
65 $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
66 $command->setDefinition(new InputDefinition());
69 public function testAddArgument()
71 $command = new \TestCommand();
72 $ret = $command->addArgument('foo');
73 $this->assertEquals($command, $ret, '->addArgument() implements a fluent interface');
74 $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
77 public function testAddOption()
79 $command = new \TestCommand();
80 $ret = $command->addOption('foo');
81 $this->assertEquals($command, $ret, '->addOption() implements a fluent interface');
82 $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command');
85 public function testGetNamespaceGetNameSetName()
87 $command = new \TestCommand();
88 $this->assertEquals('namespace:name', $command->getName(), '->getName() returns the command name');
89 $command->setName('foo');
90 $this->assertEquals('foo', $command->getName(), '->setName() sets the command name');
92 $ret = $command->setName('foobar:bar');
93 $this->assertEquals($command, $ret, '->setName() implements a fluent interface');
94 $this->assertEquals('foobar:bar', $command->getName(), '->setName() sets the command name');
97 $command->setName('');
98 $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
99 } catch (\Exception $e) {
100 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
101 $this->assertEquals('Command name "" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
105 $command->setName('foo:');
106 $this->fail('->setName() throws an \InvalidArgumentException if the name is empty');
107 } catch (\Exception $e) {
108 $this->assertInstanceOf('\InvalidArgumentException', $e, '->setName() throws an \InvalidArgumentException if the name is empty');
109 $this->assertEquals('Command name "foo:" is invalid.', $e->getMessage(), '->setName() throws an \InvalidArgumentException if the name is empty');
113 public function testGetSetDescription()
115 $command = new \TestCommand();
116 $this->assertEquals('description', $command->getDescription(), '->getDescription() returns the description');
117 $ret = $command->setDescription('description1');
118 $this->assertEquals($command, $ret, '->setDescription() implements a fluent interface');
119 $this->assertEquals('description1', $command->getDescription(), '->setDescription() sets the description');
122 public function testGetSetHelp()
124 $command = new \TestCommand();
125 $this->assertEquals('help', $command->getHelp(), '->getHelp() returns the help');
126 $ret = $command->setHelp('help1');
127 $this->assertEquals($command, $ret, '->setHelp() implements a fluent interface');
128 $this->assertEquals('help1', $command->getHelp(), '->setHelp() sets the help');
131 public function testGetProcessedHelp()
133 $command = new \TestCommand();
134 $command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
135 $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly');
136 $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%');
139 public function testGetSetAliases()
141 $command = new \TestCommand();
142 $this->assertEquals(array('name'), $command->getAliases(), '->getAliases() returns the aliases');
143 $ret = $command->setAliases(array('name1'));
144 $this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
145 $this->assertEquals(array('name1'), $command->getAliases(), '->setAliases() sets the aliases');
148 public function testGetSynopsis()
150 $command = new \TestCommand();
151 $command->addOption('foo');
152 $command->addArgument('foo');
153 $this->assertEquals('namespace:name [--foo] [foo]', $command->getSynopsis(), '->getSynopsis() returns the synopsis');
156 public function testGetHelper()
158 $application = new Application();
159 $command = new \TestCommand();
160 $command->setApplication($application);
161 $formatterHelper = new FormatterHelper();
162 $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
165 public function testGet()
167 $application = new Application();
168 $command = new \TestCommand();
169 $command->setApplication($application);
170 $formatterHelper = new FormatterHelper();
171 $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->__get() returns the correct helper');
174 public function testMergeApplicationDefinition()
176 $application1 = new Application();
177 $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
178 $application1->getDefinition()->addOptions(array(new InputOption('bar')));
179 $command = new \TestCommand();
180 $command->setApplication($application1);
181 $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
183 $r = new \ReflectionObject($command);
184 $m = $r->getMethod('mergeApplicationDefinition');
185 $m->setAccessible(true);
186 $m->invoke($command);
187 $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
188 $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments');
189 $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options');
190 $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options');
192 $m->invoke($command);
193 $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
196 public function testRun()
198 $command = new \TestCommand();
199 $tester = new CommandTester($command);
201 $tester->execute(array('--bar' => true));
202 $this->fail('->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
203 } catch (\Exception $e) {
204 $this->assertInstanceOf('\InvalidArgumentException', $e, '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
205 $this->assertEquals('The "--bar" option does not exist.', $e->getMessage(), '->run() throws a \InvalidArgumentException when the input does not validate the current InputDefinition');
208 $tester->execute(array(), array('interactive' => true));
209 $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
211 $tester->execute(array(), array('interactive' => false));
212 $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
214 $command = new Command('foo');
216 $command->run(new StringInput(''), new NullOutput());
217 $this->fail('->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
218 } catch (\Exception $e) {
219 $this->assertInstanceOf('\LogicException', $e, '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
220 $this->assertEquals('You must override the execute() method in the concrete command class.', $e->getMessage(), '->run() throws a \LogicException if the execute() method has not been overridden and no code has been provided');
224 public function testSetCode()
226 $command = new \TestCommand();
227 $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
228 $output->writeln('from the code...');
230 $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
231 $tester = new CommandTester($command);
232 $tester->execute(array());
233 $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
236 public function testAsText()
238 $command = new \TestCommand();
239 $command->setApplication(new Application());
240 $tester = new CommandTester($command);
241 $tester->execute(array('command' => $command->getName()));
242 $this->assertStringEqualsFile(self::$fixturesPath.'/command_astext.txt', $command->asText(), '->asText() returns a text representation of the command');
245 public function testAsXml()
247 $command = new \TestCommand();
248 $command->setApplication(new Application());
249 $tester = new CommandTester($command);
250 $tester->execute(array('command' => $command->getName()));
251 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/command_asxml.txt', $command->asXml(), '->asXml() returns an XML representation of the command');