|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Spawnia\Sailor\Tests\Unit\Client; |
| 4 | + |
| 5 | +use Psr\Http\Message\ResponseInterface; |
| 6 | +use React\Http\Browser; |
| 7 | +use Spawnia\Sailor\Client\ReactPhp; |
| 8 | +use Spawnia\Sailor\Error\UnexpectedResponse; |
| 9 | +use Spawnia\Sailor\Tests\TestCase; |
| 10 | + |
| 11 | +final class ReactPhpTest extends TestCase |
| 12 | +{ |
| 13 | + public function testRequest(): void |
| 14 | + { |
| 15 | + $uri = 'https://simple.bar/graphql'; |
| 16 | + $expectedBody = /* @lang JSON */ '{"query":"{simple}"}'; |
| 17 | + |
| 18 | + $browser = \Mockery::mock(Browser::class); |
| 19 | + $browser->shouldReceive('post') |
| 20 | + ->once() |
| 21 | + ->withArgs(function (string $url, array $headers, string $body) use ($uri, $expectedBody): bool { |
| 22 | + return $url === $uri |
| 23 | + && $headers === ['Content-Type' => 'application/json'] |
| 24 | + && $body === $expectedBody; |
| 25 | + }) |
| 26 | + ->andReturn(\React\Promise\resolve($this->mockResponse(200, /* @lang JSON */ '{"data": {"simple": "bar"}}'))); |
| 27 | + |
| 28 | + $client = new ReactPhp($uri, $browser); |
| 29 | + $response = $client->request(/* @lang GraphQL */ '{simple}'); |
| 30 | + |
| 31 | + self::assertEquals( |
| 32 | + (object) ['simple' => 'bar'], |
| 33 | + $response->data, |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public function testRequestWithVariables(): void |
| 38 | + { |
| 39 | + $uri = 'https://simple.bar/graphql'; |
| 40 | + $variables = (object) ['foo' => 'bar']; |
| 41 | + $expectedBody = /* @lang JSON */ '{"query":"{simple}","variables":{"foo":"bar"}}'; |
| 42 | + |
| 43 | + $browser = \Mockery::mock(Browser::class); |
| 44 | + $browser->shouldReceive('post') |
| 45 | + ->once() |
| 46 | + ->withArgs(function (string $url, array $headers, string $body) use ($uri, $expectedBody): bool { |
| 47 | + return $url === $uri |
| 48 | + && $headers === ['Content-Type' => 'application/json'] |
| 49 | + && $body === $expectedBody; |
| 50 | + }) |
| 51 | + ->andReturn(\React\Promise\resolve($this->mockResponse(200, /* @lang JSON */ '{"data": {"simple": "bar"}}'))); |
| 52 | + |
| 53 | + $client = new ReactPhp($uri, $browser); |
| 54 | + $response = $client->request(/* @lang GraphQL */ '{simple}', $variables); |
| 55 | + |
| 56 | + self::assertEquals( |
| 57 | + (object) ['simple' => 'bar'], |
| 58 | + $response->data, |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public function testNon200StatusThrows(): void |
| 63 | + { |
| 64 | + $uri = 'https://simple.bar/graphql'; |
| 65 | + |
| 66 | + $browser = \Mockery::mock(Browser::class); |
| 67 | + $browser->shouldReceive('post') |
| 68 | + ->once() |
| 69 | + ->andReturn(\React\Promise\resolve($this->mockResponse(500, 'Internal Server Error'))); |
| 70 | + |
| 71 | + $client = new ReactPhp($uri, $browser); |
| 72 | + |
| 73 | + $this->expectException(UnexpectedResponse::class); |
| 74 | + $client->request(/* @lang GraphQL */ '{simple}'); |
| 75 | + } |
| 76 | + |
| 77 | + /** @return ResponseInterface&\Mockery\MockInterface */ |
| 78 | + private function mockResponse(int $statusCode, string $body): ResponseInterface |
| 79 | + { |
| 80 | + $stream = \Mockery::mock(\Psr\Http\Message\StreamInterface::class); |
| 81 | + $stream->shouldReceive('getContents')->andReturn($body); |
| 82 | + $stream->shouldReceive('__toString')->andReturn($body); |
| 83 | + |
| 84 | + $response = \Mockery::mock(ResponseInterface::class); |
| 85 | + $response->shouldReceive('getStatusCode')->andReturn($statusCode); |
| 86 | + $response->shouldReceive('getBody')->andReturn($stream); |
| 87 | + $response->shouldReceive('getHeaders')->andReturn([]); |
| 88 | + |
| 89 | + return $response; |
| 90 | + } |
| 91 | +} |
0 commit comments