1 | <?php |
||
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 | public function testControllerResponse() |
||
67 | { |
||
68 | $matcher = $this->createMock(Routing\Matcher\UrlMatcherInterface::class); |
||
69 | // use getMock() on PHPUnit 5.3 or below |
||
70 | // $matcher = $this->getMock(Routing\Matcher\UrlMatcherInterface::class); |
||
71 | |||
72 | $matcher |
||
73 | ->expects($this->once()) |
||
74 | ->method('match') |
||
75 | ->will($this->returnValue(array( |
||
76 | '_route' => 'foo', |
||
77 | 'name' => 'Fabien', |
||
78 | '_controller' => function ($name) { |
||
79 | return new Response('Hello '.$name); |
||
80 | } |
||
81 | ))) |
||
82 | ; |
||
83 | $matcher |
||
84 | ->expects($this->once()) |
||
85 | ->method('getContext') |
||
86 | ->will($this->returnValue($this->createMock(Routing\RequestContext::class))) |
||
87 | ; |
||
88 | |||
89 | $requestStack = new RequestStack(); |
||
90 | $dispatcher = new EventDispatcher(); |
||
91 | $dispatcher->addSubscriber(new RouterListener( |
||
92 | $matcher, |
||
93 | $requestStack |
||
94 | )); |
||
95 | $dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
||
96 | $response = (new HttpKernel( |
||
97 | $dispatcher, |
||
98 | // new ContainerControllerResolver($c), |
||
99 | new ControllerResolver(), |
||
100 | $requestStack, |
||
101 | new ArgumentResolver() // OK |
||
102 | // ))->handle(Request::create('/', 'GET')) |
||
103 | ))->handle(new Request()) |
||
104 | |||
105 | $this->assertEquals(200, $response->getStatusCode()); |
||
106 | $this->assertContains('Hello Fabien', $response->getContent()); |
||
107 | } |
||
108 | |||
109 | public function testContainerCanBeCreated() |
||
110 | { |
||
111 | $this->assertInstanceOf( |
||
112 | // ...::class, // 5.4 < php |
||
113 | 'Symfony\Component\DependencyInjection\ContainerBuilder', |
||
114 | $this->container() |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | public function testContainerInterface() |
||
119 | { |
||
120 | $this->assertInstanceOf( |
||
121 | // ...::class, // 5.4 < php |
||
122 | 'psr\Container\ContainerInterface', |
||
123 | $this->container() |
||
124 | ); |
||
125 | } |
||
126 | |||
127 | public function ComponentReturnsResponse() // Not yet a test! |
||
128 | { |
||
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 | |||
141 | $this->assertInstanceOf( |
||
142 | // Response::class, // 5.4 < php |
||
143 | 'Symfony\Component\HttpFoundation\Response', |
||
144 | (new HttpKernel( |
||
145 | $dispatcher, |
||
146 | new ContainerControllerResolver($c), |
||
147 | $requestStack, |
||
148 | new ArgumentResolver() |
||
149 | ))->handle(Request::create('/', 'GET')) |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | private function configureRoutes(RouteCollectionBuilder $routes) |
||
154 | { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
||
155 | $routes->add('/', EngineAsArgumentController::class, 'index'); // .'::__invoke' |
||
156 | //^ It should be tested if the actually used controller resolver can resolve this! |
||
157 | //^ Returns Symfony/Component/Routing/Route . |
||
158 | } |
||
159 | |||
160 | private function loadRoutes(LoaderInterface $loader = null) |
||
161 | { // from Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait |
||
162 | $routes = new RouteCollectionBuilder($loader); |
||
163 | $this->configureRoutes($routes); |
||
164 | |||
165 | return $routes->build(); |
||
166 | } |
||
167 | |||
168 | private function container() |
||
169 | { |
||
170 | $c = new ContainerBuilder(); |
||
171 | // https://symfony.com/doc/current/service_container.html |
||
172 | |||
173 | $c->autowire(TemplateNameParser::class) |
||
174 | ->setAutoconfigured(true) |
||
175 | ->setPublic(false); |
||
176 | $c->setAlias(TemplateNameParserInterface::class, TemplateNameParser::class); |
||
177 | |||
178 | $c->autowire(Twig_Loader_Array::class, Twig_Loader_Array::class) |
||
179 | ->setArgument('$templates', ['index.html.twig' => 'Hello Component!']) |
||
180 | ->setAutoconfigured(true) |
||
181 | ->setPublic(false); |
||
182 | $c->setAlias(Twig_LoaderInterface::class, Twig_Loader_Array::class); |
||
183 | |||
184 | $c->autowire(Twig_Environment::class, Twig_Environment::class) |
||
185 | ->setAutoconfigured(true) |
||
186 | ->setPublic(false); |
||
187 | $c->setAlias(Twig\Environment::class, Twig_Environment::class); |
||
188 | |||
189 | $c->autowire(TwigEngine::class) |
||
190 | ->setAutoconfigured(true) |
||
191 | ->setPublic(false); |
||
192 | $c->setAlias(EngineInterface::class, TwigEngine::class); |
||
193 | |||
194 | // Unit Testing |
||
195 | // $c->autowire('test.client', Client::class) |
||
196 | // ->setPublic(true); // Public needed! |
||
197 | |||
210 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.