Skip to content

Commit

Permalink
let Group::all() throw UnexpectedResponseException
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Dec 28, 2023
1 parent cfa3527 commit dccdc9c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Redmine/Api/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final public function list(array $params = []): array
try {
$this->customFields = $this->retrieveData('/custom_fields.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('Redmine server has responded with an unexpected body.', $th->getCode(), $th);
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
}

return $this->customFields;
Expand Down
13 changes: 11 additions & 2 deletions src/Redmine/Api/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Redmine\Exception;
use Redmine\Exception\MissingParameterException;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Serializer\PathSerializer;
use Redmine\Serializer\XmlSerializer;

Expand All @@ -26,13 +27,17 @@ class Group extends AbstractApi
*
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
*
* @throws SerializerException if response body could not be converted into array
* @throws UnexpectedResponseException if response body could not be converted into array
*
* @return array list of groups found
*/
final public function list(array $params = []): array
{
$this->groups = $this->retrieveData('/groups.json', $params);
try {
$this->groups = $this->retrieveData('/groups.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
}

return $this->groups;
}
Expand All @@ -55,6 +60,10 @@ public function all(array $params = [])
try {
return $this->list($params);
} catch (Exception $e) {
if ($e instanceof UnexpectedResponseException && $e->getPrevious() !== null) {
$e = $e->getPrevious();
}

if ($this->client->getLastResponseBody() === '') {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/CustomField/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function testListThrowsException()
$api = new CustomField($client);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('Redmine server has responded with an unexpected body.');
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');

// Perform the tests
$api->list();
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Api/Group/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Redmine\Api\Group;
use Redmine\Client\Client;
use Redmine\Exception\UnexpectedResponseException;

/**
* @covers \Redmine\Api\Group::list
Expand Down Expand Up @@ -63,4 +64,29 @@ public function testListeWithParametersReturnsResponse()
// Perform the tests
$this->assertSame($expectedReturn, $api->list($parameters));
}

public function testListThrowsException()
{
// Create the used mock objects
$client = $this->createMock(Client::class);
$client->expects($this->exactly(1))
->method('requestGet')
->with('/groups.json')
->willReturn(true);
$client->expects($this->exactly(1))
->method('getLastResponseBody')
->willReturn('');
$client->expects($this->exactly(1))
->method('getLastResponseContentType')
->willReturn('application/json');

// Create the object under test
$api = new Group($client);

$this->expectException(UnexpectedResponseException::class);
$this->expectExceptionMessage('The Redmine server responded with an unexpected body.');

// Perform the tests
$api->list();
}
}

0 comments on commit dccdc9c

Please sign in to comment.