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',
'setRawData'])
50 ->disableOriginalConstructor()
53 $this->jsonEncoderMock = $this->getMockBuilder(\
Magento\Framework\Json\EncoderInterface::class)
54 ->disableOriginalConstructor()
57 $this->configMock = $this->getMockBuilder(\
Magento\NewRelicReporting\Model\Config::class)
59 'getNewRelicAccountId',
61 'getInsightsInsertKey',
65 ->disableOriginalConstructor()
70 $this->jsonEncoderMock,
71 $this->zendClientFactoryMock
82 $json =
'{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
84 $uri =
'https://example.com/listener';
86 $headers = [
'X-Insert-Key' =>
'insert_key_value',
'Content-Type' =>
'application/json'];
89 $appName =
'app_name';
90 $insightApiKey =
'insert_key_value';
92 $this->model->addData([
'eventType'=>
'Cron']);
94 $this->zendClientMock->expects($this->once())->method(
'setUri')->with($uri)->willReturnSelf();
95 $this->zendClientMock->expects($this->once())->method(
'setMethod')->with(
$method)->willReturnSelf();
96 $this->zendClientMock->expects($this->once())->method(
'setHeaders')->with($headers)->willReturnSelf();
97 $this->zendClientMock->expects($this->once())->method(
'setRawData')->with($json)->willReturnSelf();
99 $this->configMock->expects($this->once())
100 ->method(
'getNewRelicAccountId')
101 ->willReturn($accId);
103 $this->configMock->expects($this->once())
104 ->method(
'getInsightsApiUrl')
107 $this->configMock->expects($this->once())
108 ->method(
'getInsightsInsertKey')
109 ->willReturn($insightApiKey);
111 $this->configMock->expects($this->once())
112 ->method(
'getNewRelicAppName')
113 ->willReturn($appName);
115 $this->configMock->expects($this->once())
116 ->method(
'getNewRelicAppId')
117 ->willReturn($appId);
119 $this->jsonEncoderMock->expects($this->once())->method(
'encode')->willReturn($json);
121 $zendHttpResponseMock = $this->getMockBuilder(
122 \Zend_Http_Response::class
123 )->disableOriginalConstructor()->getMock();
124 $zendHttpResponseMock->expects($this->any())->method(
'getStatus')->willReturn($statusOk);
126 $this->zendClientMock->expects($this->once())->method(
'request')->willReturn($zendHttpResponseMock);
128 $this->zendClientFactoryMock->expects($this->once())
130 ->willReturn($this->zendClientMock);
132 $this->assertInternalType(
134 $this->model->sendRequest()
145 $json =
'{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
147 $uri =
'https://example.com/listener';
149 $headers = [
'X-Insert-Key' =>
'insert_key_value',
'Content-Type' =>
'application/json'];
152 $appName =
'app_name';
153 $insightApiKey =
'insert_key_value';
155 $this->zendClientMock->expects($this->once())->method(
'setUri')->with($uri)->willReturnSelf();
156 $this->zendClientMock->expects($this->once())->method(
'setMethod')->with(
$method)->willReturnSelf();
157 $this->zendClientMock->expects($this->once())->method(
'setHeaders')->with($headers)->willReturnSelf();
158 $this->zendClientMock->expects($this->once())->method(
'setRawData')->with($json)->willReturnSelf();
160 $this->configMock->expects($this->once())
161 ->method(
'getNewRelicAccountId')
162 ->willReturn($accId);
164 $this->configMock->expects($this->once())
165 ->method(
'getInsightsApiUrl')
168 $this->configMock->expects($this->once())
169 ->method(
'getInsightsInsertKey')
170 ->willReturn($insightApiKey);
172 $this->configMock->expects($this->once())
173 ->method(
'getNewRelicAppName')
174 ->willReturn($appName);
176 $this->configMock->expects($this->once())
177 ->method(
'getNewRelicAppId')
178 ->willReturn($appId);
180 $this->jsonEncoderMock->expects($this->once())->method(
'encode')->willReturn($json);
182 $zendHttpResponseMock = $this->getMockBuilder(
183 \Zend_Http_Response::class
184 )->disableOriginalConstructor()->getMock();
185 $zendHttpResponseMock->expects($this->any())->method(
'getStatus')->willReturn($statusBad);
187 $this->zendClientMock->expects($this->once())->method(
'request')->willReturn($zendHttpResponseMock);
189 $this->zendClientFactoryMock->expects($this->once())
191 ->willReturn($this->zendClientMock);
193 $this->assertInternalType(
195 $this->model->sendRequest()
208 $this->zendClientFactoryMock->expects($this->once())
210 ->willReturn($this->zendClientMock);
211 $this->configMock->expects($this->once())
212 ->method(
'getNewRelicAccountId')
213 ->willReturn($accId);
215 $this->expectException(\
Magento\Framework\Exception\LocalizedException::class);
217 $this->model->sendRequest();
testSendRequestStatusOk()
testSendRequestStatusBad()
testSendRequestException()