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