Skip to content

Commit

Permalink
Merge pull request #466 from recurly/v3-more-testing
Browse files Browse the repository at this point in the history
Adding and Updating Pager tests
  • Loading branch information
bhelx authored Mar 3, 2020
2 parents ec4ebf4 + 966c885 commit 2abffad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
47 changes: 37 additions & 10 deletions tests/Pager_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
35 changes: 20 additions & 15 deletions tests/mock_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 2abffad

Please sign in to comment.