Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CronEventTest.php
Go to the documentation of this file.
1 <?php
7 
9 use \Magento\Framework\HTTP\ZendClient;
10 
14 class CronEventTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $configMock;
25 
30 
34  protected $zendClientMock;
35 
39  protected $jsonEncoderMock;
40 
41  protected function setUp()
42  {
43  $this->zendClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
44  ->setMethods(['create'])
45  ->disableOriginalConstructor()
46  ->getMock();
47 
48  $this->zendClientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
49  ->setMethods(['request', 'setUri', 'setMethod', 'setHeaders', 'setRawData'])
50  ->disableOriginalConstructor()
51  ->getMock();
52 
53  $this->jsonEncoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
58  ->setMethods([
59  'getNewRelicAccountId',
60  'getInsightsApiUrl',
61  'getInsightsInsertKey',
62  'getNewRelicAppName',
63  'getNewRelicAppId'
64  ])
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  $this->model = new CronEvent(
69  $this->configMock,
70  $this->jsonEncoderMock,
71  $this->zendClientFactoryMock
72  );
73  }
74 
80  public function testSendRequestStatusOk()
81  {
82  $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
83  $statusOk = '200';
84  $uri = 'https://example.com/listener';
86  $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
87  $accId = 'acc_id';
88  $appId = 'app_id';
89  $appName = 'app_name';
90  $insightApiKey = 'insert_key_value';
91 
92  $this->model->addData(['eventType'=>'Cron']);
93 
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();
98 
99  $this->configMock->expects($this->once())
100  ->method('getNewRelicAccountId')
101  ->willReturn($accId);
102 
103  $this->configMock->expects($this->once())
104  ->method('getInsightsApiUrl')
105  ->willReturn($uri);
106 
107  $this->configMock->expects($this->once())
108  ->method('getInsightsInsertKey')
109  ->willReturn($insightApiKey);
110 
111  $this->configMock->expects($this->once())
112  ->method('getNewRelicAppName')
113  ->willReturn($appName);
114 
115  $this->configMock->expects($this->once())
116  ->method('getNewRelicAppId')
117  ->willReturn($appId);
118 
119  $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
120 
121  $zendHttpResponseMock = $this->getMockBuilder(
122  \Zend_Http_Response::class
123  )->disableOriginalConstructor()->getMock();
124  $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusOk);
125 
126  $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
127 
128  $this->zendClientFactoryMock->expects($this->once())
129  ->method('create')
130  ->willReturn($this->zendClientMock);
131 
132  $this->assertInternalType(
133  'bool',
134  $this->model->sendRequest()
135  );
136  }
137 
143  public function testSendRequestStatusBad()
144  {
145  $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
146  $statusBad = '401';
147  $uri = 'https://example.com/listener';
149  $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
150  $accId = 'acc_id';
151  $appId = 'app_id';
152  $appName = 'app_name';
153  $insightApiKey = 'insert_key_value';
154 
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();
159 
160  $this->configMock->expects($this->once())
161  ->method('getNewRelicAccountId')
162  ->willReturn($accId);
163 
164  $this->configMock->expects($this->once())
165  ->method('getInsightsApiUrl')
166  ->willReturn($uri);
167 
168  $this->configMock->expects($this->once())
169  ->method('getInsightsInsertKey')
170  ->willReturn($insightApiKey);
171 
172  $this->configMock->expects($this->once())
173  ->method('getNewRelicAppName')
174  ->willReturn($appName);
175 
176  $this->configMock->expects($this->once())
177  ->method('getNewRelicAppId')
178  ->willReturn($appId);
179 
180  $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
181 
182  $zendHttpResponseMock = $this->getMockBuilder(
183  \Zend_Http_Response::class
184  )->disableOriginalConstructor()->getMock();
185  $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusBad);
186 
187  $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
188 
189  $this->zendClientFactoryMock->expects($this->once())
190  ->method('create')
191  ->willReturn($this->zendClientMock);
192 
193  $this->assertInternalType(
194  'bool',
195  $this->model->sendRequest()
196  );
197  }
198 
204  public function testSendRequestException()
205  {
206  $accId = '';
207 
208  $this->zendClientFactoryMock->expects($this->once())
209  ->method('create')
210  ->willReturn($this->zendClientMock);
211  $this->configMock->expects($this->once())
212  ->method('getNewRelicAccountId')
213  ->willReturn($accId);
214 
215  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
216 
217  $this->model->sendRequest();
218  }
219 }
$method
Definition: info.phtml:13