ResponseExceptionTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. use Cloudflare\API\Adapter\ResponseException;
  3. use Cloudflare\API\Adapter\JSONException;
  4. use GuzzleHttp\Exception\RequestException;
  5. use GuzzleHttp\Psr7\Request;
  6. use GuzzleHttp\Psr7\Response;
  7. /**
  8. * @SuppressWarnings(PHPMD.StaticAccess)
  9. */
  10. class ResponseExceptionTest extends TestCase
  11. {
  12. public function testFromRequestExceptionNoResponse()
  13. {
  14. $reqErr = new RequestException('foo', new Request('GET', '/test'));
  15. $respErr = ResponseException::fromRequestException($reqErr);
  16. $this->assertInstanceOf(ResponseException::class, $respErr);
  17. $this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
  18. $this->assertEquals(0, $respErr->getCode());
  19. $this->assertEquals($reqErr, $respErr->getPrevious());
  20. }
  21. public function testFromRequestExceptionEmptyContentType()
  22. {
  23. $resp = new Response(404);
  24. $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
  25. $respErr = ResponseException::fromRequestException($reqErr);
  26. $this->assertInstanceOf(ResponseException::class, $respErr);
  27. $this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
  28. $this->assertEquals(0, $respErr->getCode());
  29. $this->assertEquals($reqErr, $respErr->getPrevious());
  30. }
  31. public function testFromRequestExceptionUnknownContentType()
  32. {
  33. $resp = new Response(404, ['Content-Type' => ['application/octet-stream']]);
  34. $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
  35. $respErr = ResponseException::fromRequestException($reqErr);
  36. $this->assertInstanceOf(ResponseException::class, $respErr);
  37. $this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
  38. $this->assertEquals(0, $respErr->getCode());
  39. $this->assertEquals($reqErr, $respErr->getPrevious());
  40. }
  41. public function testFromRequestExceptionJSONDecodeError()
  42. {
  43. $resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], '[what]');
  44. $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
  45. $respErr = ResponseException::fromRequestException($reqErr);
  46. $this->assertInstanceOf(ResponseException::class, $respErr);
  47. $this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
  48. $this->assertEquals(0, $respErr->getCode());
  49. $this->assertInstanceOf(JSONException::class, $respErr->getPrevious());
  50. $this->assertEquals($reqErr, $respErr->getPrevious()->getPrevious());
  51. }
  52. public function testFromRequestExceptionJSONWithErrors()
  53. {
  54. $body = '{
  55. "result": null,
  56. "success": false,
  57. "errors": [{"code":1003, "message":"This is an error"}],
  58. "messages": []
  59. }';
  60. $resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], $body);
  61. $reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
  62. $respErr = ResponseException::fromRequestException($reqErr);
  63. $this->assertInstanceOf(ResponseException::class, $respErr);
  64. $this->assertEquals('This is an error', $respErr->getMessage());
  65. $this->assertEquals(1003, $respErr->getCode());
  66. $this->assertEquals($reqErr, $respErr->getPrevious());
  67. }
  68. }