‪TYPO3CMS  10.4
RouteDispatcherTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
19 use Psr\Container\ContainerInterface;
20 use Psr\Http\Message\ServerRequestInterface;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
36 class ‪RouteDispatcherTest extends UnitTestCase
37 {
38  public function ‪tearDown(): void
39  {
41  GeneralUtility::purgeInstances();
42  parent::tearDown();
43  }
44 
49  {
50  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
51  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
52  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
53 
54  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
55  $routerProphecy = $this->prophesize(Router::class);
56  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
57  $routeProphecy = $this->prophesize(Route::class);
58  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
59  $routeProphecy->getOption('access')->willReturn('public');
60  $routeProphecy->getOption('referrer')->willReturn(false);
61  $routeProphecy->getOption('module')->willReturn(false);
62  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
63  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
64  $containerProphecy = $this->prophesize(ContainerInterface::class);
65  $containerProphecy->has(Argument::any())->willReturn(false);
66 
67  $target = 42;
68  $routeProphecy->getOption('target')->willReturn($target);
69  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
70 
71  $this->expectException(\InvalidArgumentException::class);
72  $this->expectExceptionCode(1425381442);
73 
74  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
75  $subject->dispatch($requestProphecy->reveal());
76  }
77 
82  {
83  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
84  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
85  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
86 
87  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
88  $routerProphecy = $this->prophesize(Router::class);
89  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
90  $routeProphecy = $this->prophesize(Route::class);
91  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
92  $routeProphecy->getOption('access')->willReturn('public');
93  $routeProphecy->getOption('referrer')->willReturn(false);
94  $routeProphecy->getOption('module')->willReturn(false);
95  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
96  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
97  $containerProphecy = $this->prophesize(ContainerInterface::class);
98  $containerProphecy->has(Argument::any())->willReturn(false);
99 
100  $target = [
102  'mainAction'
103  ];
104  $routeProphecy->getOption('target')->willReturn($target);
105  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
106 
107  $this->expectException(\RuntimeException::class);
108  $this->expectExceptionCode(1520756142);
109 
110  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
111  $subject->dispatch($requestProphecy->reveal());
112  }
113 
118  {
119  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
120  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
121  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
122 
123  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
124  $routerProphecy = $this->prophesize(Router::class);
125  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
126  $routeProphecy = $this->prophesize(Route::class);
127  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
128  $routeProphecy->getOption('access')->willReturn('public');
129  $routeProphecy->getOption('referrer')->willReturn(false);
130  $routeProphecy->getOption('module')->willReturn(false);
131  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
132  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
133  $containerProphecy = $this->prophesize(ContainerInterface::class);
134  $containerProphecy->has(Argument::any())->willReturn(false);
135 
136  $target = static function (ServerRequestInterface $request) {
137  throw new \RuntimeException('I have been called. Good!', 1520756466);
138  };
139  $routeProphecy->getOption('target')->willReturn($target);
140  $requestProphecy->withAttribute('target', Argument::type(\Closure::class))->willReturn($requestProphecy->reveal());
141 
142  $this->expectException(\RuntimeException::class);
143  $this->expectExceptionCode(1520756466);
144 
145  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
146  $subject->dispatch($requestProphecy->reveal());
147  }
148 
153  {
154  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
155  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
156  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
157 
158  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
159  $routerProphecy = $this->prophesize(Router::class);
160  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
161  $routeProphecy = $this->prophesize(Route::class);
162  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
163  $routeProphecy->getOption('access')->willReturn('public');
164  $routeProphecy->getOption('referrer')->willReturn(false);
165  $routeProphecy->getOption('module')->willReturn(false);
166  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
167  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
168  $containerProphecy = $this->prophesize(ContainerInterface::class);
169  $containerProphecy->has(Argument::any())->willReturn(false);
170 
171  $target = RouteDispatcherClassInvokeFixture::class;
172  $routeProphecy->getOption('target')->willReturn($target);
173  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
174 
175  $this->expectException(\RuntimeException::class);
176  $this->expectExceptionCode(1520756623);
177 
178  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
179  $subject->dispatch($requestProphecy->reveal());
180  }
181 
186  {
187  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
188  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
189  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
190 
191  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
192  $routerProphecy = $this->prophesize(Router::class);
193  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
194  $routeProphecy = $this->prophesize(Route::class);
195  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
196  $routeProphecy->getOption('access')->willReturn('public');
197  $routeProphecy->getOption('referrer')->willReturn(false);
198  $routeProphecy->getOption('module')->willReturn(false);
199  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
200  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
201 
202  $target = 'routedispatcher.classinvokefixture';
203  $routeProphecy->getOption('target')->willReturn($target);
204  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
205  $containerProphecy = $this->prophesize(ContainerInterface::class);
206  $containerProphecy->has($target)->willReturn(true);
207  $containerProphecy->get($target)->willReturn(new ‪RouteDispatcherClassInvokeFixture());
208 
209  $this->expectException(\RuntimeException::class);
210  $this->expectExceptionCode(1520756623);
211 
212  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
213  $subject->dispatch($requestProphecy->reveal());
214  }
215 
220  {
221  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
222  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
223  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
224 
225  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
226  $routerProphecy = $this->prophesize(Router::class);
227  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
228  $routeProphecy = $this->prophesize(Route::class);
229  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
230  $routeProphecy->getOption('access')->willReturn('public');
231  $routeProphecy->getOption('referrer')->willReturn(false);
232  $routeProphecy->getOption('module')->willReturn(false);
233  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
234  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
235  $containerProphecy = $this->prophesize(ContainerInterface::class);
236  $containerProphecy->has(Argument::any())->willReturn(false);
237 
238  $target = RouteDispatcherClassWithoutInvokeFixture::class;
239  $routeProphecy->getOption('target')->willReturn($target);
240  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
241 
242  $this->expectException(\InvalidArgumentException::class);
243  $this->expectExceptionCode(1442431631);
244 
245  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
246  $subject->dispatch($requestProphecy->reveal());
247  }
248 
253  {
254  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
255  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
256  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
257 
258  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
259  $routerProphecy = $this->prophesize(Router::class);
260  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
261  $routeProphecy = $this->prophesize(Route::class);
262  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
263  $routeProphecy->getOption('access')->willReturn('public');
264  $routeProphecy->getOption('referrer')->willReturn(false);
265  $routeProphecy->getOption('module')->willReturn(false);
266  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
267  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
268  $containerProphecy = $this->prophesize(ContainerInterface::class);
269  $containerProphecy->has(Argument::any())->willReturn(false);
270 
271  $target = RouteDispatcherClassFixture::class . '::mainAction';
272  $routeProphecy->getOption('target')->willReturn($target);
273  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
274 
275  $this->expectException(\RuntimeException::class);
276  $this->expectExceptionCode(1520756142);
277 
278  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
279  $subject->dispatch($requestProphecy->reveal());
280  }
281 
286  {
287  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
288  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
289  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
290 
291  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
292  $routerProphecy = $this->prophesize(Router::class);
293  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
294  $routeProphecy = $this->prophesize(Route::class);
295  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
296  $routeProphecy->getOption('access')->willReturn('public');
297  $routeProphecy->getOption('referrer')->willReturn(false);
298  $routeProphecy->getOption('module')->willReturn(false);
299  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
300  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
301  $containerProphecy = $this->prophesize(ContainerInterface::class);
302  $containerProphecy->has(Argument::any())->willReturn(false);
303 
304  $target = RouteDispatcherStaticClassFixture::class . '::mainAction';
305  $routeProphecy->getOption('target')->willReturn($target);
306  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
307 
308  $this->expectException(\RuntimeException::class);
309  $this->expectExceptionCode(1520757000);
310 
311  $subject = new ‪RouteDispatcher($containerProphecy->reveal());
312  $subject->dispatch($requestProphecy->reveal());
313  }
314 }
‪TYPO3\CMS\Backend\Http\RouteDispatcher
Definition: RouteDispatcher.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClassImplementingInvoke
‪dispatchCallsTargetIfTargetIsClassImplementingInvoke()
Definition: RouteDispatcherTest.php:152
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetIsNotCallable
‪dispatchThrowsExceptionIfTargetIsNotCallable()
Definition: RouteDispatcherTest.php:48
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\set
‪static set($classNameOrType, AbstractFormProtection $instance)
Definition: FormProtectionFactory.php:209
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\purgeInstances
‪static purgeInstances()
Definition: FormProtectionFactory.php:219
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke
‪dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke()
Definition: RouteDispatcherTest.php:219
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsClassMethodCombinationGivenAsString
‪dispatchCallsClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:252
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClosure
‪dispatchCallsTargetIfTargetIsClosure()
Definition: RouteDispatcherTest.php:117
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest
Definition: RouteDispatcherTest.php:37
‪TYPO3\CMS\Backend\Tests\Unit\Http
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsArray
‪dispatchCallsTargetIfTargetIsArray()
Definition: RouteDispatcherTest.php:81
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\tearDown
‪tearDown()
Definition: RouteDispatcherTest.php:38
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection
Definition: AbstractFormProtection.php:30
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:47
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsInContainer
‪dispatchCallsTargetIfTargetIsInContainer()
Definition: RouteDispatcherTest.php:185
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassFixture
Definition: RouteDispatcherClassFixture.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassWithoutInvokeFixture
Definition: RouteDispatcherClassWithoutInvokeFixture.php:22
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassInvokeFixture
Definition: RouteDispatcherClassInvokeFixture.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherStaticClassFixture
Definition: RouteDispatcherStaticClassFixture.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:34
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsStaticClassMethodCombinationGivenAsString
‪dispatchCallsStaticClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:285