‪TYPO3CMS  10.4
MiddlewareDispatcherTest.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 Psr\Container\ContainerInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Server\MiddlewareInterface;
24 use Psr\Http\Server\RequestHandlerInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪MiddlewareDispatcherTest extends UnitTestCase
35 {
40  {
41  $kernel = new class() implements RequestHandlerInterface {
42  public function handle(ServerRequestInterface $request): ResponseInterface
43  {
44  return (new ‪Response())->withStatus(204);
45  }
46  };
47 
48  $dispatcher = new ‪MiddlewareDispatcher($kernel);
49  $response = $dispatcher->handle(new ‪ServerRequest());
50 
51  self::assertSame(204, $response->getStatusCode());
52  }
53 
58  {
59  $kernel = new class() implements RequestHandlerInterface {
60  public function handle(ServerRequestInterface $request): ResponseInterface
61  {
62  return (new ‪Response())
63  ->withStatus(204)
64  ->withHeader('X-SEQ-PRE-REQ-HANDLER', $request->getHeader('X-SEQ-PRE-REQ-HANDLER'));
65  }
66  };
67 
68  $middleware1 = new class() implements MiddlewareInterface {
69  public $id = '0';
70 
71  public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
72  {
73  $request = $request->withAddedHeader('X-SEQ-PRE-REQ-HANDLER', $this->id);
74  $response = $handler->handle($request);
75 
76  return $response->withAddedHeader('X-SEQ-POST-REQ-HANDLER', $this->id);
77  }
78  };
79 
80  $middleware2 = clone $middleware1;
81  $middleware2->id = '1';
82 
84 
85  $middleware4 = clone $middleware1;
86  $middleware4->id = '3';
87 
88  $dispatcher = new ‪MiddlewareDispatcher($kernel, [$middleware1, $middleware2]);
89  $dispatcher->lazy(MiddlewareFixture::class);
90  $dispatcher->add($middleware4);
91 
92  $response = $dispatcher->handle(new ‪ServerRequest());
93 
94  self::assertSame(['3', '2', '1', '0'], $response->getHeader('X-SEQ-PRE-REQ-HANDLER'));
95  self::assertSame(['0', '1', '2', '3'], $response->getHeader('X-SEQ-POST-REQ-HANDLER'));
96  self::assertSame(204, $response->getStatusCode());
97  }
98 
103  {
104  $kernel = new class() implements RequestHandlerInterface {
105  public function handle(ServerRequestInterface $request): ResponseInterface
106  {
107  return new ‪Response();
108  }
109  };
110  $middleware = new class() implements MiddlewareInterface {
111  public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
112  {
113  return (new ‪Response())->withStatus(404);
114  }
115  };
116 
118  $dispatcher = new ‪MiddlewareDispatcher($kernel, [MiddlewareFixture::class, $middleware]);
119  $response = $dispatcher->handle(new ‪ServerRequest());
120 
122  self::assertSame(404, $response->getStatusCode());
123  }
124 
129  {
130  $this->expectException(\InvalidArgumentException::class);
131  $this->expectExceptionCode(1516821342);
132 
133  $kernel = new class() implements RequestHandlerInterface {
134  public function handle(ServerRequestInterface $request): ResponseInterface
135  {
136  return new ‪Response();
137  }
138  };
139 
141  $dispatcher = new ‪MiddlewareDispatcher($kernel);
142  $dispatcher->lazy(\stdClass::class);
143  $dispatcher->handle(new ‪ServerRequest());
144  }
145 
150  {
151  $kernel = new class() implements RequestHandlerInterface {
152  public function handle(ServerRequestInterface $request): ResponseInterface
153  {
154  return new ‪Response();
155  }
156  };
157  $middleware = new class() implements MiddlewareInterface {
158  public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
159  {
160  return (new ‪Response())->withStatus(204);
161  }
162  };
163 
164  $dispatcher = new ‪MiddlewareDispatcher($kernel);
165  $dispatcher->add($middleware);
166 
167  $response1 = $dispatcher->handle(new ‪ServerRequest());
168  $response2 = $dispatcher->handle(new ‪ServerRequest());
169 
170  self::assertSame(204, $response1->getStatusCode());
171  self::assertSame(204, $response2->getStatusCode());
172  }
173 
178  {
179  $kernel = new class() implements RequestHandlerInterface {
180  public function handle(ServerRequestInterface $request): ResponseInterface
181  {
182  return new ‪Response();
183  }
184  };
185 
186  $dispatcher = new ‪MiddlewareDispatcher($kernel);
187 
188  $dispatcher->add(new class($dispatcher) implements MiddlewareInterface {
189  private $dispatcher;
190 
191  public function __construct(RequestHandlerInterface $dispatcher)
192  {
193  $this->dispatcher = $dispatcher;
194  }
195 
196  public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
197  {
198  if ($request->hasHeader('X-NESTED')) {
199  return (new ‪Response())->withStatus(204)->withAddedHeader('X-TRACE', 'nested');
200  }
201 
202  $response = $this->dispatcher->handle($request->withAddedHeader('X-NESTED', '1'));
203 
204  return $response->withAddedHeader('X-TRACE', 'outer');
205  }
206  });
207 
208  $response = $dispatcher->handle(new ‪ServerRequest());
209 
210  self::assertSame(204, $response->getStatusCode());
211  self::assertSame(['nested', 'outer'], $response->getHeader('X-TRACE'));
212  }
213 
218  {
219  $kernel = new class() implements RequestHandlerInterface {
220  public function handle(ServerRequestInterface $request): ResponseInterface
221  {
222  return new ‪Response();
223  }
224  };
225 
226  $middleware = new class() implements MiddlewareInterface {
227  public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
228  {
229  return (new ‪Response())->withStatus(404);
230  }
231  };
232 
233  $containerProphecy = $this->prophesize();
234  $containerProphecy->willImplement(ContainerInterface::class);
235  $containerProphecy->has('somemiddlewarename')->willReturn(true);
236  $containerProphecy->get('somemiddlewarename')->willReturn($middleware);
237 
238  $dispatcher = new ‪MiddlewareDispatcher($kernel, ['somemiddlewarename'], $containerProphecy->reveal());
239  $response = $dispatcher->handle(new ‪ServerRequest());
240 
241  self::assertSame(404, $response->getStatusCode());
242  }
243 }
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\throwsExceptionForLazyNonMiddlewareInterfaceClasses
‪throwsExceptionForLazyNonMiddlewareInterfaceClasses()
Definition: MiddlewareDispatcherTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\fetchesMiddlewareFromContainer
‪fetchesMiddlewareFromContainer()
Definition: MiddlewareDispatcherTest.php:217
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\canBeExecutedMultipleTimes
‪canBeExecutedMultipleTimes()
Definition: MiddlewareDispatcherTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\canBeReExecutedRecursivelyDuringDispatch
‪canBeReExecutedRecursivelyDuringDispatch()
Definition: MiddlewareDispatcherTest.php:177
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher
Definition: MiddlewareDispatcher.php:35
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest
Definition: MiddlewareDispatcherTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Http\Fixtures\MiddlewareFixture
Definition: MiddlewareFixture.php:29
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\executesMiddlewaresLastInFirstOut
‪executesMiddlewaresLastInFirstOut()
Definition: MiddlewareDispatcherTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Http\Fixtures\MiddlewareFixture\$hasBeenInstantiated
‪static bool $hasBeenInstantiated
Definition: MiddlewareFixture.php:36
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\executesKernelWithEmptyMiddlewareStack
‪executesKernelWithEmptyMiddlewareStack()
Definition: MiddlewareDispatcherTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Http\Fixtures\MiddlewareFixture\$id
‪static string $id
Definition: MiddlewareFixture.php:32
‪TYPO3\CMS\Core\Tests\Unit\Http\MiddlewareDispatcherTest\doesNotInstantiateLazyMiddlewareInCaseOfAnEarlyReturningOuterMiddleware
‪doesNotInstantiateLazyMiddlewareInCaseOfAnEarlyReturningOuterMiddleware()
Definition: MiddlewareDispatcherTest.php:102