PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/Webapi/Test/Unit/Rest/ResponseTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 221 lines | 160 code | 16 blank | 45 comment | 1 complexity | 8918a01cd7b37df5803a9eafbb03bb73 MD5 | raw file
  1. <?php
  2. /**
  3. * Test Rest response controller.
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Webapi\Test\Unit\Rest;
  9. use Magento\Framework\Phrase;
  10. class ResponseTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /** @var \Magento\Framework\Webapi\Rest\Response */
  13. protected $responseRest;
  14. /** @var \Magento\Framework\App\State */
  15. protected $appStateMock;
  16. /** @var \Magento\Framework\Webapi\Rest\Response\Renderer\Xml */
  17. protected $rendererMock;
  18. /** @var \Magento\Framework\Webapi\ErrorProcessor */
  19. protected $errorProcessorMock;
  20. protected function setUp()
  21. {
  22. /** Mock all objects required for SUT. */
  23. $this->rendererMock = $this->getMockBuilder(
  24. 'Magento\Framework\Webapi\Rest\Response\Renderer\Json'
  25. )->disableOriginalConstructor()->getMock();
  26. $rendererFactoryMock = $this->getMockBuilder(
  27. 'Magento\Framework\Webapi\Rest\Response\RendererFactory'
  28. )->disableOriginalConstructor()->getMock();
  29. $rendererFactoryMock->expects($this->any())->method('get')->will($this->returnValue($this->rendererMock));
  30. $this->errorProcessorMock = $this->getMockBuilder('Magento\Framework\Webapi\ErrorProcessor')
  31. ->disableOriginalConstructor()->getMock();
  32. $this->appStateMock = $this->getMock('Magento\Framework\App\State', [], [], '', false);
  33. /** Init SUP. */
  34. $this->responseRest = new \Magento\Framework\Webapi\Rest\Response(
  35. $rendererFactoryMock,
  36. $this->errorProcessorMock,
  37. $this->appStateMock
  38. );
  39. }
  40. protected function tearDown()
  41. {
  42. unset(
  43. $this->responseRest,
  44. $this->appStateMock,
  45. $this->appStateMock,
  46. $this->rendererMock,
  47. $this->errorProcessorMock
  48. );
  49. }
  50. /**
  51. * Test setException method with \Magento\Framework\Webapi\Exception.
  52. */
  53. public function testSetWebapiExceptionException()
  54. {
  55. /** Init \Magento\Framework\Webapi\Exception */
  56. $apiException = new \Magento\Framework\Webapi\Exception(
  57. new Phrase('Exception message.'),
  58. 0,
  59. \Magento\Framework\Webapi\Exception::HTTP_UNAUTHORIZED
  60. );
  61. $this->responseRest->setException($apiException);
  62. /** Assert that \Magento\Framework\Webapi\Exception was set and presented in the list. */
  63. $this->assertTrue(
  64. $this->responseRest->hasExceptionOfType('Magento\Framework\Webapi\Exception'),
  65. 'Magento\Framework\Webapi\Exception was not set.'
  66. );
  67. }
  68. /**
  69. * Test sendResponse method with internal error exception during messages rendering.
  70. */
  71. public function testSendResponseRenderMessagesException()
  72. {
  73. /** Init logic exception. */
  74. $logicException = new \LogicException();
  75. /** Mock error processor to throw \LogicException in maskException method. */
  76. $this->errorProcessorMock->expects(
  77. $this->any()
  78. )->method(
  79. 'maskException'
  80. )->will(
  81. $this->throwException($logicException)
  82. );
  83. /** Assert that renderException method will be executed once with specified parameters. */
  84. $this->errorProcessorMock->expects(
  85. $this->once()
  86. )->method(
  87. 'renderException'
  88. )->with(
  89. $logicException,
  90. \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR
  91. );
  92. /** Set exception to Rest response to get in to the _renderMessages method. */
  93. $this->responseRest->setException(new \Magento\Framework\Webapi\Exception(new Phrase('Message.')));
  94. $this->responseRest->sendResponse();
  95. }
  96. /**
  97. * Test sendResponse method with HTTP Not Acceptable error exception during messages rendering.
  98. */
  99. public function testSendResponseRenderMessagesHttpNotAcceptable()
  100. {
  101. $exception = new \Magento\Framework\Webapi\Exception(
  102. new Phrase('Message'),
  103. 0,
  104. \Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE
  105. );
  106. /** Mock error processor to throw \LogicException in maskException method. */
  107. $this->errorProcessorMock->expects(
  108. $this->any()
  109. )->method(
  110. 'maskException'
  111. )->will(
  112. $this->throwException($exception)
  113. );
  114. /** Assert that renderException method will be executed once with specified parameters. */
  115. $this->errorProcessorMock->expects(
  116. $this->once()
  117. )->method(
  118. 'renderException'
  119. )->with(
  120. $exception,
  121. \Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE
  122. );
  123. /** Set exception to Rest response to get in to the _renderMessages method. */
  124. $this->responseRest->setException(
  125. new \Magento\Framework\Webapi\Exception(
  126. new Phrase('Message.'),
  127. 0,
  128. \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST
  129. )
  130. );
  131. $this->responseRest->sendResponse();
  132. }
  133. /**
  134. * Test sendResponse method with exception rendering.
  135. */
  136. public function testSendResponseWithException()
  137. {
  138. /** Mock all required objects. */
  139. $this->rendererMock->expects(
  140. $this->any()
  141. )->method(
  142. 'getMimeType'
  143. )->will(
  144. $this->returnValue('application/json')
  145. );
  146. $this->rendererMock->expects(
  147. $this->any()
  148. )->method(
  149. 'render'
  150. )->will(
  151. $this->returnCallback([$this, 'callbackForSendResponseTest'], $this->returnArgument(0))
  152. );
  153. $exceptionMessage = 'Message';
  154. $exceptionHttpCode = \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST;
  155. $exception = new \Magento\Framework\Webapi\Exception(new Phrase($exceptionMessage), 0, $exceptionHttpCode);
  156. $this->errorProcessorMock->expects(
  157. $this->any()
  158. )->method(
  159. 'maskException'
  160. )->will(
  161. $this->returnValue($exception)
  162. );
  163. $this->responseRest->setException($exception);
  164. /** Start output buffering. */
  165. ob_start();
  166. $this->responseRest->sendResponse();
  167. /** Clear output buffering. */
  168. ob_end_clean();
  169. $actualResponse = $this->responseRest->getBody();
  170. $expectedResult = '{"message":"' .
  171. $exceptionMessage .
  172. '"}';
  173. $this->assertStringStartsWith($expectedResult, $actualResponse, 'Response body is invalid');
  174. }
  175. /**
  176. * Callback for testSendResponseRenderMessages method.
  177. *
  178. * @param $data
  179. * @return string
  180. */
  181. public function callbackForSendResponseTest($data)
  182. {
  183. return json_encode($data);
  184. }
  185. /**
  186. * Test sendResponse method without any exception
  187. */
  188. public function testSendResponseSuccessHandling()
  189. {
  190. $this->responseRest->sendResponse();
  191. $this->assertTrue($this->responseRest->getHttpResponseCode() == \Magento\Framework\Webapi\Response::HTTP_OK);
  192. }
  193. public function testHasExceptionOfType()
  194. {
  195. $this->responseRest->setException(new \Exception());
  196. $hasException = $this->responseRest->hasExceptionOfType('Exception');
  197. $this->assertTrue($hasException);
  198. }
  199. public function testHasExceptionOfTypeIfExceptionsIsEmpty()
  200. {
  201. $this->responseRest->setException(new \Exception());
  202. $hasException = $this->responseRest->hasExceptionOfType('Test\Exception');
  203. $this->assertFalse($hasException);
  204. }
  205. }