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