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