123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\NewRelicReporting\Test\Unit\Model;
- use Magento\NewRelicReporting\Model\CronEvent;
- use \Magento\Framework\HTTP\ZendClient;
- /**
- * Class CronEventTest
- */
- class CronEventTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\NewRelicReporting\Model\CronEvent
- */
- protected $model;
- /**
- * @var \Magento\NewRelicReporting\Model\Config|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $configMock;
- /**
- * @var \Magento\Framework\HTTP\ZendClientFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $zendClientFactoryMock;
- /**
- * @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $zendClientMock;
- /**
- * @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $jsonEncoderMock;
- protected function setUp()
- {
- $this->zendClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->zendClientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
- ->setMethods(['request', 'setUri', 'setMethod', 'setHeaders', 'setRawData'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->jsonEncoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
- ->setMethods([
- 'getNewRelicAccountId',
- 'getInsightsApiUrl',
- 'getInsightsInsertKey',
- 'getNewRelicAppName',
- 'getNewRelicAppId'
- ])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = new CronEvent(
- $this->configMock,
- $this->jsonEncoderMock,
- $this->zendClientFactoryMock
- );
- }
- /**
- * Tests client request with Ok status
- *
- * @return void
- */
- public function testSendRequestStatusOk()
- {
- $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
- $statusOk = '200';
- $uri = 'https://example.com/listener';
- $method = ZendClient::POST;
- $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
- $accId = 'acc_id';
- $appId = 'app_id';
- $appName = 'app_name';
- $insightApiKey = 'insert_key_value';
- $this->model->addData(['eventType'=>'Cron']);
- $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf();
- $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf();
- $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf();
- $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf();
- $this->configMock->expects($this->once())
- ->method('getNewRelicAccountId')
- ->willReturn($accId);
- $this->configMock->expects($this->once())
- ->method('getInsightsApiUrl')
- ->willReturn($uri);
- $this->configMock->expects($this->once())
- ->method('getInsightsInsertKey')
- ->willReturn($insightApiKey);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppName')
- ->willReturn($appName);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppId')
- ->willReturn($appId);
- $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
- $zendHttpResponseMock = $this->getMockBuilder(
- \Zend_Http_Response::class
- )->disableOriginalConstructor()->getMock();
- $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusOk);
- $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
- $this->zendClientFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->zendClientMock);
- $this->assertInternalType(
- 'bool',
- $this->model->sendRequest()
- );
- }
- /**
- * Tests client request with Bad status
- *
- * @return void
- */
- public function testSendRequestStatusBad()
- {
- $json = '{"eventType":"Cron","appName":"app_name","appId":"app_id"}';
- $statusBad = '401';
- $uri = 'https://example.com/listener';
- $method = ZendClient::POST;
- $headers = ['X-Insert-Key' => 'insert_key_value', 'Content-Type' => 'application/json'];
- $accId = 'acc_id';
- $appId = 'app_id';
- $appName = 'app_name';
- $insightApiKey = 'insert_key_value';
- $this->zendClientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf();
- $this->zendClientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf();
- $this->zendClientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf();
- $this->zendClientMock->expects($this->once())->method('setRawData')->with($json)->willReturnSelf();
- $this->configMock->expects($this->once())
- ->method('getNewRelicAccountId')
- ->willReturn($accId);
- $this->configMock->expects($this->once())
- ->method('getInsightsApiUrl')
- ->willReturn($uri);
- $this->configMock->expects($this->once())
- ->method('getInsightsInsertKey')
- ->willReturn($insightApiKey);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppName')
- ->willReturn($appName);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppId')
- ->willReturn($appId);
- $this->jsonEncoderMock->expects($this->once())->method('encode')->willReturn($json);
- $zendHttpResponseMock = $this->getMockBuilder(
- \Zend_Http_Response::class
- )->disableOriginalConstructor()->getMock();
- $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($statusBad);
- $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
- $this->zendClientFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->zendClientMock);
- $this->assertInternalType(
- 'bool',
- $this->model->sendRequest()
- );
- }
- /**
- * Tests client request with exception
- *
- * @return void
- */
- public function testSendRequestException()
- {
- $accId = '';
- $this->zendClientFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->zendClientMock);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAccountId')
- ->willReturn($accId);
- $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
- $this->model->sendRequest();
- }
- }
|