TYPO3  7.6
console/Tests/Command/CommandTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Tests\Command;
13 
25 
26 class CommandTest extends \PHPUnit_Framework_TestCase
27 {
28  protected static $fixturesPath;
29 
30  public static function setUpBeforeClass()
31  {
32  self::$fixturesPath = __DIR__.'/../Fixtures/';
33  require_once self::$fixturesPath.'/TestCommand.php';
34  }
35 
36  public function testConstructor()
37  {
38  $command = new Command('foo:bar');
39  $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument');
40  }
41 
46  public function testCommandNameCannotBeEmpty()
47  {
48  new Command();
49  }
50 
51  public function testSetApplication()
52  {
53  $application = new Application();
54  $command = new \TestCommand();
55  $command->setApplication($application);
56  $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application');
57  }
58 
59  public function testSetGetDefinition()
60  {
61  $command = new \TestCommand();
62  $ret = $command->setDefinition($definition = new InputDefinition());
63  $this->assertEquals($command, $ret, '->setDefinition() implements a fluent interface');
64  $this->assertEquals($definition, $command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
65  $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar')));
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');
68  $command->setDefinition(new InputDefinition());
69  }
70 
71  public function testAddArgument()
72  {
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');
77  }
78 
79  public function testAddOption()
80  {
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');
85  }
86 
88  {
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');
93 
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');
97  }
98 
102  public function testInvalidCommandNames($name)
103  {
104  $this->setExpectedException('InvalidArgumentException', sprintf('Command name "%s" is invalid.', $name));
105 
106  $command = new \TestCommand();
107  $command->setName($name);
108  }
109 
110  public function provideInvalidCommandNames()
111  {
112  return array(
113  array(''),
114  array('foo:'),
115  );
116  }
117 
118  public function testGetSetDescription()
119  {
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');
125  }
126 
127  public function testGetSetHelp()
128  {
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');
136  }
137 
138  public function testGetProcessedHelp()
139  {
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%');
144  }
145 
146  public function testGetSetAliases()
147  {
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');
153  }
154 
155  public function testGetSynopsis()
156  {
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');
161  }
162 
163  public function testGetHelper()
164  {
165  $application = new Application();
166  $command = new \TestCommand();
167  $command->setApplication($application);
168  $formatterHelper = new FormatterHelper();
169  $this->assertEquals($formatterHelper->getName(), $command->getHelper('formatter')->getName(), '->getHelper() returns the correct helper');
170  }
171 
173  {
174  $application1 = new Application();
175  $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
176  $application1->getDefinition()->addOptions(array(new InputOption('bar')));
177  $command = new \TestCommand();
178  $command->setApplication($application1);
179  $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo'))));
180 
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');
189 
190  $m->invoke($command);
191  $this->assertEquals(3, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments and options');
192  }
193 
195  {
196  $application1 = new Application();
197  $application1->getDefinition()->addArguments(array(new InputArgument('foo')));
198  $application1->getDefinition()->addOptions(array(new InputOption('bar')));
199  $command = new \TestCommand();
200  $command->setApplication($application1);
201  $command->setDefinition($definition = new InputDefinition(array()));
202 
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');
209 
210  $m->invoke($command, true);
211  $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition(true) merges the application arguments and the command arguments');
212 
213  $m->invoke($command);
214  $this->assertEquals(2, $command->getDefinition()->getArgumentCount(), '->mergeApplicationDefinition() does not try to merge twice the application arguments');
215  }
216 
217  public function testRunInteractive()
218  {
219  $tester = new CommandTester(new \TestCommand());
220 
221  $tester->execute(array(), array('interactive' => true));
222 
223  $this->assertEquals('interact called'.PHP_EOL.'execute called'.PHP_EOL, $tester->getDisplay(), '->run() calls the interact() method if the input is interactive');
224  }
225 
226  public function testRunNonInteractive()
227  {
228  $tester = new CommandTester(new \TestCommand());
229 
230  $tester->execute(array(), array('interactive' => false));
231 
232  $this->assertEquals('execute called'.PHP_EOL, $tester->getDisplay(), '->run() does not call the interact() method if the input is not interactive');
233  }
234 
240  {
241  $command = new Command('foo');
242  $command->run(new StringInput(''), new NullOutput());
243  }
244 
249  public function testRunWithInvalidOption()
250  {
251  $command = new \TestCommand();
252  $tester = new CommandTester($command);
253  $tester->execute(array('--bar' => true));
254  }
255 
257  {
258  $command = new \TestCommand();
259  $exitCode = $command->run(new StringInput(''), new NullOutput());
260  $this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');
261 
262  $command = $this->getMock('TestCommand', array('execute'));
263  $command->expects($this->once())
264  ->method('execute')
265  ->will($this->returnValue('2.3'));
266  $exitCode = $command->run(new StringInput(''), new NullOutput());
267  $this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
268  }
269 
270  public function testRunReturnsAlwaysInteger()
271  {
272  $command = new \TestCommand();
273 
274  $this->assertSame(0, $command->run(new StringInput(''), new NullOutput()));
275  }
276 
277  public function testSetCode()
278  {
279  $command = new \TestCommand();
280  $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) {
281  $output->writeln('from the code...');
282  });
283  $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
284  $tester = new CommandTester($command);
285  $tester->execute(array());
286  $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
287  }
288 
290  {
291  $command = new \TestCommand();
292  $ret = $command->setCode(array($this, 'callableMethodCommand'));
293  $this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
294  $tester = new CommandTester($command);
295  $tester->execute(array());
296  $this->assertEquals('interact called'.PHP_EOL.'from the code...'.PHP_EOL, $tester->getDisplay());
297  }
298 
303  public function testSetCodeWithNonCallable()
304  {
305  $command = new \TestCommand();
306  $command->setCode(array($this, 'nonExistentMethod'));
307  }
308 
309  public function callableMethodCommand(InputInterface $input, OutputInterface $output)
310  {
311  $output->writeln('from the code...');
312  }
313 
317  public function testLegacyAsText()
318  {
319  $command = new \TestCommand();
320  $command->setApplication(new Application());
321  $tester = new CommandTester($command);
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');
324  }
325 
329  public function testLegacyAsXml()
330  {
331  $command = new \TestCommand();
332  $command->setApplication(new Application());
333  $tester = new CommandTester($command);
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');
336  }
337 }