Skip to content

Commit e5a21a2

Browse files
authored
bug KnpLabs#956 Fixed issues with ResultPager and various github responses (acrobat)
This PR was merged into the 3.0.x-dev branch. Discussion ---------- I was testing the upcoming 3.0 version in a project using the resultpager and the current changes broke some of the expected responses. For example when calling the commit status endpoints, it returns a general "state" property and a "statuses" property with the individual state values for each check. In this case the string keys have to be preserved. @GrahamCampbell Can you review this PR as you did the initial improvements? Commits ------- fd43963 Fixed issues with ResultPager and various github responses
2 parents 96a68f6 + fd43963 commit e5a21a2

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"require": {
2020
"php": "^7.2.5 || ^8.0",
21+
"ext-json": "*",
2122
"php-http/cache-plugin": "^1.7.1",
2223
"php-http/client-common": "^2.3",
2324
"php-http/discovery": "^1.12",

lib/Github/ResultPager.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,25 @@ public function fetchAllLazy(AbstractApi $api, string $method, array $parameters
106106
{
107107
$result = $this->fetch($api, $method, $parameters);
108108

109-
foreach ($result['items'] ?? $result as $item) {
110-
yield $item;
109+
foreach ($result['items'] ?? $result as $key => $item) {
110+
if (is_string($key)) {
111+
yield $key => $item;
112+
} else {
113+
yield $item;
114+
}
111115
}
112116

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

116-
foreach ($result['items'] ?? $result as $item) {
117-
yield $item;
120+
foreach ($result['items'] ?? $result as $key => $item) {
121+
if (is_string($key)) {
122+
yield $key => $item;
123+
} else {
124+
yield $item;
125+
}
118126
}
119127
}
120-
121-
return $result;
122128
}
123129

124130
/**

test/Github/Tests/ResultPagerTest.php

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace Github\Tests;
44

5+
use Github\Api\Issue;
56
use Github\Api\Organization\Members;
7+
use Github\Api\Repository\Statuses;
68
use Github\Api\Search;
9+
use Github\Client;
710
use Github\ResultPager;
811
use Github\Tests\Mock\PaginatedResponse;
12+
use GuzzleHttp\Psr7\Response;
13+
use GuzzleHttp\Psr7\Utils;
914
use Http\Client\HttpClient;
1015
use Psr\Http\Client\ClientInterface;
1116

@@ -37,14 +42,14 @@ public function shouldGetAllResults(string $fetchMethod)
3742

3843
// httpClient mock
3944
$httpClientMock = $this->getMockBuilder(ClientInterface::class)
40-
->setMethods(['sendRequest'])
45+
->onlyMethods(['sendRequest'])
4146
->getMock();
4247
$httpClientMock
4348
->expects($this->exactly($amountLoops))
4449
->method('sendRequest')
4550
->will($this->returnValue($response));
4651

47-
$client = \Github\Client::createWithHttpClient($httpClientMock);
52+
$client = Client::createWithHttpClient($httpClientMock);
4853

4954
// memberApi Mock
5055
$memberApi = new Members($client);
@@ -91,14 +96,14 @@ public function shouldGetAllSearchResults()
9196

9297
// httpClient mock
9398
$httpClientMock = $this->getMockBuilder(ClientInterface::class)
94-
->setMethods(['sendRequest'])
99+
->onlyMethods(['sendRequest'])
95100
->getMock();
96101
$httpClientMock
97102
->expects($this->exactly($amountLoops))
98103
->method('sendRequest')
99-
->will($this->returnValue($response));
104+
->willReturn($response);
100105

101-
$client = \Github\Client::createWithHttpClient($httpClientMock);
106+
$client = Client::createWithHttpClient($httpClientMock);
102107

103108
$searchApi = new Search($client);
104109
$method = 'users';
@@ -115,7 +120,7 @@ public function testFetch()
115120
$parameters = ['baz'];
116121
$api = $this->getMockBuilder(Members::class)
117122
->disableOriginalConstructor()
118-
->setMethods(['all'])
123+
->onlyMethods(['all'])
119124
->getMock();
120125
$api->expects($this->once())
121126
->method('all')
@@ -124,12 +129,72 @@ public function testFetch()
124129

125130
$paginator = $this->getMockBuilder(ResultPager::class)
126131
->disableOriginalConstructor()
127-
->setMethods(['postFetch'])
132+
->onlyMethods(['postFetch'])
128133
->getMock();
129134

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

133138
$this->assertEquals($result, $paginator->fetch($api, $method, $parameters));
134139
}
140+
141+
public function testFetchAllPreserveKeys()
142+
{
143+
$content = [
144+
'state' => 'success',
145+
'statuses' => [
146+
['description' => 'status 1', 'state' => 'success'],
147+
['description' => 'status 2', 'state' => 'failure'],
148+
],
149+
'sha' => '43068834af7e501778708ed13106de95f782328c',
150+
];
151+
152+
$response = new Response(200, ['Content-Type'=>'application/json'], Utils::streamFor(json_encode($content)));
153+
154+
// httpClient mock
155+
$httpClientMock = $this->getMockBuilder(HttpClient::class)
156+
->onlyMethods(['sendRequest'])
157+
->getMock();
158+
$httpClientMock
159+
->method('sendRequest')
160+
->willReturn($response);
161+
162+
$client = Client::createWithHttpClient($httpClientMock);
163+
164+
$api = new Statuses($client);
165+
$paginator = new ResultPager($client);
166+
$result = $paginator->fetchAll($api, 'combined', ['knplabs', 'php-github-api', '43068834af7e501778708ed13106de95f782328c']);
167+
168+
$this->assertArrayHasKey('state', $result);
169+
$this->assertArrayHasKey('statuses', $result);
170+
$this->assertCount(2, $result['statuses']);
171+
}
172+
173+
public function testFetchAllWithoutKeys()
174+
{
175+
$content = [
176+
['title' => 'issue 1'],
177+
['title' => 'issue 2'],
178+
['title' => 'issue 3'],
179+
];
180+
181+
$response = new PaginatedResponse(3, $content);
182+
183+
// httpClient mock
184+
$httpClientMock = $this->getMockBuilder(HttpClient::class)
185+
->onlyMethods(['sendRequest'])
186+
->getMock();
187+
$httpClientMock
188+
->expects($this->exactly(3))
189+
->method('sendRequest')
190+
->willReturn($response);
191+
192+
$client = Client::createWithHttpClient($httpClientMock);
193+
194+
$api = new Issue($client);
195+
$paginator = new ResultPager($client);
196+
$result = $paginator->fetchAll($api, 'all', ['knplabs', 'php-github-api']);
197+
198+
$this->assertCount(9, $result);
199+
}
135200
}

0 commit comments

Comments
 (0)