diff --git a/src/Redmine/Api/TimeEntryActivity.php b/src/Redmine/Api/TimeEntryActivity.php index c3e5bcd9..23adbe65 100644 --- a/src/Redmine/Api/TimeEntryActivity.php +++ b/src/Redmine/Api/TimeEntryActivity.php @@ -2,6 +2,8 @@ namespace Redmine\Api; +use Redmine\Exception; + /** * Listing time entry activities. * @@ -34,13 +36,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 time entry activities found + * @return array|string|false list of time entry activities 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/TimeEntryActivityTest.php b/tests/Unit/Api/TimeEntryActivityTest.php index da1a5ce6..492e563e 100644 --- a/tests/Unit/Api/TimeEntryActivityTest.php +++ b/tests/Unit/Api/TimeEntryActivityTest.php @@ -44,34 +44,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( - $this->stringStartsWith('/enumerations/time_entry_activities.json') - ) + ->with('/enumerations/time_entry_activities.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 TimeEntryActivity($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], + ]; } /**