Skip to content

Commit 7301517

Browse files
committed
Merge pull request #271 from KnpLabs/feature/search-pager
Support Search Api response in pager
2 parents d890eec + 892a25f commit 7301517

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

lib/Github/ResultPager.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github;
44

55
use Github\Api\ApiInterface;
6+
use Github\Api\Search;
67
use Github\HttpClient\Message\ResponseMediator;
78

89
/**
@@ -69,6 +70,8 @@ public function fetch(ApiInterface $api, $method, array $parameters = array())
6970
*/
7071
public function fetchAll(ApiInterface $api, $method, array $parameters = array())
7172
{
73+
$isSearch = $api instanceof Search;
74+
7275
// get the perPage from the api
7376
$perPage = $api->getPerPage();
7477

@@ -79,8 +82,18 @@ public function fetchAll(ApiInterface $api, $method, array $parameters = array()
7982
$result = call_user_func_array(array($api, $method), $parameters);
8083
$this->postFetch();
8184

85+
if ($isSearch) {
86+
$result = isset($result['items']) ? $result['items'] : $result;
87+
}
88+
8289
while ($this->hasNext()) {
83-
$result = array_merge($result, $this->fetchNext());
90+
$next = $this->fetchNext();
91+
92+
if ($isSearch) {
93+
$result = array_merge($result, $next['items']);
94+
} else {
95+
$result = array_merge($result, $next);
96+
}
8497
}
8598

8699
// restore the perPage

test/Github/Tests/Functional/ResultPagerTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function shouldPaginateGetRequests()
1717
$repositoriesApi = $this->client->api('user');
1818
$repositoriesApi->setPerPage(10);
1919

20-
$pager = new ResultPager($this->client);
20+
$pager = $this->createPager();
2121

2222
$repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs'));
2323
$this->assertCount(10, $repositories);
@@ -26,4 +26,33 @@ public function shouldPaginateGetRequests()
2626
$repositories = $pager->fetch($repositoriesApi, 'repositories', array('KnpLabs'));
2727
$this->assertCount(20, $repositories);
2828
}
29+
30+
/**
31+
* @test
32+
*
33+
* response in a search api has different format:
34+
*
35+
* {
36+
* "total_count": 1,
37+
* "incomplete_results": false,
38+
* "items": []
39+
* }
40+
*
41+
* and we need to extract result from `items`
42+
*/
43+
public function shouldGetAllResultsFromSearchApi()
44+
{
45+
$searchApi = $this->client->search();
46+
$searchApi->setPerPage(10);
47+
48+
$pager = $this->createPager();
49+
50+
$users = $pager->fetch($searchApi, 'users', array('location:Kyiv'));
51+
$this->assertCount(10, $users);
52+
}
53+
54+
private function createPager()
55+
{
56+
return new ResultPager($this->client);
57+
}
2958
}

test/Github/Tests/ResultPagerTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace Github\Tests;
44

55
use Github;
6-
use Github\Client;
7-
use Github\ResultPager;
86
use Github\HttpClient\HttpClientInterface;
97
use Github\Tests\Mock\TestResponse;
108

@@ -53,6 +51,50 @@ public function shouldGetAllResults()
5351
$this->assertEquals($amountLoops * count($content), count($result));
5452
}
5553

54+
/**
55+
* @test
56+
*
57+
* response in a search api has different format:
58+
*
59+
* {
60+
* "total_count": 1,
61+
* "incomplete_results": false,
62+
* "items": []
63+
* }
64+
*
65+
* and we need to extract result from `items`
66+
*/
67+
public function shouldGetAllSearchResults()
68+
{
69+
$amountLoops = 3;
70+
71+
$content = array(
72+
'total_count' => 12,
73+
'items' => array(1, 2, 3, 4)
74+
);
75+
$responseMock = new TestResponse($amountLoops, $content);
76+
77+
$httpClientMock = $this->getHttpClientMock($responseMock);
78+
$httpClientMock
79+
->expects($this->exactly($amountLoops))
80+
->method('get')
81+
->will($this->returnValue($responseMock));
82+
83+
$clientMock = $this->getClientMock($httpClientMock);
84+
85+
$searchApiMock = $this->getApiMock('Github\Api\Search');
86+
$searchApiMock
87+
->expects($this->once())
88+
->method('users')
89+
->will($this->returnValue(array()));
90+
91+
$method = 'users';
92+
$paginator = new Github\ResultPager($clientMock);
93+
$result = $paginator->fetchAll($searchApiMock, $method, array('knplabs'));
94+
95+
$this->assertEquals($amountLoops * count($content['items']), count($result));
96+
}
97+
5698
/**
5799
* @test
58100
*

0 commit comments

Comments
 (0)