12 namespace Symfony\Component\Console\Tests\Command;
32 self::$fixturesPath = __DIR__.
'/../Fixtures/';
33 require_once self::$fixturesPath.
'/TestCommand.php';
38 $command =
new Command(
'foo:bar');
39 $this->assertEquals(
'foo:bar', $command->getName(),
'__construct() takes the command name as its first argument');
54 $command = new \TestCommand();
55 $command->setApplication($application);
56 $this->assertEquals($application, $command->getApplication(),
'->setApplication() sets the current application');
61 $command = new \TestCommand();
63 $this->assertEquals($command, $ret,
'->setDefinition() implements a fluent interface');
64 $this->assertEquals($definition, $command->getDefinition(),
'->setDefinition() sets the current InputDefinition instance');
66 $this->assertTrue($command->getDefinition()->hasArgument(
'foo'),
'->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
67 $this->assertTrue($command->getDefinition()->hasOption(
'bar'),
'->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
73 $command = new \TestCommand();
74 $ret = $command->addArgument(
'foo');
75 $this->assertEquals($command, $ret,
'->addArgument() implements a fluent interface');
76 $this->assertTrue($command->getDefinition()->hasArgument(
'foo'),
'->addArgument() adds an argument to the command');
81 $command = new \TestCommand();
82 $ret = $command->addOption(
'foo');
83 $this->assertEquals($command, $ret,
'->addOption() implements a fluent interface');
84 $this->assertTrue($command->getDefinition()->hasOption(
'foo'),
'->addOption() adds an option to the command');
89 $command = new \TestCommand();
90 $this->assertEquals(
'namespace:name', $command->getName(),
'->getName() returns the command name');
91 $command->setName(
'foo');
92 $this->assertEquals(
'foo', $command->getName(),
'->setName() sets the command name');
94 $ret = $command->setName(
'foobar:bar');
95 $this->assertEquals($command, $ret,
'->setName() implements a fluent interface');
96 $this->assertEquals(
'foobar:bar', $command->getName(),
'->setName() sets the command name');
104 $this->setExpectedException(
'InvalidArgumentException', sprintf(
'Command name "%s" is invalid.', $name));
106 $command = new \TestCommand();
107 $command->setName($name);
120 $command = new \TestCommand();
121 $this->assertEquals(
'description', $command->getDescription(),
'->getDescription() returns the description');
122 $ret = $command->setDescription(
'description1');
123 $this->assertEquals($command, $ret,
'->setDescription() implements a fluent interface');
124 $this->assertEquals(
'description1', $command->getDescription(),
'->setDescription() sets the description');
129 $command = new \TestCommand();
130 $this->assertEquals(
'help', $command->getHelp(),
'->getHelp() returns the help');
131 $ret = $command->setHelp(
'help1');
132 $this->assertEquals($command, $ret,
'->setHelp() implements a fluent interface');
133 $this->assertEquals(
'help1', $command->getHelp(),
'->setHelp() sets the help');
134 $command->setHelp(
'');
135 $this->assertEquals(
'description', $command->getHelp(),
'->getHelp() fallback to the description');
140 $command = new \TestCommand();
141 $command->setHelp(
'The %command.name% command does... Example: php %command.full_name%.');
142 $this->assertContains(
'The namespace:name command does...', $command->getProcessedHelp(),
'->getProcessedHelp() replaces %command.name% correctly');
143 $this->assertNotContains(
'%command.full_name%', $command->getProcessedHelp(),
'->getProcessedHelp() replaces %command.full_name%');
148 $command = new \TestCommand();
149 $this->assertEquals(array(
'name'), $command->getAliases(),
'->getAliases() returns the aliases');
150 $ret = $command->setAliases(array(
'name1'));
151 $this->assertEquals($command, $ret,
'->setAliases() implements a fluent interface');
152 $this->assertEquals(array(
'name1'), $command->getAliases(),
'->setAliases() sets the aliases');
157 $command = new \TestCommand();
158 $command->addOption(
'foo');
159 $command->addArgument(
'bar');
160 $this->assertEquals(
'namespace:name [--foo] [--] [<bar>]', $command->getSynopsis(),
'->getSynopsis() returns the synopsis');
166 $command = new \TestCommand();
167 $command->setApplication($application);
169 $this->assertEquals($formatterHelper->getName(), $command->getHelper(
'formatter')->getName(),
'->getHelper() returns the correct helper');
175 $application1->getDefinition()->addArguments(array(
new InputArgument(
'foo')));
176 $application1->getDefinition()->addOptions(array(
new InputOption(
'bar')));
177 $command = new \TestCommand();
178 $command->setApplication($application1);
181 $r = new \ReflectionObject($command);
182 $m = $r->getMethod(
'mergeApplicationDefinition');
183 $m->setAccessible(
true);
184 $m->invoke($command);
185 $this->assertTrue($command->getDefinition()->hasArgument(
'foo'),
'->mergeApplicationDefinition() merges the application arguments and the command arguments');
186 $this->assertTrue($command->getDefinition()->hasArgument(
'bar'),
'->mergeApplicationDefinition() merges the application arguments and the command arguments');
187 $this->assertTrue($command->getDefinition()->hasOption(
'foo'),
'->mergeApplicationDefinition() merges the application options and the command options');
188 $this->assertTrue($command->getDefinition()->hasOption(
'bar'),
'->mergeApplicationDefinition() merges the application options and the command options');
190 $m->invoke($command);
191 $this->assertEquals(3, $command->getDefinition()->getArgumentCount(),
'->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
197 $application1->getDefinition()->addArguments(array(
new InputArgument(
'foo')));
198 $application1->getDefinition()->addOptions(array(
new InputOption(
'bar')));
199 $command = new \TestCommand();
200 $command->setApplication($application1);
203 $r = new \ReflectionObject($command);
204 $m = $r->getMethod(
'mergeApplicationDefinition');
205 $m->setAccessible(
true);
206 $m->invoke($command,
false);
207 $this->assertTrue($command->getDefinition()->hasOption(
'bar'),
'->mergeApplicationDefinition(false) merges the application and the command options');
208 $this->assertFalse($command->getDefinition()->hasArgument(
'foo'),
'->mergeApplicationDefinition(false) does not merge the application arguments');
210 $m->invoke($command,
true);
211 $this->assertTrue($command->getDefinition()->hasArgument(
'foo'),
'->mergeApplicationDefinition(true) merges the application arguments and the command arguments');
213 $m->invoke($command);
214 $this->assertEquals(2, $command->getDefinition()->getArgumentCount(),
'->mergeApplicationDefinition() does not try to merge twice the application arguments');
221 $tester->execute(array(), array(
'interactive' =>
true));
223 $this->assertEquals(
'interact called'.PHP_EOL.
'execute called'.PHP_EOL, $tester->getDisplay(),
'->run() calls the interact() method if the input is interactive');
230 $tester->execute(array(), array(
'interactive' =>
false));
232 $this->assertEquals(
'execute called'.PHP_EOL, $tester->getDisplay(),
'->run() does not call the interact() method if the input is not interactive');
251 $command = new \TestCommand();
253 $tester->execute(array(
'--bar' =>
true));
258 $command = new \TestCommand();
260 $this->assertSame(0, $exitCode,
'->run() returns integer exit code (treats null as 0)');
262 $command = $this->getMock(
'TestCommand', array(
'execute'));
263 $command->expects($this->once())
265 ->will($this->returnValue(
'2.3'));
267 $this->assertSame(2, $exitCode,
'->run() returns integer exit code (casts numeric to int)');
272 $command = new \TestCommand();
279 $command = new \TestCommand();
281 $output->
writeln(
'from the code...');
283 $this->assertEquals($command, $ret,
'->setCode() implements a fluent interface');
285 $tester->execute(array());
286 $this->assertEquals(
'interact called'.PHP_EOL.
'from the code...'.PHP_EOL, $tester->getDisplay());
291 $command = new \TestCommand();
292 $ret = $command->setCode(array($this,
'callableMethodCommand'));
293 $this->assertEquals($command, $ret,
'->setCode() implements a fluent interface');
295 $tester->execute(array());
296 $this->assertEquals(
'interact called'.PHP_EOL.
'from the code...'.PHP_EOL, $tester->getDisplay());
305 $command = new \TestCommand();
306 $command->setCode(array($this,
'nonExistentMethod'));
311 $output->
writeln(
'from the code...');
319 $command = new \TestCommand();
322 $tester->execute(array(
'command' => $command->getName()));
323 $this->assertStringEqualsFile(self::$fixturesPath.
'/command_astext.txt', $command->asText(),
'->asText() returns a text representation of the command');
331 $command = new \TestCommand();
334 $tester->execute(array(
'command' => $command->getName()));
335 $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.
'/command_asxml.txt', $command->asXml(),
'->asXml() returns an XML representation of the command');