|
2 | 2 |
|
3 | 3 | namespace FrameworkX\Tests; |
4 | 4 |
|
| 5 | +use FrameworkX\AccessLogHandler; |
5 | 6 | use FrameworkX\App; |
| 7 | +use FrameworkX\FiberHandler; |
6 | 8 | use FrameworkX\RouteHandler; |
7 | 9 | use PHPUnit\Framework\TestCase; |
8 | 10 | use Psr\Http\Message\ResponseInterface; |
@@ -169,7 +171,7 @@ public function testMapMethodWithMiddlewareAddsGivenMethodsOnRouter() |
169 | 171 |
|
170 | 172 | public function testMiddlewareCallsNextReturnsResponseFromRouter() |
171 | 173 | { |
172 | | - $app = $this->createAppWithoutLogger(); |
| 174 | + $app = $this->createAppWithoutFibersOrLogger(); |
173 | 175 |
|
174 | 176 | $middleware = function (ServerRequestInterface $request, callable $next) { |
175 | 177 | return $next($request); |
@@ -203,7 +205,7 @@ public function testMiddlewareCallsNextReturnsResponseFromRouter() |
203 | 205 |
|
204 | 206 | public function testMiddlewareCallsNextWithModifiedRequestReturnsResponseFromRouter() |
205 | 207 | { |
206 | | - $app = $this->createAppWithoutLogger(); |
| 208 | + $app = $this->createAppWithoutFibersOrLogger(); |
207 | 209 |
|
208 | 210 | $middleware = function (ServerRequestInterface $request, callable $next) { |
209 | 211 | return $next($request->withAttribute('name', 'Alice')); |
@@ -237,7 +239,7 @@ public function testMiddlewareCallsNextWithModifiedRequestReturnsResponseFromRou |
237 | 239 |
|
238 | 240 | public function testMiddlewareCallsNextReturnsResponseModifiedInMiddlewareFromRouter() |
239 | 241 | { |
240 | | - $app = $this->createAppWithoutLogger(); |
| 242 | + $app = $this->createAppWithoutFibersOrLogger(); |
241 | 243 |
|
242 | 244 | $middleware = function (ServerRequestInterface $request, callable $next) { |
243 | 245 | $response = $next($request); |
@@ -364,7 +366,7 @@ public function testMiddlewareCallsNextReturnsCoroutineResponseModifiedInMiddlew |
364 | 366 |
|
365 | 367 | public function testMiddlewareCallsNextWhichThrowsExceptionReturnsInternalServerErrorResponse() |
366 | 368 | { |
367 | | - $app = $this->createAppWithoutLogger(); |
| 369 | + $app = $this->createAppWithoutFibersOrLogger(); |
368 | 370 |
|
369 | 371 | $middleware = function (ServerRequestInterface $request, callable $next) { |
370 | 372 | return $next($request); |
@@ -395,7 +397,7 @@ public function testMiddlewareCallsNextWhichThrowsExceptionReturnsInternalServer |
395 | 397 |
|
396 | 398 | public function testMiddlewareWhichThrowsExceptionReturnsInternalServerErrorResponse() |
397 | 399 | { |
398 | | - $app = $this->createAppWithoutLogger(); |
| 400 | + $app = $this->createAppWithoutFibersOrLogger(); |
399 | 401 |
|
400 | 402 | $line = __LINE__ + 2; |
401 | 403 | $middleware = function (ServerRequestInterface $request, callable $next) { |
@@ -424,7 +426,7 @@ public function testMiddlewareWhichThrowsExceptionReturnsInternalServerErrorResp |
424 | 426 |
|
425 | 427 | public function testGlobalMiddlewareCallsNextReturnsResponseFromController() |
426 | 428 | { |
427 | | - $app = $this->createAppWithoutLogger(function (ServerRequestInterface $request, callable $next) { |
| 429 | + $app = $this->createAppWithoutFibersOrLogger(function (ServerRequestInterface $request, callable $next) { |
428 | 430 | return $next($request); |
429 | 431 | }); |
430 | 432 |
|
@@ -461,7 +463,7 @@ public function __invoke(ServerRequestInterface $request, callable $next) |
461 | 463 | } |
462 | 464 | }; |
463 | 465 |
|
464 | | - $app = $this->createAppWithoutLogger($middleware); |
| 466 | + $app = $this->createAppWithoutFibersOrLogger($middleware); |
465 | 467 |
|
466 | 468 | $app->get('/', function () { |
467 | 469 | return new Response( |
@@ -496,7 +498,7 @@ public function __invoke(ServerRequestInterface $request, callable $next) |
496 | 498 | } |
497 | 499 | }; |
498 | 500 |
|
499 | | - $app = $this->createAppWithoutLogger(get_class($middleware)); |
| 501 | + $app = $this->createAppWithoutFibersOrLogger(get_class($middleware)); |
500 | 502 |
|
501 | 503 | $app->get('/', function () { |
502 | 504 | return new Response( |
@@ -532,7 +534,7 @@ public function __invoke(ServerRequestInterface $request, callable $next) |
532 | 534 | } |
533 | 535 | }; |
534 | 536 |
|
535 | | - $app = $this->createAppWithoutLogger(get_class($middleware)); |
| 537 | + $app = $this->createAppWithoutFibersOrLogger(get_class($middleware)); |
536 | 538 |
|
537 | 539 | $app->get('/', get_class($middleware), function (ServerRequestInterface $request) { |
538 | 540 | return new Response( |
@@ -560,7 +562,7 @@ public function __invoke(ServerRequestInterface $request, callable $next) |
560 | 562 |
|
561 | 563 | public function testGlobalMiddlewareCallsNextWithModifiedRequestWillBeUsedForRouting() |
562 | 564 | { |
563 | | - $app = $this->createAppWithoutLogger(function (ServerRequestInterface $request, callable $next) { |
| 565 | + $app = $this->createAppWithoutFibersOrLogger(function (ServerRequestInterface $request, callable $next) { |
564 | 566 | return $next($request->withUri($request->getUri()->withPath('/users'))); |
565 | 567 | }); |
566 | 568 |
|
@@ -590,7 +592,7 @@ public function testGlobalMiddlewareCallsNextWithModifiedRequestWillBeUsedForRou |
590 | 592 |
|
591 | 593 | public function testGlobalMiddlewareCallsNextReturnsModifiedResponseWhenModifyingResponseFromRouter() |
592 | 594 | { |
593 | | - $app = $this->createAppWithoutLogger(function (ServerRequestInterface $request, callable $next) { |
| 595 | + $app = $this->createAppWithoutFibersOrLogger(function (ServerRequestInterface $request, callable $next) { |
594 | 596 | $response = $next($request); |
595 | 597 | assert($response instanceof ResponseInterface); |
596 | 598 |
|
@@ -621,7 +623,7 @@ public function testGlobalMiddlewareCallsNextReturnsModifiedResponseWhenModifyin |
621 | 623 |
|
622 | 624 | public function testGlobalMiddlewareReturnsResponseWithoutCallingNextReturnsResponseWithoutCallingRouter() |
623 | 625 | { |
624 | | - $app = $this->createAppWithoutLogger(function () { |
| 626 | + $app = $this->createAppWithoutFibersOrLogger(function () { |
625 | 627 | return new Response( |
626 | 628 | 200, |
627 | 629 | [ |
@@ -788,8 +790,46 @@ private function createAppWithoutLogger(...$middleware): App |
788 | 790 | $ref->setAccessible(true); |
789 | 791 | $handlers = $ref->getValue($middleware); |
790 | 792 |
|
791 | | - unset($handlers[0]); |
792 | | - $ref->setValue($middleware, array_values($handlers)); |
| 793 | + if (PHP_VERSION_ID >= 80100) { |
| 794 | + $first = array_shift($handlers); |
| 795 | + $this->assertInstanceOf(FiberHandler::class, $first); |
| 796 | + |
| 797 | + $next = array_shift($handlers); |
| 798 | + $this->assertInstanceOf(AccessLogHandler::class, $next); |
| 799 | + |
| 800 | + array_unshift($handlers, $next, $first); |
| 801 | + } |
| 802 | + |
| 803 | + $first = array_shift($handlers); |
| 804 | + $this->assertInstanceOf(AccessLogHandler::class, $first); |
| 805 | + |
| 806 | + $ref->setValue($middleware, $handlers); |
| 807 | + |
| 808 | + return $app; |
| 809 | + } |
| 810 | + |
| 811 | + /** @param callable|class-string ...$middleware */ |
| 812 | + private function createAppWithoutFibersOrLogger(...$middleware): App |
| 813 | + { |
| 814 | + $app = new App(...$middleware); |
| 815 | + |
| 816 | + $ref = new \ReflectionProperty($app, 'handler'); |
| 817 | + $ref->setAccessible(true); |
| 818 | + $middleware = $ref->getValue($app); |
| 819 | + |
| 820 | + $ref = new \ReflectionProperty($middleware, 'handlers'); |
| 821 | + $ref->setAccessible(true); |
| 822 | + $handlers = $ref->getValue($middleware); |
| 823 | + |
| 824 | + if (PHP_VERSION_ID >= 80100) { |
| 825 | + $first = array_shift($handlers); |
| 826 | + $this->assertInstanceOf(FiberHandler::class, $first); |
| 827 | + } |
| 828 | + |
| 829 | + $first = array_shift($handlers); |
| 830 | + $this->assertInstanceOf(AccessLogHandler::class, $first); |
| 831 | + |
| 832 | + $ref->setValue($middleware, $handlers); |
793 | 833 |
|
794 | 834 | return $app; |
795 | 835 | } |
|
0 commit comments