Skip to content

Commit

Permalink
let IssuePriority::list() throw UnexpectedResponseException
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Dec 28, 2023
1 parent d29c098 commit fa925b7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Redmine/Api/IssuePriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Redmine\Exception;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;

/**
* Listing issue priorities.
Expand All @@ -23,13 +24,17 @@ class IssuePriority 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 issue priorities found
*/
final public function list(array $params = []): array
{
$this->issuePriorities = $this->retrieveData('/enumerations/issue_priorities.json', $params);
try {
$this->issuePriorities = $this->retrieveData('/enumerations/issue_priorities.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
}

return $this->issuePriorities;
}
Expand All @@ -56,6 +61,10 @@ public function all(array $params = [])
return false;
}

if ($e instanceof UnexpectedResponseException && $e->getPrevious() !== null) {
$e = $e->getPrevious();
}

return $e->getMessage();
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Api/IssuePriority/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\IssuePriority;
use Redmine\Client\Client;
use Redmine\Exception\UnexpectedResponseException;

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

public function testListThrowsException()
{
// Create the used mock objects
$client = $this->createMock(Client::class);
$client->expects($this->exactly(1))
->method('requestGet')
->with('/enumerations/issue_priorities.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 IssuePriority($client);

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

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

0 comments on commit fa925b7

Please sign in to comment.