Skip to content

Commit

Permalink
let News::list() and News::listByProject() throw UnexpectedResponseEx…
Browse files Browse the repository at this point in the history
…ception
  • Loading branch information
Art4 committed Dec 29, 2023
1 parent 6508e1f commit 9780ffb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Redmine/Api/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Redmine\Exception;
use Redmine\Exception\InvalidParameterException;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;

/**
* @see http://www.redmine.org/projects/redmine/wiki/Rest_News
Expand All @@ -24,7 +25,7 @@ class News extends AbstractApi
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
*
* @throws InvalidParameterException if $projectIdentifier is not of type int or string
* @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 news found
*/
Expand All @@ -37,7 +38,11 @@ final public function listByProject($projectIdentifier, array $params = []): arr
));
}

$this->news = $this->retrieveData('/projects/'.strval($projectIdentifier).'/news.json', $params);
try {
$this->news = $this->retrieveData('/projects/'.strval($projectIdentifier).'/news.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
}

return $this->news;
}
Expand All @@ -49,13 +54,17 @@ final public function listByProject($projectIdentifier, array $params = []): arr
*
* @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 news found
*/
final public function list(array $params = []): array
{
$this->news = $this->retrieveData('/news.json', $params);
try {
$this->news = $this->retrieveData('/news.json', $params);
} catch (SerializerException $th) {
throw new UnexpectedResponseException('The Redmine server responded with an unexpected body.', $th->getCode(), $th);
}

return $this->news;
}
Expand Down Expand Up @@ -87,6 +96,10 @@ public function all($project = null, 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/News/ListByProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Redmine\Api\News;
use Redmine\Client\Client;
use Redmine\Exception\InvalidParameterException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Tests\Fixtures\MockClient;
use stdClass;

Expand Down Expand Up @@ -93,4 +94,29 @@ public static function getInvalidProjectIdentifiers(): array
'object' => [new stdClass()],
];
}

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

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

// Perform the tests
$api->listByProject(5);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Api/News/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\News;
use Redmine\Client\Client;
use Redmine\Exception\UnexpectedResponseException;

/**
* @covers \Redmine\Api\News::list
Expand Down Expand Up @@ -63,4 +64,29 @@ public function testListWithParametersReturnsResponse()
// 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('/news.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 News($client);

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

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

0 comments on commit 9780ffb

Please sign in to comment.