ApiExceptionTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Exceptions;
  4. use MeiliSearch\Exceptions\ApiException;
  5. use Tests\TestCase;
  6. final class ApiExceptionTest extends TestCase
  7. {
  8. public function testException(): void
  9. {
  10. $httpBodyExample = [
  11. 'message' => 'This is the message',
  12. 'errorCode' => 'this_is_the_error_code',
  13. 'errorType' => 'this_is_the_error_type',
  14. 'errorLink' => 'https://docs.meilisearch.com/errors',
  15. ];
  16. $statusCode = 400;
  17. try {
  18. throw new ApiException($statusCode, $httpBodyExample);
  19. } catch (ApiException $apiException) {
  20. $this->assertEquals($statusCode, $apiException->httpStatus);
  21. $this->assertEquals($httpBodyExample['message'],
  22. $apiException->message);
  23. $this->assertEquals($httpBodyExample['errorCode'], $apiException->errorCode);
  24. $this->assertEquals($httpBodyExample['errorType'], $apiException->errorType);
  25. $this->assertEquals($httpBodyExample['errorLink'], $apiException->errorLink);
  26. $expectedExceptionToString = "MeiliSearch HTTPRequestException: Http Status: {$statusCode} - Message: {$httpBodyExample['message']} - Error code: {$httpBodyExample['errorCode']} - Error type: {$httpBodyExample['errorType']} - Error link: {$httpBodyExample['errorLink']}";
  27. $this->assertEquals($expectedExceptionToString, (string) $apiException);
  28. }
  29. }
  30. }