Skip to content

Commit d2acc15

Browse files
committed
[Console] Added tests for Command and Application
1 parent a9b2a20 commit d2acc15

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests;
1313

1414
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Command\CommandConfiguration;
1516
use Symfony\Component\Console\Helper\HelperSet;
1617
use Symfony\Component\Console\Helper\FormatterHelper;
1718
use Symfony\Component\Console\Input\ArgvInput;
@@ -1019,6 +1020,27 @@ public function testCanCheckIfTerminalIsInteractive()
10191020
$inputStream = $application->getHelperSet()->get('question')->getInputStream();
10201021
$this->assertEquals($tester->getInput()->isInteractive(), @posix_isatty($inputStream));
10211022
}
1023+
1024+
public function testAddingSeparateConfiguration()
1025+
{
1026+
$resolver = $this->getMockForAbstractClass('Symfony\Component\Console\Command\Resolver\CommandResolverInterface');
1027+
1028+
$application = new Application();
1029+
$application->setCommandResolver($resolver);
1030+
$application->addCommandConfiguration(CommandConfiguration::create('foo:bar'));
1031+
1032+
$this->assertTrue($application->has('foo:bar'));
1033+
}
1034+
1035+
/**
1036+
* @expectedException \RuntimeException
1037+
* @expectedExceptionMessage You have to specify command resolver in order to register separate command configuration
1038+
*/
1039+
public function testAddingSeparateConfigurationWithoutResolver()
1040+
{
1041+
$application = new Application();
1042+
$application->addCommandConfiguration(CommandConfiguration::create('foo:bar'));
1043+
}
10221044
}
10231045

10241046
class CustomApplication extends Application

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static function setUpBeforeClass()
3232
{
3333
self::$fixturesPath = __DIR__.'/../Fixtures/';
3434
require_once self::$fixturesPath.'/TestCommand.php';
35+
require_once self::$fixturesPath.'/TestCommandWithSeparateConfiguration.php';
3536
}
3637

3738
public function testConstructor()
@@ -336,4 +337,96 @@ public function testInconsistentDefinition()
336337
{
337338
new Command('foo', new CommandConfiguration('bar'));
338339
}
340+
341+
public function testThatCommandIsNotLoaded()
342+
{
343+
$resolver = $this->getMockForAbstractClass('Symfony\Component\Console\Command\Resolver\CommandResolverInterface');
344+
345+
$resolver
346+
->expects($this->never())
347+
->method('resolve')
348+
;
349+
350+
$command = Command::registerLazyLoaded(CommandConfiguration::create('foo:bar'), $resolver);
351+
352+
$this->assertSame('foo:bar', $command->getName());
353+
}
354+
355+
public function testCommandWithSeparateConfiguration()
356+
{
357+
$configuration =
358+
CommandConfiguration::create('foo:baz')
359+
->withDescription('foo description')
360+
->withDefinition(
361+
new InputDefinition(array(
362+
new InputArgument('name')
363+
))
364+
);
365+
366+
$resolver = $this->getMockForAbstractClass('Symfony\Component\Console\Command\Resolver\CommandResolverInterface');
367+
368+
$resolver
369+
->expects($this->once())
370+
->method('resolve')
371+
->with('foo:baz')
372+
->will($this->returnCallback(function() {
373+
return new \TestCommandWithSeparateConfiguration();
374+
}))
375+
;
376+
377+
$command = Command::registerLazyLoaded($configuration, $resolver);
378+
$command->setApplication(new Application());
379+
380+
$tester = new CommandTester($command);
381+
$tester->execute(array('name' => 'Alice'));
382+
383+
$this->assertSame(
384+
'interact called'.PHP_EOL.'Hello, Alice'.PHP_EOL.'Command name: foo:baz'.PHP_EOL.'Description foo description'.PHP_EOL,
385+
$tester->getDisplay()
386+
);
387+
}
388+
389+
public function testThatResolverIsAllowedToReturnCallable()
390+
{
391+
$resolver = $this->getMockForAbstractClass('Symfony\Component\Console\Command\Resolver\CommandResolverInterface');
392+
393+
$resolver
394+
->expects($this->once())
395+
->method('resolve')
396+
->will($this->returnValue(function (InputInterface $input, OutputInterface $output) {
397+
$output->writeln('Hello, world');
398+
399+
return 42;
400+
}))
401+
;
402+
403+
$command = Command::registerLazyLoaded(CommandConfiguration::create('foo:bar'), $resolver);
404+
405+
$tester = new CommandTester($command);
406+
$tester->execute(array(), array());
407+
408+
$this->assertSame('Hello, world'.PHP_EOL, $tester->getDisplay());
409+
$this->assertSame(42, $tester->getStatusCode());
410+
}
411+
412+
/**
413+
* @expectedException \RuntimeException
414+
* @expectedExceptionMessage Command or callable was expected, string given
415+
*/
416+
public function testInvalidResolver()
417+
{
418+
$resolver = $this->getMockForAbstractClass('Symfony\Component\Console\Command\Resolver\CommandResolverInterface');
419+
420+
$resolver
421+
->expects($this->once())
422+
->method('resolve')
423+
->with('foo:bar')
424+
->will($this->returnValue('foo'))
425+
;
426+
427+
$command = Command::registerLazyLoaded(CommandConfiguration::create('foo:bar'), $resolver);
428+
429+
$tester = new CommandTester($command);
430+
$tester->execute(array());
431+
}
339432
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class TestCommandWithSeparateConfiguration extends Command
8+
{
9+
protected function execute(InputInterface $input, OutputInterface $output)
10+
{
11+
$output->writeln(sprintf('Hello, %s', $input->getArgument('name')));
12+
$output->writeln(sprintf('Command name: %s', $this->getName()));
13+
$output->writeln(sprintf('Description %s', $this->getDescription()));
14+
}
15+
16+
protected function interact(InputInterface $input, OutputInterface $output)
17+
{
18+
$output->writeln('interact called');
19+
}
20+
}

0 commit comments

Comments
 (0)