| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?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\DependencyInjection\Tests\Compiler;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\DependencyInjection\Reference;
- use Symfony\Component\DependencyInjection\Compiler\ResolveInvalidReferencesPass;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- class ResolveInvalidReferencesPassTest extends TestCase
- {
- public function testProcess()
- {
- $container = new ContainerBuilder();
- $def = $container
- ->register('foo')
- ->setArguments(array(
- new Reference('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE),
- new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
- ))
- ->addMethodCall('foo', array(new Reference('moo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
- ;
- $this->process($container);
- $arguments = $def->getArguments();
- $this->assertSame(array(null, null), $arguments);
- $this->assertCount(0, $def->getMethodCalls());
- }
- public function testProcessIgnoreInvalidArgumentInCollectionArgument()
- {
- $container = new ContainerBuilder();
- $container->register('baz');
- $def = $container
- ->register('foo')
- ->setArguments(array(
- array(
- new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
- $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
- new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
- ),
- ))
- ;
- $this->process($container);
- $arguments = $def->getArguments();
- $this->assertSame(array($baz, null), $arguments[0]);
- }
- public function testProcessKeepMethodCallOnInvalidArgumentInCollectionArgument()
- {
- $container = new ContainerBuilder();
- $container->register('baz');
- $def = $container
- ->register('foo')
- ->addMethodCall('foo', array(
- array(
- new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
- $baz = new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
- new Reference('moo', ContainerInterface::NULL_ON_INVALID_REFERENCE),
- ),
- ))
- ;
- $this->process($container);
- $calls = $def->getMethodCalls();
- $this->assertCount(1, $def->getMethodCalls());
- $this->assertSame(array($baz, null), $calls[0][1][0]);
- }
- public function testProcessIgnoreNonExistentServices()
- {
- $container = new ContainerBuilder();
- $def = $container
- ->register('foo')
- ->setArguments(array(new Reference('bar')))
- ;
- $this->process($container);
- $arguments = $def->getArguments();
- $this->assertEquals('bar', (string) $arguments[0]);
- }
- public function testProcessRemovesPropertiesOnInvalid()
- {
- $container = new ContainerBuilder();
- $def = $container
- ->register('foo')
- ->setProperty('foo', new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
- ;
- $this->process($container);
- $this->assertEquals(array(), $def->getProperties());
- }
- public function testProcessRemovesArgumentsOnInvalid()
- {
- $container = new ContainerBuilder();
- $def = $container
- ->register('foo')
- ->addArgument(array(
- array(
- new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
- new ServiceClosureArgument(new Reference('baz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
- ),
- ))
- ;
- $this->process($container);
- $this->assertSame(array(array(array())), $def->getArguments());
- }
- protected function process(ContainerBuilder $container)
- {
- $pass = new ResolveInvalidReferencesPass();
- $pass->process($container);
- }
- }
|