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