HandlerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Tests\Unit\Exceptions;
  3. use App\Exceptions\Handler;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Contracts\Container\Container;
  7. use Tests\TestCase;
  8. /**
  9. * @covers \App\Exceptions\Handler
  10. */
  11. class HandlerTest extends TestCase
  12. {
  13. /**
  14. * @test
  15. *
  16. * @dataProvider provideExceptionsforBadRequest
  17. */
  18. public function test_exceptions_returns_badRequest_json_response($exception)
  19. {
  20. $request = $this->createMock(Request::class);
  21. $instance = new Handler($this->createMock(Container::class));
  22. $class = new \ReflectionClass(Handler::class);
  23. $method = $class->getMethod('render');
  24. $method->setAccessible(true);
  25. $response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
  26. $this->assertInstanceOf(JsonResponse::class, $response);
  27. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  28. $response->assertStatus(400)
  29. ->assertJsonStructure([
  30. 'message'
  31. ]);
  32. }
  33. /**
  34. * Provide Valid data for validation test
  35. */
  36. public function provideExceptionsforBadRequest() : array
  37. {
  38. return [
  39. [
  40. '\App\Exceptions\InvalidOtpParameterException'
  41. ],
  42. [
  43. '\App\Exceptions\InvalidQrCodeException'
  44. ],
  45. [
  46. '\App\Exceptions\InvalidSecretException'
  47. ],
  48. [
  49. '\App\Exceptions\DbEncryptionException'
  50. ],
  51. ];
  52. }
  53. /**
  54. * @test
  55. *
  56. * @dataProvider provideExceptionsforNotFound
  57. */
  58. public function test_exceptions_returns_notFound_json_response($exception)
  59. {
  60. $request = $this->createMock(Request::class);
  61. $instance = new Handler($this->createMock(Container::class));
  62. $class = new \ReflectionClass(Handler::class);
  63. $method = $class->getMethod('render');
  64. $method->setAccessible(true);
  65. $response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
  66. $this->assertInstanceOf(JsonResponse::class, $response);
  67. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  68. $response->assertStatus(404)
  69. ->assertJsonStructure([
  70. 'message'
  71. ]);
  72. }
  73. /**
  74. * Provide Valid data for validation test
  75. */
  76. public function provideExceptionsforNotFound() : array
  77. {
  78. return [
  79. [
  80. '\Illuminate\Database\Eloquent\ModelNotFoundException'
  81. ],
  82. [
  83. '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
  84. ],
  85. ];
  86. }
  87. }