/vendor/magento/module-new-relic-reporting/Test/Unit/Model/CronEventTest.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 215 lines · 138 code · 40 blank · 37 comment · 0 complexity · 94f504782908772b75a1d7eeb068fe56 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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')
  37. ->setMethods(['create'])
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->zendClientMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClient')
  41. ->setMethods(['request', 'setUri', 'setMethod', 'setHeaders', 'setRawData'])
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->jsonEncoderMock = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface')
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->configMock = $this->getMockBuilder('Magento\NewRelicReporting\Model\Config')
  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('Zend_Http_Response')->disableOriginalConstructor()->getMock();
  101. $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusOk);
  102. $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  103. $this->zendClientFactoryMock->expects($this->once())
  104. ->method('create')
  105. ->willReturn($this->zendClientMock);
  106. $this->assertInternalType(
  107. 'bool',
  108. $this->model->sendRequest()
  109. );
  110. }
  111. /**
  112. * Tests client request with Bad status
  113. *
  114. * @return void
  115. */
  116. public function testSendRequestStatusBad()
  117. {
  118. $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
  119. $statusBad = '401';
  120. $uri = 'https://example.com/listener';
  121. $method = ZendClient::POST;
  122. $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
  123. $accId = 'acc_id';
  124. $appId = 'app_id';
  125. $appName = 'app_name';
  126. $insightApiKey = 'insert_key_value';
  127. $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf();
  128. $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf();
  129. $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf();
  130. $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf();
  131. $this->configMock->expects($this->once())
  132. ->method('getNewRelicAccountId')
  133. ->willReturn($accId);
  134. $this->configMock->expects($this->once())
  135. ->method('getInsightsApiUrl')
  136. ->willReturn($uri);
  137. $this->configMock->expects($this->once())
  138. ->method('getInsightsInsertKey')
  139. ->willReturn($insightApiKey);
  140. $this->configMock->expects($this->once())
  141. ->method('getNewRelicAppName')
  142. ->willReturn($appName);
  143. $this->configMock->expects($this->once())
  144. ->method('getNewRelicAppId')
  145. ->willReturn($appId);
  146. $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
  147. $zendHttpResponseMock = $this->getMockBuilder('Zend_Http_Response')->disableOriginalConstructor()->getMock();
  148. $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusBad);
  149. $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  150. $this->zendClientFactoryMock->expects($this->once())
  151. ->method('create')
  152. ->willReturn($this->zendClientMock);
  153. $this->assertInternalType(
  154. 'bool',
  155. $this->model->sendRequest()
  156. );
  157. }
  158. /**
  159. * Tests client request with exception
  160. *
  161. * @return void
  162. */
  163. public function testSendRequestException()
  164. {
  165. $accId = '';
  166. $this->zendClientFactoryMock->expects($this->once())
  167. ->method('create')
  168. ->willReturn($this->zendClientMock);
  169. $this->configMock->expects($this->once())
  170. ->method('getNewRelicAccountId')
  171. ->willReturn($accId);
  172. $this->setExpectedException('Magento\Framework\Exception\LocalizedException');
  173. $this->model->sendRequest();
  174. }
  175. }