TunnelTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Controller\Adminhtml\Dashboard;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class TunnelTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $_request;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_response;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_objectManager;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $resultRaw;
  28. protected function setUp()
  29. {
  30. $this->_request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  31. $this->_response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  32. $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  33. }
  34. protected function tearDown()
  35. {
  36. $this->_request = null;
  37. $this->_response = null;
  38. $this->_objectManager = null;
  39. }
  40. public function testTunnelAction()
  41. {
  42. $fixture = uniqid();
  43. $this->_request->expects($this->at(0))
  44. ->method('getParam')
  45. ->with('ga')
  46. ->will($this->returnValue(urlencode(base64_encode(json_encode([1])))));
  47. $this->_request->expects($this->at(1))->method('getParam')->with('h')->will($this->returnValue($fixture));
  48. $tunnelResponse = $this->createMock(\Magento\Framework\App\Response\Http::class);
  49. $httpClient = $this->createPartialMock(
  50. \Magento\Framework\HTTP\ZendClient::class,
  51. ['setUri', 'setParameterGet', 'setConfig', 'request', 'getHeaders']
  52. );
  53. /** @var $helper \Magento\Backend\Helper\Dashboard\Data|\PHPUnit_Framework_MockObject_MockObject */
  54. $helper = $this->createPartialMock(\Magento\Backend\Helper\Dashboard\Data::class, ['getChartDataHash']);
  55. $helper->expects($this->any())->method('getChartDataHash')->will($this->returnValue($fixture));
  56. $this->_objectManager->expects($this->at(0))
  57. ->method('get')
  58. ->with(\Magento\Backend\Helper\Dashboard\Data::class)
  59. ->will($this->returnValue($helper));
  60. $this->_objectManager->expects($this->at(1))
  61. ->method('create')
  62. ->with(\Magento\Framework\HTTP\ZendClient::class)
  63. ->will($this->returnValue($httpClient));
  64. $httpClient->expects($this->once())->method('setUri')->will($this->returnValue($httpClient));
  65. $httpClient->expects($this->once())->method('setParameterGet')->will($this->returnValue($httpClient));
  66. $httpClient->expects($this->once())->method('setConfig')->will($this->returnValue($httpClient));
  67. $httpClient->expects($this->once())->method('request')->with('GET')->will($this->returnValue($tunnelResponse));
  68. $tunnelResponse->expects($this->any())->method('getHeaders')
  69. ->will($this->returnValue(['Content-type' => 'test_header']));
  70. $tunnelResponse->expects($this->any())->method('getBody')->will($this->returnValue('success_msg'));
  71. $this->_response->expects($this->any())->method('getBody')->will($this->returnValue('success_msg'));
  72. $controller = $this->_factory($this->_request, $this->_response);
  73. $this->resultRaw->expects($this->once())
  74. ->method('setHeader')
  75. ->with('Content-type', 'test_header')
  76. ->willReturnSelf();
  77. $this->resultRaw->expects($this->once())
  78. ->method('setContents')
  79. ->with('success_msg')
  80. ->willReturnSelf();
  81. $controller->execute();
  82. $this->assertEquals('success_msg', $controller->getResponse()->getBody());
  83. }
  84. public function testTunnelAction400()
  85. {
  86. $controller = $this->_factory($this->_request, $this->_response);
  87. $this->resultRaw->expects($this->once())
  88. ->method('setHeader')
  89. ->willReturnSelf();
  90. $this->resultRaw->expects($this->once())
  91. ->method('setHttpResponseCode')
  92. ->with(400)
  93. ->willReturnSelf();
  94. $this->resultRaw->expects($this->once())
  95. ->method('setContents')
  96. ->with('Service unavailable: invalid request')
  97. ->willReturnSelf();
  98. $controller->execute();
  99. }
  100. public function testTunnelAction503()
  101. {
  102. $fixture = uniqid();
  103. $this->_request->expects($this->at(0))
  104. ->method('getParam')
  105. ->with('ga')
  106. ->will($this->returnValue(urlencode(base64_encode(json_encode([1])))));
  107. $this->_request->expects($this->at(1))->method('getParam')->with('h')->will($this->returnValue($fixture));
  108. /** @var $helper \Magento\Backend\Helper\Dashboard\Data|\PHPUnit_Framework_MockObject_MockObject */
  109. $helper = $this->createPartialMock(\Magento\Backend\Helper\Dashboard\Data::class, ['getChartDataHash']);
  110. $helper->expects($this->any())->method('getChartDataHash')->will($this->returnValue($fixture));
  111. $this->_objectManager->expects($this->at(0))
  112. ->method('get')
  113. ->with(\Magento\Backend\Helper\Dashboard\Data::class)
  114. ->will($this->returnValue($helper));
  115. $exceptionMock = new \Exception();
  116. $this->_objectManager->expects($this->at(1))
  117. ->method('create')
  118. ->with(\Magento\Framework\HTTP\ZendClient::class)
  119. ->will($this->throwException($exceptionMock));
  120. $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  121. $loggerMock->expects($this->once())->method('critical')->with($exceptionMock);
  122. $this->_objectManager->expects($this->at(2))
  123. ->method('get')
  124. ->with(\Psr\Log\LoggerInterface::class)
  125. ->will($this->returnValue($loggerMock));
  126. $controller = $this->_factory($this->_request, $this->_response);
  127. $this->resultRaw->expects($this->once())
  128. ->method('setHeader')
  129. ->willReturnSelf();
  130. $this->resultRaw->expects($this->once())
  131. ->method('setHttpResponseCode')
  132. ->with(503)
  133. ->willReturnSelf();
  134. $this->resultRaw->expects($this->once())
  135. ->method('setContents')
  136. ->with('Service unavailable: see error log for details')
  137. ->willReturnSelf();
  138. $controller->execute();
  139. }
  140. /**
  141. * Create the tested object
  142. *
  143. * @param \Magento\Framework\App\Request\Http $request
  144. * @param \Magento\Framework\App\Response\Http|null $response
  145. * @return \Magento\Backend\Controller\Adminhtml\Dashboard|\PHPUnit_Framework_MockObject_MockObject
  146. */
  147. protected function _factory($request, $response = null)
  148. {
  149. if (!$response) {
  150. /** @var $response \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
  151. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  152. $response->headersSentThrowsException = false;
  153. }
  154. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  155. $varienFront = $helper->getObject(\Magento\Framework\App\FrontController::class);
  156. $arguments = [
  157. 'request' => $request,
  158. 'response' => $response,
  159. 'objectManager' => $this->_objectManager,
  160. 'frontController' => $varienFront,
  161. ];
  162. $this->resultRaw = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
  163. ->disableOriginalConstructor()
  164. ->getMock();
  165. $resultRawFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
  166. ->disableOriginalConstructor()
  167. ->setMethods(['create'])
  168. ->getMock();
  169. $resultRawFactory->expects($this->atLeastOnce())
  170. ->method('create')
  171. ->willReturn($this->resultRaw);
  172. $context = $helper->getObject(\Magento\Backend\App\Action\Context::class, $arguments);
  173. return new \Magento\Backend\Controller\Adminhtml\Dashboard\Tunnel($context, $resultRawFactory);
  174. }
  175. }