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\ContainerControllerResolver; |
22
|
|
|
use Symfony\Component\HttpKernel\EventListener\ResponseListener; |
23
|
|
|
use Symfony\Component\HttpKernel\EventListener\RouterListener; |
24
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
25
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
26
|
|
|
use Symfony\Component\Routing\RequestContext; |
27
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
28
|
|
|
use Symfony\Component\Templating\EngineInterface; |
29
|
|
|
use Symfony\Component\Templating\TemplateNameParser; |
30
|
|
|
use Symfony\Component\Templating\TemplateNameParserInterface; |
31
|
|
|
use SymfonyUtil\Controller\EngineAsArgumentController; |
32
|
|
|
use Tests\Component\AppKernel; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers \SymfonyUtil\Controller\EngineAsArgumentController |
36
|
|
|
*/ |
37
|
|
|
final class EngineAsArgumentInKernelControllerTest 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('/', 'GET')) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/* |
|
|
|
|
67
|
|
|
public function testComponentReturnsResponse() |
68
|
|
|
{ |
69
|
|
|
$requestStack = new RequestStack(); |
70
|
|
|
$routes = new RouteCollectionBuilder(); // Because I know how to use it. |
71
|
|
|
$routes->add('/', EngineAsArgumentController::class, 'index'); // .'::__invoke' |
72
|
|
|
//^ It should be tested if the actually used controller resolver can resolve this! |
73
|
|
|
//^ Returns Symfony/Component/Routing/Route . |
74
|
|
|
$dispatcher = new EventDispatcher(); |
75
|
|
|
$dispatcher->addSubscriber(new RouterListener( |
76
|
|
|
new UrlMatcher( |
77
|
|
|
$routes->build(), |
78
|
|
|
new RequestContext() |
79
|
|
|
), |
80
|
|
|
$requestStack |
81
|
|
|
)); |
82
|
|
|
$dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
83
|
|
|
|
84
|
|
|
$c = new ContainerBuilder(); |
85
|
|
|
// https://symfony.com/doc/current/service_container.html |
86
|
|
|
|
87
|
|
|
$c->autowire(TemplateNameParser::class) |
88
|
|
|
->setAutoconfigured(true) |
89
|
|
|
->setPublic(false); |
90
|
|
|
$c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class); |
91
|
|
|
|
92
|
|
|
$c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class) |
93
|
|
|
->setArgument('$templates', ['index.html.twig' => 'Hello Component!']) |
94
|
|
|
->setAutoconfigured(true) |
95
|
|
|
->setPublic(false); |
96
|
|
|
$c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class); |
97
|
|
|
|
98
|
|
|
$c->autowire(Twig_Environment::class, Twig_Environment::class) |
99
|
|
|
->setAutoconfigured(true) |
100
|
|
|
->setPublic(false); |
101
|
|
|
$c->setAlias(Twig\Environment::class, Twig_Environment::class); |
102
|
|
|
|
103
|
|
|
$c->autowire(TwigEngine::class) |
104
|
|
|
->setAutoconfigured(true) |
105
|
|
|
->setPublic(false); |
106
|
|
|
$c->setAlias(EngineInterface::class, TwigEngine::class); |
107
|
|
|
|
108
|
|
|
// Unit Testing |
109
|
|
|
// $c->autowire('test.client', Client::class) |
110
|
|
|
// ->setPublic(true); // Public needed! |
111
|
|
|
|
112
|
|
|
//Controllers |
113
|
|
|
$c->autowire(EngineAsArgumentController::class) |
114
|
|
|
->setAutoconfigured(true) |
115
|
|
|
->addTag('controller.service_arguments') |
116
|
|
|
->setPublic(false); |
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf( |
119
|
|
|
// Response::class, // 5.4 < php |
120
|
|
|
'Symfony\Component\HttpFoundation\Response', |
121
|
|
|
(new HttpKernel( |
122
|
|
|
$dispatcher, |
123
|
|
|
new ContainerControllerResolver($c), |
124
|
|
|
$requestStack, |
125
|
|
|
new ArgumentResolver() |
126
|
|
|
))->handle(Request::create('/', 'GET')) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
*/ |
130
|
|
|
|
131
|
|
|
private function container() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
return new ContainerBuilder(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html |
138
|
|
|
// http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html |
139
|
|
|
|
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: