4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Scope;
16 use Symfony\Component\DependencyInjection\Compiler\CheckReferenceValidityPass;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 use Symfony\Component\DependencyInjection\Reference;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 class CheckReferenceValidityPassTest extends TestCase
26 public function testProcessIgnoresScopeWideningIfNonStrictReference()
28 $container = new ContainerBuilder();
29 $container->register('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
30 $container->register('b')->setScope('prototype');
32 $this->process($container);
34 $this->addToAssertionCount(1);
38 * @expectedException \RuntimeException
41 public function testProcessDetectsScopeWidening()
43 $container = new ContainerBuilder();
44 $container->register('a')->addArgument(new Reference('b'));
45 $container->register('b')->setScope('prototype');
47 $this->process($container);
49 $this->addToAssertionCount(1);
55 public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
57 $container = new ContainerBuilder();
58 $container->addScope(new Scope('a'));
59 $container->addScope(new Scope('b'));
61 $container->register('a')->setScope('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
62 $container->register('b')->setScope('b');
64 $this->process($container);
66 $this->addToAssertionCount(1);
70 * @expectedException \RuntimeException
73 public function testProcessDetectsCrossScopeHierarchyReference()
75 $container = new ContainerBuilder();
76 $container->addScope(new Scope('a'));
77 $container->addScope(new Scope('b'));
79 $container->register('a')->setScope('a')->addArgument(new Reference('b'));
80 $container->register('b')->setScope('b');
82 $this->process($container);
86 * @expectedException \RuntimeException
88 public function testProcessDetectsReferenceToAbstractDefinition()
90 $container = new ContainerBuilder();
92 $container->register('a')->setAbstract(true);
93 $container->register('b')->addArgument(new Reference('a'));
95 $this->process($container);
98 public function testProcess()
100 $container = new ContainerBuilder();
101 $container->register('a')->addArgument(new Reference('b'));
102 $container->register('b');
104 $this->process($container);
106 $this->addToAssertionCount(1);
109 protected function process(ContainerBuilder $container)
111 $pass = new CheckReferenceValidityPass();
112 $pass->process($container);