Skip to content

Commit

Permalink
feature #1132 Fix type error in ResultPager::fetch (nunoplopes)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.14-dev branch.

Discussion
----------

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.:
```php
$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
```

Commits
-------

773747a Fix type error in ResultPager::fetch
  • Loading branch information
acrobat authored Sep 23, 2024
2 parents a6f0f4f + 773747a commit 3db6f27
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 3db6f27

Please sign in to comment.