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\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver; |
20
|
|
|
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver; |
21
|
|
|
use Symfony\Component\HttpKernel\EventListener\ResponseListener; |
22
|
|
|
use Symfony\Component\HttpKernel\EventListener\RouterListener; |
23
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
24
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
25
|
|
|
use Symfony\Component\Routing\RequestContext; |
26
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
27
|
|
|
use SymfonyUtil\Controller\EngineAsArgumentController; |
28
|
|
|
use Tests\Component\AppKernel; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @covers \SymfonyUtil\Controller\EngineAsArgumentController |
32
|
|
|
*/ |
33
|
|
|
final class EngineAsArgumentInKernelControllerTest extends TestCase |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
public function testCanBeCreated() |
36
|
|
|
{ |
37
|
|
|
$this->assertInstanceOf( |
38
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
39
|
|
|
'Symfony\Component\HttpKernel\Kernel', |
40
|
|
|
new AppKernel('dev', true) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testKernelInterface() |
45
|
|
|
{ |
46
|
|
|
$this->assertInstanceOf( |
47
|
|
|
// ...::class, // 5.4 < php |
|
|
|
|
48
|
|
|
'Symfony\Component\HttpKernel\KernelInterface', |
49
|
|
|
new AppKernel('dev', true) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFrameworkReturnsResponse() |
54
|
|
|
{ |
55
|
|
|
$this->assertInstanceOf( |
56
|
|
|
// Response::class, // 5.4 < php |
57
|
|
|
'Symfony\Component\HttpFoundation\Response', |
58
|
|
|
(new AppKernel('dev', true))->handle(Request::create('/', 'GET')) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testComponentReturnsResponse() |
63
|
|
|
{ |
64
|
|
|
$requestStack = new RequestStack(); |
65
|
|
|
$routes = new RouteCollectionBuilder(); // Because I know how to use it. |
66
|
|
|
$routes->add('/', EngineAsArgumentController::class, 'index'); // returns Symfony/Component/Routing/Route |
67
|
|
|
$dispatcher = new EventDispatcher(); |
68
|
|
|
$dispatcher->addSubscriber(new RouterListener( |
69
|
|
|
new UrlMatcher( |
70
|
|
|
$routes->build(), |
71
|
|
|
new RequestContext() |
72
|
|
|
), |
73
|
|
|
$requestStack |
74
|
|
|
)); |
75
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
76
|
|
|
|
77
|
|
|
$c = new ContainerBuilder(); |
78
|
|
|
// https://symfony.com/doc/current/service_container.html |
79
|
|
|
|
80
|
|
|
$c->autowire(TemplateNameParser::class) |
81
|
|
|
->setAutoconfigured(true) |
82
|
|
|
->setPublic(false); |
83
|
|
|
$c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class); |
84
|
|
|
|
85
|
|
|
$c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class) |
86
|
|
|
->setArgument('$templates', ['index.html.twig' => 'Hello Component!']) |
87
|
|
|
->setAutoconfigured(true) |
88
|
|
|
->setPublic(false); |
89
|
|
|
$c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class); |
90
|
|
|
|
91
|
|
|
$c->autowire(Twig_Environment::class, Twig_Environment::class) |
92
|
|
|
->setAutoconfigured(true) |
93
|
|
|
->setPublic(false); |
94
|
|
|
$c->setAlias(Twig\Environment::class, Twig_Environment::class); |
95
|
|
|
|
96
|
|
|
$c->autowire(TwigEngine::class) |
97
|
|
|
->setAutoconfigured(true) |
98
|
|
|
->setPublic(false); |
99
|
|
|
$c->setAlias(EngineInterface::class, TwigEngine::class); |
100
|
|
|
|
101
|
|
|
if (in_array($this->getEnvironment(), ['test'], true)) { |
102
|
|
|
$c->autowire('test.client', Client::class) |
103
|
|
|
->setPublic(true); // Public needed! |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//Controllers |
107
|
|
|
$c->autowire(EngineAsArgumentController::class) |
108
|
|
|
->setAutoconfigured(true) |
109
|
|
|
->addTag('controller.service_arguments') |
110
|
|
|
->setPublic(false); |
111
|
|
|
|
112
|
|
|
$this->assertInstanceOf( |
113
|
|
|
// Response::class, // 5.4 < php |
114
|
|
|
'Symfony\Component\HttpFoundation\Response', |
115
|
|
|
(new HttpKernel( |
116
|
|
|
$dispatcher, |
117
|
|
|
new ContainerControllerResolver($c), |
118
|
|
|
$requestStack, |
119
|
|
|
new ArgumentResolver() |
120
|
|
|
))->handle(Request::create('/', 'GET')) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html |
126
|
|
|
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html |
127
|
|
|
|
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: