HandlerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. '\App\Exceptions\InvalidGoogleAuthMigration'
  53. ],
  54. [
  55. '\App\Exceptions\UndecipherableException'
  56. ],
  57. [
  58. '\App\Exceptions\UnsupportedOtpTypeException'
  59. ],
  60. ];
  61. }
  62. /**
  63. * @test
  64. *
  65. * @dataProvider provideExceptionsforNotFound
  66. */
  67. public function test_exceptions_returns_notFound_json_response($exception)
  68. {
  69. $request = $this->createMock(Request::class);
  70. $instance = new Handler($this->createMock(Container::class));
  71. $class = new \ReflectionClass(Handler::class);
  72. $method = $class->getMethod('render');
  73. $method->setAccessible(true);
  74. $response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
  75. $this->assertInstanceOf(JsonResponse::class, $response);
  76. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  77. $response->assertStatus(404)
  78. ->assertJsonStructure([
  79. 'message'
  80. ]);
  81. }
  82. /**
  83. * Provide Valid data for validation test
  84. */
  85. public function provideExceptionsforNotFound() : array
  86. {
  87. return [
  88. [
  89. '\Illuminate\Database\Eloquent\ModelNotFoundException'
  90. ],
  91. [
  92. '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
  93. ],
  94. ];
  95. }
  96. /**
  97. * @test
  98. */
  99. public function test_authenticationException_returns_proxyAuthRequired_json_response_with_proxy_guard()
  100. {
  101. $request = $this->createMock(Request::class);
  102. $instance = new Handler($this->createMock(Container::class));
  103. $class = new \ReflectionClass(Handler::class);
  104. $method = $class->getMethod('render');
  105. $method->setAccessible(true);
  106. $mockException = $this->createMock(\Illuminate\Auth\AuthenticationException::class);
  107. $mockException->method("guards")->willReturn(['reverse-proxy-guard']);
  108. $response = $method->invokeArgs($instance, [$request, $mockException]);
  109. $this->assertInstanceOf(JsonResponse::class, $response);
  110. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  111. $response->assertStatus(407)
  112. ->assertJsonStructure([
  113. 'message'
  114. ]);
  115. }
  116. }