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->assertInstanceOf( |
60
|
|
|
// Response::class, // 5.4 < php |
61
|
|
|
'Symfony\Component\HttpFoundation\Response', |
62
|
|
|
(new AppKernel('dev', true))->handle(Request::create('/constructor', 'GET')) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testControllerResponse() |
67
|
|
|
{ // From: https://symfony.com/doc/current/create_framework/unit_testing.html |
68
|
|
|
// TODO: Try with a real matcher see next test... |
69
|
|
|
// TODO: Use real controller to be tested! OK |
70
|
|
|
$matcher = $this->createMock(UrlMatcherInterface::class); // What about another test with RequestMatcherInterface? |
71
|
|
|
// use getMock() on PHPUnit 5.3 or below |
72
|
|
|
// $matcher = $this->getMock(UrlMatcherInterface::class); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$matcher |
75
|
|
|
->expects($this->once()) |
76
|
|
|
->method('match') |
77
|
|
|
->will($this->returnValue([ |
78
|
|
|
'_route' => 'foo', |
79
|
|
|
'name' => 'Fabien', |
80
|
|
|
'_controller' => TemplatingController::class, |
81
|
|
|
])) |
82
|
|
|
; |
83
|
|
|
$matcher |
84
|
|
|
->expects($this->once()) |
85
|
|
|
->method('getContext') |
86
|
|
|
->will($this->returnValue($this->createMock(RequestContext::class))) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$c = $this->container(); |
90
|
|
|
$c->compile(); |
91
|
|
|
$requestStack = new RequestStack(); |
92
|
|
|
$dispatcher = new EventDispatcher(); |
93
|
|
|
$dispatcher->addSubscriber(new RouterListener($matcher, $requestStack)); // Returns nothing. |
94
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
95
|
|
|
$response = (new HttpKernel( |
96
|
|
|
$dispatcher, |
97
|
|
|
new ContainerControllerResolver($c), |
98
|
|
|
$requestStack, |
99
|
|
|
new ArgumentResolver() |
100
|
|
|
))->handle(new Request()); // Mock will inject the controller. |
101
|
|
|
|
102
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
103
|
|
|
$this->assertContains('Hello Component!', $response->getContent()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testContainerCanBeCreated() |
107
|
|
|
{ |
108
|
|
|
$this->assertInstanceOf( |
109
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
110
|
|
|
'Symfony\Component\DependencyInjection\ContainerBuilder', |
111
|
|
|
$this->container() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testContainerInterface() |
116
|
|
|
{ |
117
|
|
|
$this->assertInstanceOf( |
118
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
119
|
|
|
'psr\Container\ContainerInterface', |
120
|
|
|
$this->container() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testComponentReturnsResponse() |
125
|
|
|
{ |
126
|
|
|
$c = $this->container(); |
127
|
|
|
$c->compile(); |
128
|
|
|
$requestStack = new RequestStack(); |
129
|
|
|
$dispatcher = new EventDispatcher(); |
130
|
|
|
$dispatcher->addSubscriber(new RouterListener( |
131
|
|
|
new UrlMatcher( |
132
|
|
|
$this->loadRoutes(), |
133
|
|
|
new RequestContext() |
134
|
|
|
), |
135
|
|
|
$requestStack |
136
|
|
|
)); |
137
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
138
|
|
|
|
139
|
|
|
$this->assertInstanceOf( |
140
|
|
|
// Response::class, // 5.4 < php |
141
|
|
|
'Symfony\Component\HttpFoundation\Response', |
142
|
|
|
(new HttpKernel( |
143
|
|
|
$dispatcher, |
144
|
|
|
new ContainerControllerResolver($c), |
145
|
|
|
$requestStack, |
146
|
|
|
new ArgumentResolver() |
147
|
|
|
))->handle(Request::create('/', 'GET')) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function configureRoutes(RouteCollectionBuilder $routes) |
152
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
153
|
|
|
$routes->add('/', TemplatingController::class, 'index'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function loadRoutes(LoaderInterface $loader = null) |
157
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
158
|
|
|
$routes = new RouteCollectionBuilder($loader); |
159
|
|
|
$this->configureRoutes($routes); |
160
|
|
|
|
161
|
|
|
return $routes->build(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function configureJustHelloRoutes(RouteCollectionBuilder $routes) |
165
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
166
|
|
|
$routes->add( |
167
|
|
|
'/', |
168
|
|
|
function () { |
169
|
|
|
return new Response('Hello'); |
170
|
|
|
}, |
171
|
|
|
'index' |
172
|
|
|
); |
173
|
|
|
//^ Returns Symfony/Component/Routing/Route . |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function loadJustHelloRoutes(LoaderInterface $loader = null) |
|
|
|
|
177
|
|
|
{ // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
178
|
|
|
$routes = new RouteCollectionBuilder($loader); |
179
|
|
|
$this->configureJustHelloRoutes($routes); |
180
|
|
|
|
181
|
|
|
return $routes->build(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function container() |
185
|
|
|
{ |
186
|
|
|
$c = new ContainerBuilder(); |
187
|
|
|
|
188
|
|
|
$c->autowire(TwigEngine::class) // From Bridge or Bundle |
189
|
|
|
->setArgument('$environment', new Twig_Environment(new Twig_Loader_Array(['index.html.twig' => 'Hello Component!']))) |
190
|
|
|
->setArgument('$parser', new TemplateNameParser()) // From Templating or Framework |
191
|
|
|
->setAutoconfigured(true) |
192
|
|
|
->setPublic(false); |
193
|
|
|
$c->setAlias(EngineInterface::class, TwigEngine::class); |
194
|
|
|
|
195
|
|
|
// Unit Testing |
196
|
|
|
// $c->autowire('test.client', Client::class) |
|
|
|
|
197
|
|
|
// ->setPublic(true); // Public needed! |
|
|
|
|
198
|
|
|
|
199
|
|
|
//Controllers |
200
|
|
|
$c->autowire(TemplatingController::class) |
201
|
|
|
->setAutoconfigured(true) |
202
|
|
|
->addTag('controller.service_arguments') |
203
|
|
|
->setPublic(true); |
204
|
|
|
|
205
|
|
|
return $c; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html |
210
|
|
|
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html |
211
|
|
|
|
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: