13 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
20 private static $errorMessage =
'Some error';
25 private static $testJson =
'{"id": 1}';
30 private static $successfulCode = 200;
35 private static $phpVersionId = 50000;
40 private $objectManager;
45 private $responseHandler;
61 $this->dataDecoder = $this->getMockBuilder(DecoderInterface::class)
62 ->disableOriginalConstructor()
65 $this->response = $this->getMockBuilder(Response::class)
66 ->disableOriginalConstructor()
67 ->setMethods([
'getStatus',
'getBody'])
70 $this->responseHandler = $this->objectManager->getObject(ResponseHandler::class, [
71 'dataDecoder' => $this->dataDecoder
80 $this->response->expects($this->any())
84 $this->response->expects($this->once())
86 ->willReturn(self::$errorMessage);
89 $this->responseHandler->handle($this->response);
91 $this->assertEquals($e->getMessage(), sprintf(
$message, self::$errorMessage));
101 [400,
'Bad Request - The request could not be parsed. Response: %s'],
102 [401,
'Unauthorized - user is not logged in, could not be authenticated. Response: %s'],
103 [403,
'Forbidden - Cannot access resource. Response: %s'],
104 [404,
'Not Found - resource does not exist. Response: %s'],
107 'Conflict - with state of the resource on server. Can occur with (too rapid) PUT requests. Response: %s' 109 [500,
'Server error. Response: %s']
119 $this->response->expects($this->any())
120 ->method(
'getStatus')
121 ->willReturn(self::$successfulCode);
123 $this->response->expects($this->once())
127 $r = new \ReflectionObject($this->responseHandler);
128 $prop = $r->getProperty(
'phpVersionId');
129 $prop->setAccessible(
true);
130 $prop->setValue(self::$phpVersionId);
132 $this->responseHandler->handle($this->response);
141 $this->response->expects($this->any())
142 ->method(
'getStatus')
143 ->willReturn(self::$successfulCode);
145 $this->response->expects($this->once())
147 ->willReturn(
'param');
149 $this->dataDecoder = $this->getMockBuilder(DecoderInterface::class)
150 ->disableOriginalConstructor()
153 $this->dataDecoder->expects($this->once())
156 ->willThrowException(
new \Exception(self::$errorMessage, 30));
158 $this->responseHandler = $this->objectManager->getObject(ResponseHandler::class, [
159 'dataDecoder' => $this->dataDecoder
162 $this->responseHandler->handle($this->response);
167 $this->response->expects($this->any())
168 ->method(
'getStatus')
169 ->willReturn(self::$successfulCode);
171 $this->response->expects($this->once())
173 ->willReturn(self::$testJson);
175 $this->dataDecoder->expects($this->once())
177 ->willReturn(json_decode(self::$testJson, 1));
179 $decodedResponseBody = $this->responseHandler->handle($this->response);
180 $this->assertEquals($decodedResponseBody, [
'id' => 1]);
testHandleEmptyJsonException()
testHandleFailureMessage($code, $message)