diff --git a/lib/Github/ResultPager.php b/lib/Github/ResultPager.php index 01c689a58d9..21bc08ec3f4 100644 --- a/lib/Github/ResultPager.php +++ b/lib/Github/ResultPager.php @@ -3,6 +3,7 @@ namespace Github; use Github\Api\ApiInterface; +use Github\Api\Search; use Github\HttpClient\Message\ResponseMediator; /** @@ -69,6 +70,8 @@ public function fetch(ApiInterface $api, $method, array $parameters = array()) */ public function fetchAll(ApiInterface $api, $method, array $parameters = array()) { + $isSearch = $api instanceof Search; + // get the perPage from the api $perPage = $api->getPerPage(); @@ -79,8 +82,18 @@ public function fetchAll(ApiInterface $api, $method, array $parameters = array() $result = call_user_func_array(array($api, $method), $parameters); $this->postFetch(); + if ($isSearch) { + $result = isset($result['items']) ? $result['items'] : $result; + } + while ($this->hasNext()) { - $result = array_merge($result, $this->fetchNext()); + $next = $this->fetchNext(); + + if ($isSearch) { + $result = array_merge($result, $next['items']); + } else { + $result = array_merge($result, $next); + } } // restore the perPage diff --git a/test/Github/Tests/Functional/ResultPagerTest.php b/test/Github/Tests/Functional/ResultPagerTest.php index 9c67b39d25a..9c4abc7b699 100644 --- a/test/Github/Tests/Functional/ResultPagerTest.php +++ b/test/Github/Tests/Functional/ResultPagerTest.php @@ -17,7 +17,7 @@ public function shouldPaginateGetRequests() $repositoriesApi = $this->client->api('user'); $repositoriesApi->setPerPage(10); - $pager = new ResultPager($this->client); + $pager = $this->createPager(); $repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs')); $this->assertCount(10, $repositories); @@ -26,4 +26,33 @@ public function shouldPaginateGetRequests() $repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs')); $this->assertCount(20, $repositories); } + + /** + * @test + * + * response in a search api has different format: + * + * { + * "total_count": 1, + * "incomplete_results": false, + * "items": [] + * } + * + * and we need to extract result from `items` + */ + public function shouldGetAllResultsFromSearchApi() + { + $searchApi = $this->client->search(); + $searchApi->setPerPage(10); + + $pager = $this->createPager(); + + $users = $pager->fetch($searchApi, 'users', array('location:Kyiv')); + $this->assertCount(10, $users); + } + + private function createPager() + { + return new ResultPager($this->client); + } } diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index 075226587f7..494731523db 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -3,8 +3,6 @@ namespace Github\Tests; use Github; -use Github\Client; -use Github\ResultPager; use Github\HttpClient\HttpClientInterface; use Github\Tests\Mock\TestResponse; @@ -53,6 +51,50 @@ public function shouldGetAllResults() $this->assertEquals($amountLoops * count($content), count($result)); } + /** + * @test + * + * response in a search api has different format: + * + * { + * "total_count": 1, + * "incomplete_results": false, + * "items": [] + * } + * + * and we need to extract result from `items` + */ + public function shouldGetAllSearchResults() + { + $amountLoops = 3; + + $content = array( + 'total_count' => 12, + 'items' => array(1, 2, 3, 4) + ); + $responseMock = new TestResponse($amountLoops, $content); + + $httpClientMock = $this->getHttpClientMock($responseMock); + $httpClientMock + ->expects($this->exactly($amountLoops)) + ->method('get') + ->will($this->returnValue($responseMock)); + + $clientMock = $this->getClientMock($httpClientMock); + + $searchApiMock = $this->getApiMock('Github\Api\Search'); + $searchApiMock + ->expects($this->once()) + ->method('users') + ->will($this->returnValue(array())); + + $method = 'users'; + $paginator = new Github\ResultPager($clientMock); + $result = $paginator->fetchAll($searchApiMock, $method, array('knplabs')); + + $this->assertEquals($amountLoops * count($content['items']), count($result)); + } + /** * @test *