/
ResolveClassPassTest.php
97 lines (78 loc) · 3.14 KB
/
ResolveClassPassTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
class ResolveClassPassTest extends TestCase
{
/**
* @dataProvider provideValidClassId
*/
public function testResolveClassFromId($serviceId)
{
$container = new ContainerBuilder();
$def = $container->register($serviceId);
(new ResolveClassPass())->process($container);
$this->assertSame($serviceId, $def->getClass());
}
public function provideValidClassId()
{
yield array('Acme\UnknownClass');
yield array(CaseSensitiveClass::class);
}
/**
* @dataProvider provideInvalidClassId
*/
public function testWontResolveClassFromId($serviceId)
{
$container = new ContainerBuilder();
$def = $container->register($serviceId);
(new ResolveClassPass())->process($container);
$this->assertNull($def->getClass());
}
public function provideInvalidClassId()
{
yield array(\stdClass::class);
yield array('bar');
yield array('\DateTime');
}
public function testNonFqcnChildDefinition()
{
$container = new ContainerBuilder();
$parent = $container->register('App\Foo', null);
$child = $container->setDefinition('App\Foo.child', new ChildDefinition('App\Foo'));
(new ResolveClassPass())->process($container);
$this->assertSame('App\Foo', $parent->getClass());
$this->assertNull($child->getClass());
}
public function testClassFoundChildDefinition()
{
$container = new ContainerBuilder();
$parent = $container->register('App\Foo', null);
$child = $container->setDefinition(self::class, new ChildDefinition('App\Foo'));
(new ResolveClassPass())->process($container);
$this->assertSame('App\Foo', $parent->getClass());
$this->assertSame(self::class, $child->getClass());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Service definition "App\Foo\Child" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.
*/
public function testAmbiguousChildDefinition()
{
$container = new ContainerBuilder();
$parent = $container->register('App\Foo', null);
$child = $container->setDefinition('App\Foo\Child', new ChildDefinition('App\Foo'));
(new ResolveClassPass())->process($container);
}
}