Skip to content

Commit

Permalink
Fix type error in ResultPager::fetch
Browse files Browse the repository at this point in the history
When using etags, the reply from github may be empty and the underlying APIs will return an empty string.
We need to flip those to empty arrays.
e.g.:
```
$github_builder
  ->addPlugin(new Http\Client\Common\Plugin\HeaderSetPlugin([
    'If-None-Match' => $etag,
  ]));

$api       = $github_client->user('user');
$paginator = new \Github\ResultPager($github_client);
$data      = $paginator->fetch($api, 'events', [$username]);
// $data should be [] if $etag is the latest
```
  • Loading branch information
nunoplopes committed Mar 25, 2024
1 parent 71fec50 commit 773747a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Github/ResultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function fetch(AbstractApi $api, string $method, array $parameters = []):
$api = $closure($api);
$result = $api->$method(...$parameters);

if ($result === '' && $this->client->getLastResponse()->getStatusCode() === 204) {
if ($result === '') {
$result = [];
}

Expand Down
24 changes: 24 additions & 0 deletions test/Github/Tests/ResultPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Github\Api\Repo;
use Github\Api\Repository\Statuses;
use Github\Api\Search;
use Github\Api\User;
use Github\Client;
use Github\ResultPager;
use Github\Tests\Mock\PaginatedResponse;
Expand Down Expand Up @@ -176,6 +177,29 @@ public function testFetch()
$this->assertEquals($result, $paginator->fetch($api, $method, $parameters));
}

public function testEmptyFetch()
{
$parameters = ['username'];
$api = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->onlyMethods(['events'])
->getMock();
$api->expects($this->once())
->method('events')
->with(...$parameters)
->willReturn('');

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

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

$this->assertEquals([], $paginator->fetch($api, 'events', $parameters));
}

public function testFetchAllPreserveKeys()
{
$content = [
Expand Down

0 comments on commit 773747a

Please sign in to comment.