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