123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
- use Symfony\Component\DependencyInjection\ContainerAwareInterface;
- use Symfony\Component\DependencyInjection\ContainerAwareTrait;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\DependencyInjection\ServiceLocator;
- use Symfony\Component\DependencyInjection\TypedReference;
- use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
- class RegisterControllerArgumentLocatorsPassTest extends TestCase
- {
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Class "Symfony\Component\HttpKernel\Tests\DependencyInjection\NotFound" used for service "foo" cannot be found.
- */
- public function testInvalidClass()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', NotFound::class)
- ->addTag('controller.service_arguments')
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Missing "action" attribute on tag "controller.service_arguments" {"argument":"bar"} for service "foo".
- */
- public function testNoAction()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('argument' => 'bar'))
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Missing "argument" attribute on tag "controller.service_arguments" {"action":"fooAction"} for service "foo".
- */
- public function testNoArgument()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('action' => 'fooAction'))
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Missing "id" attribute on tag "controller.service_arguments" {"action":"fooAction","argument":"bar"} for service "foo".
- */
- public function testNoService()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('action' => 'fooAction', 'argument' => 'bar'))
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Invalid "action" attribute on tag "controller.service_arguments" for service "foo": no public "barAction()" method found on class "Symfony\Component\HttpKernel\Tests\DependencyInjection\RegisterTestController".
- */
- public function testInvalidMethod()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('action' => 'barAction', 'argument' => 'bar', 'id' => 'bar_service'))
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Invalid "controller.service_arguments" tag for service "foo": method "fooAction()" has no "baz" argument on class "Symfony\Component\HttpKernel\Tests\DependencyInjection\RegisterTestController".
- */
- public function testInvalidArgument()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('action' => 'fooAction', 'argument' => 'baz', 'id' => 'bar'))
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- public function testAllActions()
- {
- $container = new ContainerBuilder();
- $resolver = $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments')
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
- $this->assertEquals(array('foo:fooAction'), array_keys($locator));
- $this->assertInstanceof(ServiceClosureArgument::class, $locator['foo:fooAction']);
- $locator = $container->getDefinition((string) $locator['foo:fooAction']->getValues()[0]);
- $this->assertSame(ServiceLocator::class, $locator->getClass());
- $this->assertFalse($locator->isPublic());
- $expected = array('bar' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, RegisterTestController::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
- $this->assertEquals($expected, $locator->getArgument(0));
- }
- public function testExplicitArgument()
- {
- $container = new ContainerBuilder();
- $resolver = $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('action' => 'fooAction', 'argument' => 'bar', 'id' => 'bar'))
- ->addTag('controller.service_arguments', array('action' => 'fooAction', 'argument' => 'bar', 'id' => 'baz')) // should be ignored, the first wins
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
- $locator = $container->getDefinition((string) $locator['foo:fooAction']->getValues()[0]);
- $expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, RegisterTestController::class)));
- $this->assertEquals($expected, $locator->getArgument(0));
- }
- public function testOptionalArgument()
- {
- $container = new ContainerBuilder();
- $resolver = $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', RegisterTestController::class)
- ->addTag('controller.service_arguments', array('action' => 'fooAction', 'argument' => 'bar', 'id' => '?bar'))
- ;
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
- $locator = $container->getDefinition((string) $locator['foo:fooAction']->getValues()[0]);
- $expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, RegisterTestController::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
- $this->assertEquals($expected, $locator->getArgument(0));
- }
- public function testSkipSetContainer()
- {
- $container = new ContainerBuilder();
- $resolver = $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', ContainerAwareRegisterTestController::class)
- ->addTag('controller.service_arguments');
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
- $this->assertSame(array('foo:fooAction'), array_keys($locator));
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Cannot determine controller argument for "Symfony\Component\HttpKernel\Tests\DependencyInjection\NonExistentClassController::fooAction()": the $nonExistent argument is type-hinted with the non-existent class or interface: "Symfony\Component\HttpKernel\Tests\DependencyInjection\NonExistentClass". Did you forget to add a use statement?
- */
- public function testExceptionOnNonExistentTypeHint()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', NonExistentClassController::class)
- ->addTag('controller.service_arguments');
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- /**
- * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
- * @expectedExceptionMessage Cannot determine controller argument for "Symfony\Component\HttpKernel\Tests\DependencyInjection\NonExistentClassDifferentNamespaceController::fooAction()": the $nonExistent argument is type-hinted with the non-existent class or interface: "Acme\NonExistentClass".
- */
- public function testExceptionOnNonExistentTypeHintDifferentNamespace()
- {
- $container = new ContainerBuilder();
- $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', NonExistentClassDifferentNamespaceController::class)
- ->addTag('controller.service_arguments');
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- }
- public function testNoExceptionOnNonExistentTypeHintOptionalArg()
- {
- $container = new ContainerBuilder();
- $resolver = $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', NonExistentClassOptionalController::class)
- ->addTag('controller.service_arguments');
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
- $this->assertSame(array('foo:barAction', 'foo:fooAction'), array_keys($locator));
- }
- public function testArgumentWithNoTypeHintIsOk()
- {
- $container = new ContainerBuilder();
- $resolver = $container->register('argument_resolver.service')->addArgument(array());
- $container->register('foo', ArgumentWithoutTypeController::class)
- ->addTag('controller.service_arguments');
- $pass = new RegisterControllerArgumentLocatorsPass();
- $pass->process($container);
- $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
- $this->assertEmpty(array_keys($locator));
- }
- }
- class RegisterTestController
- {
- public function __construct(ControllerDummy $bar)
- {
- }
- public function fooAction(ControllerDummy $bar)
- {
- }
- protected function barAction(ControllerDummy $bar)
- {
- }
- }
- class ContainerAwareRegisterTestController implements ContainerAwareInterface
- {
- use ContainerAwareTrait;
- public function fooAction(ControllerDummy $bar)
- {
- }
- }
- class ControllerDummy
- {
- }
- class NonExistentClassController
- {
- public function fooAction(NonExistentClass $nonExistent)
- {
- }
- }
- class NonExistentClassDifferentNamespaceController
- {
- public function fooAction(\Acme\NonExistentClass $nonExistent)
- {
- }
- }
- class NonExistentClassOptionalController
- {
- public function fooAction(NonExistentClass $nonExistent = null)
- {
- }
- public function barAction(NonExistentClass $nonExistent = null, $bar)
- {
- }
- }
- class ArgumentWithoutTypeController
- {
- public function fooAction($someArg)
- {
- }
- }
|