‪TYPO3CMS  9.5
RouteDispatcherTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Prophecy\Argument;
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪RouteDispatcherTest extends UnitTestCase
36 {
37  public function ‪tearDown()
38  {
40  GeneralUtility::purgeInstances();
41  parent::tearDown();
42  }
43 
48  {
49  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
50  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
51  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
52 
53  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
54  $responseProphecy = $this->prophesize(ResponseInterface::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 
65  $target = 42;
66  $routeProphecy->getOption('target')->willReturn($target);
67  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
68 
69  $this->expectException(\InvalidArgumentException::class);
70  $this->expectExceptionCode(1425381442);
71 
72  $subject = new ‪RouteDispatcher();
73  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
74  }
75 
80  {
81  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
82  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
83  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
84 
85  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
86  $responseProphecy = $this->prophesize(ResponseInterface::class);
87  $routerProphecy = $this->prophesize(Router::class);
88  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
89  $routeProphecy = $this->prophesize(Route::class);
90  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
91  $routeProphecy->getOption('access')->willReturn('public');
92  $routeProphecy->getOption('referrer')->willReturn(false);
93  $routeProphecy->getOption('module')->willReturn(false);
94  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
95  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
96 
97  $target = RouteDispatcherClassFixture::class . '::mainAction';
98  $routeProphecy->getOption('target')->willReturn($target);
99  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
100 
101  $this->expectException(\RuntimeException::class);
102  $this->expectExceptionCode(1520756142);
103 
104  $subject = new ‪RouteDispatcher();
105  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
106  }
107 
112  {
113  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
114  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
115  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
116 
117  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
118  $responseProphecy = $this->prophesize(ResponseInterface::class);
119  $routerProphecy = $this->prophesize(Router::class);
120  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
121  $routeProphecy = $this->prophesize(Route::class);
122  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
123  $routeProphecy->getOption('access')->willReturn('public');
124  $routeProphecy->getOption('referrer')->willReturn(false);
125  $routeProphecy->getOption('module')->willReturn(false);
126  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
127  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
128 
129  $target = static function (ServerRequestInterface $request) {
130  throw new \RuntimeException('I have been called. Good!', 1520756466);
131  };
132  $routeProphecy->getOption('target')->willReturn($target);
133  $requestProphecy->withAttribute('target', Argument::type(\Closure::class))->willReturn($requestProphecy->reveal());
134 
135  $this->expectException(\RuntimeException::class);
136  $this->expectExceptionCode(1520756466);
137 
138  $subject = new ‪RouteDispatcher();
139  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
140  }
141 
146  {
147  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
148  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
149  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
150 
151  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
152  $responseProphecy = $this->prophesize(ResponseInterface::class);
153  $routerProphecy = $this->prophesize(Router::class);
154  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
155  $routeProphecy = $this->prophesize(Route::class);
156  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
157  $routeProphecy->getOption('access')->willReturn('public');
158  $routeProphecy->getOption('referrer')->willReturn(false);
159  $routeProphecy->getOption('module')->willReturn(false);
160  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
161  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
162 
163  $target = RouteDispatcherClassInvokeFixture::class;
164  $routeProphecy->getOption('target')->willReturn($target);
165  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
166 
167  $this->expectException(\RuntimeException::class);
168  $this->expectExceptionCode(1520756623);
169 
170  $subject = new ‪RouteDispatcher();
171  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
172  }
173 
178  {
179  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
180  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
181  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
182 
183  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
184  $responseProphecy = $this->prophesize(ResponseInterface::class);
185  $routerProphecy = $this->prophesize(Router::class);
186  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
187  $routeProphecy = $this->prophesize(Route::class);
188  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
189  $routeProphecy->getOption('access')->willReturn('public');
190  $routeProphecy->getOption('referrer')->willReturn(false);
191  $routeProphecy->getOption('module')->willReturn(false);
192  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
193  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
194 
195  $target = RouteDispatcherClassWithoutInvokeFixture::class;
196  $routeProphecy->getOption('target')->willReturn($target);
197  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
198 
199  $this->expectException(\InvalidArgumentException::class);
200  $this->expectExceptionCode(1442431631);
201 
202  $subject = new ‪RouteDispatcher();
203  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
204  }
205 
210  {
211  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
212  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
213  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
214 
215  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
216  $responseProphecy = $this->prophesize(ResponseInterface::class);
217  $routerProphecy = $this->prophesize(Router::class);
218  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
219  $routeProphecy = $this->prophesize(Route::class);
220  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
221  $routeProphecy->getOption('access')->willReturn('public');
222  $routeProphecy->getOption('referrer')->willReturn(false);
223  $routeProphecy->getOption('module')->willReturn(false);
224  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
225  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
226 
227  $target = RouteDispatcherClassFixture::class . '::mainAction';
228  $routeProphecy->getOption('target')->willReturn($target);
229  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
230 
231  $this->expectException(\RuntimeException::class);
232  $this->expectExceptionCode(1520756142);
233 
234  $subject = new ‪RouteDispatcher();
235  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
236  }
237 
242  {
243  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
244  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
245  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
246 
247  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
248  $responseProphecy = $this->prophesize(ResponseInterface::class);
249  $routerProphecy = $this->prophesize(Router::class);
250  GeneralUtility::setSingletonInstance(Router::class, $routerProphecy->reveal());
251  $routeProphecy = $this->prophesize(Route::class);
252  $routerProphecy->matchRequest($requestProphecy->reveal())->willReturn($routeProphecy->reveal());
253  $routeProphecy->getOption('access')->willReturn('public');
254  $routeProphecy->getOption('referrer')->willReturn(false);
255  $routeProphecy->getOption('module')->willReturn(false);
256  $requestProphecy->withAttribute('route', $routeProphecy->reveal())->willReturn($requestProphecy->reveal());
257  $requestProphecy->getAttribute('route')->willReturn($routeProphecy->reveal());
258 
259  $target = RouteDispatcherStaticClassFixture::class . '::mainAction';
260  $routeProphecy->getOption('target')->willReturn($target);
261  $requestProphecy->withAttribute('target', $target)->willReturn($requestProphecy->reveal());
262 
263  $this->expectException(\RuntimeException::class);
264  $this->expectExceptionCode(1520757000);
265 
266  $subject = new ‪RouteDispatcher();
267  $subject->dispatch($requestProphecy->reveal(), $responseProphecy->reveal());
268  }
269 }
‪TYPO3\CMS\Backend\Http\RouteDispatcher
Definition: RouteDispatcher.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClassImplementingInvoke
‪dispatchCallsTargetIfTargetIsClassImplementingInvoke()
Definition: RouteDispatcherTest.php:145
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetIsNotCallable
‪dispatchThrowsExceptionIfTargetIsNotCallable()
Definition: RouteDispatcherTest.php:47
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\set
‪static set($classNameOrType, AbstractFormProtection $instance)
Definition: FormProtectionFactory.php:207
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\purgeInstances
‪static purgeInstances()
Definition: FormProtectionFactory.php:217
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke
‪dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke()
Definition: RouteDispatcherTest.php:177
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:23
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsClassMethodCombinationGivenAsString
‪dispatchCallsClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:209
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClosure
‪dispatchCallsTargetIfTargetIsClosure()
Definition: RouteDispatcherTest.php:111
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest
Definition: RouteDispatcherTest.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Http
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsArray
‪dispatchCallsTargetIfTargetIsArray()
Definition: RouteDispatcherTest.php:79
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\tearDown
‪tearDown()
Definition: RouteDispatcherTest.php:37
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection
Definition: AbstractFormProtection.php:29
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:45
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassFixture
Definition: RouteDispatcherClassFixture.php:23
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassWithoutInvokeFixture
Definition: RouteDispatcherClassWithoutInvokeFixture.php:21
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassInvokeFixture
Definition: RouteDispatcherClassInvokeFixture.php:23
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherStaticClassFixture
Definition: RouteDispatcherStaticClassFixture.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsStaticClassMethodCombinationGivenAsString
‪dispatchCallsStaticClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:241