|
| 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\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->setResourceTracking(false); |
| 29 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 30 | + $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); |
| 31 | + |
| 32 | + $definition = new Definition('%my-command.class%'); |
| 33 | + $definition->setPublic($public); |
| 34 | + $definition->addTag('console.command'); |
| 35 | + $container->setDefinition('my-command', $definition); |
| 36 | + |
| 37 | + $container->compile(); |
| 38 | + |
| 39 | + $id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand'; |
| 40 | + $this->assertTrue($container->hasParameter('console.command.ids')); |
| 41 | + $this->assertSame(array($id), $container->getParameter('console.command.ids')); |
| 42 | + } |
| 43 | + |
| 44 | + public function visibilityProvider() |
| 45 | + { |
| 46 | + return array( |
| 47 | + array(true), |
| 48 | + array(false), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @expectedException \InvalidArgumentException |
| 54 | + * @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract. |
| 55 | + */ |
| 56 | + public function testProcessThrowAnExceptionIfTheServiceIsAbstract() |
| 57 | + { |
| 58 | + $container = new ContainerBuilder(); |
| 59 | + $container->setResourceTracking(false); |
| 60 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 61 | + |
| 62 | + $definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); |
| 63 | + $definition->addTag('console.command'); |
| 64 | + $definition->setAbstract(true); |
| 65 | + $container->setDefinition('my-command', $definition); |
| 66 | + |
| 67 | + $container->compile(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @expectedException \InvalidArgumentException |
| 72 | + * @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command". |
| 73 | + */ |
| 74 | + public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand() |
| 75 | + { |
| 76 | + $container = new ContainerBuilder(); |
| 77 | + $container->setResourceTracking(false); |
| 78 | + $container->addCompilerPass(new AddConsoleCommandPass()); |
| 79 | + |
| 80 | + $definition = new Definition('SplObjectStorage'); |
| 81 | + $definition->addTag('console.command'); |
| 82 | + $container->setDefinition('my-command', $definition); |
| 83 | + |
| 84 | + $container->compile(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class MyCommand extends Command |
| 89 | +{ |
| 90 | +} |
| 91 | + |
| 92 | +class ExtensionPresentBundle extends Bundle |
| 93 | +{ |
| 94 | +} |
0 commit comments