|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Definition; |
| 18 | +use Symfony\Component\HttpKernel\Bundle\Bundle; |
| 19 | + |
| 20 | +class AddConsoleCommandPassTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @dataProvider visibilityProvider |
| 24 | + */ |
| 25 | + public function testProcess($public) |
| 26 | + { |
| 27 | + $container = new ContainerBuilder(); |
| 28 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 29 | + $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); |
| 30 | + |
| 31 | + $definition = new Definition('%my-command.class%'); |
| 32 | + $definition->setPublic($public); |
| 33 | + $definition->addTag('console.command'); |
| 34 | + $container->setDefinition('my-command', $definition); |
| 35 | + |
| 36 | + $container->compile(); |
| 37 | + |
| 38 | + $id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand'; |
| 39 | + $this->assertTrue($container->hasParameter('console.command.ids')); |
| 40 | + $this->assertSame(array($id), $container->getParameter('console.command.ids')); |
| 41 | + } |
| 42 | + |
| 43 | + public function visibilityProvider() |
| 44 | + { |
| 45 | + return array( |
| 46 | + array(true), |
| 47 | + array(false), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @expectedException \InvalidArgumentException |
| 53 | + * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract. |
| 54 | + */ |
| 55 | + public function testProcessThrowAnExceptionIfTheServiceIsAbstract() |
| 56 | + { |
| 57 | + $container = new ContainerBuilder(); |
| 58 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 59 | + |
| 60 | + $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); |
| 61 | + $definition->addTag('console.command'); |
| 62 | + $definition->setAbstract(true); |
| 63 | + $container->setDefinition('my-command', $definition); |
| 64 | + |
| 65 | + $container->compile(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @expectedException \InvalidArgumentException |
| 70 | + * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command". |
| 71 | + */ |
| 72 | + public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand() |
| 73 | + { |
| 74 | + $container = new ContainerBuilder(); |
| 75 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 76 | + |
| 77 | + $definition = new Definition('SplObjectStorage'); |
| 78 | + $definition->addTag('console.command'); |
| 79 | + $container->setDefinition('my-command', $definition); |
| 80 | + |
| 81 | + $container->compile(); |
| 82 | + } |
| 83 | + |
| 84 | + public function testHttpKernelRegisterCommandsIngoreCommandAsAService() |
| 85 | + { |
| 86 | + $container = new ContainerBuilder(); |
| 87 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 88 | + $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); |
| 89 | + $definition->addTag('console.command'); |
| 90 | + $container->setDefinition('my-command', $definition); |
| 91 | + $container->compile(); |
| 92 | + |
| 93 | + $application = $this->getMock('Symfony\Component\Console\Application'); |
| 94 | + // Never called, because it's the |
| 95 | + // Symfony\Bundle\FrameworkBundle\Console\Application that register |
| 96 | + // commands as a service |
| 97 | + $application->expects($this->never())->method('add'); |
| 98 | + |
| 99 | + $bundle = new ExtensionPresentBundle(); |
| 100 | + |
| 101 | + $bundle->setContainer($container); |
| 102 | + $bundle->registerCommands($application); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +class MyCommand extends Command |
| 107 | +{ |
| 108 | +} |
| 109 | + |
| 110 | +class ExtensionPresentBundle extends Bundle |
| 111 | +{ |
| 112 | +} |
0 commit comments