diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 748208c..a558d95 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "liateam/api-response": "^2.0" + "liateam/api-response": "2.*" }, "repositories": { "liateam/api-response" : { @@ -20,12 +20,8 @@ }, "autoload": { "psr-4": { - "Liateam\\ApiExceptions\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "Tests\\": "tests" + "Liateam\\ApiExceptions\\": "src", + "Liateam\\ApiExceptions\\Tests\\" : "tests" } }, "extra": { diff --git a/src/Contracts/ApiException.php b/src/Contracts/ApiException.php deleted file mode 100644 index 9fbd612..0000000 --- a/src/Contracts/ApiException.php +++ /dev/null @@ -1,63 +0,0 @@ -failureResponse = new FailureResponse($code, $message); - $this->message = $message; - $this->code = $code; - $this->previous = $previous; - parent::__construct($message, $code, $previous); - } - - /** - * renders the error for api - * - * @return ResponseContract - */ - public function render() - { - $debugMode = env('APP_DEBUG') ?? false; - - $response = $this->failureResponse - ->setMessage($this->message) - ->setCode($this->code); - - // if the application is in debug mode , - // so attach errors and stack trace to response and return it. - if ($debugMode) { - $response = $response - ->setResponseKey('error') - ->setResponseValue([ - 'previous' => $this->previous, - 'trace' => $this->getTrace(), - 'line' => $this->getLine(), - ]); - } - - return $response; - } -} diff --git a/src/Contracts/ApiExceptionAbstract.php b/src/Contracts/ApiExceptionAbstract.php new file mode 100755 index 0000000..7b946ad --- /dev/null +++ b/src/Contracts/ApiExceptionAbstract.php @@ -0,0 +1,69 @@ +exception = $exception; + parent::__construct($exception->getMessage(), $exception->getCode()); + } + + /** + * renders the error for api + * + * @return ResponseContract + */ + public function render() + { + $this->setCode($this->exception->getCode()); + $this->setMessage($this->exception->getMessage()); + $response = new FailureResponse($this->getCode(), $this->getMessage()); + return $response + ->setResponseKey('error') + ->setResponseValue($this->getErrors()) + ->render(); + } + + public function setErrors($errors) + { + if (env('APP_DEBUG')) { + $this->errors= [ + 'trace' => $this->exception->getTrace(), + 'line' => $this->exception->getLine(), + ]; + } + return $this; + } + + /** + * @return array + */ + public function getErrors() + { + return $this->errors; + } + +} diff --git a/src/Exceptions/CustomAuthenticationException.php b/src/Exceptions/CustomAuthenticationException.php old mode 100644 new mode 100755 index cd1f97f..12b218b --- a/src/Exceptions/CustomAuthenticationException.php +++ b/src/Exceptions/CustomAuthenticationException.php @@ -4,18 +4,43 @@ use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomAuthenticationException extends ApiException +class CustomAuthenticationException extends ApiExceptionAbstract { + /** + * @var Throwable $exception + */ + public $exception; + /** * CustomAuthenticationException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @param $exception + */ + + public function __construct(Throwable $exception) + { + $this->exception = $exception; + parent::__construct($exception); + } + + /** + * @param $code + * @return $this|CustomAuthenticationException + */ + public function setCode($code = null): self + { + $this->code = ($code) ? $code : Response::HTTP_FORBIDDEN; + return $this; + } + + /** + * @param $message + * @return $this|CustomAuthenticationException */ - public function __construct($message = "", $code = Response::HTTP_UNAUTHORIZED, Throwable $previous = null) + public function setMessage($message = null): self { - parent::__construct($message, $code, $previous); + $this->message = !empty($message) ? $message : 'Unauthenticated Exception'; + return $this; } } diff --git a/src/Exceptions/CustomDefaultException.php b/src/Exceptions/CustomDefaultException.php old mode 100644 new mode 100755 index 25416b7..faabd2f --- a/src/Exceptions/CustomDefaultException.php +++ b/src/Exceptions/CustomDefaultException.php @@ -2,19 +2,44 @@ namespace Liateam\ApiExceptions\Exceptions; +use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomDefaultException extends ApiException +class CustomDefaultException extends ApiExceptionAbstract { + /** + * @var Throwable $exception + */ + public $exception; + /** * CustomAuthenticationException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @param $exception + */ + public function __construct(Throwable $exception) + { + parent::__construct($exception); + $this->exception = $exception; + } + + /** + * @param $code + * @return CustomDefaultException + */ + public function setCode($code = null): self + { + $this->code = $code ? $code : Response::HTTP_INTERNAL_SERVER_ERROR; + return $this; + } + + /** + * @param $message + * @return CustomDefaultException */ - public function __construct($message = "", $code = Response::HTTP_UNPROCESSABLE_ENTITY, Throwable $previous = null) + public function setMessage($message = null): self { - parent::__construct($message, $code, $previous); + $this->message = !empty($message) ? $message : 'Whoops! something went wrong!'; + return $this; } } diff --git a/src/Exceptions/CustomModelNotFoundException.php b/src/Exceptions/CustomModelNotFoundException.php old mode 100644 new mode 100755 index 6daa1da..1ccc65c --- a/src/Exceptions/CustomModelNotFoundException.php +++ b/src/Exceptions/CustomModelNotFoundException.php @@ -4,18 +4,42 @@ use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomModelNotFoundException extends ApiException +class CustomModelNotFoundException extends ApiExceptionAbstract { /** - * CustomModelNotFoundException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var Throwable $exception */ - public function __construct($message = "", $code = Response::HTTP_INTERNAL_SERVER_ERROR, Throwable $previous = null) + public $exception; + + /** + * CustomAuthenticationException constructor. + * @param $exception + */ + public function __construct(Throwable $exception) + { + $this->exception = $exception; + parent::__construct($exception); + } + + /** + * @param $code + * @return self + */ + public function setCode($code = null) + { + $this->code = $code ? $code : Response::HTTP_NOT_FOUND; + return $this; + } + + /** + * @param $message + * @return string + */ + public function setMessage($message = null) { - parent::__construct($message, $code, $previous); + $this->message = !empty($message) ? $message : 'Model Not Found'; + return $this; } } diff --git a/src/Exceptions/CustomNotFoundHttpException.php b/src/Exceptions/CustomNotFoundHttpException.php old mode 100644 new mode 100755 index 56d25af..624d137 --- a/src/Exceptions/CustomNotFoundHttpException.php +++ b/src/Exceptions/CustomNotFoundHttpException.php @@ -4,18 +4,43 @@ use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomNotFoundHttpException extends ApiException +class CustomNotFoundHttpException extends ApiExceptionAbstract { /** - * CustomNotFoundHttpException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var Throwable $exception */ - public function __construct($message = "", $code = Response::HTTP_NOT_FOUND, Throwable $previous = null) + public $exception; + + /** + * CustomAuthenticationException constructor. + * @param $exception + */ + public function __construct(Throwable $exception) + { + parent::__construct($exception); + $this->exception = $exception; + } + + /** + * @param null $code + * @return CustomNotFoundHttpException + */ + public function setCode($code = null) + { + + $this->code = $code ? $code : Response::HTTP_NOT_FOUND; + return $this; + } + + /** + * @param null $message + * @return $this|CustomNotFoundHttpException + */ + public function setMessage($message = null) { - parent::__construct($message, $code, $previous); + $this->message = !empty($message) ? $message : 'Not Found'; + return $this; } } diff --git a/src/Exceptions/CustomRouteNotFoundException.php b/src/Exceptions/CustomRouteNotFoundException.php old mode 100644 new mode 100755 index ff42599..1c57f35 --- a/src/Exceptions/CustomRouteNotFoundException.php +++ b/src/Exceptions/CustomRouteNotFoundException.php @@ -4,18 +4,42 @@ use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomRouteNotFoundException extends ApiException +class CustomRouteNotFoundException extends ApiExceptionAbstract { /** - * CustomRouteNotFoundException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var Throwable $exception */ - public function __construct($message = "", $code = Response::HTTP_INTERNAL_SERVER_ERROR, Throwable $previous = null) + public $exception; + + /** + * CustomAuthenticationException constructor. + * @param $exception + */ + public function __construct(Throwable $exception) + { + parent::__construct($exception); + $this->exception = $exception; + } + + /** + * @param $code + * @return self + */ + public function setCode($code = null) + { + $this->code = $code ? $code : Response::HTTP_NOT_FOUND; + return $this; + } + + /** + * @param $message + * @return self + */ + public function setMessage($message = null) { - parent::__construct($message, $code, $previous); + $this->message = !empty($message) ? $message : 'Route not found'; + return $this; } } diff --git a/src/Exceptions/CustomUnauthorizedException.php b/src/Exceptions/CustomUnauthorizedException.php old mode 100644 new mode 100755 index 306d37c..a227f23 --- a/src/Exceptions/CustomUnauthorizedException.php +++ b/src/Exceptions/CustomUnauthorizedException.php @@ -4,18 +4,42 @@ use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomUnauthorizedException extends ApiException +class CustomUnauthorizedException extends ApiExceptionAbstract { /** - * CustomUnauthorizedException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var Throwable $exception */ - public function __construct($message = "", $code = Response::HTTP_FORBIDDEN, Throwable $previous = null) + public $exception; + + /** + * CustomAuthenticationException constructor. + * @param $exception + */ + public function __construct(Throwable $exception) + { + parent::__construct($exception); + $this->exception = $exception; + } + + /** + * @param $code + * @return $this|CustomUnauthorizedException + */ + public function setCode($code = null) + { + $this->code = $code ? $code : Response::HTTP_FORBIDDEN; + return $this; + } + + /** + * @param $message + * @return $this|CustomUnauthorizedException + */ + public function setMessage($message = null) { - parent::__construct($message, $code, $previous); + $this->message = !empty($message) ? $message : 'Unauthorized'; + return $this; } } diff --git a/src/Exceptions/CustomUnexpectedException.php b/src/Exceptions/CustomUnexpectedException.php old mode 100644 new mode 100755 index 7888aa6..9ae974b --- a/src/Exceptions/CustomUnexpectedException.php +++ b/src/Exceptions/CustomUnexpectedException.php @@ -4,9 +4,9 @@ use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomUnexpectedException extends ApiException +class CustomUnexpectedException extends ApiExceptionAbstract { /** * CustomUnexpectedException constructor. @@ -14,8 +14,29 @@ class CustomUnexpectedException extends ApiException * @param int $code * @param Throwable|null $previous */ - public function __construct($message = "", $code = Response::HTTP_BAD_REQUEST, Throwable $previous = null) + public function __construct(Throwable $exception) { - parent::__construct($message, $code, $previous); + parent::__construct($exception); + $this->exception = $exception; + } + + /** + * @param $code + * @return $this|CustomUnauthorizedException + */ + public function setCode($code = null) + { + $this->code = $code ? $code : Response::HTTP_INTERNAL_SERVER_ERROR; + return $this; + } + + /** + * @param $message + * @return $this|CustomUnauthorizedException + */ + public function setMessage($message = null) + { + $this->message = !empty($message) ? $message : 'Unexpected Exception'; + return $this; } } diff --git a/src/Exceptions/CustomValidationException.php b/src/Exceptions/CustomValidationException.php old mode 100644 new mode 100755 index 6bdf743..2488f3f --- a/src/Exceptions/CustomValidationException.php +++ b/src/Exceptions/CustomValidationException.php @@ -2,20 +2,61 @@ namespace Liateam\ApiExceptions\Exceptions; +use Exception; use Throwable; use Illuminate\Http\Response; -use Liateam\ApiExceptions\Contracts\ApiException; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; -class CustomValidationException extends ApiException +class CustomValidationException extends ApiExceptionAbstract { /** - * CustomValidationException constructor. - * @param string $message - * @param int $code - * @param Throwable|null $previous + * @var Throwable $exception */ - public function __construct($message = "", $code = Response::HTTP_UNPROCESSABLE_ENTITY, Throwable $previous = null) + public $exception; + + /** + * CustomAuthenticationException constructor. + * @param $exception + */ + public function __construct(Exception $exception) + { + $this->exception = $exception; + parent::__construct($exception); + $this->setErrors(); + } + + /** + * @param $code + * @return $this + */ + public function setCode($code = null): self { - parent::__construct($message, $code, $previous); + $this->code = $code ?? Response::HTTP_BAD_REQUEST; + return $this; + } + + /** + * @param $message + * @return $this + */ + public function setMessage($message = null) + { + $this->message = $message ?? 'Validation Exception'; + return $this; + } + + /** + * @param array $errors + * @return $this + */ + public function setErrors($errors = null) + { + if (! empty($errors)) { + $this->errors = $errors; + return $this; + } + + $this->errors = $this->exception->validdator->getMessageBag()->all(); + return $this; } } diff --git a/src/Handlers/ApiException.php b/src/Handlers/ApiException.php old mode 100644 new mode 100755 index 9965d7d..65950ce --- a/src/Handlers/ApiException.php +++ b/src/Handlers/ApiException.php @@ -3,7 +3,6 @@ namespace Liateam\ApiExceptions\Handlers; use Throwable; -use Illuminate\Http\Response; use Liateam\ApiResponse\Contracts\ResponseContract; use Liateam\ApiExceptions\Exceptions\CustomDefaultException; @@ -29,17 +28,9 @@ class ApiException public static function handle(Throwable $exception) { $customException = static::getCustomException($exception); - $message = static::prepareMessage($exception); - $code = static::prepareCode($exception); - - if (class_exists($customException)) { - - return (new $customException($message, $code)) - ->render(); - } - - return (new CustomDefaultException($message, $code)) - ->render(); + $exceptionObject = (class_exists($customException)) ? new $customException($exception) : new CustomDefaultException($exception); + + return $exceptionObject; } /** @@ -50,33 +41,12 @@ public static function handle(Throwable $exception) */ private static function getCustomException($exception) { - $exceptions = config('exceptions.list'); $class = get_class($exception); - if (array_key_exists($class, $exceptions)) { - return $exceptions[$class]; - } - - return ''; - } - - private static function prepareCode(Throwable $exception) - { - $code = $exception->getCode(); - if ($code === 0) { - return Response::HTTP_NOT_ACCEPTABLE; - } - - return $code; - } - - private static function prepareMessage(Throwable $exception) - { - $message = $exception->getMessage(); - if (!empty($message)) { - return $message; + if (array_key_exists($class, config('exceptions.list'))) { + return config('exceptions.list')[$class]; } - return 'Whoops! something went wrong... :('; + return null; } } diff --git a/src/Providers/ApiExceptionServiceProvider.php b/src/Providers/ApiExceptionServiceProvider.php old mode 100644 new mode 100755 index 42a1be5..514bed2 --- a/src/Providers/ApiExceptionServiceProvider.php +++ b/src/Providers/ApiExceptionServiceProvider.php @@ -4,6 +4,9 @@ use Illuminate\Support\ServiceProvider; +/** + * @codeCoverageIgnore + */ class ApiExceptionServiceProvider extends ServiceProvider { /** diff --git a/src/config/exceptions.php b/src/config/exceptions.php old mode 100644 new mode 100755 index 8353079..ebfdec7 --- a/src/config/exceptions.php +++ b/src/config/exceptions.php @@ -1,5 +1,9 @@ method = $this->class->getMethod('render'); $this->method->setAccessible(true); $this->expected = FailureResponse::class; + + $this->faker = Factory::create(); } + + /** + * Creates the application. + * + * @return \Laravel\Lumen\Application + */ + public function createApplication() + { + return require __DIR__.'/../../../../bootstrap/app.php'; + } } diff --git a/tests/Feature/HandlerTest.php b/tests/Feature/HandlerTest.php new file mode 100644 index 0000000..1edf313 --- /dev/null +++ b/tests/Feature/HandlerTest.php @@ -0,0 +1,140 @@ +faker->sentence; + $code = Response::HTTP_FORBIDDEN; + + $response = ApiException::handle( + new CustomAuthenticationException( + new Exception($fakeTest, $code) + ) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } + + + public function test_it_can_handle_default_exception(): void + { + $fakeTest = $this->faker->sentence; + $code = Response::HTTP_INTERNAL_SERVER_ERROR; + + $response = ApiException::handle( + new Exception($fakeTest, $code) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } + + + public function test_it_can_handle_model_not_found_exception(): void + { + $fakeTest = $this->faker->sentence; + $code = Response::HTTP_NOT_FOUND; + + $response = ApiException::handle( + new CustomModelNotFoundException( + new Exception($fakeTest, $code) + ) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } + + + public function test_it_can_handle_not_found_http_exception(): void + { + $fakeTest = $this->faker->sentence; + $code = Response::HTTP_NOT_FOUND; + + $response = ApiException::handle( + new CustomNotFoundHttpException( + new Exception($fakeTest, $code) + ) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } + + + public function test_it_can_handle_route_not_found_exception(): void + { + $fakeTest = $this->faker->sentence; + $code = Response::HTTP_NOT_FOUND; + + $response = ApiException::handle( + new CustomRouteNotFoundException( + new Exception($fakeTest, $code) + ) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } + + + public function test_it_can_handle_unauthorized_exception(): void + { + $fakeTest = $this->faker->sentence; + $code = Response::HTTP_UNAUTHORIZED; + + $response = ApiException::handle( + new CustomUnauthorizedException( + new Exception($fakeTest, $code) + ) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } + + + public function test_it_can_handle_unexpected_exception(): void + { + $fakeTest = $this->faker->sentence; + $code = Response::HTTP_INTERNAL_SERVER_ERROR; + + $response = ApiException::handle( + new CustomUnexpectedException( + new Exception($fakeTest, $code) + ) + )->render(); + + self::assertInstanceOf(JsonResponse::class, $response); + self::assertEquals($fakeTest, $response->getData()->message); + self::assertEquals($code, $response->getStatusCode()); + } +} diff --git a/tests/Unit/CustomAuthenticationExceptionTest.php b/tests/Unit/CustomAuthenticationExceptionTest.php old mode 100644 new mode 100755 index 76bc02b..45e47a5 --- a/tests/Unit/CustomAuthenticationExceptionTest.php +++ b/tests/Unit/CustomAuthenticationExceptionTest.php @@ -2,24 +2,51 @@ namespace Liateam\ApiExceptions\Tests\Unit; +use Mockery\Exception; use Illuminate\Http\Response; -use Liateam\ApiException\Exceptions\CustomAuthenticationException; -use Liateam\ApiException\Tests\BaseTestCase; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Exceptions\CustomAuthenticationException; class CustomAuthenticationExceptionTest extends BaseTestCase { - /** - * @throws \Throwable - * @covers CustomAuthenticationException::render(); - */ - public function test_can_render_authentication_exception() : void + + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomAuthenticationException(new Exception))->render(); + } + + + public function test_custom_authentication_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomAuthenticationException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_authentication_exception(): void + { + self::assertEquals(Response::HTTP_FORBIDDEN, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected, $actual); - $this->assertEquals(Response::HTTP_UNAUTHORIZED, $actual->getCode()); + + public function test_can_override_code_from_authentication_exception(): void + { + $exception = (new CustomAuthenticationException((new Exception('',Response::HTTP_NOT_FOUND))))->render(); + self::assertEquals(Response::HTTP_NOT_FOUND, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_authentication_exception(): void + { + self::assertEquals('Unauthenticated Exception' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_authentication_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomAuthenticationException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomDefaultExceptionTest.php b/tests/Unit/CustomDefaultExceptionTest.php old mode 100644 new mode 100755 index d4ed373..85166c3 --- a/tests/Unit/CustomDefaultExceptionTest.php +++ b/tests/Unit/CustomDefaultExceptionTest.php @@ -2,22 +2,51 @@ namespace Liateam\ApiExceptions\Tests\Unit; -use Liateam\ApiException\Tests\BaseTestCase; -use Liateam\ApiException\Exceptions\CustomDefaultException; +use Mockery\Exception; +use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Exceptions\CustomDefaultException; class CustomDefaultExceptionTest extends BaseTestCase { - /** - * @throws \Throwable - * @covers \Liateam\ApiException\Exceptions\CustomDefaultException::render() - */ - public function test_can_render_default_exception(): void + + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomDefaultException(new Exception))->render(); + } + + + public function test_custom_Default_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomDefaultException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_Default_exception(): void + { + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected, $actual); + + public function test_can_override_code_from_Default_exception(): void + { + $exception = (new CustomDefaultException((new Exception('',Response::HTTP_NOT_FOUND))))->render(); + self::assertEquals(Response::HTTP_NOT_FOUND, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_Default_exception(): void + { + self::assertEquals('Whoops! something went wrong!' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_Default_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomDefaultException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomModelNotFoundExceptionTest.php b/tests/Unit/CustomModelNotFoundExceptionTest.php old mode 100644 new mode 100755 index abec9c4..c87c8c7 --- a/tests/Unit/CustomModelNotFoundExceptionTest.php +++ b/tests/Unit/CustomModelNotFoundExceptionTest.php @@ -2,24 +2,51 @@ namespace Liateam\ApiExceptions\Tests\Unit; +use Mockery\Exception; use Illuminate\Http\Response; -use Liateam\ApiException\Exceptions\CustomModelNotFoundException; -use Liateam\ApiException\Tests\BaseTestCase; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Exceptions\CustomModelNotFoundException; class CustomModelNotFoundExceptionTest extends BaseTestCase { - /** - * @throws \Throwable - * @covers CustomModelNotFoundException::render() - */ - public function test_can_render_model_not_found_exception(): void + + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomModelNotFoundException(new Exception))->render(); + } + + + public function test_custom_mode_not_found_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomModelNotFoundException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_mode_not_found_exception(): void + { + self::assertEquals(Response::HTTP_NOT_FOUND, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected, $actual); - $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $actual->getCode()); + + public function test_can_override_code_from_mode_not_found_exception(): void + { + $exception = (new CustomModelNotFoundException((new Exception('',Response::HTTP_INTERNAL_SERVER_ERROR))))->render(); + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_mode_not_found_exception(): void + { + self::assertEquals('Model Not Found' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_mode_not_found_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomModelNotFoundException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomNotFoundHttpExceptionTest.php b/tests/Unit/CustomNotFoundHttpExceptionTest.php old mode 100644 new mode 100755 index 94c61dc..aa0ebce --- a/tests/Unit/CustomNotFoundHttpExceptionTest.php +++ b/tests/Unit/CustomNotFoundHttpExceptionTest.php @@ -2,25 +2,51 @@ namespace Liateam\ApiExceptions\Tests\Unit; +use Mockery\Exception; use Illuminate\Http\Response; -use Throwable; -use Liateam\ApiException\Tests\BaseTestCase; -use Liateam\ApiException\Exceptions\CustomNotFoundHttpException; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Exceptions\CustomNotFoundHttpException; class CustomNotFoundHttpExceptionTest extends BaseTestCase { - /** - * @throws Throwable - * @covers CustomNotFoundHttpException::render() - */ - public function test_can_render_http_not_found_exception(): void + + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomNotFoundHttpException(new Exception))->render(); + } + + + public function test_custom_not_found_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomNotFoundHttpException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_not_found_exception(): void + { + self::assertEquals(Response::HTTP_NOT_FOUND, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected, $actual); - $this->assertEquals(Response::HTTP_NOT_FOUND, $actual->getCode()); + + public function test_can_override_code_from_not_found_exception(): void + { + $exception = (new CustomNotFoundHttpException((new Exception('',Response::HTTP_INTERNAL_SERVER_ERROR))))->render(); + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_not_found_exception(): void + { + self::assertEquals('Not Found' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_not_found_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomNotFoundHttpException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomRouteNotFoundExceptionTest.php b/tests/Unit/CustomRouteNotFoundExceptionTest.php old mode 100644 new mode 100755 index 76d0587..18b8231 --- a/tests/Unit/CustomRouteNotFoundExceptionTest.php +++ b/tests/Unit/CustomRouteNotFoundExceptionTest.php @@ -2,24 +2,54 @@ namespace Liateam\ApiExceptions\Tests\Unit; +use Throwable; +use Mockery\Exception; use Illuminate\Http\Response; -use Liateam\ApiException\Exceptions\CustomRouteNotFoundException; -use Liateam\ApiException\Tests\BaseTestCase; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Contracts\ApiExceptionAbstract; +use Liateam\ApiExceptions\Exceptions\CustomRouteNotFoundException; class CustomRouteNotFoundExceptionTest extends BaseTestCase { - /** - * @throws \Throwable - * * @covers CustomRouteNotFoundException::render(); - */ - public function test_can_render_route_not_found_exception(): void + + + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomRouteNotFoundException(new Exception))->render(); + } + + + public function test_custom_not_found_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomRouteNotFoundException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_not_found_exception(): void + { + self::assertEquals(Response::HTTP_NOT_FOUND, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected,$actual); - $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $actual->getCode()); + + public function test_can_override_code_from_not_found_exception(): void + { + $exception = (new CustomRouteNotFoundException((new Exception('',Response::HTTP_INTERNAL_SERVER_ERROR))))->render(); + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_not_found_exception(): void + { + self::assertEquals('Route not found' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_not_found_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomRouteNotFoundException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomUnauthorizedExceptionTest.php b/tests/Unit/CustomUnauthorizedExceptionTest.php old mode 100644 new mode 100755 index 9e7f80e..e5f3b70 --- a/tests/Unit/CustomUnauthorizedExceptionTest.php +++ b/tests/Unit/CustomUnauthorizedExceptionTest.php @@ -2,26 +2,50 @@ namespace Liateam\ApiExceptions\Tests\Unit; +use Mockery\Exception; use Illuminate\Http\Response; -use Throwable; -use Liateam\ApiResponse\ApiResponse; -use Liateam\ApiException\Tests\BaseTestCase; -use Liateam\ApiException\Exceptions\CustomUnauthorizedException; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Exceptions\CustomUnauthorizedException; class CustomUnauthorizedExceptionTest extends BaseTestCase -{ - /** - * @throws Throwable - * @covers CustomUnauthorizedException::render() - */ - public function test_can_render_unauthorized_exception(): void +{ + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomUnauthorizedException(new Exception))->render(); + } + + + public function test_custom_not_found_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomUnauthorizedException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_not_found_exception(): void + { + self::assertEquals(Response::HTTP_FORBIDDEN, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected, $actual); - $this->assertEquals(Response::HTTP_FORBIDDEN , $actual->getCode()); + + public function test_can_override_code_from_not_found_exception(): void + { + $exception = (new CustomUnauthorizedException((new Exception('',Response::HTTP_INTERNAL_SERVER_ERROR))))->render(); + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_not_found_exception(): void + { + self::assertEquals('Unauthorized' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_not_found_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomUnauthorizedException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomUnexpectedExceptionTest.php b/tests/Unit/CustomUnexpectedExceptionTest.php old mode 100644 new mode 100755 index 8966d9a..f43ea6b --- a/tests/Unit/CustomUnexpectedExceptionTest.php +++ b/tests/Unit/CustomUnexpectedExceptionTest.php @@ -2,24 +2,50 @@ namespace Liateam\ApiExceptions\Tests\Unit; +use Mockery\Exception; use Illuminate\Http\Response; -use Liateam\ApiException\Exceptions\CustomUnexpectedException; -use Liateam\ApiException\Tests\BaseTestCase; +use Illuminate\Http\JsonResponse; +use Liateam\ApiExceptions\Tests\BaseTestCase; +use Liateam\ApiExceptions\Exceptions\CustomUnexpectedException; class CustomUnexpectedExceptionTest extends BaseTestCase { - /** - * @throws \Throwable - * @covers CustomUnexpectedException::render - */ - public function test_can_render_unexpected_exception(): void + private $instance; + + public function setUp(): void + { + parent::setUp(); + $this->instance = (new CustomUnexpectedException(new Exception))->render(); + } + + + public function test_custom_not_found_is_instance_of_ApiException(): void { - $actual = $this->handler->render( - $this->request, - new CustomUnexpectedException - ); + self::assertInstanceOf(JsonResponse::class, $this->instance); + } + + + public function test_can_get_correct_code_from_not_found_exception(): void + { + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $this->instance->getData()->code); + } - $this->assertInstanceOf($this->expected, $actual); - $this->assertEquals(Response::HTTP_BAD_REQUEST, $actual->getCode()); + + public function test_can_override_code_from_not_found_exception(): void + { + $exception = (new CustomUnexpectedException((new Exception('',Response::HTTP_FORBIDDEN))))->render(); + self::assertEquals(Response::HTTP_FORBIDDEN, $exception->getData()->code); + } + + public function test_can_get_correct_message_from_not_found_exception(): void + { + self::assertEquals('Unexpected Exception' , $this->instance->getData()->message); + } + + public function test_can_override_message_from_not_found_exception() + { + $fakeText = $this->faker->sentence; + $exception = (new CustomUnexpectedException((new Exception($fakeText))))->render(); + self::assertEquals($fakeText, $exception->getData()->message); } } diff --git a/tests/Unit/CustomValidationExceptionTest.php b/tests/Unit/CustomValidationExceptionTest.php deleted file mode 100644 index 667548e..0000000 --- a/tests/Unit/CustomValidationExceptionTest.php +++ /dev/null @@ -1,25 +0,0 @@ -handler->render( - $this->request, - new CustomValidationException - ); - - $this->assertInstanceOf($this->expected , $actual); - $this->assertEquals(Response::HTTP_UNPROCESSABLE_ENTITY, $actual->getCode()); - } -}