9 use \Magento\Framework\HTTP\ZendClient;
43 $this->zendClientFactoryMock = $this->getMockBuilder(\
Magento\Framework\HTTP\ZendClientFactory::class)
44 ->setMethods([
'create'])
45 ->disableOriginalConstructor()
48 $this->zendClientMock = $this->getMockBuilder(\
Magento\Framework\HTTP\ZendClient::class)
49 ->setMethods([
'request',
'setUri',
'setMethod',
'setHeaders',
'setParameterPost'])
50 ->disableOriginalConstructor()
53 $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
54 ->disableOriginalConstructor()
57 $this->configMock = $this->getMockBuilder(\
Magento\NewRelicReporting\Model\Config::class)
58 ->setMethods([
'getNewRelicApiUrl',
'getNewRelicApiKey',
'getNewRelicAppName',
'getNewRelicAppId'])
59 ->disableOriginalConstructor()
65 $this->zendClientFactoryMock
76 $data = $this->getDataVariables();
78 $this->zendClientMock->expects($this->once())
80 ->with(
$data[
'self_uri'])
83 $this->zendClientMock->expects($this->once())
85 ->with(
$data[
'method'])
88 $this->zendClientMock->expects($this->once())
89 ->method(
'setHeaders')
90 ->with(
$data[
'headers'])
93 $this->zendClientMock->expects($this->once())
94 ->method(
'setParameterPost')
95 ->with(
$data[
'params'])
98 $this->configMock->expects($this->once())
99 ->method(
'getNewRelicApiUrl')
102 $this->loggerMock->expects($this->once())->method(
'notice');
104 $this->configMock->expects($this->once())
105 ->method(
'getNewRelicApiKey')
106 ->willReturn(
$data[
'api_key']);
108 $this->configMock->expects($this->once())
109 ->method(
'getNewRelicAppName')
110 ->willReturn(
$data[
'app_name']);
112 $this->configMock->expects($this->once())
113 ->method(
'getNewRelicAppId')
114 ->willReturn(
$data[
'app_id']);
116 $zendHttpResponseMock = $this->getMockBuilder(
117 \Zend_Http_Response::class
118 )->disableOriginalConstructor()->getMock();
119 $zendHttpResponseMock->expects($this->any())->method(
'getStatus')->willReturn(
$data[
'status_ok']);
120 $zendHttpResponseMock->expects($this->once())->method(
'getBody')->willReturn(
$data[
'response_body']);
122 $this->zendClientMock->expects($this->once())->method(
'request')->willReturn($zendHttpResponseMock);
124 $this->zendClientFactoryMock->expects($this->once())
126 ->willReturn($this->zendClientMock);
128 $this->assertInternalType(
130 $this->model->setDeployment(
$data[
'description'],
$data[
'change'],
$data[
'user'])
141 $data = $this->getDataVariables();
143 $this->zendClientMock->expects($this->once())
148 $this->zendClientMock->expects($this->once())
149 ->method(
'setMethod')
150 ->with(
$data[
'method'])
153 $this->zendClientMock->expects($this->once())
154 ->method(
'setHeaders')
155 ->with(
$data[
'headers'])
158 $this->zendClientMock->expects($this->once())
159 ->method(
'setParameterPost')
160 ->with(
$data[
'params'])
163 $this->configMock->expects($this->once())
164 ->method(
'getNewRelicApiUrl')
165 ->willReturn(
$data[
'uri']);
167 $this->configMock->expects($this->once())
168 ->method(
'getNewRelicApiKey')
169 ->willReturn(
$data[
'api_key']);
171 $this->configMock->expects($this->once())
172 ->method(
'getNewRelicAppName')
173 ->willReturn(
$data[
'app_name']);
175 $this->configMock->expects($this->once())
176 ->method(
'getNewRelicAppId')
177 ->willReturn(
$data[
'app_id']);
179 $zendHttpResponseMock = $this->getMockBuilder(
180 \Zend_Http_Response::class
181 )->disableOriginalConstructor()->getMock();
182 $zendHttpResponseMock->expects($this->any())->method(
'getStatus')->willReturn(
$data[
'status_bad']);
184 $this->zendClientMock->expects($this->once())->method(
'request')->willReturn($zendHttpResponseMock);
185 $this->loggerMock->expects($this->once())->method(
'warning');
187 $this->zendClientFactoryMock->expects($this->once())
189 ->willReturn($this->zendClientMock);
191 $this->assertInternalType(
193 $this->model->setDeployment(
$data[
'description'],
$data[
'change'],
$data[
'user'])
202 $data = $this->getDataVariables();
204 $this->zendClientMock->expects($this->once())
209 $this->zendClientMock->expects($this->once())
210 ->method(
'setMethod')
211 ->with(
$data[
'method'])
214 $this->zendClientMock->expects($this->once())
215 ->method(
'setHeaders')
216 ->with(
$data[
'headers'])
219 $this->zendClientMock->expects($this->once())
220 ->method(
'setParameterPost')
221 ->with(
$data[
'params'])
224 $this->configMock->expects($this->once())
225 ->method(
'getNewRelicApiUrl')
226 ->willReturn(
$data[
'uri']);
228 $this->configMock->expects($this->once())
229 ->method(
'getNewRelicApiKey')
230 ->willReturn(
$data[
'api_key']);
232 $this->configMock->expects($this->once())
233 ->method(
'getNewRelicAppName')
234 ->willReturn(
$data[
'app_name']);
236 $this->configMock->expects($this->once())
237 ->method(
'getNewRelicAppId')
238 ->willReturn(
$data[
'app_id']);
240 $this->zendClientMock->expects($this->once())->method(
'request')->willThrowException(
243 $this->loggerMock->expects($this->once())->method(
'critical');
245 $this->zendClientFactoryMock->expects($this->once())
247 ->willReturn($this->zendClientMock);
249 $this->assertInternalType(
251 $this->model->setDeployment(
$data[
'description'],
$data[
'change'],
$data[
'user'])
258 private function getDataVariables()
261 $change =
'flush the cache username';
263 $uri =
'https://example.com/listener';
264 $selfUri =
'https://api.newrelic.com/deployments.xml';
266 $appName =
'app_name';
267 $appId =
'application_id';
269 $headers = [
'x-api-key' => $apiKey];
270 $responseBody =
'Response body content';
274 'deployment[app_name]' => $appName,
275 'deployment[application_id]' => $appId,
277 'deployment[changelog]' =>
$change,
278 'deployment[user]' =>
$user 285 'self_uri' => $selfUri,
286 'api_key' => $apiKey,
287 'app_name' => $appName,
290 'headers' => $headers,
291 'status_ok' => $statusOk,
292 'status_bad' => $statusBad,
293 'response_body' => $responseBody,
testSetDeploymentBadStatus()
testSetDeploymentRequestOk()
testSetDeploymentRequestFail()
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]