Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/Github/ResultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Github;

use Github\Api\ApiInterface;
use Github\Api\Search;
use Github\HttpClient\Message\ResponseMediator;

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand Down
31 changes: 30 additions & 1 deletion test/Github/Tests/Functional/ResultPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
46 changes: 44 additions & 2 deletions test/Github/Tests/ResultPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Github\Tests;

use Github;
use Github\Client;
use Github\ResultPager;
use Github\HttpClient\HttpClientInterface;
use Github\Tests\Mock\TestResponse;

Expand Down Expand Up @@ -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
*
Expand Down