1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\ConfigBundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\ConfigBundle\Entity\ConfigurationInterface; |
6
|
|
|
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; |
7
|
|
|
use Kunstmaan\ConfigBundle\DependencyInjection\Compiler\KunstmaanConfigConfigurationPass; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
|
12
|
|
|
class FakeDoctrine |
13
|
|
|
{ |
14
|
|
|
public function getManagerForClass() |
15
|
|
|
{ |
16
|
|
|
return true; |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class FakeDoctrineFalse |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @throws \ReflectionException |
24
|
|
|
*/ |
25
|
|
|
public function getManagerForClass() |
26
|
|
|
{ |
27
|
|
|
throw new ReflectionException(); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
class RandomEntity implements ConfigurationInterface |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getDefaultAdminType() |
37
|
|
|
{ |
38
|
|
|
return 'whatever'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getInternalName() |
45
|
|
|
{ |
46
|
|
|
return 'whatever'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getLabel() |
53
|
|
|
{ |
54
|
|
|
return 'whatever'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getRoles() |
61
|
|
|
{ |
62
|
|
|
return []; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
class RandomNonConfigEntity |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
class KunstmaanConfigConfigurationPassCompilerPassTest extends AbstractCompilerPassTestCase |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
protected function registerCompilerPass(ContainerBuilder $container) |
73
|
|
|
{ |
74
|
|
|
$container->addCompilerPass(new KunstmaanConfigConfigurationPass()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testContainerKeys() |
78
|
|
|
{ |
79
|
|
|
$doctrine = new FakeDoctrine(); |
80
|
|
|
|
81
|
|
|
$svcId = 'kunstmaan_config'; |
82
|
|
|
|
83
|
|
|
$this->container->setParameter($svcId, [ |
84
|
|
|
'entities' => [ |
85
|
|
|
'Test\Kunstmaan\ConfigBundle\DependencyInjection\Compiler\RandomEntity' |
86
|
|
|
], |
87
|
|
|
]); |
88
|
|
|
$this->container->setDefinition('doctrine', new Definition($doctrine)); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$svc = new Definition(); |
91
|
|
|
$svc->addTag('doctrine'); |
92
|
|
|
$this->setDefinition($svcId, $svc); |
93
|
|
|
|
94
|
|
|
$this->compile(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testExceptionIsThrownWhenNoEntitiesDefined() |
98
|
|
|
{ |
99
|
|
|
// No longer fails on compile, just returns an empty array, but proof will show in code coverage |
100
|
|
|
$svcId = 'kunstmaan_config'; |
101
|
|
|
|
102
|
|
|
$this->container->setParameter($svcId, []); |
103
|
|
|
$this->compile(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testExceptionIsThrownWhenNoEntitiesFoundByDoctrine() |
107
|
|
|
{ |
108
|
|
|
$doctrine = new FakeDoctrineFalse(); |
109
|
|
|
|
110
|
|
|
$svcId = 'kunstmaan_config'; |
111
|
|
|
|
112
|
|
|
$this->container->setParameter($svcId, [ |
113
|
|
|
'entities' => [ |
114
|
|
|
'BrokenEntity' |
115
|
|
|
], |
116
|
|
|
]); |
117
|
|
|
$this->container->setDefinition('doctrine', new Definition($doctrine)); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$svc = new Definition(); |
120
|
|
|
$svc->addTag('doctrine'); |
121
|
|
|
$this->setDefinition($svcId, $svc); |
122
|
|
|
|
123
|
|
|
$this->compile(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testExceptionIsThrownWhenEntityNotAConfigInterface() |
127
|
|
|
{ |
128
|
|
|
$doctrine = new FakeDoctrine(); |
129
|
|
|
|
130
|
|
|
$svcId = 'kunstmaan_config'; |
131
|
|
|
|
132
|
|
|
$this->container->setParameter($svcId, [ |
133
|
|
|
'entities' => [ |
134
|
|
|
'Test\Kunstmaan\ConfigBundle\DependencyInjection\Compiler\RandomNonConfigEntity' |
135
|
|
|
], |
136
|
|
|
]); |
137
|
|
|
$this->container->setDefinition('doctrine', new Definition($doctrine)); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$svc = new Definition(); |
140
|
|
|
$svc->addTag('doctrine'); |
141
|
|
|
$this->setDefinition($svcId, $svc); |
142
|
|
|
|
143
|
|
|
$this->compile(); |
144
|
|
|
} |
145
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.