diff --git a/CHANGELOG.md b/CHANGELOG.md index f02475785..05468bc9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Request/Request.php b/src/Request/Request.php index 09ad13114..736743199 100755 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -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 * @@ -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 = []; diff --git a/tests/unit_tests/Request/RequestTest.php b/tests/unit_tests/Request/RequestTest.php index ae333fb80..0bb6f89fb 100644 --- a/tests/unit_tests/Request/RequestTest.php +++ b/tests/unit_tests/Request/RequestTest.php @@ -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))