DeploymentsTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Test\Unit\Model\Apm;
  7. use Magento\NewRelicReporting\Model\Apm\Deployments;
  8. use \Magento\Framework\HTTP\ZendClient;
  9. /**
  10. * Class DeploymentsTest
  11. */
  12. class DeploymentsTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\NewRelicReporting\Model\Apm\Deployments
  16. */
  17. protected $model;
  18. /**
  19. * @var \Magento\NewRelicReporting\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $configMock;
  22. /**
  23. * @var \Magento\Framework\HTTP\ZendClientFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $zendClientFactoryMock;
  26. /**
  27. * @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $zendClientMock;
  30. /**
  31. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $loggerMock;
  34. protected function setUp()
  35. {
  36. $this->zendClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
  37. ->setMethods(['create'])
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->zendClientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
  41. ->setMethods(['request', 'setUri', 'setMethod', 'setHeaders', 'setParameterPost'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
  48. ->setMethods(['getNewRelicApiUrl', 'getNewRelicApiKey', 'getNewRelicAppName', 'getNewRelicAppId'])
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->model = new Deployments(
  52. $this->configMock,
  53. $this->loggerMock,
  54. $this->zendClientFactoryMock
  55. );
  56. }
  57. /**
  58. * Tests client request with Ok status
  59. *
  60. * @return void
  61. */
  62. public function testSetDeploymentRequestOk()
  63. {
  64. $data = $this->getDataVariables();
  65. $this->zendClientMock->expects($this->once())
  66. ->method('setUri')
  67. ->with($data['self_uri'])
  68. ->willReturnSelf();
  69. $this->zendClientMock->expects($this->once())
  70. ->method('setMethod')
  71. ->with($data['method'])
  72. ->willReturnSelf();
  73. $this->zendClientMock->expects($this->once())
  74. ->method('setHeaders')
  75. ->with($data['headers'])
  76. ->willReturnSelf();
  77. $this->zendClientMock->expects($this->once())
  78. ->method('setParameterPost')
  79. ->with($data['params'])
  80. ->willReturnSelf();
  81. $this->configMock->expects($this->once())
  82. ->method('getNewRelicApiUrl')
  83. ->willReturn('');
  84. $this->loggerMock->expects($this->once())->method('notice');
  85. $this->configMock->expects($this->once())
  86. ->method('getNewRelicApiKey')
  87. ->willReturn($data['api_key']);
  88. $this->configMock->expects($this->once())
  89. ->method('getNewRelicAppName')
  90. ->willReturn($data['app_name']);
  91. $this->configMock->expects($this->once())
  92. ->method('getNewRelicAppId')
  93. ->willReturn($data['app_id']);
  94. $zendHttpResponseMock = $this->getMockBuilder(
  95. \Zend_Http_Response::class
  96. )->disableOriginalConstructor()->getMock();
  97. $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_ok']);
  98. $zendHttpResponseMock->expects($this->once())->method('getBody')->willReturn($data['response_body']);
  99. $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  100. $this->zendClientFactoryMock->expects($this->once())
  101. ->method('create')
  102. ->willReturn($this->zendClientMock);
  103. $this->assertInternalType(
  104. 'string',
  105. $this->model->setDeployment($data['description'], $data['change'], $data['user'])
  106. );
  107. }
  108. /**
  109. * Tests client request with bad status
  110. *
  111. * @return void
  112. */
  113. public function testSetDeploymentBadStatus()
  114. {
  115. $data = $this->getDataVariables();
  116. $this->zendClientMock->expects($this->once())
  117. ->method('setUri')
  118. ->with($data['uri'])
  119. ->willReturnSelf();
  120. $this->zendClientMock->expects($this->once())
  121. ->method('setMethod')
  122. ->with($data['method'])
  123. ->willReturnSelf();
  124. $this->zendClientMock->expects($this->once())
  125. ->method('setHeaders')
  126. ->with($data['headers'])
  127. ->willReturnSelf();
  128. $this->zendClientMock->expects($this->once())
  129. ->method('setParameterPost')
  130. ->with($data['params'])
  131. ->willReturnSelf();
  132. $this->configMock->expects($this->once())
  133. ->method('getNewRelicApiUrl')
  134. ->willReturn($data['uri']);
  135. $this->configMock->expects($this->once())
  136. ->method('getNewRelicApiKey')
  137. ->willReturn($data['api_key']);
  138. $this->configMock->expects($this->once())
  139. ->method('getNewRelicAppName')
  140. ->willReturn($data['app_name']);
  141. $this->configMock->expects($this->once())
  142. ->method('getNewRelicAppId')
  143. ->willReturn($data['app_id']);
  144. $zendHttpResponseMock = $this->getMockBuilder(
  145. \Zend_Http_Response::class
  146. )->disableOriginalConstructor()->getMock();
  147. $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_bad']);
  148. $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  149. $this->loggerMock->expects($this->once())->method('warning');
  150. $this->zendClientFactoryMock->expects($this->once())
  151. ->method('create')
  152. ->willReturn($this->zendClientMock);
  153. $this->assertInternalType(
  154. 'bool',
  155. $this->model->setDeployment($data['description'], $data['change'], $data['user'])
  156. );
  157. }
  158. /**
  159. * Tests client request will fail
  160. */
  161. public function testSetDeploymentRequestFail()
  162. {
  163. $data = $this->getDataVariables();
  164. $this->zendClientMock->expects($this->once())
  165. ->method('setUri')
  166. ->with($data['uri'])
  167. ->willReturnSelf();
  168. $this->zendClientMock->expects($this->once())
  169. ->method('setMethod')
  170. ->with($data['method'])
  171. ->willReturnSelf();
  172. $this->zendClientMock->expects($this->once())
  173. ->method('setHeaders')
  174. ->with($data['headers'])
  175. ->willReturnSelf();
  176. $this->zendClientMock->expects($this->once())
  177. ->method('setParameterPost')
  178. ->with($data['params'])
  179. ->willReturnSelf();
  180. $this->configMock->expects($this->once())
  181. ->method('getNewRelicApiUrl')
  182. ->willReturn($data['uri']);
  183. $this->configMock->expects($this->once())
  184. ->method('getNewRelicApiKey')
  185. ->willReturn($data['api_key']);
  186. $this->configMock->expects($this->once())
  187. ->method('getNewRelicAppName')
  188. ->willReturn($data['app_name']);
  189. $this->configMock->expects($this->once())
  190. ->method('getNewRelicAppId')
  191. ->willReturn($data['app_id']);
  192. $this->zendClientMock->expects($this->once())->method('request')->willThrowException(
  193. new \Zend_Http_Client_Exception()
  194. );
  195. $this->loggerMock->expects($this->once())->method('critical');
  196. $this->zendClientFactoryMock->expects($this->once())
  197. ->method('create')
  198. ->willReturn($this->zendClientMock);
  199. $this->assertInternalType(
  200. 'bool',
  201. $this->model->setDeployment($data['description'], $data['change'], $data['user'])
  202. );
  203. }
  204. /**
  205. * @return array
  206. */
  207. private function getDataVariables()
  208. {
  209. $description = 'Event description';
  210. $change = 'flush the cache username';
  211. $user = 'username';
  212. $uri = 'https://example.com/listener';
  213. $selfUri = 'https://api.newrelic.com/deployments.xml';
  214. $apiKey = '1234';
  215. $appName = 'app_name';
  216. $appId = 'application_id';
  217. $method = ZendClient::POST;
  218. $headers = ['x-api-key' => $apiKey];
  219. $responseBody = 'Response body content';
  220. $statusOk = '200';
  221. $statusBad = '401';
  222. $params = [
  223. 'deployment[app_name]' => $appName,
  224. 'deployment[application_id]' => $appId,
  225. 'deployment[description]' => $description,
  226. 'deployment[changelog]' => $change,
  227. 'deployment[user]' => $user
  228. ];
  229. return ['description' => $description,
  230. 'change' => $change,
  231. 'user' => $user,
  232. 'uri' => $uri,
  233. 'self_uri' => $selfUri,
  234. 'api_key' => $apiKey,
  235. 'app_name' => $appName,
  236. 'app_id' => $appId,
  237. 'method' => $method,
  238. 'headers' => $headers,
  239. 'status_ok' => $statusOk,
  240. 'status_bad' => $statusBad,
  241. 'response_body' => $responseBody,
  242. 'params' => $params
  243. ];
  244. }
  245. }