From 818d8a4bdfc06ae2b5ec56d4da6537a8c2fcc900 Mon Sep 17 00:00:00 2001 From: Evgeniy Guseletov Date: Mon, 18 May 2015 16:54:16 +0800 Subject: [PATCH 1/6] Add search api functional test --- .../Tests/Functional/ResultPagerTest.php | 23 ++++++++++++++++++- test/Github/Tests/ResultPagerTest.php | 2 -- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/test/Github/Tests/Functional/ResultPagerTest.php b/test/Github/Tests/Functional/ResultPagerTest.php index 9c67b39d25a..67b23eeb70f 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,25 @@ public function shouldPaginateGetRequests() $repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs')); $this->assertCount(20, $repositories); } + + /** + * @test + * + * results in a search api has different format, see docs + */ + 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..95c349d688b 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; From 0e0c8e0ce9cd1a6135b73b5d1d9a2b8c5903e1d6 Mon Sep 17 00:00:00 2001 From: Evgeniy Guseletov Date: Thu, 21 May 2015 15:24:07 +0800 Subject: [PATCH 2/6] Support Search Api response in pager --- lib/Github/ResultPager.php | 15 ++++++- .../Tests/Functional/ResultPagerTest.php | 10 ++++- test/Github/Tests/ResultPagerTest.php | 44 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) 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 67b23eeb70f..9978a75ad82 100644 --- a/test/Github/Tests/Functional/ResultPagerTest.php +++ b/test/Github/Tests/Functional/ResultPagerTest.php @@ -30,7 +30,15 @@ public function shouldPaginateGetRequests() /** * @test * - * results in a search api has different format, see docs + * results 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() { diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index 95c349d688b..f512caf3848 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -51,6 +51,50 @@ public function shouldGetAllResults() $this->assertEquals($amountLoops * count($content), count($result)); } + /** + * @test + * + * results 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); + + $this->assertEquals($amountLoops * count($content['items']), count($result)); + } + /** * @test * From c89ebe4726ce7b4b9ddeac7bf124a96425b28a7f Mon Sep 17 00:00:00 2001 From: Evgeniy Guseletov Date: Thu, 21 May 2015 15:33:59 +0800 Subject: [PATCH 3/6] Missing argument in test --- test/Github/Tests/ResultPagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index f512caf3848..a678cbdeb4b 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -90,7 +90,7 @@ public function shouldGetAllSearchResults() $method = 'users'; $paginator = new Github\ResultPager($clientMock); - $result = $paginator->fetchAll($searchApiMock, $method); + $result = $paginator->fetchAll($searchApiMock, $method, ['knplabs']); $this->assertEquals($amountLoops * count($content['items']), count($result)); } From 2ea276ad49beb638d76eb5099daf2577caf17f69 Mon Sep 17 00:00:00 2001 From: Evgeniy Guseletov Date: Thu, 21 May 2015 15:40:02 +0800 Subject: [PATCH 4/6] Old array --- test/Github/Tests/ResultPagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index a678cbdeb4b..3dee96edaa1 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -90,7 +90,7 @@ public function shouldGetAllSearchResults() $method = 'users'; $paginator = new Github\ResultPager($clientMock); - $result = $paginator->fetchAll($searchApiMock, $method, ['knplabs']); + $result = $paginator->fetchAll($searchApiMock, $method, array('knplabs')); $this->assertEquals($amountLoops * count($content['items']), count($result)); } From d892d6299b5e53d8e28ce1037081cac2e8c674b1 Mon Sep 17 00:00:00 2001 From: Evgeniy Guseletov Date: Thu, 21 May 2015 16:07:59 +0800 Subject: [PATCH 5/6] Update ResultPagerTest.php --- test/Github/Tests/Functional/ResultPagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Github/Tests/Functional/ResultPagerTest.php b/test/Github/Tests/Functional/ResultPagerTest.php index 9978a75ad82..9c4abc7b699 100644 --- a/test/Github/Tests/Functional/ResultPagerTest.php +++ b/test/Github/Tests/Functional/ResultPagerTest.php @@ -30,7 +30,7 @@ public function shouldPaginateGetRequests() /** * @test * - * results in a search api has different format: + * response in a search api has different format: * * { * "total_count": 1, From 892a25f7a850596ddfe9b48e6544d97f62a28a4b Mon Sep 17 00:00:00 2001 From: Evgeniy Guseletov Date: Thu, 21 May 2015 16:08:17 +0800 Subject: [PATCH 6/6] Update ResultPagerTest.php --- test/Github/Tests/ResultPagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index 3dee96edaa1..494731523db 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -54,7 +54,7 @@ public function shouldGetAllResults() /** * @test * - * results in a search api has different format: + * response in a search api has different format: * * { * "total_count": 1,