|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Http\Client\Common; |
| 4 | + |
| 5 | +use Http\Client\Common\HttpMethodsClient; |
| 6 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 7 | +use Nyholm\Psr7\Response; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Psr\Http\Client\ClientInterface; |
| 10 | +use Psr\Http\Message\RequestInterface; |
| 11 | + |
| 12 | +class HttpMethodsClientTest extends TestCase |
| 13 | +{ |
| 14 | + private const URI = '/uri'; |
| 15 | + private const HEADER_NAME = 'Content-Type'; |
| 16 | + private const HEADER_VALUE = 'text/plain'; |
| 17 | + private const BODY = 'body'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var ClientInterface |
| 21 | + */ |
| 22 | + private $httpClient; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var HttpMethodsClient |
| 26 | + */ |
| 27 | + private $httpMethodsClient; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $this->httpClient = $this->createMock(ClientInterface::class); |
| 32 | + $streamFactory = $requestFactory = new Psr17Factory(); |
| 33 | + $this->httpMethodsClient = new HttpMethodsClient($this->httpClient, $requestFactory, $streamFactory); |
| 34 | + } |
| 35 | + |
| 36 | + public function testGet(): void |
| 37 | + { |
| 38 | + $this->expectSendRequest('get'); |
| 39 | + } |
| 40 | + |
| 41 | + public function testHead(): void |
| 42 | + { |
| 43 | + $this->expectSendRequest('head'); |
| 44 | + } |
| 45 | + |
| 46 | + public function testTrace(): void |
| 47 | + { |
| 48 | + $this->expectSendRequest('trace'); |
| 49 | + } |
| 50 | + |
| 51 | + public function testPost(): void |
| 52 | + { |
| 53 | + $this->expectSendRequest('post', self::BODY); |
| 54 | + } |
| 55 | + |
| 56 | + public function testPut(): void |
| 57 | + { |
| 58 | + $this->expectSendRequest('put', self::BODY); |
| 59 | + } |
| 60 | + |
| 61 | + public function testPatch(): void |
| 62 | + { |
| 63 | + $this->expectSendRequest('patch', self::BODY); |
| 64 | + } |
| 65 | + |
| 66 | + public function testDelete(): void |
| 67 | + { |
| 68 | + $this->expectSendRequest('delete', self::BODY); |
| 69 | + } |
| 70 | + |
| 71 | + public function testOptions(): void |
| 72 | + { |
| 73 | + $this->expectSendRequest('options', self::BODY); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Run the actual test. |
| 78 | + * |
| 79 | + * As there is no data provider in phpspec, we keep separate methods to get new mocks for each test. |
| 80 | + */ |
| 81 | + private function expectSendRequest(string $method, string $body = null): void |
| 82 | + { |
| 83 | + $response = new Response(); |
| 84 | + $this->httpClient->expects($this->once()) |
| 85 | + ->method('sendRequest') |
| 86 | + ->with(self::callback(static function (RequestInterface $request) use ($body, $method): bool { |
| 87 | + self::assertSame(strtoupper($method), $request->getMethod()); |
| 88 | + self::assertSame(self::URI, (string) $request->getUri()); |
| 89 | + self::assertSame([self::HEADER_NAME => [self::HEADER_VALUE]], $request->getHeaders()); |
| 90 | + self::assertSame((string) $body, (string) $request->getBody()); |
| 91 | + |
| 92 | + return true; |
| 93 | + })) |
| 94 | + ->willReturn($response) |
| 95 | + ; |
| 96 | + |
| 97 | + $actualResponse = $this->httpMethodsClient->$method(self::URI, [self::HEADER_NAME => self::HEADER_VALUE], self::BODY); |
| 98 | + $this->assertSame($response, $actualResponse); |
| 99 | + } |
| 100 | +} |
0 commit comments