‪TYPO3CMS  11.5
RouteDispatcherTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Psr\Container\ContainerInterface;
23 use Psr\Http\Message\ServerRequestInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
39 class ‪RouteDispatcherTest extends UnitTestCase
40 {
41  use ProphecyTrait;
42 
43  public function ‪tearDown(): void
44  {
46  GeneralUtility::purgeInstances();
47  parent::tearDown();
48  }
49 
54  {
55  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
56  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
57  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
58 
59  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
60  $route = new ‪Route('not important', ['access' => 'public', 'referrer' => false, 'target' => 42]);
61  $requestProphecy->getAttribute('route')->willReturn($route);
62  $containerProphecy = $this->prophesize(ContainerInterface::class);
63  $containerProphecy->has(Argument::any())->willReturn(false);
64  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
65  $this->expectException(\InvalidArgumentException::class);
66  $this->expectExceptionCode(1425381442);
67 
68  $subject = new ‪RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
69  $subject->dispatch($requestProphecy->reveal());
70  }
71 
75  public function ‪dispatchCallsTargetIfTargetIsArray(): void
76  {
77  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
78  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
79  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
80  $target = [
81  new RouteDispatcherClassFixture(),
82  'mainAction',
83  ];
84  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
85  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
86  $requestProphecy->getAttribute('route')->willReturn($route);
87  $containerProphecy = $this->prophesize(ContainerInterface::class);
88  $containerProphecy->has(Argument::any())->willReturn(false);
89  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
90 
91  $this->expectException(\RuntimeException::class);
92  $this->expectExceptionCode(1520756142);
93 
94  $subject = new ‪RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
95  $subject->dispatch($requestProphecy->reveal());
96  }
97 
101  public function ‪dispatchCallsTargetIfTargetIsClosure(): void
102  {
103  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
104  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
105  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
106 
107  $target = static function (ServerRequestInterface $request) {
108  throw new \RuntimeException('I have been called. Good!', 1520756466);
109  };
110  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
111  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
112  $requestProphecy->getAttribute('route')->willReturn($route);
113  $containerProphecy = $this->prophesize(ContainerInterface::class);
114  $containerProphecy->has(Argument::any())->willReturn(false);
115  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
116 
117  $this->expectException(\RuntimeException::class);
118  $this->expectExceptionCode(1520756466);
119 
120  $subject = new ‪RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
121  $subject->dispatch($requestProphecy->reveal());
122  }
123 
128  {
129  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
130  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
131  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
132  $target = RouteDispatcherClassInvokeFixture::class;
133  $route = new Route('not important', ['access' => 'public', 'target' => $target]);
134 
135  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
136  $requestProphecy->getAttribute('route')->willReturn($route);
137  $containerProphecy = $this->prophesize(ContainerInterface::class);
138  $containerProphecy->has(Argument::any())->willReturn(false);
139  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
140 
141  $this->expectException(\RuntimeException::class);
142  $this->expectExceptionCode(1520756623);
143 
144  $subject = new RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
145  $subject->dispatch($requestProphecy->reveal());
146  }
147 
151  public function ‪dispatchCallsTargetIfTargetIsInContainer(): void
152  {
153  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
154  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
155  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
156 
157  $target = 'routedispatcher.classinvokefixture';
158  $route = new Route('not important', ['access' => 'public', 'target' => $target]);
159  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
160  $requestProphecy->getAttribute('route')->willReturn($route);
161  $containerProphecy = $this->prophesize(ContainerInterface::class);
162  $containerProphecy->has($target)->willReturn(true);
163  $containerProphecy->get($target)->willReturn(new RouteDispatcherClassInvokeFixture());
164  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
165 
166  $this->expectException(\RuntimeException::class);
167  $this->expectExceptionCode(1520756623);
168 
169  $subject = new RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
170  $subject->dispatch($requestProphecy->reveal());
171  }
172 
177  {
178  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
179  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
180  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
181 
182  $containerProphecy = $this->prophesize(ContainerInterface::class);
183  $containerProphecy->has(Argument::any())->willReturn(false);
184 
185  $target = RouteDispatcherClassWithoutInvokeFixture::class;
186  $route = new Route('not important', ['access' => 'public', 'target' => $target]);
187  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
188  $requestProphecy->getAttribute('route')->willReturn($route);
189  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
190 
191  $this->expectException(\InvalidArgumentException::class);
192  $this->expectExceptionCode(1442431631);
193 
194  $subject = new RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
195  $subject->dispatch($requestProphecy->reveal());
196  }
197 
202  {
203  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
204  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
205  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
206 
207  $target = RouteDispatcherClassFixture::class . '::mainAction';
208  $route = new Route('not important', ['access' => 'public', 'target' => $target]);
209  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
210  $requestProphecy->getAttribute('route')->willReturn($route);
211  $containerProphecy = $this->prophesize(ContainerInterface::class);
212  $containerProphecy->has(Argument::any())->willReturn(false);
213  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
214 
215  $this->expectException(\RuntimeException::class);
216  $this->expectExceptionCode(1520756142);
217 
218  $subject = new RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
219  $subject->dispatch($requestProphecy->reveal());
220  }
221 
226  {
227  $formProtectionProphecy = $this->prophesize(AbstractFormProtection::class);
228  $formProtectionProphecy->validateToken(Argument::cetera())->willReturn(true);
229  ‪FormProtectionFactory::set('default', $formProtectionProphecy->reveal());
230 
231  $target = RouteDispatcherStaticClassFixture::class . '::mainAction';
232  $route = new Route('not important', ['access' => 'public', 'target' => $target]);
233  $requestProphecy = $this->prophesize(ServerRequestInterface::class);
234  $requestProphecy->getAttribute('route')->willReturn($route);
235  $containerProphecy = $this->prophesize(ContainerInterface::class);
236  $containerProphecy->has(Argument::any())->willReturn(false);
237  $uriBuilderProphecy = $this->prophesize(UriBuilder::class);
238 
239  $this->expectException(\RuntimeException::class);
240  $this->expectExceptionCode(1520757000);
241 
242  $subject = new RouteDispatcher($containerProphecy->reveal(), $uriBuilderProphecy->reveal());
243  $subject->dispatch($requestProphecy->reveal());
244  }
245 }
‪TYPO3\CMS\Backend\Http\RouteDispatcher
Definition: RouteDispatcher.php:41
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClassImplementingInvoke
‪dispatchCallsTargetIfTargetIsClassImplementingInvoke()
Definition: RouteDispatcherTest.php:126
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetIsNotCallable
‪dispatchThrowsExceptionIfTargetIsNotCallable()
Definition: RouteDispatcherTest.php:52
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\set
‪static set($classNameOrType, AbstractFormProtection $instance)
Definition: FormProtectionFactory.php:221
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\purgeInstances
‪static purgeInstances()
Definition: FormProtectionFactory.php:231
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke
‪dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke()
Definition: RouteDispatcherTest.php:175
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsClassMethodCombinationGivenAsString
‪dispatchCallsClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:200
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClosure
‪dispatchCallsTargetIfTargetIsClosure()
Definition: RouteDispatcherTest.php:100
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest
Definition: RouteDispatcherTest.php:40
‪TYPO3\CMS\Backend\Tests\Unit\Http
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsArray
‪dispatchCallsTargetIfTargetIsArray()
Definition: RouteDispatcherTest.php:74
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\tearDown
‪tearDown()
Definition: RouteDispatcherTest.php:42
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection
Definition: AbstractFormProtection.php:30
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:48
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsInContainer
‪dispatchCallsTargetIfTargetIsInContainer()
Definition: RouteDispatcherTest.php:150
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassFixture
Definition: RouteDispatcherClassFixture.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassWithoutInvokeFixture
Definition: RouteDispatcherClassWithoutInvokeFixture.php:23
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherClassInvokeFixture
Definition: RouteDispatcherClassInvokeFixture.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Http\Fixtures\RouteDispatcherStaticClassFixture
Definition: RouteDispatcherStaticClassFixture.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsStaticClassMethodCombinationGivenAsString
‪dispatchCallsStaticClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:224