Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeploymentsTest.php
Go to the documentation of this file.
1 <?php
7 
9 use \Magento\Framework\HTTP\ZendClient;
10 
14 class DeploymentsTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $configMock;
25 
30 
34  protected $zendClientMock;
35 
39  protected $loggerMock;
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', 'setParameterPost'])
50  ->disableOriginalConstructor()
51  ->getMock();
52 
53  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
58  ->setMethods(['getNewRelicApiUrl', 'getNewRelicApiKey', 'getNewRelicAppName', 'getNewRelicAppId'])
59  ->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->model = new Deployments(
63  $this->configMock,
64  $this->loggerMock,
65  $this->zendClientFactoryMock
66  );
67  }
68 
74  public function testSetDeploymentRequestOk()
75  {
76  $data = $this->getDataVariables();
77 
78  $this->zendClientMock->expects($this->once())
79  ->method('setUri')
80  ->with($data['self_uri'])
81  ->willReturnSelf();
82 
83  $this->zendClientMock->expects($this->once())
84  ->method('setMethod')
85  ->with($data['method'])
86  ->willReturnSelf();
87 
88  $this->zendClientMock->expects($this->once())
89  ->method('setHeaders')
90  ->with($data['headers'])
91  ->willReturnSelf();
92 
93  $this->zendClientMock->expects($this->once())
94  ->method('setParameterPost')
95  ->with($data['params'])
96  ->willReturnSelf();
97 
98  $this->configMock->expects($this->once())
99  ->method('getNewRelicApiUrl')
100  ->willReturn('');
101 
102  $this->loggerMock->expects($this->once())->method('notice');
103 
104  $this->configMock->expects($this->once())
105  ->method('getNewRelicApiKey')
106  ->willReturn($data['api_key']);
107 
108  $this->configMock->expects($this->once())
109  ->method('getNewRelicAppName')
110  ->willReturn($data['app_name']);
111 
112  $this->configMock->expects($this->once())
113  ->method('getNewRelicAppId')
114  ->willReturn($data['app_id']);
115 
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']);
121 
122  $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
123 
124  $this->zendClientFactoryMock->expects($this->once())
125  ->method('create')
126  ->willReturn($this->zendClientMock);
127 
128  $this->assertInternalType(
129  'string',
130  $this->model->setDeployment($data['description'], $data['change'], $data['user'])
131  );
132  }
133 
139  public function testSetDeploymentBadStatus()
140  {
141  $data = $this->getDataVariables();
142 
143  $this->zendClientMock->expects($this->once())
144  ->method('setUri')
145  ->with($data['uri'])
146  ->willReturnSelf();
147 
148  $this->zendClientMock->expects($this->once())
149  ->method('setMethod')
150  ->with($data['method'])
151  ->willReturnSelf();
152 
153  $this->zendClientMock->expects($this->once())
154  ->method('setHeaders')
155  ->with($data['headers'])
156  ->willReturnSelf();
157 
158  $this->zendClientMock->expects($this->once())
159  ->method('setParameterPost')
160  ->with($data['params'])
161  ->willReturnSelf();
162 
163  $this->configMock->expects($this->once())
164  ->method('getNewRelicApiUrl')
165  ->willReturn($data['uri']);
166 
167  $this->configMock->expects($this->once())
168  ->method('getNewRelicApiKey')
169  ->willReturn($data['api_key']);
170 
171  $this->configMock->expects($this->once())
172  ->method('getNewRelicAppName')
173  ->willReturn($data['app_name']);
174 
175  $this->configMock->expects($this->once())
176  ->method('getNewRelicAppId')
177  ->willReturn($data['app_id']);
178 
179  $zendHttpResponseMock = $this->getMockBuilder(
180  \Zend_Http_Response::class
181  )->disableOriginalConstructor()->getMock();
182  $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_bad']);
183 
184  $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
185  $this->loggerMock->expects($this->once())->method('warning');
186 
187  $this->zendClientFactoryMock->expects($this->once())
188  ->method('create')
189  ->willReturn($this->zendClientMock);
190 
191  $this->assertInternalType(
192  'bool',
193  $this->model->setDeployment($data['description'], $data['change'], $data['user'])
194  );
195  }
196 
201  {
202  $data = $this->getDataVariables();
203 
204  $this->zendClientMock->expects($this->once())
205  ->method('setUri')
206  ->with($data['uri'])
207  ->willReturnSelf();
208 
209  $this->zendClientMock->expects($this->once())
210  ->method('setMethod')
211  ->with($data['method'])
212  ->willReturnSelf();
213 
214  $this->zendClientMock->expects($this->once())
215  ->method('setHeaders')
216  ->with($data['headers'])
217  ->willReturnSelf();
218 
219  $this->zendClientMock->expects($this->once())
220  ->method('setParameterPost')
221  ->with($data['params'])
222  ->willReturnSelf();
223 
224  $this->configMock->expects($this->once())
225  ->method('getNewRelicApiUrl')
226  ->willReturn($data['uri']);
227 
228  $this->configMock->expects($this->once())
229  ->method('getNewRelicApiKey')
230  ->willReturn($data['api_key']);
231 
232  $this->configMock->expects($this->once())
233  ->method('getNewRelicAppName')
234  ->willReturn($data['app_name']);
235 
236  $this->configMock->expects($this->once())
237  ->method('getNewRelicAppId')
238  ->willReturn($data['app_id']);
239 
240  $this->zendClientMock->expects($this->once())->method('request')->willThrowException(
242  );
243  $this->loggerMock->expects($this->once())->method('critical');
244 
245  $this->zendClientFactoryMock->expects($this->once())
246  ->method('create')
247  ->willReturn($this->zendClientMock);
248 
249  $this->assertInternalType(
250  'bool',
251  $this->model->setDeployment($data['description'], $data['change'], $data['user'])
252  );
253  }
254 
258  private function getDataVariables()
259  {
260  $description = 'Event description';
261  $change = 'flush the cache username';
262  $user = 'username';
263  $uri = 'https://example.com/listener';
264  $selfUri = 'https://api.newrelic.com/deployments.xml';
265  $apiKey = '1234';
266  $appName = 'app_name';
267  $appId = 'application_id';
269  $headers = ['x-api-key' => $apiKey];
270  $responseBody = 'Response body content';
271  $statusOk = '200';
272  $statusBad = '401';
273  $params = [
274  'deployment[app_name]' => $appName,
275  'deployment[application_id]' => $appId,
276  'deployment[description]' => $description,
277  'deployment[changelog]' => $change,
278  'deployment[user]' => $user
279  ];
280 
281  return ['description' => $description,
282  'change' => $change,
283  'user' => $user,
284  'uri' => $uri,
285  'self_uri' => $selfUri,
286  'api_key' => $apiKey,
287  'app_name' => $appName,
288  'app_id' => $appId,
289  'method' => $method,
290  'headers' => $headers,
291  'status_ok' => $statusOk,
292  'status_bad' => $statusBad,
293  'response_body' => $responseBody,
294  'params' => $params
295  ];
296  }
297 }
$user
Definition: dummy_user.php:13
$method
Definition: info.phtml:13
$change
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18