1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony-Util package. |
5
|
|
|
* |
6
|
|
|
* (c) Jean-Bernard Addor |
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
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Bridge\Twig\TwigEngine; |
14
|
|
|
// use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; // Do not know how to configure this. |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver; |
21
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver; |
22
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver; |
23
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver; |
24
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver; |
25
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver; |
26
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver; |
27
|
|
|
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver; |
28
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolver; |
29
|
|
|
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory; |
30
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass; |
31
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass; |
32
|
|
|
use Symfony\Component\HttpKernel\EventListener\ResponseListener; |
33
|
|
|
use Symfony\Component\HttpKernel\EventListener\RouterListener; |
34
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
35
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; // != Symfony\Bundle\FrameworkBundle\Routing\Router |
36
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; |
37
|
|
|
use Symfony\Component\Routing\RequestContext; |
38
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
39
|
|
|
use Symfony\Component\Templating\EngineInterface; |
40
|
|
|
use Symfony\Component\Templating\TemplateNameParser; |
41
|
|
|
use Symfony\Component\Templating\TemplateNameParserInterface; |
42
|
|
|
use SymfonyUtil\Controller\EngineAsArgumentController; |
43
|
|
|
use Tests\Component\AppKernel; |
|
|
|
|
44
|
|
|
use Tests\Component\EngineAsArgumentFrameworkController; |
45
|
|
|
|
46
|
|
|
final class EngineAsArgumentInKernelControllerTest extends TestCase |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
public function testCanBeCreated() |
49
|
|
|
{ |
50
|
|
|
$this->assertInstanceOf( |
51
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
52
|
|
|
'Symfony\Component\HttpKernel\Kernel', |
53
|
|
|
new AppKernel('dev', true) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testKernelInterface() |
58
|
|
|
{ |
59
|
|
|
$this->assertInstanceOf( |
60
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
61
|
|
|
'Symfony\Component\HttpKernel\KernelInterface', |
62
|
|
|
new AppKernel('dev', true) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function FrameworkReturnsResponse() // failling: no more a test |
67
|
|
|
{ |
68
|
|
|
$this->assertInstanceOf( |
69
|
|
|
// Response::class, // 5.4 < php |
70
|
|
|
'Symfony\Component\HttpFoundation\Response', |
71
|
|
|
(new AppKernel('dev', true))->handle(Request::create('/', 'GET')) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testControllerResponse() |
76
|
|
|
{ // From: https://symfony.com/doc/current/create_framework/unit_testing.html |
77
|
|
|
// TODO: Try with a real matcher |
78
|
|
|
// TODO: Use real controller to be tested! |
79
|
|
|
$matcher = $this->createMock(UrlMatcherInterface::class); |
80
|
|
|
// use getMock() on PHPUnit 5.3 or below |
81
|
|
|
// $matcher = $this->getMock(UrlMatcherInterface::class); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$matcher |
84
|
|
|
->expects($this->once()) |
85
|
|
|
->method('match') |
86
|
|
|
->will($this->returnValue([ |
87
|
|
|
'_route' => 'foo', |
88
|
|
|
'name' => 'Fabien', |
89
|
|
|
'_controller' => function ($name) { |
90
|
|
|
return new Response('Hello '.$name); |
91
|
|
|
}, |
92
|
|
|
])) |
93
|
|
|
; |
94
|
|
|
$matcher |
95
|
|
|
->expects($this->once()) |
96
|
|
|
->method('getContext') |
97
|
|
|
->will($this->returnValue($this->createMock(RequestContext::class))) |
98
|
|
|
; |
99
|
|
|
|
100
|
|
|
$c = $this->container(); |
101
|
|
|
$c->compile(); |
102
|
|
|
$requestStack = new RequestStack(); |
103
|
|
|
$dispatcher = new EventDispatcher(); |
104
|
|
|
$dispatcher->addSubscriber(new RouterListener( |
105
|
|
|
$matcher, |
106
|
|
|
$requestStack |
107
|
|
|
)); // Returns nothing. |
108
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
109
|
|
|
$response = (new HttpKernel( |
110
|
|
|
$dispatcher, |
111
|
|
|
new ContainerControllerResolver($c), |
112
|
|
|
// new ControllerResolver(), |
|
|
|
|
113
|
|
|
$requestStack, |
114
|
|
|
new ArgumentResolver( |
115
|
|
|
new ArgumentMetadataFactory(), |
116
|
|
|
[ |
117
|
|
|
new RequestAttributeValueResolver(), |
118
|
|
|
new RequestValueResolver(), |
119
|
|
|
new SessionValueResolver(), |
120
|
|
|
new ServiceValueResolver($c), |
121
|
|
|
new DefaultValueResolver(), |
122
|
|
|
new VariadicValueResolver(), |
123
|
|
|
] |
124
|
|
|
) |
125
|
|
|
// ))->handle(Request::create('/', 'GET')); |
|
|
|
|
126
|
|
|
))->handle(new Request()); |
127
|
|
|
|
128
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
129
|
|
|
$this->assertContains('Hello Fabien', $response->getContent()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testContainerCanBeCreated() |
133
|
|
|
{ |
134
|
|
|
$this->assertInstanceOf( |
135
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
136
|
|
|
'Symfony\Component\DependencyInjection\ContainerBuilder', |
137
|
|
|
$this->container() |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testContainerInterface() |
142
|
|
|
{ |
143
|
|
|
$this->assertInstanceOf( |
144
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
145
|
|
|
'psr\Container\ContainerInterface', |
146
|
|
|
$this->container() |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function ComponentReturnsResponse() // Not yet a test! |
151
|
|
|
{ |
152
|
|
|
// TODO: Use real controller to be tested! |
153
|
|
|
$c = $this->container(); |
154
|
|
|
$c->compile(); |
155
|
|
|
$requestStack = new RequestStack(); |
156
|
|
|
$dispatcher = new EventDispatcher(); |
157
|
|
|
$dispatcher->addSubscriber(new RouterListener( |
158
|
|
|
new UrlMatcher( |
159
|
|
|
// $this->loadJustHelloRoutes(), |
|
|
|
|
160
|
|
|
$this->loadRoutes(), |
161
|
|
|
new RequestContext() |
162
|
|
|
), |
163
|
|
|
$requestStack |
164
|
|
|
)); |
165
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
166
|
|
|
|
167
|
|
|
$this->assertInstanceOf( |
168
|
|
|
// Response::class, // 5.4 < php |
169
|
|
|
'Symfony\Component\HttpFoundation\Response', |
170
|
|
|
(new HttpKernel( |
171
|
|
|
$dispatcher, |
172
|
|
|
new ContainerControllerResolver($c), |
173
|
|
|
// new ControllerResolver(), |
|
|
|
|
174
|
|
|
$requestStack, |
175
|
|
|
new ArgumentResolver( |
176
|
|
|
new ArgumentMetadataFactory(), |
177
|
|
|
[ |
178
|
|
|
new RequestAttributeValueResolver(), |
179
|
|
|
new RequestValueResolver(), |
180
|
|
|
new SessionValueResolver(), |
181
|
|
|
new ServiceValueResolver($c), |
182
|
|
|
new DefaultValueResolver(), |
183
|
|
|
new VariadicValueResolver(), |
184
|
|
|
] |
185
|
|
|
) |
186
|
|
|
))->handle(Request::create('/', 'GET')) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function configureRoutes(RouteCollectionBuilder $routes) |
191
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
192
|
|
|
$routes->add('/', EngineAsArgumentController::class, 'index'); // .'::__invoke' |
193
|
|
|
// $routes->add('/', EngineAsArgumentFrameworkController::class, 'index'); // .'::__invoke' |
|
|
|
|
194
|
|
|
//^ It should be tested if the actually used controller resolver can resolve this! |
195
|
|
|
//^ Returns Symfony/Component/Routing/Route . |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private function loadRoutes(LoaderInterface $loader = null) |
199
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
200
|
|
|
$routes = new RouteCollectionBuilder($loader); |
201
|
|
|
$this->configureRoutes($routes); |
202
|
|
|
|
203
|
|
|
return $routes->build(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
private function configureJustHelloRoutes(RouteCollectionBuilder $routes) |
207
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
208
|
|
|
$routes->add( |
209
|
|
|
'/', |
210
|
|
|
function () { |
211
|
|
|
return new Response('Hello'); |
212
|
|
|
}, |
213
|
|
|
'index' |
214
|
|
|
); // .'::__invoke' |
215
|
|
|
//^ It should be tested if the actually used controller resolver can resolve this! |
216
|
|
|
//^ Returns Symfony/Component/Routing/Route . |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
private function loadJustHelloRoutes(LoaderInterface $loader = null) |
|
|
|
|
220
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
221
|
|
|
$routes = new RouteCollectionBuilder($loader); |
222
|
|
|
$this->configureJustHelloRoutes($routes); |
223
|
|
|
|
224
|
|
|
return $routes->build(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
private function container() |
228
|
|
|
{ |
229
|
|
|
$c = new ContainerBuilder(); |
230
|
|
|
// https://symfony.com/doc/current/service_container.html |
231
|
|
|
|
232
|
|
|
$c->autowire(TemplateNameParser::class) |
233
|
|
|
->setAutoconfigured(true) |
234
|
|
|
->setPublic(false); |
235
|
|
|
$c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class); |
236
|
|
|
|
237
|
|
|
$c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class) |
238
|
|
|
->setArgument('$templates', ['index.html.twig' => 'Hello Component!']) |
239
|
|
|
->setAutoconfigured(true) |
240
|
|
|
->setPublic(false); |
241
|
|
|
$c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class); |
242
|
|
|
|
243
|
|
|
$c->autowire(Twig_Environment::class, Twig_Environment::class) |
244
|
|
|
->setAutoconfigured(true) |
245
|
|
|
->setPublic(false); |
246
|
|
|
$c->setAlias(Twig\Environment::class, Twig_Environment::class); |
247
|
|
|
|
248
|
|
|
$c->autowire(TwigEngine::class) |
249
|
|
|
->setAutoconfigured(true) |
250
|
|
|
->setPublic(false); |
251
|
|
|
$c->setAlias(EngineInterface::class, TwigEngine::class); |
252
|
|
|
$c->setAlias('templating', TwigEngine::class); // Read Symfony source code to understand! |
253
|
|
|
|
254
|
|
|
// Unit Testing |
255
|
|
|
// $c->autowire('test.client', Client::class) |
|
|
|
|
256
|
|
|
// ->setPublic(true); // Public needed! |
|
|
|
|
257
|
|
|
|
258
|
|
|
//Controllers |
259
|
|
|
$c->autowire(EngineAsArgumentController::class) |
260
|
|
|
->setAutoconfigured(true) |
261
|
|
|
->addTag('controller.service_arguments') |
262
|
|
|
->setPublic(true); // Checking if needed... |
263
|
|
|
|
264
|
|
|
$c->autowire(EngineAsArgumentFrameworkController::class) |
265
|
|
|
->setAutoconfigured(true) |
266
|
|
|
->addTag('controller.service_arguments') |
267
|
|
|
->setPublic(true); // Checking if needed... |
268
|
|
|
|
269
|
|
|
$c->addCompilerPass(new ControllerArgumentValueResolverPass()); |
270
|
|
|
$c->addCompilerPass(new RegisterControllerArgumentLocatorsPass()); |
271
|
|
|
|
272
|
|
|
return $c; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html |
277
|
|
|
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html |
278
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: