|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SharkMachine\Psr18Shark\Testing\Handler; |
| 6 | + |
| 7 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | +use SharkMachine\Psr18Shark\Handler\RequestMutationHandlerCollection; |
| 10 | +use SharkMachine\Psr18Shark\Handler\RequestMutationHandlerInterface; |
| 11 | +use SharkMachine\Psr18Shark\Testing\TestCase; |
| 12 | +use stdClass; |
| 13 | +use TypeError; |
| 14 | + |
| 15 | +final class RequestMutationHandlerCollectionTest extends TestCase |
| 16 | +{ |
| 17 | + public function testHandlers(): void |
| 18 | + { |
| 19 | + $handlerOne = new class implements RequestMutationHandlerInterface { |
| 20 | + public function handleRequest(RequestInterface $request): RequestInterface |
| 21 | + { |
| 22 | + return (new Psr17Factory())->createRequest('GET', 'https://example.com')->withHeader('X-Foo', 'bar'); |
| 23 | + } |
| 24 | + }; |
| 25 | + $handlerTwo = new class implements RequestMutationHandlerInterface { |
| 26 | + public function handleRequest(RequestInterface $request): RequestInterface |
| 27 | + { |
| 28 | + return (new Psr17Factory())->createRequest('GET', 'https://example.com')->withHeader('X-Foo', 'bar'); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + $handlerCollection = new RequestMutationHandlerCollection([$handlerOne, $handlerTwo]); |
| 33 | + foreach ($handlerCollection as $index => $handler) { |
| 34 | + if ($index === 0) { |
| 35 | + self::assertSame($handlerOne, $handler); |
| 36 | + } else { |
| 37 | + self::assertSame($handlerTwo, $handler); |
| 38 | + } |
| 39 | + $request = $handler->handleRequest((new Psr17Factory())->createRequest('GET', 'https://example.com')); |
| 40 | + self::assertSame('bar', $request->getHeaderLine('X-Foo')); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public function testIncorrectHandler(): void |
| 45 | + { |
| 46 | + try { |
| 47 | + new RequestMutationHandlerCollection([new stdClass()]); |
| 48 | + self::fail('Expected TypeError'); |
| 49 | + } catch (TypeError $e) { |
| 50 | + self::assertStringContainsString('must be of type ' . RequestMutationHandlerInterface::class . ', stdClass given', $e->getMessage()); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments