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