ResponseHandlerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Test\Unit\Model\SignifydGateway\Client;
  7. use Magento\Signifyd\Model\SignifydGateway\Client\ResponseHandler;
  8. use Magento\Framework\Json\DecoderInterface;
  9. use Magento\Signifyd\Model\SignifydGateway\ApiCallException;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use \Zend_Http_Response as Response;
  12. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  13. class ResponseHandlerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var string
  17. */
  18. private static $errorMessage = 'Some error';
  19. /**
  20. * @var string
  21. */
  22. private static $testJson = '{"id": 1}';
  23. /**
  24. * @var int
  25. */
  26. private static $successfulCode = 200;
  27. /**
  28. * @var int
  29. */
  30. private static $phpVersionId = 50000;
  31. /**
  32. * @var ObjectManager
  33. */
  34. private $objectManager;
  35. /**
  36. * @var ResponseHandler|MockObject
  37. */
  38. private $responseHandler;
  39. /**
  40. * @var DecoderInterface|MockObject
  41. */
  42. private $dataDecoder;
  43. /**
  44. * @var Response|MockObject
  45. */
  46. private $response;
  47. public function setUp()
  48. {
  49. $this->objectManager = new ObjectManager($this);
  50. $this->dataDecoder = $this->getMockBuilder(DecoderInterface::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->response = $this->getMockBuilder(Response::class)
  54. ->disableOriginalConstructor()
  55. ->setMethods(['getStatus', 'getBody'])
  56. ->getMock();
  57. $this->responseHandler = $this->objectManager->getObject(ResponseHandler::class, [
  58. 'dataDecoder' => $this->dataDecoder
  59. ]);
  60. }
  61. /**
  62. * @dataProvider errorsProvider
  63. */
  64. public function testHandleFailureMessage($code, $message)
  65. {
  66. $this->response->expects($this->any())
  67. ->method('getStatus')
  68. ->willReturn($code);
  69. $this->response->expects($this->once())
  70. ->method('getBody')
  71. ->willReturn(self::$errorMessage);
  72. try {
  73. $this->responseHandler->handle($this->response);
  74. } catch (ApiCallException $e) {
  75. $this->assertEquals($e->getMessage(), sprintf($message, self::$errorMessage));
  76. }
  77. }
  78. /**
  79. * @return array
  80. */
  81. public function errorsProvider()
  82. {
  83. return [
  84. [400, 'Bad Request - The request could not be parsed. Response: %s'],
  85. [401, 'Unauthorized - user is not logged in, could not be authenticated. Response: %s'],
  86. [403, 'Forbidden - Cannot access resource. Response: %s'],
  87. [404, 'Not Found - resource does not exist. Response: %s'],
  88. [
  89. 409,
  90. 'Conflict - with state of the resource on server. Can occur with (too rapid) PUT requests. Response: %s'
  91. ],
  92. [500, 'Server error. Response: %s']
  93. ];
  94. }
  95. /**
  96. * @expectedException \Magento\Signifyd\Model\SignifydGateway\ApiCallException
  97. * @expectedExceptionMessage Response is not valid JSON: Decoding failed: Syntax error
  98. */
  99. public function testHandleEmptyJsonException()
  100. {
  101. $this->response->expects($this->any())
  102. ->method('getStatus')
  103. ->willReturn(self::$successfulCode);
  104. $this->response->expects($this->once())
  105. ->method('getBody')
  106. ->willReturn('');
  107. $r = new \ReflectionObject($this->responseHandler);
  108. $prop = $r->getProperty('phpVersionId');
  109. $prop->setAccessible(true);
  110. $prop->setValue(self::$phpVersionId);
  111. $this->responseHandler->handle($this->response);
  112. }
  113. /**
  114. * @expectedException \Magento\Signifyd\Model\SignifydGateway\ApiCallException
  115. * @expectedExceptionMessage Response is not valid JSON: Some error
  116. */
  117. public function testHandleInvalidJson()
  118. {
  119. $this->response->expects($this->any())
  120. ->method('getStatus')
  121. ->willReturn(self::$successfulCode);
  122. $this->response->expects($this->once())
  123. ->method('getBody')
  124. ->willReturn('param');
  125. $this->dataDecoder = $this->getMockBuilder(DecoderInterface::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $this->dataDecoder->expects($this->once())
  129. ->method('decode')
  130. ->with('param')
  131. ->willThrowException(new \Exception(self::$errorMessage, 30));
  132. $this->responseHandler = $this->objectManager->getObject(ResponseHandler::class, [
  133. 'dataDecoder' => $this->dataDecoder
  134. ]);
  135. $this->responseHandler->handle($this->response);
  136. }
  137. public function testHandle()
  138. {
  139. $this->response->expects($this->any())
  140. ->method('getStatus')
  141. ->willReturn(self::$successfulCode);
  142. $this->response->expects($this->once())
  143. ->method('getBody')
  144. ->willReturn(self::$testJson);
  145. $this->dataDecoder->expects($this->once())
  146. ->method('decode')
  147. ->willReturn(json_decode(self::$testJson, 1));
  148. $decodedResponseBody = $this->responseHandler->handle($this->response);
  149. $this->assertEquals($decodedResponseBody, ['id' => 1]);
  150. }
  151. }