diff --git a/tests/Pager_Test.php b/tests/Pager_Test.php index f4f29a01..ccf48792 100644 --- a/tests/Pager_Test.php +++ b/tests/Pager_Test.php @@ -7,6 +7,7 @@ final class PagerTest extends RecurlyTestCase public function setUp(): void { parent::setUp(); + $this->client = new MockClient(); $this->count = 3; $client_stub = $this->createMock(\Recurly\BaseClient::class); @@ -34,35 +35,61 @@ public function setUp(): void $this->pager = new \Recurly\Pager($client_stub, 'page_one'); } + public function testMapArrayParams(): void + { + $ids = ['id-1', 'id-2']; + $idsCsv = join('%2C', $ids); + $url = "https://v3.recurly.com/resources?ids=$idsCsv"; + $result = '{"object":"list","has_more":true,"next":"page_two","data":[{"object":"test_resource","name":"resource one"}]}'; + $this->client->addScenario("GET", $url, NULL, $result, "200 OK"); + + $pager = new \Recurly\Pager($this->client, '/resources', ['ids' => $ids]); + $pager->rewind(); + $this->assertInstanceOf(\Recurly\Response::class, $pager->getResponse()); + } + public function testGetResponse(): void { - $this->pager->rewind(); - $this->assertInstanceOf(\Recurly\Response::class, $this->pager->getResponse()); + $url = "https://v3.recurly.com/resources"; + $result = '{"object":"list","has_more":false,"next":null,"data":[{"object":"test_resource","name":"resource one"}]}'; + $this->client->addScenario("GET", $url, NULL, $result, "200 OK"); + + $pager = new \Recurly\Pager($this->client, '/resources'); + $pager->rewind(); + $this->assertInstanceOf(\Recurly\Response::class, $pager->getResponse()); } public function testGetFirst(): void { + $url = "https://v3.recurly.com/resources?limit=1"; + $result = '{"object":"list","has_more":true,"next":"page_two","data":[{"object":"test_resource","name":"resource one"}]}'; + $this->client->addScenario("GET", $url, NULL, $result, "200 OK"); + + $pager = new \Recurly\Pager($this->client, '/resources'); $this->assertInstanceOf( \Recurly\Resources\TestResource::class, - $this->pager->getFirst() + $pager->getFirst() ); } public function testGetFirstNoResults(): void { - $json_string = $this->fixtures->loadJsonFixture('page_limit_1_empty', ['type' => 'string']); - $response = new \Recurly\Response($json_string); - $response->setHeaders(array('HTTP/1.1 200 OK')); - $client_stub = $this->createMock(\Recurly\BaseClient::class); - $client_stub->method('nextPage')->willReturn($response->toResource()); - $pager = new \Recurly\Pager($client_stub, 'page_one'); + $url = "https://v3.recurly.com/resources?limit=1"; + $result = '{"object":"list","has_more":false,"next":null,"data":[]}'; + $this->client->addScenario("GET", $url, NULL, $result, "200 OK"); + $pager = new \Recurly\Pager($this->client, '/resources'); $this->assertNull($pager->getFirst()); } public function testGetCount(): void { - $this->assertEquals($this->count, $this->pager->getCount()); + $url = "https://v3.recurly.com/resources"; + $this->client->addScenario("HEAD", $url, NULL, '', "200 OK", ['Recurly-Total-Records' => 3]); + + $pager = new \Recurly\Pager($this->client, '/resources'); + + $this->assertEquals($this->count, $pager->getCount()); } public function testGetResponseChanges(): void diff --git a/tests/mock_client.php b/tests/mock_client.php index f0f371ec..98144808 100644 --- a/tests/mock_client.php +++ b/tests/mock_client.php @@ -52,9 +52,9 @@ public function deleteResource(string $resource_id): \Recurly\EmptyResource return $this->makeRequest('DELETE', $path, null, null); } - public function addScenario($method, $url, $body, $result, $status): void + public function addScenario($method, $url, $body, $result, $status, $additional_headers = []): void { - $resp_header = self::_generateRespHeader($status); + $resp_header = self::_generateRespHeader($status, $additional_headers); $this->http->method('execute')->with( $method, $url, @@ -68,20 +68,25 @@ public function clearScenarios(): void $this->http = (new Generator())->getMock(HttpAdapter::class); } - private static function _generateRespHeader($status): array + private static function _generateRespHeader($status, $additional_headers): array { - return [ - "HTTP/1.1 $status", - "Date: Wed, 19 Feb 2020 17:52:05 GMT", - "Content-Type: application/json; charset=utf-8", - "Recurly-Version: recurly.v2999-01-01", - "X-RateLimit-Limit: 2000", - "X-RateLimit-Remaining: 1996", - "X-RateLimit-Reset: 1582135020", - "X-Request-Id: 567a17af7875e3ba-ATL", - "Server: cloudflare", - "CF-RAY: 567a17af7875e3ba-ATL" - ]; + $header_map = array_merge([ + "Date" => "Wed, 19 Feb 2020 17:52:05 GMT", + "Content-Type" => "application/json; charset=utf-8", + "Recurly-Version" => "recurly.v2999-01-01", + "X-RateLimit-Limit" => "2000", + "X-RateLimit-Remaining" => "1996", + "X-RateLimit-Reset" => "1582135020", + "X-Request-Id" => "567a17af7875e3ba-ATL", + "Server" => "cloudflare", + "CF-RAY" => "567a17af7875e3ba-ATL" + ], $additional_headers); + + $headers = ["HTTP/1.1 $status"]; + foreach ($header_map as $k => $v) { + array_push($headers, "$k: $v"); + } + return $headers; } private static function _expectedHeaders(): array