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; // From Bridge or Bundle. |
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\ContainerControllerResolver; |
25
|
|
|
use Symfony\Component\HttpKernel\EventListener\ResponseListener; |
26
|
|
|
use Symfony\Component\HttpKernel\EventListener\RouterListener; |
27
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
28
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; // != Symfony\Bundle\FrameworkBundle\Routing\Router |
29
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcherInterface; |
30
|
|
|
use Symfony\Component\Routing\RequestContext; |
31
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
32
|
|
|
use Symfony\Component\Templating\EngineInterface; |
33
|
|
|
use Symfony\Component\Templating\TemplateNameParser; // != Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser |
34
|
|
|
use SymfonyUtil\Controller\TemplatingController; |
35
|
|
|
use Tests\Component\AppKernel; |
|
|
|
|
36
|
|
|
|
37
|
|
|
final class InKernelTemplatingControllerTest extends TestCase |
38
|
|
|
{ |
39
|
|
|
public function testCanBeCreated() |
40
|
|
|
{ |
41
|
|
|
$this->assertInstanceOf( |
42
|
|
|
// ...::class, // 5.4 < php |
43
|
|
|
'Symfony\Component\HttpKernel\Kernel', |
44
|
|
|
new AppKernel('dev', true) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testKernelInterface() |
49
|
|
|
{ |
50
|
|
|
$this->assertInstanceOf( |
51
|
|
|
// ...::class, // 5.4 < php |
52
|
|
|
'Symfony\Component\HttpKernel\KernelInterface', |
53
|
|
|
new AppKernel('dev', true) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testFrameworkReturnsResponse() |
58
|
|
|
{ |
59
|
|
|
$this->markTestIncomplete(); // Test does not work any more with Symfony 3.4 |
60
|
|
|
|
61
|
|
|
$this->assertInstanceOf( |
62
|
|
|
// Response::class, // 5.4 < php |
63
|
|
|
'Symfony\Component\HttpFoundation\Response', |
64
|
|
|
(new AppKernel('dev', true))->handle(Request::create('/constructor', '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 see next test... |
71
|
|
|
// TODO: Use real controller to be tested! OK |
72
|
|
|
$matcher = $this->createMock(UrlMatcherInterface::class); // What about another test with RequestMatcherInterface? |
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
|
|
|
->willReturn([ |
81
|
|
|
'_route' => 'foo', |
82
|
|
|
'name' => 'Fabien', |
83
|
|
|
'_controller' => TemplatingController::class, |
84
|
|
|
// ])) |
85
|
|
|
]) |
86
|
|
|
; |
87
|
|
|
$matcher |
88
|
|
|
->expects($this->once()) |
89
|
|
|
->method('getContext') |
90
|
|
|
// ->will($this->returnValue($this->createMock(RequestContext::class))) |
91
|
|
|
->willReturn($this->createMock(RequestContext::class)) |
92
|
|
|
; |
93
|
|
|
|
94
|
|
|
$c = $this->container(); |
95
|
|
|
$c->compile(); |
96
|
|
|
$requestStack = new RequestStack(); |
97
|
|
|
$dispatcher = new EventDispatcher(); |
98
|
|
|
$dispatcher->addSubscriber(new RouterListener($matcher, $requestStack)); // Returns nothing. |
99
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
100
|
|
|
$response = (new HttpKernel( |
101
|
|
|
$dispatcher, |
102
|
|
|
new ContainerControllerResolver($c), // Psr\Container\ContainerInterface |
103
|
|
|
$requestStack, |
104
|
|
|
new ArgumentResolver() // This argument will be optional in Symfony 4.0 |
105
|
|
|
))->handle(new Request()); // Mock will inject the controller. |
106
|
|
|
|
107
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
108
|
|
|
$this->assertContains('Hello Component!', $response->getContent()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testContainerCanBeCreated() |
112
|
|
|
{ |
113
|
|
|
$this->assertInstanceOf( |
114
|
|
|
// ...::class, // 5.4 < php |
115
|
|
|
'Symfony\Component\DependencyInjection\ContainerBuilder', |
116
|
|
|
$this->container() |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testContainerInterface() |
121
|
|
|
{ |
122
|
|
|
$this->assertInstanceOf( |
123
|
|
|
// ...::class, // 5.4 < php |
124
|
|
|
'psr\Container\ContainerInterface', |
125
|
|
|
$this->container() |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testComponentReturnsResponse() |
130
|
|
|
{ |
131
|
|
|
$c = $this->container(); |
132
|
|
|
$c->compile(); |
133
|
|
|
$requestStack = new RequestStack(); |
134
|
|
|
$dispatcher = new EventDispatcher(); |
135
|
|
|
$dispatcher->addSubscriber(new RouterListener( |
136
|
|
|
new UrlMatcher( |
137
|
|
|
$this->loadRoutes(), |
138
|
|
|
new RequestContext() |
139
|
|
|
), |
140
|
|
|
$requestStack |
141
|
|
|
)); |
142
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
143
|
|
|
|
144
|
|
|
$this->assertInstanceOf( |
145
|
|
|
// Response::class, // 5.4 < php |
146
|
|
|
'Symfony\Component\HttpFoundation\Response', |
147
|
|
|
(new HttpKernel( |
148
|
|
|
$dispatcher, |
149
|
|
|
new ContainerControllerResolver($c), // Psr\Container\ContainerInterface |
150
|
|
|
$requestStack, |
151
|
|
|
new ArgumentResolver() // This argument will be optional in Symfony 4.0 |
152
|
|
|
))->handle(Request::create('/', 'GET')) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function configureRoutes(RouteCollectionBuilder $routes) |
157
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
158
|
|
|
$routes->add('/', TemplatingController::class, 'index'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function loadRoutes(LoaderInterface $loader = null) |
162
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
163
|
|
|
$routes = new RouteCollectionBuilder($loader); |
164
|
|
|
$this->configureRoutes($routes); |
165
|
|
|
|
166
|
|
|
return $routes->build(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function configureJustHelloRoutes(RouteCollectionBuilder $routes) |
170
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
171
|
|
|
$routes->add( |
172
|
|
|
'/', |
173
|
|
|
function () { |
|
|
|
|
174
|
|
|
return new Response('Hello'); |
175
|
|
|
}, |
176
|
|
|
'index' |
177
|
|
|
); |
178
|
|
|
//^ Returns Symfony/Component/Routing/Route . |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function loadJustHelloRoutes(LoaderInterface $loader = null) |
|
|
|
|
182
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
183
|
|
|
$routes = new RouteCollectionBuilder($loader); |
184
|
|
|
$this->configureJustHelloRoutes($routes); |
185
|
|
|
|
186
|
|
|
return $routes->build(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function container() |
190
|
|
|
{ |
191
|
|
|
$c = new ContainerBuilder(); |
192
|
|
|
|
193
|
|
|
$c->autowire(TwigEngine::class) // From Bridge or Bundle |
194
|
|
|
->setArgument('$environment', new Twig_Environment(new Twig_Loader_Array(['index.html.twig' => 'Hello Component!']))) |
|
|
|
|
195
|
|
|
->setArgument('$parser', new TemplateNameParser()) // From Templating or Framework |
196
|
|
|
->setAutoconfigured(true) |
197
|
|
|
->setPublic(false); |
198
|
|
|
$c->setAlias(EngineInterface::class, TwigEngine::class); |
199
|
|
|
|
200
|
|
|
// Unit Testing |
201
|
|
|
// $c->autowire('test.client', Client::class) |
202
|
|
|
// ->setPublic(true); // Public needed! |
203
|
|
|
|
204
|
|
|
//Controllers |
205
|
|
|
$c->autowire(TemplatingController::class) |
206
|
|
|
->setAutoconfigured(true) |
207
|
|
|
->addTag('controller.service_arguments') |
208
|
|
|
->setPublic(true); |
209
|
|
|
|
210
|
|
|
return $c; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html |
215
|
|
|
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html |
216
|
|
|
|
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: