ExceptionsTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // Part of "Hackware Web Services Core", released under the MIT License terms.
  4. namespace Hawese\Tests;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. use Illuminate\Validation\ValidationException;
  8. use Hawese\Core\Exceptions
  9. \{Handler, JsonExceptionRenderer, ModelValidationException,
  10. ModelObjectNotFoundException, UnknownForeignObjectException,
  11. WrongCredentialsException};
  12. use Exception;
  13. class ExceptionsTest extends TestCase
  14. {
  15. public function testBuildCode()
  16. {
  17. $renderer = new JsonExceptionRenderer(new Exception());
  18. $this->assertArrayNotHasKey('code', $renderer->build());
  19. $renderer = new JsonExceptionRenderer(new Exception('', 42));
  20. $this->assertSame(42, $renderer->build()['code']);
  21. }
  22. public function testBuildMessage()
  23. {
  24. $renderer = new JsonExceptionRenderer(
  25. (new HttpException(
  26. Response::HTTP_BAD_REQUEST
  27. ))
  28. );
  29. $this->assertSame(
  30. Response::$statusTexts[Response::HTTP_BAD_REQUEST],
  31. $renderer->build()['message']
  32. );
  33. $renderer = new JsonExceptionRenderer(
  34. (new HttpException(
  35. Response::HTTP_BAD_REQUEST,
  36. 'Custom message'
  37. ))
  38. );
  39. $this->assertSame(
  40. 'Custom message',
  41. $renderer->build()['message']
  42. );
  43. }
  44. public function testBuildDetail()
  45. {
  46. $renderer = new JsonExceptionRenderer(new Exception());
  47. $this->assertArrayNotHasKey('detail', $renderer->build());
  48. $renderer = new JsonExceptionRenderer(
  49. new UnknownForeignObjectException('model', 'attribute')
  50. );
  51. $this->assertEqualsCanonicalizing(
  52. ['model' => 'model', 'attribute' => 'attribute'],
  53. $renderer->build()['detail']
  54. );
  55. // ValidationException instance tested on testModelValidationException
  56. }
  57. public function testBuildDebug()
  58. {
  59. $renderer = new JsonExceptionRenderer((new Exception()));
  60. $this->assertSame('Exception', $renderer->build()['debug']['exception']);
  61. $_ENV['APP_DEBUG'] = false;
  62. $this->assertArrayNotHasKey('debug', $renderer->build());
  63. }
  64. public function testResponseAndRender()
  65. {
  66. $response = JsonExceptionRenderer::render(new Exception());
  67. $this->assertObjectHasAttribute('error', $response->getData());
  68. $this->assertSame(
  69. Response::HTTP_INTERNAL_SERVER_ERROR,
  70. $response->getStatusCode()
  71. );
  72. $response = JsonExceptionRenderer::render(
  73. (new HttpException(
  74. Response::HTTP_BAD_REQUEST
  75. ))
  76. );
  77. $this->assertSame(
  78. Response::HTTP_BAD_REQUEST,
  79. $response->getStatusCode()
  80. );
  81. }
  82. public function testModelObjectNotFoundException()
  83. {
  84. $response = JsonExceptionRenderer::render(
  85. new ModelObjectNotFoundException('model', 'key', 'value')
  86. );
  87. $this->assertSame(
  88. 'model key value could not be found',
  89. $response->getData()->error->message
  90. );
  91. $response = JsonExceptionRenderer::render(
  92. new ModelObjectNotFoundException('model', ['key1', 'key2'], 'value')
  93. );
  94. $this->assertSame(
  95. 'model key1 or key2 value could not be found',
  96. $response->getData()->error->message
  97. );
  98. }
  99. public function testModelValidationException()
  100. {
  101. $validator = app('validator')->make([], ['attr' => 'required']);
  102. $response = JsonExceptionRenderer::render(
  103. new ModelValidationException('model', $validator)
  104. );
  105. $this->assertSame(
  106. Response::HTTP_UNPROCESSABLE_ENTITY,
  107. $response->getStatusCode()
  108. );
  109. $this->assertStringContainsString(
  110. 'Failed',
  111. $response->getData()->error->message
  112. );
  113. $this->assertStringContainsString(
  114. 'required',
  115. $response->getData()->error->detail->attributes->attr[0]
  116. );
  117. }
  118. public function testUnknownForeignObjectException()
  119. {
  120. $response = JsonExceptionRenderer::render(
  121. new UnknownForeignObjectException('model', 'attribute')
  122. );
  123. $this->assertSame(
  124. Response::HTTP_BAD_REQUEST,
  125. $response->getStatusCode()
  126. );
  127. $this->assertEqualsCanonicalizing(
  128. (object) ['model' => 'model', 'attribute' => 'attribute'],
  129. $response->getData()->error->detail
  130. );
  131. }
  132. public function testWrongCredentialsException()
  133. {
  134. $response = JsonExceptionRenderer::render(
  135. new WrongCredentialsException('model', 'identifier')
  136. );
  137. $this->assertSame(
  138. Response::HTTP_FORBIDDEN,
  139. $response->getStatusCode()
  140. );
  141. $this->assertSame(
  142. 'Wrong secret for model identifier',
  143. $response->getData()->error->message
  144. );
  145. }
  146. }