Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TunnelTest.php
Go to the documentation of this file.
1 <?php
7 
11 class TunnelTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_request;
17 
21  protected $_response;
22 
26  protected $_objectManager;
27 
31  protected $resultRaw;
32 
33  protected function setUp()
34  {
35  $this->_request = $this->createMock(\Magento\Framework\App\Request\Http::class);
36  $this->_response = $this->createMock(\Magento\Framework\App\Response\Http::class);
37  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
38  }
39 
40  protected function tearDown()
41  {
42  $this->_request = null;
43  $this->_response = null;
44  $this->_objectManager = null;
45  }
46 
47  public function testTunnelAction()
48  {
49  $fixture = uniqid();
50  $this->_request->expects($this->at(0))
51  ->method('getParam')
52  ->with('ga')
53  ->will($this->returnValue(urlencode(base64_encode(json_encode([1])))));
54  $this->_request->expects($this->at(1))->method('getParam')->with('h')->will($this->returnValue($fixture));
55  $tunnelResponse = $this->createMock(\Magento\Framework\App\Response\Http::class);
56  $httpClient = $this->createPartialMock(
57  \Magento\Framework\HTTP\ZendClient::class,
58  ['setUri', 'setParameterGet', 'setConfig', 'request', 'getHeaders']
59  );
61  $helper = $this->createPartialMock(\Magento\Backend\Helper\Dashboard\Data::class, ['getChartDataHash']);
62  $helper->expects($this->any())->method('getChartDataHash')->will($this->returnValue($fixture));
63 
64  $this->_objectManager->expects($this->at(0))
65  ->method('get')
66  ->with(\Magento\Backend\Helper\Dashboard\Data::class)
67  ->will($this->returnValue($helper));
68  $this->_objectManager->expects($this->at(1))
69  ->method('create')
70  ->with(\Magento\Framework\HTTP\ZendClient::class)
71  ->will($this->returnValue($httpClient));
72  $httpClient->expects($this->once())->method('setUri')->will($this->returnValue($httpClient));
73  $httpClient->expects($this->once())->method('setParameterGet')->will($this->returnValue($httpClient));
74  $httpClient->expects($this->once())->method('setConfig')->will($this->returnValue($httpClient));
75  $httpClient->expects($this->once())->method('request')->with('GET')->will($this->returnValue($tunnelResponse));
76  $tunnelResponse->expects($this->any())->method('getHeaders')
77  ->will($this->returnValue(['Content-type' => 'test_header']));
78  $tunnelResponse->expects($this->any())->method('getBody')->will($this->returnValue('success_msg'));
79  $this->_response->expects($this->any())->method('getBody')->will($this->returnValue('success_msg'));
80 
81  $controller = $this->_factory($this->_request, $this->_response);
82  $this->resultRaw->expects($this->once())
83  ->method('setHeader')
84  ->with('Content-type', 'test_header')
85  ->willReturnSelf();
86  $this->resultRaw->expects($this->once())
87  ->method('setContents')
88  ->with('success_msg')
89  ->willReturnSelf();
90 
91  $controller->execute();
92  $this->assertEquals('success_msg', $controller->getResponse()->getBody());
93  }
94 
95  public function testTunnelAction400()
96  {
97  $controller = $this->_factory($this->_request, $this->_response);
98 
99  $this->resultRaw->expects($this->once())
100  ->method('setHeader')
101  ->willReturnSelf();
102  $this->resultRaw->expects($this->once())
103  ->method('setHttpResponseCode')
104  ->with(400)
105  ->willReturnSelf();
106  $this->resultRaw->expects($this->once())
107  ->method('setContents')
108  ->with('Service unavailable: invalid request')
109  ->willReturnSelf();
110 
111  $controller->execute();
112  }
113 
114  public function testTunnelAction503()
115  {
116  $fixture = uniqid();
117  $this->_request->expects($this->at(0))
118  ->method('getParam')
119  ->with('ga')
120  ->will($this->returnValue(urlencode(base64_encode(json_encode([1])))));
121  $this->_request->expects($this->at(1))->method('getParam')->with('h')->will($this->returnValue($fixture));
123  $helper = $this->createPartialMock(\Magento\Backend\Helper\Dashboard\Data::class, ['getChartDataHash']);
124  $helper->expects($this->any())->method('getChartDataHash')->will($this->returnValue($fixture));
125 
126  $this->_objectManager->expects($this->at(0))
127  ->method('get')
128  ->with(\Magento\Backend\Helper\Dashboard\Data::class)
129  ->will($this->returnValue($helper));
130  $exceptionMock = new \Exception();
131  $this->_objectManager->expects($this->at(1))
132  ->method('create')
133  ->with(\Magento\Framework\HTTP\ZendClient::class)
134  ->will($this->throwException($exceptionMock));
135  $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
136  $loggerMock->expects($this->once())->method('critical')->with($exceptionMock);
137  $this->_objectManager->expects($this->at(2))
138  ->method('get')
139  ->with(\Psr\Log\LoggerInterface::class)
140  ->will($this->returnValue($loggerMock));
141 
142  $controller = $this->_factory($this->_request, $this->_response);
143 
144  $this->resultRaw->expects($this->once())
145  ->method('setHeader')
146  ->willReturnSelf();
147  $this->resultRaw->expects($this->once())
148  ->method('setHttpResponseCode')
149  ->with(503)
150  ->willReturnSelf();
151  $this->resultRaw->expects($this->once())
152  ->method('setContents')
153  ->with('Service unavailable: see error log for details')
154  ->willReturnSelf();
155 
156  $controller->execute();
157  }
158 
166  protected function _factory($request, $response = null)
167  {
168  if (!$response) {
170  $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
171  $response->headersSentThrowsException = false;
172  }
173  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
174  $varienFront = $helper->getObject(\Magento\Framework\App\FrontController::class);
175 
176  $arguments = [
177  'request' => $request,
178  'response' => $response,
179  'objectManager' => $this->_objectManager,
180  'frontController' => $varienFront,
181  ];
182  $this->resultRaw = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
183  ->disableOriginalConstructor()
184  ->getMock();
185 
186  $resultRawFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
187  ->disableOriginalConstructor()
188  ->setMethods(['create'])
189  ->getMock();
190  $resultRawFactory->expects($this->atLeastOnce())
191  ->method('create')
192  ->willReturn($this->resultRaw);
193  $context = $helper->getObject(\Magento\Backend\App\Action\Context::class, $arguments);
194  return new \Magento\Backend\Controller\Adminhtml\Dashboard\Tunnel($context, $resultRawFactory);
195  }
196 }
$response
Definition: 404.php:11
$helper
Definition: iframe.phtml:13
$arguments
$controller
Definition: info.phtml:14