‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\Test;
21 use Psr\Container\ContainerInterface;
22 use Psr\Http\Message\ServerRequestInterface;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
37 final class ‪RouteDispatcherTest extends UnitTestCase
38 {
39  public function ‪tearDown(): void
40  {
41  GeneralUtility::purgeInstances();
42  parent::tearDown();
43  }
44 
45  #[Test]
47  {
48  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
49  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
50  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
51  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
52  $accessFactoryMock = $this->createMock(AccessFactory::class);
53  $accessStorageMock = $this->createMock(AccessStorage::class);
54 
55  $containerMock = $this->createMock(ContainerInterface::class);
56  $containerMock->method('has')->with(self::anything())->willReturn(false);
57 
58  $this->expectException(\InvalidArgumentException::class);
59  $this->expectExceptionCode(1425381442);
60 
61  $route = new ‪Route('not important', ['access' => 'public', 'target' => 42]);
62  $request = (new ‪ServerRequest())->withAttribute('route', $route);
63 
64  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
65  $subject->dispatch($request);
66  }
67 
68  #[Test]
69  public function ‪dispatchCallsTargetIfTargetIsArray(): void
70  {
71  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
72  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
73  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
74  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
75  $accessFactoryMock = $this->createMock(AccessFactory::class);
76  $accessStorageMock = $this->createMock(AccessStorage::class);
77 
78  $containerMock = $this->createMock(ContainerInterface::class);
79  $containerMock->method('has')->with(self::anything())->willReturn(false);
80 
81  $this->expectException(\RuntimeException::class);
82  $this->expectExceptionCode(1520756142);
83 
84  $target = [
86  'mainAction',
87  ];
88  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
89  $request = (new ‪ServerRequest())->withAttribute('route', $route);
90 
91  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
92  $subject->dispatch($request);
93  }
94 
95  #[Test]
97  {
98  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
99  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
100  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
101  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
102  $accessFactoryMock = $this->createMock(AccessFactory::class);
103  $accessStorageMock = $this->createMock(AccessStorage::class);
104 
105  $containerMock = $this->createMock(ContainerInterface::class);
106  $containerMock->method('has')->with(self::anything())->willReturn(false);
107 
108  $this->expectException(\RuntimeException::class);
109  $this->expectExceptionCode(1520756466);
110 
111  $target = static function (ServerRequestInterface $request) {
112  throw new \RuntimeException('I have been called. Good!', 1520756466);
113  };
114  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
115  $request = (new ‪ServerRequest())->withAttribute('route', $route);
116 
117  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
118  $subject->dispatch($request);
119  }
120 
121  #[Test]
123  {
124  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
125  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
126  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
127  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
128  $accessFactoryMock = $this->createMock(AccessFactory::class);
129  $accessStorageMock = $this->createMock(AccessStorage::class);
130 
131  $containerMock = $this->createMock(ContainerInterface::class);
132  $containerMock->method('has')->with(self::anything())->willReturn(false);
133 
134  $this->expectException(\RuntimeException::class);
135  $this->expectExceptionCode(1520756623);
136 
137  $target = RouteDispatcherClassInvokeFixture::class;
138  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
139  $request = (new ‪ServerRequest())->withAttribute('route', $route);
140 
141  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
142  $subject->dispatch($request);
143  }
144 
145  #[Test]
147  {
148  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
149  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
150  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
151  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
152  $accessFactoryMock = $this->createMock(AccessFactory::class);
153  $accessStorageMock = $this->createMock(AccessStorage::class);
154 
155  $target = 'routedispatcher.classinvokefixture';
156  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
157  $request = (new ‪ServerRequest())->withAttribute('route', $route);
158 
159  $containerMock = $this->createMock(ContainerInterface::class);
160  $containerMock->method('has')->with($target)->willReturn(true);
161  $containerMock->method('get')->with($target)->willReturn(new ‪RouteDispatcherClassInvokeFixture());
162 
163  $this->expectException(\RuntimeException::class);
164  $this->expectExceptionCode(1520756623);
165 
166  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
167  $subject->dispatch($request);
168  }
169 
170  #[Test]
172  {
173  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
174  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
175  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
176  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
177  $accessFactoryMock = $this->createMock(AccessFactory::class);
178  $accessStorageMock = $this->createMock(AccessStorage::class);
179 
180  $containerMock = $this->createMock(ContainerInterface::class);
181  $containerMock->method('has')->with(self::anything())->willReturn(false);
182 
183  $this->expectException(\InvalidArgumentException::class);
184  $this->expectExceptionCode(1442431631);
185 
186  $target = RouteDispatcherClassWithoutInvokeFixture::class;
187  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
188  $request = (new ‪ServerRequest())->withAttribute('route', $route);
189 
190  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
191  $subject->dispatch($request);
192  }
193 
194  #[Test]
196  {
197  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
198  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
199  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
200  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
201  $accessFactoryMock = $this->createMock(AccessFactory::class);
202  $accessStorageMock = $this->createMock(AccessStorage::class);
203 
204  $containerMock = $this->createMock(ContainerInterface::class);
205  $containerMock->method('has')->with(self::anything())->willReturn(false);
206 
207  $this->expectException(\RuntimeException::class);
208  $this->expectExceptionCode(1520756142);
209 
210  $target = RouteDispatcherClassFixture::class . '::mainAction';
211  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
212  $request = (new ‪ServerRequest())->withAttribute('route', $route);
213 
214  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
215  $subject->dispatch($request);
216  }
217 
218  #[Test]
220  {
221  $formProtectionMock = $this->createMock(AbstractFormProtection::class);
222  $formProtectionMock->method('validateToken')->with(self::anything())->willReturn(true);
223  $formProtectionFactory = $this->createMock(FormProtectionFactory::class);
224  $formProtectionFactory->method('createFromRequest')->willReturn($formProtectionMock);
225  $accessFactoryMock = $this->createMock(AccessFactory::class);
226  $accessStorageMock = $this->createMock(AccessStorage::class);
227 
228  $containerMock = $this->createMock(ContainerInterface::class);
229  $containerMock->method('has')->with(self::anything())->willReturn(false);
230 
231  $this->expectException(\RuntimeException::class);
232  $this->expectExceptionCode(1520757000);
233 
234  $target = RouteDispatcherStaticClassFixture::class . '::mainAction';
235  $route = new ‪Route('not important', ['access' => 'public', 'target' => $target]);
236  $request = (new ‪ServerRequest())->withAttribute('route', $route);
237 
238  $subject = new ‪RouteDispatcher($formProtectionFactory, $accessFactoryMock, $accessStorageMock, $containerMock);
239  $subject->dispatch($request);
240  }
241 }
‪TYPO3\CMS\Backend\Http\RouteDispatcher
Definition: RouteDispatcher.php:41
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClassImplementingInvoke
‪dispatchCallsTargetIfTargetIsClassImplementingInvoke()
Definition: RouteDispatcherTest.php:122
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetIsNotCallable
‪dispatchThrowsExceptionIfTargetIsNotCallable()
Definition: RouteDispatcherTest.php:46
‪TYPO3\CMS\Backend\Security\SudoMode\Access\AccessStorage
Definition: AccessStorage.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke
‪dispatchThrowsExceptionIfTargetWithClassNameOnlyDoesNotImplementInvoke()
Definition: RouteDispatcherTest.php:171
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsClassMethodCombinationGivenAsString
‪dispatchCallsClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:195
‪TYPO3\CMS\Backend\Security\SudoMode\Access\AccessFactory
Definition: AccessFactory.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsClosure
‪dispatchCallsTargetIfTargetIsClosure()
Definition: RouteDispatcherTest.php:96
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest
Definition: RouteDispatcherTest.php:38
‪TYPO3\CMS\Backend\Tests\Unit\Http
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsArray
‪dispatchCallsTargetIfTargetIsArray()
Definition: RouteDispatcherTest.php:69
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\tearDown
‪tearDown()
Definition: RouteDispatcherTest.php:39
‪TYPO3\CMS\Core\FormProtection\AbstractFormProtection
Definition: AbstractFormProtection.php:31
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:43
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsTargetIfTargetIsInContainer
‪dispatchCallsTargetIfTargetIsInContainer()
Definition: RouteDispatcherTest.php:146
‪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:52
‪TYPO3\CMS\Backend\Tests\Unit\Http\RouteDispatcherTest\dispatchCallsStaticClassMethodCombinationGivenAsString
‪dispatchCallsStaticClassMethodCombinationGivenAsString()
Definition: RouteDispatcherTest.php:219