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 a840ded
Show file tree
Hide file tree
Showing 3 changed files with 58 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
53 changes: 53 additions & 0 deletions tests/unit_tests/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,59 @@ 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];

$expected_options = $request_options;
$expected_options[1] .= '?limit=' . Request::PAGED_REQUEST_ENTRY_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);

$actual = $this->request->pagedRequest($uri, $request_options);
$expected = [
'data' => [(object)['id' => 'abc123',],],
];
$this->assertEquals($expected, $actual);
}

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

0 comments on commit a840ded

Please sign in to comment.