Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55e1075

Browse files
committed
Add unit tests for ServiceLocatorTagPass
1 parent 336524d commit 55e1075

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\DependencyInjection\Tests\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\ServiceLocator;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
20+
21+
require_once __DIR__.'/../Fixtures/includes/classes.php';
22+
23+
class ServiceLocatorTagPassTest extends TestCase
24+
{
25+
/**
26+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
27+
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set.
28+
*/
29+
public function testNoServices()
30+
{
31+
$container = new ContainerBuilder();
32+
33+
$container->register('foo', ServiceLocator::class)
34+
->addTag('container.service_locator')
35+
;
36+
37+
(new ServiceLocatorTagPass())->process($container);
38+
}
39+
40+
/**
41+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
42+
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set, "string" found for key "0".
43+
*/
44+
public function testInvalidServices()
45+
{
46+
$container = new ContainerBuilder();
47+
48+
$container->register('foo', ServiceLocator::class)
49+
->setArguments(array(array(
50+
'dummy',
51+
)))
52+
->addTag('container.service_locator')
53+
;
54+
55+
(new ServiceLocatorTagPass())->process($container);
56+
}
57+
58+
public function testProcessValue()
59+
{
60+
$container = new ContainerBuilder();
61+
62+
$container->register('bar', CustomDefinition::class);
63+
$container->register('baz', CustomDefinition::class);
64+
65+
$container->register('foo', ServiceLocator::class)
66+
->setArguments(array(array(
67+
new Reference('bar'),
68+
new Reference('baz'),
69+
'some.service' => new Reference('bar'),
70+
)))
71+
->addTag('container.service_locator')
72+
;
73+
74+
(new ServiceLocatorTagPass())->process($container);
75+
76+
/** @var ServiceLocator $locator */
77+
$locator = $container->get('foo');
78+
79+
$this->assertEquals(CustomDefinition::class, \get_class($locator('bar')));
80+
$this->assertEquals(CustomDefinition::class, \get_class($locator('baz')));
81+
$this->assertEquals(CustomDefinition::class, \get_class($locator('some.service')));
82+
}
83+
}

0 commit comments

Comments
 (0)