Skip to content

Commit

Permalink
Added a test for Request::pagedRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
tesladethray committed Jan 11, 2017
1 parent 973f3ad commit c13ce7c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. This projec

## MASTER
### Added
- Added `--to=` option to `backup:get` to allow specifying of a local download location. (#15XX)
- Added `--to=` option to `backup:get` to allow specifying of a local download location. (#1520)

## 1.0.0-beta.2 - 2017-01-10
### Fixed
Expand Down
8 changes: 4 additions & 4 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Request implements ConfigAwareInterface, ContainerAwareInterface, LoggerAw
use LoggerAwareTrait;
use SessionAwareTrait;

const PAGED_REQUEST_ENTRY_LIMIT = 100;

/**
* Download file from target URL
*
Expand Down Expand Up @@ -63,14 +65,12 @@ public function download($url, $target)
* @param array $options Options for the request
* string method GET is default
* array form_params Fed into the body of the request
* integer limit Max number of entries to return
* @return array
*/
public function pagedRequest($path, array $options = [])
{
$limit = 100;
if (isset($options['limit'])) {
$limit = $options['limit'];
}
$limit = isset($options['limit']) ? $options['limit'] : self::PAGED_REQUEST_ENTRY_LIMIT;

//$results is an associative array so we don't refetch
$results = [];
Expand Down
63 changes: 63 additions & 0 deletions tests/unit_tests/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,69 @@ public function testRequestNoVerify()
$this->makeRequest($client_options, $request_options, 'foo/bar');
}

public function testPagedRequest()
{
$this->session->method('get')->with('session')->willReturn(false);

$client_options = ['base_uri' => 'https://example.com:443', RequestOptions::VERIFY => true];

$method = 'GET';
$uri = 'https://example.com:443/api/foo/bar';
$headers = [
'Content-type' => 'application/json',
'User-Agent' => 'Terminus/1.1.1 (php_version=7.0.0&script=foo/bar/baz.php)',
];
$body = '';
$request_options = [$method, $uri, $headers, $body];
$actual = $this->makePagedRequest(
$client_options,
$request_options,
'foo/bar',
Request::PAGED_REQUEST_ENTRY_LIMIT,
['headers' => ['foo' => 'bar',],]
);
$expected = [
'data' => [(object)['id' => 'abc123',],],
];
$this->assertEquals($expected, $actual);
}

private function makePagedRequest($client_options, $request_options, $url, $limit, $options = [])
{
$expected_options = $request_options;
$expected_options[1] .= "?limit=$limit";

$this->container->expects($this->at(0))
->method('get')
->with(Client::class, [$client_options])
->willReturn($this->client);
$this->container->expects($this->at(1))
->method('get')
->with(HttpRequest::class, $expected_options)
->willReturn($this->http_request);

$message = $this->getMock(Response::class);
$body = $this->getMockBuilder(Stream::class)
->disableOriginalConstructor()
->getMock();
$body->method('getContents')->willReturn(json_encode(['abc' => (object)['id' => 'abc123',],]));
$message->expects($this->once())
->method('getBody')
->willReturn($body);
$message->expects($this->once())
->method('getHeaders')
->willReturn(['Content-type' => 'application/json']);
$message->expects($this->once())
->method('getStatusCode')
->willReturn(200);
$this->client->expects($this->once())
->method('send')
->with($this->http_request)
->willReturn($message);

return $this->request->pagedRequest($url, $options);
}

private function makeRequest($client_options, $request_options, $url, $options = [])
{
$this->container->expects($this->at(0))
Expand Down

0 comments on commit c13ce7c

Please sign in to comment.