Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ResponseHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
12 use \Zend_Http_Response as Response;
13 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
14 
15 class ResponseHandlerTest extends \PHPUnit\Framework\TestCase
16 {
20  private static $errorMessage = 'Some error';
21 
25  private static $testJson = '{"id": 1}';
26 
30  private static $successfulCode = 200;
31 
35  private static $phpVersionId = 50000;
36 
40  private $objectManager;
41 
45  private $responseHandler;
46 
50  private $dataDecoder;
51 
55  private $response;
56 
57  public function setUp()
58  {
59  $this->objectManager = new ObjectManager($this);
60 
61  $this->dataDecoder = $this->getMockBuilder(DecoderInterface::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64 
65  $this->response = $this->getMockBuilder(Response::class)
66  ->disableOriginalConstructor()
67  ->setMethods(['getStatus', 'getBody'])
68  ->getMock();
69 
70  $this->responseHandler = $this->objectManager->getObject(ResponseHandler::class, [
71  'dataDecoder' => $this->dataDecoder
72  ]);
73  }
74 
79  {
80  $this->response->expects($this->any())
81  ->method('getStatus')
82  ->willReturn($code);
83 
84  $this->response->expects($this->once())
85  ->method('getBody')
86  ->willReturn(self::$errorMessage);
87 
88  try {
89  $this->responseHandler->handle($this->response);
90  } catch (ApiCallException $e) {
91  $this->assertEquals($e->getMessage(), sprintf($message, self::$errorMessage));
92  }
93  }
94 
98  public function errorsProvider()
99  {
100  return [
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'],
105  [
106  409,
107  'Conflict - with state of the resource on server. Can occur with (too rapid) PUT requests. Response: %s'
108  ],
109  [500, 'Server error. Response: %s']
110  ];
111  }
112 
118  {
119  $this->response->expects($this->any())
120  ->method('getStatus')
121  ->willReturn(self::$successfulCode);
122 
123  $this->response->expects($this->once())
124  ->method('getBody')
125  ->willReturn('');
126 
127  $r = new \ReflectionObject($this->responseHandler);
128  $prop = $r->getProperty('phpVersionId');
129  $prop->setAccessible(true);
130  $prop->setValue(self::$phpVersionId);
131 
132  $this->responseHandler->handle($this->response);
133  }
134 
139  public function testHandleInvalidJson()
140  {
141  $this->response->expects($this->any())
142  ->method('getStatus')
143  ->willReturn(self::$successfulCode);
144 
145  $this->response->expects($this->once())
146  ->method('getBody')
147  ->willReturn('param');
148 
149  $this->dataDecoder = $this->getMockBuilder(DecoderInterface::class)
150  ->disableOriginalConstructor()
151  ->getMock();
152 
153  $this->dataDecoder->expects($this->once())
154  ->method('decode')
155  ->with('param')
156  ->willThrowException(new \Exception(self::$errorMessage, 30));
157 
158  $this->responseHandler = $this->objectManager->getObject(ResponseHandler::class, [
159  'dataDecoder' => $this->dataDecoder
160  ]);
161 
162  $this->responseHandler->handle($this->response);
163  }
164 
165  public function testHandle()
166  {
167  $this->response->expects($this->any())
168  ->method('getStatus')
169  ->willReturn(self::$successfulCode);
170 
171  $this->response->expects($this->once())
172  ->method('getBody')
173  ->willReturn(self::$testJson);
174 
175  $this->dataDecoder->expects($this->once())
176  ->method('decode')
177  ->willReturn(json_decode(self::$testJson, 1));
178 
179  $decodedResponseBody = $this->responseHandler->handle($this->response);
180  $this->assertEquals($decodedResponseBody, ['id' => 1]);
181  }
182 }
$message
$code
Definition: info.phtml:12