Skip to content

Commit

Permalink
restore BC in AbstractApi::retrieveAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Dec 20, 2023
1 parent 9fa08c3 commit 308be81
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/Redmine/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Redmine\Api;
use Redmine\Client\Client;
use Redmine\Exception;
use Redmine\Exception\SerializerException;
use Redmine\Serializer\JsonSerializer;
use Redmine\Serializer\PathSerializer;
Expand Down Expand Up @@ -169,7 +170,7 @@ protected function sanitizeParams(array $defaults, array $params)
* @param string $endpoint API end point
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
*
* @return array|false elements found
* @return string|array|false elements found or error message of false
*/
protected function retrieveAll($endpoint, array $params = [])
{
Expand All @@ -178,6 +179,8 @@ protected function retrieveAll($endpoint, array $params = [])
try {
$data = $this->retrieveData(strval($endpoint), $params);
} catch (SerializerException $e) {
return 'Error decoding body as JSON: '.$e->getPrevious()->getMessage();
} catch (Exception $e) {

Check warning on line 183 in src/Redmine/Api/AbstractApi.php

View check run for this annotation

Codecov / codecov/patch

src/Redmine/Api/AbstractApi.php#L183

Added line #L183 was not covered by tests
$data = false;
}

Expand Down Expand Up @@ -308,10 +311,6 @@ private function getLastResponseBodyAsArray(): array
{
$body = $this->client->getLastResponseBody();

if ($body === '') {
return [];
}

$contentType = $this->client->getLastResponseContentType();
$returnData = null;

Expand Down
35 changes: 32 additions & 3 deletions tests/Unit/Api/AbstractApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Redmine\Api\AbstractApi;
use Redmine\Client\Client;
use Redmine\Exception\SerializerException;
use ReflectionMethod;
use SimpleXMLElement;

Expand Down Expand Up @@ -236,14 +237,42 @@ public function testRetrieveData($response, $contentType, $expected)
$method->setAccessible(true);

$this->assertSame($expected, $method->invoke($api, '/issues.json'));

}

public static function retrieveDataData(): array
{
return [
'test decode by default' => ['{"foo_bar": 12345}', 'application/json', ['foo_bar' => 12345]],
'Empty body' => ['', 'application/json', []],
];
}

/**
* @covers \Redmine\Api\AbstractApi::retrieveData
*
* @dataProvider retrieveDataToExceptionData
*/
public function testRetrieveDataThrowsException($response, $contentType, $expectedException, $expectedMessage)
{
$client = $this->createMock(Client::class);
$client->method('requestGet')->willReturn(true);
$client->method('getLastResponseBody')->willReturn($response);
$client->method('getLastResponseContentType')->willReturn($contentType);

$api = new class($client) extends AbstractApi {};

$method = new ReflectionMethod($api, 'retrieveData');
$method->setAccessible(true);

$this->expectException($expectedException);
$this->expectExceptionMessage($expectedMessage);

$method->invoke($api, '/issues.json');
}

public static function retrieveDataToExceptionData(): array
{
return [
'Empty body' => ['', 'application/json', SerializerException::class, 'Syntax error" while decoding JSON: '],
];
}

Expand Down Expand Up @@ -271,7 +300,7 @@ public static function retrieveAllData(): array
{
return [
'test decode by default' => ['{"foo_bar": 12345}', 'application/json', ['foo_bar' => 12345]],
'Empty body' => ['', 'application/json', []],
'Empty body' => ['', 'application/json', 'Error decoding body as JSON: Syntax error'],
];
}

Expand Down

0 comments on commit 308be81

Please sign in to comment.