diff --git a/src/Redmine/Api/Project.php b/src/Redmine/Api/Project.php index 4e4b5655..2aa89bfe 100755 --- a/src/Redmine/Api/Project.php +++ b/src/Redmine/Api/Project.php @@ -2,6 +2,7 @@ namespace Redmine\Api; +use Redmine\Exception; use Redmine\Exception\MissingParameterException; use Redmine\Serializer\PathSerializer; use Redmine\Serializer\XmlSerializer; @@ -42,13 +43,21 @@ final public function list(array $params = []): array * * @param array $params optional parameters to be passed to the api (offset, limit, ...) * - * @return array list of projects found + * @return array|string|false list of projects found or error message or false */ public function all(array $params = []) { @trigger_error('`'.__METHOD__.'()` is deprecated since v2.4.0, use `'.__CLASS__.'::list()` instead.', E_USER_DEPRECATED); - return $this->list($params); + try { + return $this->list($params); + } catch (Exception $e) { + if ($this->client->getLastResponseBody() === '') { + return false; + } + + return $e->getMessage(); + } } /** diff --git a/tests/Unit/Api/ProjectTest.php b/tests/Unit/Api/ProjectTest.php index f780fc37..6025c916 100644 --- a/tests/Unit/Api/ProjectTest.php +++ b/tests/Unit/Api/ProjectTest.php @@ -47,32 +47,38 @@ function ($errno, $errstr): bool { * Test all(). * * @covers ::all + * @dataProvider getAllData * @test */ - public function testAllReturnsClientGetResponse() + public function testAllReturnsClientGetResponse($response, $responseType, $expectedResponse) { - // Test values - $response = '["API Response"]'; - $expectedReturn = ['API Response']; - // Create the used mock objects $client = $this->createMock(Client::class); - $client->expects($this->once()) + $client->expects($this->exactly(1)) ->method('requestGet') ->with('/projects.json') ->willReturn(true); - $client->expects($this->exactly(1)) + $client->expects($this->atLeast(1)) ->method('getLastResponseBody') ->willReturn($response); $client->expects($this->exactly(1)) ->method('getLastResponseContentType') - ->willReturn('application/json'); + ->willReturn($responseType); // Create the object under test $api = new Project($client); // Perform the tests - $this->assertSame($expectedReturn, $api->all()); + $this->assertSame($expectedResponse, $api->all()); + } + + public static function getAllData(): array + { + return [ + 'array response' => ['["API Response"]', 'application/json', ['API Response']], + 'string response' => ['"string"', 'application/json', 'Could not convert response body into array: "string"'], + 'false response' => ['', 'application/json', false], + ]; } /**