From 773747a72f4aad5248d05f109f40291bf8543baf Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Mon, 25 Mar 2024 09:25:55 +0000 Subject: [PATCH] Fix type error in ResultPager::fetch 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 ``` --- lib/Github/ResultPager.php | 2 +- test/Github/Tests/ResultPagerTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/Github/ResultPager.php b/lib/Github/ResultPager.php index a1d94785ca6..f23b6c6f418 100644 --- a/lib/Github/ResultPager.php +++ b/lib/Github/ResultPager.php @@ -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 = []; } diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index 3df610ee250..b898528483b 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -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; @@ -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 = [