diff --git a/CHANGELOG.md b/CHANGELOG.md index f80ee679..3dbf5ed6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/kbsali/php-redmine-api/compare/v2.0.1...v2.x) +### Added + +- New interface `Redmine\Exception` that is implemented by every library-related exception +- New exception `Redmine\Exception\ClientException` for client related exceptions +- New exception `Redmine\Exception\InvalidApiNameException` if an invalid API instance is requested +- New exception `Redmine\Exception\InvalidParameterException` for invalid parameter provided to an API instance +- New exception `Redmine\Exception\MissingParameterException` for missing parameter while using an API instance + ### Changed - Switched from Travis-CI to Github Actions diff --git a/docs/migrate-to-psr18client.md b/docs/migrate-to-psr18client.md index e3cc8fdd..35066a7d 100644 --- a/docs/migrate-to-psr18client.md +++ b/docs/migrate-to-psr18client.md @@ -1,6 +1,6 @@ # Migrate from `Redmine\Client` to `Redmine\Client\Psr18Client` -Since `php-redmine-api` v1.7.0 there is a new PSR-18 based client `Redmine\Client\Psr18Client`. This guide will help you to migrate your code if you want to use an app-wide PSR-18 HTTP client. +Since `v1.7.0` there is a new PSR-18 based client `Redmine\Client\Psr18Client`. This guide will help you to migrate your code if you want to use an app-wide PSR-18 HTTP client. ## 1. Use new client methods diff --git a/docs/usage.md b/docs/usage.md index 149ff130..07110609 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -51,6 +51,8 @@ client. #### Native cURL Client `Redmine\Client\NativeCurlClient` +> :bulb: The `Redmine\Client\NativeCurlClient` class was introduced in `v1.8.0`. + Every client requires a URL to your Redmine instance and either a valid Apikey... @@ -100,6 +102,8 @@ $client = new \Redmine\Client('https://redmine.example.com', '1234567890abcdfgh' #### Psr-18 compatible Client `Redmine\Client\Psr18Client` +> :bulb: The `Redmine\Client\Psr18Client` class was introduced in `v1.7.0`. + The `Psr18Client` requires - a `Psr\Http\Client\ClientInterface` implementation (like guzzlehttp/guzzle) ([possible implementations](https://packagist.org/providers/psr/http-client-implementation)) @@ -117,7 +121,7 @@ The `Psr18Client` requires require_once 'vendor/autoload.php'; + +$guzzle = \GuzzleHttp\Client(); -+$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); ++$psr17Factory = new \GuzzleHttp\Psr7\HttpFactory(); + +// Instantiate with ApiKey +$client = new Redmine\Client\Prs18Client($guzzle, $psr17Factory, $psr17Factory, 'https://redmine.example.com', '1234567890abcdfgh'); @@ -147,7 +151,7 @@ require_once 'vendor/autoload.php'; +use Psr\Http\Message\ResponseInterface; + $guzzle = \GuzzleHttp\Client(); -$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); +$psr17Factory = new \GuzzleHttp\Psr7\HttpFactory(); +$guzzleWrapper = new class(\GuzzleHttp\Client $guzzle) implements ClientInterface +{ @@ -195,6 +199,22 @@ $client->startImpersonateUser('kim'); $client->stopImpersonateUser(); ``` +## Error handling + +Every exception implement the interface `Redmine\Exception` making it easy to catch Redmine specific issues. + +> :bulb: The `Redmine\Exception` interface was introduced in `v2.1.0`. + +```php +try { + $client->getApi('issue')->create($data); +} catch (\Redmine\Exception $e) { + // exceptions from kbsali/redmine-api +} catch (\Throwable $e) { + // other errors +} +``` + ## API You can now use the `getApi()` method to create and get a specific Redmine API. diff --git a/src/Redmine/Api/Group.php b/src/Redmine/Api/Group.php index 65eef20b..3b364af9 100644 --- a/src/Redmine/Api/Group.php +++ b/src/Redmine/Api/Group.php @@ -2,6 +2,9 @@ namespace Redmine\Api; +use Exception; +use Redmine\Exception\MissingParameterException; + /** * Handling of groups. * @@ -56,7 +59,7 @@ public function listing($forceUpdate = false) * * @param array $params the new group data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return \SimpleXMLElement */ @@ -71,7 +74,7 @@ public function create(array $params = []) if ( !isset($params['name']) ) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `name`'); } $xml = $this->buildXML($params); @@ -86,11 +89,11 @@ public function create(array $params = []) * * @param int $id * - * @throws \Exception Not implemented + * @throws Exception Not implemented */ public function update($id, array $params = []) { - throw new \Exception('Not implemented'); + throw new Exception('Not implemented'); } /** diff --git a/src/Redmine/Api/IssueCategory.php b/src/Redmine/Api/IssueCategory.php index 5e2cbba6..66eaed62 100644 --- a/src/Redmine/Api/IssueCategory.php +++ b/src/Redmine/Api/IssueCategory.php @@ -2,6 +2,8 @@ namespace Redmine\Api; +use Redmine\Exception\MissingParameterException; + /** * Listing issue categories, creating, editing. * @@ -91,7 +93,7 @@ public function show($id) * @param string|int $project project id or literal identifier * @param array $params the new issue category data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return string|false */ @@ -106,7 +108,7 @@ public function create($project, array $params = []) if ( !isset($params['name']) ) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `name`'); } $xml = new \SimpleXMLElement(''); diff --git a/src/Redmine/Api/Membership.php b/src/Redmine/Api/Membership.php index a552e3d3..99695f5f 100644 --- a/src/Redmine/Api/Membership.php +++ b/src/Redmine/Api/Membership.php @@ -2,6 +2,8 @@ namespace Redmine\Api; +use Redmine\Exception\MissingParameterException; + /** * Handling project memberships. * @@ -38,7 +40,7 @@ public function all($project, array $params = []) * @param string|int $project project id or literal identifier * @param array $params the new membership data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return string|false */ @@ -51,7 +53,7 @@ public function create($project, array $params = []) $params = $this->sanitizeParams($defaults, $params); if (!isset($params['user_id']) || !isset($params['role_ids'])) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `user_id`, `role_ids`'); } $xml = $this->buildXML($params); @@ -67,7 +69,7 @@ public function create($project, array $params = []) * @param int $id id of the membership * @param array $params the new membership data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return string|false */ @@ -79,7 +81,7 @@ public function update($id, array $params = []) $params = $this->sanitizeParams($defaults, $params); if (!isset($params['role_ids'])) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Missing mandatory parameters'); } $xml = $this->buildXML($params); diff --git a/src/Redmine/Api/Project.php b/src/Redmine/Api/Project.php index c7c97b75..d12f5d1a 100755 --- a/src/Redmine/Api/Project.php +++ b/src/Redmine/Api/Project.php @@ -2,6 +2,8 @@ namespace Redmine\Api; +use Redmine\Exception\MissingParameterException; + /** * Listing projects, creating, editing. * @@ -98,7 +100,7 @@ public function show($id, array $params = []) * * @param array $params the new project data * - * @throws \Exception + * @throws MissingParameterException * * @return \SimpleXMLElement */ @@ -115,7 +117,7 @@ public function create(array $params = []) !isset($params['name']) || !isset($params['identifier']) ) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `name`, `identifier`'); } $xml = $this->prepareParamsXml($params); diff --git a/src/Redmine/Api/TimeEntry.php b/src/Redmine/Api/TimeEntry.php index 81966b22..e91491f0 100644 --- a/src/Redmine/Api/TimeEntry.php +++ b/src/Redmine/Api/TimeEntry.php @@ -2,6 +2,8 @@ namespace Redmine\Api; +use Redmine\Exception\MissingParameterException; + /** * Listing time entries, creating, editing. * @@ -50,7 +52,7 @@ public function show($id) * * @param array $params the new time entry data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return string|false */ @@ -70,7 +72,7 @@ public function create(array $params = []) (!isset($params['issue_id']) && !isset($params['project_id'])) || !isset($params['hours']) ) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `issue_id` or `project_id`, `hours`'); } $xml = new \SimpleXMLElement(''); diff --git a/src/Redmine/Api/User.php b/src/Redmine/Api/User.php index dde78bbf..d1f70b82 100644 --- a/src/Redmine/Api/User.php +++ b/src/Redmine/Api/User.php @@ -2,6 +2,8 @@ namespace Redmine\Api; +use Redmine\Exception\MissingParameterException; + /** * Listing users, creating, editing. * @@ -129,7 +131,7 @@ public function show($id, array $params = []) * * @param array $params the new user data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return string|false */ @@ -150,7 +152,7 @@ public function create(array $params = []) || !isset($params['firstname']) || !isset($params['mail']) ) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `login`, `lastname`, `firstname`, `mail`'); } $xml = new \SimpleXMLElement(''); foreach ($params as $k => $v) { diff --git a/src/Redmine/Api/Version.php b/src/Redmine/Api/Version.php index 5685b680..121dad71 100644 --- a/src/Redmine/Api/Version.php +++ b/src/Redmine/Api/Version.php @@ -2,6 +2,9 @@ namespace Redmine\Api; +use Redmine\Exception\InvalidParameterException; +use Redmine\Exception\MissingParameterException; + /** * Listing versions, creating, editing. * @@ -94,7 +97,7 @@ public function show($id) * @param string|int $project project id or literal identifier * @param array $params the new issue category data * - * @throws \Exception Missing mandatory parameters + * @throws MissingParameterException Missing mandatory parameters * * @return string|false */ @@ -112,7 +115,7 @@ public function create($project, array $params = []) if ( !isset($params['name']) ) { - throw new \Exception('Missing mandatory parameters'); + throw new MissingParameterException('Theses parameters are mandatory: `name`'); } $this->validateStatus($params); $this->validateSharing($params); @@ -171,7 +174,7 @@ private function validateStatus(array $params = []) 'closed', ]; if (isset($params['status']) && !in_array($params['status'], $arrStatus)) { - throw new \Exception('Possible values for status : '.implode(', ', $arrStatus)); + throw new InvalidParameterException('Possible values for status : '.implode(', ', $arrStatus)); } } @@ -185,7 +188,7 @@ private function validateSharing(array $params = []) 'system' => 'With all projects', ]; if (isset($params['sharing']) && !isset($arrSharing[$params['sharing']])) { - throw new \Exception('Possible values for sharing : '.implode(', ', array_keys($arrSharing))); + throw new InvalidParameterException('Possible values for sharing : '.implode(', ', array_keys($arrSharing))); } } diff --git a/src/Redmine/Client/Client.php b/src/Redmine/Client/Client.php index e7daf2f2..610b5234 100644 --- a/src/Redmine/Client/Client.php +++ b/src/Redmine/Client/Client.php @@ -2,8 +2,8 @@ namespace Redmine\Client; -use InvalidArgumentException; use Redmine\Api; +use Redmine\Exception\InvalidApiNameException; /** * client interface. @@ -11,7 +11,7 @@ interface Client { /** - * @throws InvalidArgumentException if $name is not a valid api name + * @throws InvalidApiNameException if $name is not a valid api name */ public function getApi(string $name): Api; diff --git a/src/Redmine/Client/ClientApiTrait.php b/src/Redmine/Client/ClientApiTrait.php index 2c1af81b..b8b47bb4 100644 --- a/src/Redmine/Client/ClientApiTrait.php +++ b/src/Redmine/Client/ClientApiTrait.php @@ -2,8 +2,8 @@ namespace Redmine\Client; -use InvalidArgumentException; use Redmine\Api; +use Redmine\Exception\InvalidApiNameException; /** * Provide API instantiation to clients. @@ -38,12 +38,12 @@ trait ClientApiTrait ]; /** - * @throws InvalidArgumentException if $name is not a valid api name + * @throws InvalidApiNameException if $name is not a valid api name */ public function getApi(string $name): Api { if (!isset($this->apiClassnames[$name])) { - throw new InvalidArgumentException(sprintf('`%s` is not a valid api. Possible apis are `%s`', $name, implode('`, `', array_keys($this->apiClassnames)))); + throw new InvalidApiNameException(sprintf('`%s` is not a valid api. Possible apis are `%s`', $name, implode('`, `', array_keys($this->apiClassnames)))); } if (isset($this->apiInstances[$name])) { return $this->apiInstances[$name]; diff --git a/src/Redmine/Client/NativeCurlClient.php b/src/Redmine/Client/NativeCurlClient.php index 0b074fd0..4553a788 100644 --- a/src/Redmine/Client/NativeCurlClient.php +++ b/src/Redmine/Client/NativeCurlClient.php @@ -4,8 +4,8 @@ namespace Redmine\Client; -use Exception; use Redmine\Api; +use Redmine\Exception\ClientException; /** * Native cURL client. @@ -210,7 +210,7 @@ private function unsetHttpHeader(string $name): void } /** - * @throws Exception If anything goes wrong on curl request + * @throws ClientException If anything goes wrong on curl request */ private function request(string $method, string $path, string $body = ''): bool { @@ -225,7 +225,7 @@ private function request(string $method, string $path, string $body = ''): bool $curlErrorNumber = curl_errno($curl); if (CURLE_OK !== $curlErrorNumber) { - $e = new Exception(curl_error($curl), $curlErrorNumber); + $e = new ClientException(curl_error($curl), $curlErrorNumber); curl_close($curl); throw $e; } diff --git a/src/Redmine/Client/Psr18Client.php b/src/Redmine/Client/Psr18Client.php index cefc3b28..b7c17509 100644 --- a/src/Redmine/Client/Psr18Client.php +++ b/src/Redmine/Client/Psr18Client.php @@ -4,13 +4,13 @@ namespace Redmine\Client; -use Exception; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamFactoryInterface; +use Redmine\Exception\ClientException; /** * Psr18 client. @@ -136,7 +136,7 @@ public function getLastResponseBody(): string /** * Create and run a request. * - * @throws Exception If anything goes wrong on the request + * @throws ClientException If anything goes wrong on the request * * @return bool true if status code of the response is not 4xx oder 5xx */ @@ -149,7 +149,7 @@ private function runRequest(string $method, string $path, string $body = ''): bo try { $this->lastResponse = $this->httpClient->sendRequest($request); } catch (ClientExceptionInterface $e) { - throw new Exception($e->getMessage(), $e->getCode(), $e); + throw new ClientException($e->getMessage(), $e->getCode(), $e); } return $this->lastResponse->getStatusCode() < 400; diff --git a/src/Redmine/Exception.php b/src/Redmine/Exception.php new file mode 100644 index 00000000..fdc82d4b --- /dev/null +++ b/src/Redmine/Exception.php @@ -0,0 +1,9 @@ +client->getApi('group'); $this->assertInstanceOf('Redmine\Api\Group', $api); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); $api->create(); } diff --git a/tests/Integration/IssueCategoryXmlTest.php b/tests/Integration/IssueCategoryXmlTest.php index ae9c88fe..d7207be7 100644 --- a/tests/Integration/IssueCategoryXmlTest.php +++ b/tests/Integration/IssueCategoryXmlTest.php @@ -5,6 +5,7 @@ use DOMDocument; use Exception; use PHPUnit\Framework\TestCase; +use Redmine\Exception\MissingParameterException; use Redmine\Tests\Fixtures\MockClient; use SimpleXMLElement; @@ -25,8 +26,8 @@ public function testCreateBlank() $api = $this->client->getApi('issue_category'); $this->assertInstanceOf('Redmine\Api\IssueCategory', $api); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); $api->create('aProject'); } diff --git a/tests/Integration/MembershipXmlTest.php b/tests/Integration/MembershipXmlTest.php index 3cce1dc6..0dcd5614 100644 --- a/tests/Integration/MembershipXmlTest.php +++ b/tests/Integration/MembershipXmlTest.php @@ -5,6 +5,7 @@ use DOMDocument; use Exception; use PHPUnit\Framework\TestCase; +use Redmine\Exception\MissingParameterException; use Redmine\Tests\Fixtures\MockClient; use SimpleXMLElement; @@ -25,8 +26,8 @@ public function testCreateBlank() $api = $this->client->getApi('membership'); $this->assertInstanceOf('Redmine\Api\Membership', $api); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `user_id`, `role_ids`'); $api->create('aProject'); } diff --git a/tests/Integration/ProjectXmlTest.php b/tests/Integration/ProjectXmlTest.php index 83624905..88afd570 100644 --- a/tests/Integration/ProjectXmlTest.php +++ b/tests/Integration/ProjectXmlTest.php @@ -5,6 +5,7 @@ use DOMDocument; use Exception; use PHPUnit\Framework\TestCase; +use Redmine\Exception\MissingParameterException; use Redmine\Tests\Fixtures\MockClient; use SimpleXMLElement; @@ -25,8 +26,8 @@ public function testCreateBlank() $api = $this->client->getApi('project'); $this->assertInstanceOf('Redmine\Api\Project', $api); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`, `identifier`'); $api->create(); } diff --git a/tests/Integration/UserXmlTest.php b/tests/Integration/UserXmlTest.php index 90c42a3f..ea858bb7 100644 --- a/tests/Integration/UserXmlTest.php +++ b/tests/Integration/UserXmlTest.php @@ -5,6 +5,7 @@ use DOMDocument; use Exception; use PHPUnit\Framework\TestCase; +use Redmine\Exception\MissingParameterException; use Redmine\Tests\Fixtures\MockClient; use SimpleXMLElement; @@ -25,8 +26,8 @@ public function testCreateBlank() $api = $this->client->getApi('user'); $this->assertInstanceOf('Redmine\Api\User', $api); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `login`, `lastname`, `firstname`, `mail`'); $api->create(); } diff --git a/tests/Unit/Api/GroupTest.php b/tests/Unit/Api/GroupTest.php index 294db809..dd3e2e6b 100644 --- a/tests/Unit/Api/GroupTest.php +++ b/tests/Unit/Api/GroupTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\Group; use Redmine\Client\Client; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\Group @@ -341,7 +342,7 @@ public function testCreateCallsPost() * * @test */ - public function testCreateThrowsExceptionIsNameIsMissing() + public function testCreateThrowsExceptionIfNameIsMissing() { // Test values $postParameter = []; @@ -352,8 +353,8 @@ public function testCreateThrowsExceptionIsNameIsMissing() // Create the object under test $api = new Group($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); // Perform the tests $api->create($postParameter); diff --git a/tests/Unit/Api/IssueCategoryTest.php b/tests/Unit/Api/IssueCategoryTest.php index 83bb3427..c7b6ff8c 100644 --- a/tests/Unit/Api/IssueCategoryTest.php +++ b/tests/Unit/Api/IssueCategoryTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\IssueCategory; use Redmine\Client\Client; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\IssueCategory @@ -353,8 +354,8 @@ public function testCreateThrowsExceptionIfNameIsMissing() // Create the object under test $api = new IssueCategory($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); // Perform the tests $api->create(5, $parameters); diff --git a/tests/Unit/Api/MembershipTest.php b/tests/Unit/Api/MembershipTest.php index 757a8a36..9fb952c1 100644 --- a/tests/Unit/Api/MembershipTest.php +++ b/tests/Unit/Api/MembershipTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\Membership; use Redmine\Client\Client; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\Membership @@ -133,8 +134,8 @@ public function testCreateThrowsExceptionWithEmptyParameters() // Create the object under test $api = new Membership($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `user_id`, `role_ids`'); // Perform the tests $this->assertSame($response, $api->create(5)); @@ -158,8 +159,8 @@ public function testCreateThrowsExceptionIfRoleIdsAreMissingInParameters() // Create the object under test $api = new Membership($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `user_id`, `role_ids`'); // Perform the tests $this->assertSame($response, $api->create(5, ['user_id' => 4])); diff --git a/tests/Unit/Api/ProjectTest.php b/tests/Unit/Api/ProjectTest.php index 2e96f70f..27f757da 100644 --- a/tests/Unit/Api/ProjectTest.php +++ b/tests/Unit/Api/ProjectTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\Project; use Redmine\Client\Client; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\Project @@ -337,8 +338,8 @@ public function testCreateThrowsExceptionWithEmptyParameters() // Create the object under test $api = new Project($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`, `identifier`'); // Perform the tests $this->assertSame($response, $api->create(['id' => 5])); @@ -365,8 +366,8 @@ public function testCreateThrowsExceptionIfIdentifierIsMissingInParameters() // Create the object under test $api = new Project($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`, `identifier`'); // Perform the tests $this->assertSame($response, $api->create($parameters)); @@ -393,8 +394,8 @@ public function testCreateThrowsExceptionIfNameIsMissingInParameters() // Create the object under test $api = new Project($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`, `identifier`'); // Perform the tests $this->assertSame($response, $api->create($parameters)); diff --git a/tests/Unit/Api/TimeEntryTest.php b/tests/Unit/Api/TimeEntryTest.php index 90694674..efb9e92e 100644 --- a/tests/Unit/Api/TimeEntryTest.php +++ b/tests/Unit/Api/TimeEntryTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\TimeEntry; use Redmine\Client\Client; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\TimeEntry @@ -158,8 +159,8 @@ public function testCreateThrowsExceptionWithEmptyParameters() // Create the object under test $api = new TimeEntry($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `issue_id` or `project_id`, `hours`'); // Perform the tests $this->assertSame($response, $api->create(['id' => 5])); @@ -186,8 +187,8 @@ public function testCreateThrowsExceptionIfIssueIdAndProjectIdAreMissingInParame // Create the object under test $api = new TimeEntry($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `issue_id` or `project_id`, `hours`'); // Perform the tests $this->assertSame($response, $api->create($parameters)); @@ -215,8 +216,8 @@ public function testCreateThrowsExceptionIfHoursAreMissingInParameters() // Create the object under test $api = new TimeEntry($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `issue_id` or `project_id`, `hours`'); // Perform the tests $this->assertSame($response, $api->create($parameters)); diff --git a/tests/Unit/Api/UserTest.php b/tests/Unit/Api/UserTest.php index 095b44d1..b1b7c96a 100644 --- a/tests/Unit/Api/UserTest.php +++ b/tests/Unit/Api/UserTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\User; use Redmine\Client\Client; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\User @@ -263,8 +264,8 @@ public function testCreateThrowsExceptionWithEmptyParameters() // Create the object under test $api = new User($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `login`, `lastname`, `firstname`, `mail`'); // Perform the tests $this->assertSame($response, $api->create()); @@ -288,8 +289,8 @@ public function testCreateThrowsExceptionIfValueIsMissingInParameters($parameter // Create the object under test $api = new User($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `login`, `lastname`, `firstname`, `mail`'); // Perform the tests $api->create($parameters); diff --git a/tests/Unit/Api/VersionTest.php b/tests/Unit/Api/VersionTest.php index 230744ec..dfe9c1ed 100644 --- a/tests/Unit/Api/VersionTest.php +++ b/tests/Unit/Api/VersionTest.php @@ -6,6 +6,8 @@ use PHPUnit\Framework\TestCase; use Redmine\Api\Version; use Redmine\Client\Client; +use Redmine\Exception\InvalidParameterException; +use Redmine\Exception\MissingParameterException; /** * @coversDefaultClass \Redmine\Api\Version @@ -211,8 +213,8 @@ public function testCreateThrowsExceptionWithEmptyParameters() // Create the object under test $api = new Version($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); // Perform the tests $api->create(5); @@ -238,8 +240,8 @@ public function testCreateThrowsExceptionWithMissingNameInParameters() // Create the object under test $api = new Version($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); // Perform the tests $api->create(5, $parameters); @@ -267,8 +269,8 @@ public function testCreateThrowsExceptionWithInvalidStatus() // Create the object under test $api = new Version($client); - $this->expectException(Exception::class); - $this->expectExceptionMessage('Missing mandatory parameters'); + $this->expectException(MissingParameterException::class); + $this->expectExceptionMessage('Theses parameters are mandatory: `name`'); // Perform the tests $api->create('test', $parameters); @@ -377,7 +379,7 @@ public function testUpdateThrowsExceptionWithInvalidStatus() // Create the object under test $api = new Version($client); - $this->expectException(Exception::class); + $this->expectException(InvalidParameterException::class); $this->expectExceptionMessage('Possible values for status : open, locked, closed'); // Perform the tests @@ -757,7 +759,7 @@ public function testCreateThrowsExceptionWithInvalidSharing($sharingValue) // Create the object under test $api = new Version($client); - $this->expectException(Exception::class); + $this->expectException(InvalidParameterException::class); $this->expectExceptionMessage('Possible values for sharing : none, descendants, hierarchy, tree, system'); // Perform the tests @@ -880,7 +882,7 @@ public function testUpdateThrowsExceptionWithInvalidSharing($sharingValue) // Update the object under test $api = new Version($client); - $this->expectException(Exception::class); + $this->expectException(InvalidParameterException::class); $this->expectExceptionMessage('Possible values for sharing : none, descendants, hierarchy, tree, system'); // Perform the tests diff --git a/tests/Unit/Exception/ClientExceptionTest.php b/tests/Unit/Exception/ClientExceptionTest.php new file mode 100644 index 00000000..c0195534 --- /dev/null +++ b/tests/Unit/Exception/ClientExceptionTest.php @@ -0,0 +1,22 @@ +assertInstanceOf(Exception::class, $exception); + $this->assertInstanceOf(RedmineException::class, $exception); + } +} diff --git a/tests/Unit/Exception/InvalidApiNameExceptionTest.php b/tests/Unit/Exception/InvalidApiNameExceptionTest.php new file mode 100644 index 00000000..838aade8 --- /dev/null +++ b/tests/Unit/Exception/InvalidApiNameExceptionTest.php @@ -0,0 +1,22 @@ +assertInstanceOf(InvalidArgumentException::class, $exception); + $this->assertInstanceOf(RedmineException::class, $exception); + } +} diff --git a/tests/Unit/Exception/InvalidParameterExceptionTest.php b/tests/Unit/Exception/InvalidParameterExceptionTest.php new file mode 100644 index 00000000..0324d9f4 --- /dev/null +++ b/tests/Unit/Exception/InvalidParameterExceptionTest.php @@ -0,0 +1,24 @@ +assertInstanceOf(Exception::class, $exception); + $this->assertInstanceOf(InvalidArgumentException::class, $exception); + $this->assertInstanceOf(RedmineException::class, $exception); + } +} diff --git a/tests/Unit/Exception/MissingParameterExceptionTest.php b/tests/Unit/Exception/MissingParameterExceptionTest.php new file mode 100644 index 00000000..31b00c0e --- /dev/null +++ b/tests/Unit/Exception/MissingParameterExceptionTest.php @@ -0,0 +1,24 @@ +assertInstanceOf(Exception::class, $exception); + $this->assertInstanceOf(InvalidArgumentException::class, $exception); + $this->assertInstanceOf(RedmineException::class, $exception); + } +}