CronEventTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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;
  7. use Magento\NewRelicReporting\Model\CronEvent;
  8. use \Magento\Framework\HTTP\ZendClient;
  9. /**
  10. * Class CronEventTest
  11. */
  12. class CronEventTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\NewRelicReporting\Model\CronEvent
  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 \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $jsonEncoderMock;
  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', 'setRawData'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->jsonEncoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
  48. ->setMethods([
  49. 'getNewRelicAccountId',
  50. 'getInsightsApiUrl',
  51. 'getInsightsInsertKey',
  52. 'getNewRelicAppName',
  53. 'getNewRelicAppId'
  54. ])
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->model = new CronEvent(
  58. $this->configMock,
  59. $this->jsonEncoderMock,
  60. $this->zendClientFactoryMock
  61. );
  62. }
  63. /**
  64. * Tests client request with Ok status
  65. *
  66. * @return void
  67. */
  68. public function testSendRequestStatusOk()
  69. {
  70. $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
  71. $statusOk = '200';
  72. $uri = 'https://example.com/listener';
  73. $method = ZendClient::POST;
  74. $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
  75. $accId = 'acc_id';
  76. $appId = 'app_id';
  77. $appName = 'app_name';
  78. $insightApiKey = 'insert_key_value';
  79. $this->model->addData(['eventType'=>'Cron']);
  80. $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf();
  81. $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf();
  82. $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf();
  83. $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf();
  84. $this->configMock->expects($this->once())
  85. ->method('getNewRelicAccountId')
  86. ->willReturn($accId);
  87. $this->configMock->expects($this->once())
  88. ->method('getInsightsApiUrl')
  89. ->willReturn($uri);
  90. $this->configMock->expects($this->once())
  91. ->method('getInsightsInsertKey')
  92. ->willReturn($insightApiKey);
  93. $this->configMock->expects($this->once())
  94. ->method('getNewRelicAppName')
  95. ->willReturn($appName);
  96. $this->configMock->expects($this->once())
  97. ->method('getNewRelicAppId')
  98. ->willReturn($appId);
  99. $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
  100. $zendHttpResponseMock = $this->getMockBuilder(
  101. \Zend_Http_Response::class
  102. )->disableOriginalConstructor()->getMock();
  103. $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusOk);
  104. $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  105. $this->zendClientFactoryMock->expects($this->once())
  106. ->method('create')
  107. ->willReturn($this->zendClientMock);
  108. $this->assertInternalType(
  109. 'bool',
  110. $this->model->sendRequest()
  111. );
  112. }
  113. /**
  114. * Tests client request with Bad status
  115. *
  116. * @return void
  117. */
  118. public function testSendRequestStatusBad()
  119. {
  120. $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
  121. $statusBad = '401';
  122. $uri = 'https://example.com/listener';
  123. $method = ZendClient::POST;
  124. $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
  125. $accId = 'acc_id';
  126. $appId = 'app_id';
  127. $appName = 'app_name';
  128. $insightApiKey = 'insert_key_value';
  129. $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf();
  130. $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf();
  131. $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf();
  132. $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf();
  133. $this->configMock->expects($this->once())
  134. ->method('getNewRelicAccountId')
  135. ->willReturn($accId);
  136. $this->configMock->expects($this->once())
  137. ->method('getInsightsApiUrl')
  138. ->willReturn($uri);
  139. $this->configMock->expects($this->once())
  140. ->method('getInsightsInsertKey')
  141. ->willReturn($insightApiKey);
  142. $this->configMock->expects($this->once())
  143. ->method('getNewRelicAppName')
  144. ->willReturn($appName);
  145. $this->configMock->expects($this->once())
  146. ->method('getNewRelicAppId')
  147. ->willReturn($appId);
  148. $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
  149. $zendHttpResponseMock = $this->getMockBuilder(
  150. \Zend_Http_Response::class
  151. )->disableOriginalConstructor()->getMock();
  152. $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusBad);
  153. $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  154. $this->zendClientFactoryMock->expects($this->once())
  155. ->method('create')
  156. ->willReturn($this->zendClientMock);
  157. $this->assertInternalType(
  158. 'bool',
  159. $this->model->sendRequest()
  160. );
  161. }
  162. /**
  163. * Tests client request with exception
  164. *
  165. * @return void
  166. */
  167. public function testSendRequestException()
  168. {
  169. $accId = '';
  170. $this->zendClientFactoryMock->expects($this->once())
  171. ->method('create')
  172. ->willReturn($this->zendClientMock);
  173. $this->configMock->expects($this->once())
  174. ->method('getNewRelicAccountId')
  175. ->willReturn($accId);
  176. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  177. $this->model->sendRequest();
  178. }
  179. }