Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues with ResultPager and various github responses #956

Merged
merged 1 commit into from
Dec 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"require": {
"php": "^7.2.5 || ^8.0",
"ext-json": "*",
"php-http/cache-plugin": "^1.7.1",
"php-http/client-common": "^2.3",
"php-http/discovery": "^1.12",
Expand Down
18 changes: 12 additions & 6 deletions lib/Github/ResultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,25 @@ public function fetchAllLazy(AbstractApi $api, string $method, array $parameters
{
$result = $this->fetch($api, $method, $parameters);

foreach ($result['items'] ?? $result as $item) {
yield $item;
foreach ($result['items'] ?? $result as $key => $item) {
if (is_string($key)) {
yield $key => $item;
} else {
yield $item;
}
}

while ($this->hasNext()) {
$result = $this->fetchNext();

foreach ($result['items'] ?? $result as $item) {
yield $item;
foreach ($result['items'] ?? $result as $key => $item) {
if (is_string($key)) {
yield $key => $item;
} else {
yield $item;
}
}
}

return $result;
}

/**
Expand Down
79 changes: 72 additions & 7 deletions test/Github/Tests/ResultPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace Github\Tests;

use Github\Api\Issue;
use Github\Api\Organization\Members;
use Github\Api\Repository\Statuses;
use Github\Api\Search;
use Github\Client;
use Github\ResultPager;
use Github\Tests\Mock\PaginatedResponse;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

Expand Down Expand Up @@ -37,14 +42,14 @@ public function shouldGetAllResults(string $fetchMethod)

// httpClient mock
$httpClientMock = $this->getMockBuilder(ClientInterface::class)
->setMethods(['sendRequest'])
->onlyMethods(['sendRequest'])
->getMock();
$httpClientMock
->expects($this->exactly($amountLoops))
->method('sendRequest')
->will($this->returnValue($response));

$client = \Github\Client::createWithHttpClient($httpClientMock);
$client = Client::createWithHttpClient($httpClientMock);

// memberApi Mock
$memberApi = new Members($client);
Expand Down Expand Up @@ -91,14 +96,14 @@ public function shouldGetAllSearchResults()

// httpClient mock
$httpClientMock = $this->getMockBuilder(ClientInterface::class)
->setMethods(['sendRequest'])
->onlyMethods(['sendRequest'])
->getMock();
$httpClientMock
->expects($this->exactly($amountLoops))
->method('sendRequest')
->will($this->returnValue($response));
->willReturn($response);

$client = \Github\Client::createWithHttpClient($httpClientMock);
$client = Client::createWithHttpClient($httpClientMock);

$searchApi = new Search($client);
$method = 'users';
Expand All @@ -115,7 +120,7 @@ public function testFetch()
$parameters = ['baz'];
$api = $this->getMockBuilder(Members::class)
->disableOriginalConstructor()
->setMethods(['all'])
->onlyMethods(['all'])
->getMock();
$api->expects($this->once())
->method('all')
Expand All @@ -124,12 +129,72 @@ public function testFetch()

$paginator = $this->getMockBuilder(ResultPager::class)
->disableOriginalConstructor()
->setMethods(['postFetch'])
->onlyMethods(['postFetch'])
->getMock();

$paginator->expects($this->once())
->method('postFetch');

$this->assertEquals($result, $paginator->fetch($api, $method, $parameters));
}

public function testFetchAllPreserveKeys()
{
$content = [
'state' => 'success',
'statuses' => [
['description' => 'status 1', 'state' => 'success'],
['description' => 'status 2', 'state' => 'failure'],
],
'sha' => '43068834af7e501778708ed13106de95f782328c',
];

$response = new Response(200, ['Content-Type'=>'application/json'], Utils::streamFor(json_encode($content)));

// httpClient mock
$httpClientMock = $this->getMockBuilder(HttpClient::class)
->onlyMethods(['sendRequest'])
->getMock();
$httpClientMock
->method('sendRequest')
->willReturn($response);

$client = Client::createWithHttpClient($httpClientMock);

$api = new Statuses($client);
$paginator = new ResultPager($client);
$result = $paginator->fetchAll($api, 'combined', ['knplabs', 'php-github-api', '43068834af7e501778708ed13106de95f782328c']);

$this->assertArrayHasKey('state', $result);
$this->assertArrayHasKey('statuses', $result);
$this->assertCount(2, $result['statuses']);
}

public function testFetchAllWithoutKeys()
{
$content = [
['title' => 'issue 1'],
['title' => 'issue 2'],
['title' => 'issue 3'],
];

$response = new PaginatedResponse(3, $content);

// httpClient mock
$httpClientMock = $this->getMockBuilder(HttpClient::class)
->onlyMethods(['sendRequest'])
->getMock();
$httpClientMock
->expects($this->exactly(3))
->method('sendRequest')
->willReturn($response);

$client = Client::createWithHttpClient($httpClientMock);

$api = new Issue($client);
$paginator = new ResultPager($client);
$result = $paginator->fetchAll($api, 'all', ['knplabs', 'php-github-api']);

$this->assertCount(9, $result);
}
}