|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Test; |
| 4 | + |
| 5 | +use ByJG\ApiTools\AbstractRequester; |
| 6 | +use ByJG\ApiTools\Base\Body; |
| 7 | +use ByJG\ApiTools\Base\Schema; |
| 8 | +use GuzzleHttp\Psr7\Request; |
| 9 | +use GuzzleHttp\Psr7\Response; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class AbstractRequesterTest extends TestCase |
| 14 | +{ |
| 15 | + /** @var MockObject|AbstractRequester */ |
| 16 | + private $requester; |
| 17 | + |
| 18 | + /** @var MockObject|Schema */ |
| 19 | + private $schema; |
| 20 | + |
| 21 | + protected function setUp() |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->requester = $this->getMockForAbstractClass(AbstractRequester::class); |
| 26 | + |
| 27 | + $this->schema = $this->getMockBuilder(Schema::class) |
| 28 | + ->disableOriginalConstructor() |
| 29 | + ->getMock(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testDefault() |
| 33 | + { |
| 34 | + // request body part of the schema |
| 35 | + $requestBody = $this->getMockBuilder(Body::class) |
| 36 | + ->disableOriginalConstructor() |
| 37 | + ->getMock(); |
| 38 | + $requestBody->expects($this->once()) |
| 39 | + ->method('match') |
| 40 | + ->with(null); |
| 41 | + |
| 42 | + // response body part of the schema |
| 43 | + $responseBody = $this->getMockBuilder(Body::class) |
| 44 | + ->disableOriginalConstructor() |
| 45 | + ->getMock(); |
| 46 | + $responseBody->expects($this->once()) |
| 47 | + ->method('match') |
| 48 | + ->with(null); |
| 49 | + |
| 50 | + // set up schema |
| 51 | + $this->schema->method('getServerUrl') |
| 52 | + ->willReturn('https://api.example.com'); |
| 53 | + $this->schema->method('getBasePath') |
| 54 | + ->willReturn('/v1'); |
| 55 | + $this->schema->method('getRequestParameters') |
| 56 | + ->with( |
| 57 | + '/v1/endpoint', |
| 58 | + 'POST' |
| 59 | + ) |
| 60 | + ->willReturn($requestBody); |
| 61 | + $this->schema->method('getResponseParameters') |
| 62 | + ->with( |
| 63 | + '/v1/endpoint', |
| 64 | + 'POST', |
| 65 | + 200 |
| 66 | + ) |
| 67 | + ->willReturn($responseBody); |
| 68 | + |
| 69 | + // set up abstract function to validate the request being sent |
| 70 | + $this->requester->expects($this->once()) |
| 71 | + ->method('handleRequest') |
| 72 | + ->with($this->isInstanceOf(Request::class)) |
| 73 | + /** @var Request $request */ |
| 74 | + ->willReturnCallback(function ($request) { |
| 75 | + // validate headers |
| 76 | + $headers = $request->getHeaders(); |
| 77 | + $this->assertEquals($headers['Host'], ['api.example.com']); |
| 78 | + $this->assertEquals($headers['Accept'], ['application/json']); |
| 79 | + // validate method |
| 80 | + $this->assertEquals('POST', $request->getMethod()); |
| 81 | + // validate URI |
| 82 | + $uri = $request->getUri(); |
| 83 | + $this->assertEquals('https', $uri->getScheme()); |
| 84 | + $this->assertEquals('', $uri->getUserInfo()); |
| 85 | + $this->assertEquals('api.example.com', $uri->getHost()); |
| 86 | + $this->assertEquals('/endpoint', $uri->getPath()); |
| 87 | + $this->assertEquals('id=42', $uri->getQuery()); |
| 88 | + $this->assertEquals('', $uri->getFragment()); |
| 89 | + |
| 90 | + return new Response(200); |
| 91 | + }); |
| 92 | + |
| 93 | + $this->requester->withSchema($this->schema); |
| 94 | + $this->requester->withMethod('POST'); |
| 95 | + $this->requester->withPath('/endpoint'); |
| 96 | + $this->requester->withQuery(['id' => 42]); |
| 97 | + |
| 98 | + $res = $this->requester->send(); |
| 99 | + |
| 100 | + $this->assertNull($res); |
| 101 | + } |
| 102 | +} |
0 commit comments