123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\NewRelicReporting\Test\Unit\Model\Apm;
- use Magento\NewRelicReporting\Model\Apm\Deployments;
- use \Magento\Framework\HTTP\ZendClient;
- /**
- * Class DeploymentsTest
- */
- class DeploymentsTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\NewRelicReporting\Model\Apm\Deployments
- */
- 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 \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $loggerMock;
- 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', 'setParameterPost'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
- ->setMethods(['getNewRelicApiUrl', 'getNewRelicApiKey', 'getNewRelicAppName', 'getNewRelicAppId'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = new Deployments(
- $this->configMock,
- $this->loggerMock,
- $this->zendClientFactoryMock
- );
- }
- /**
- * Tests client request with Ok status
- *
- * @return void
- */
- public function testSetDeploymentRequestOk()
- {
- $data = $this->getDataVariables();
- $this->zendClientMock->expects($this->once())
- ->method('setUri')
- ->with($data['self_uri'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setMethod')
- ->with($data['method'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setHeaders')
- ->with($data['headers'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setParameterPost')
- ->with($data['params'])
- ->willReturnSelf();
- $this->configMock->expects($this->once())
- ->method('getNewRelicApiUrl')
- ->willReturn('');
- $this->loggerMock->expects($this->once())->method('notice');
- $this->configMock->expects($this->once())
- ->method('getNewRelicApiKey')
- ->willReturn($data['api_key']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppName')
- ->willReturn($data['app_name']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppId')
- ->willReturn($data['app_id']);
- $zendHttpResponseMock = $this->getMockBuilder(
- \Zend_Http_Response::class
- )->disableOriginalConstructor()->getMock();
- $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_ok']);
- $zendHttpResponseMock->expects($this->once())->method('getBody')->willReturn($data['response_body']);
- $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
- $this->zendClientFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->zendClientMock);
- $this->assertInternalType(
- 'string',
- $this->model->setDeployment($data['description'], $data['change'], $data['user'])
- );
- }
- /**
- * Tests client request with bad status
- *
- * @return void
- */
- public function testSetDeploymentBadStatus()
- {
- $data = $this->getDataVariables();
- $this->zendClientMock->expects($this->once())
- ->method('setUri')
- ->with($data['uri'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setMethod')
- ->with($data['method'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setHeaders')
- ->with($data['headers'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setParameterPost')
- ->with($data['params'])
- ->willReturnSelf();
- $this->configMock->expects($this->once())
- ->method('getNewRelicApiUrl')
- ->willReturn($data['uri']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicApiKey')
- ->willReturn($data['api_key']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppName')
- ->willReturn($data['app_name']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppId')
- ->willReturn($data['app_id']);
- $zendHttpResponseMock = $this->getMockBuilder(
- \Zend_Http_Response::class
- )->disableOriginalConstructor()->getMock();
- $zendHttpResponseMock->expects($this->any())->method('getStatus')->willReturn($data['status_bad']);
- $this->zendClientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
- $this->loggerMock->expects($this->once())->method('warning');
- $this->zendClientFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->zendClientMock);
- $this->assertInternalType(
- 'bool',
- $this->model->setDeployment($data['description'], $data['change'], $data['user'])
- );
- }
- /**
- * Tests client request will fail
- */
- public function testSetDeploymentRequestFail()
- {
- $data = $this->getDataVariables();
- $this->zendClientMock->expects($this->once())
- ->method('setUri')
- ->with($data['uri'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setMethod')
- ->with($data['method'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setHeaders')
- ->with($data['headers'])
- ->willReturnSelf();
- $this->zendClientMock->expects($this->once())
- ->method('setParameterPost')
- ->with($data['params'])
- ->willReturnSelf();
- $this->configMock->expects($this->once())
- ->method('getNewRelicApiUrl')
- ->willReturn($data['uri']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicApiKey')
- ->willReturn($data['api_key']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppName')
- ->willReturn($data['app_name']);
- $this->configMock->expects($this->once())
- ->method('getNewRelicAppId')
- ->willReturn($data['app_id']);
- $this->zendClientMock->expects($this->once())->method('request')->willThrowException(
- new \Zend_Http_Client_Exception()
- );
- $this->loggerMock->expects($this->once())->method('critical');
- $this->zendClientFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->zendClientMock);
- $this->assertInternalType(
- 'bool',
- $this->model->setDeployment($data['description'], $data['change'], $data['user'])
- );
- }
- /**
- * @return array
- */
- private function getDataVariables()
- {
- $description = 'Event description';
- $change = 'flush the cache username';
- $user = 'username';
- $uri = 'https://example.com/listener';
- $selfUri = 'https://api.newrelic.com/deployments.xml';
- $apiKey = '1234';
- $appName = 'app_name';
- $appId = 'application_id';
- $method = ZendClient::POST;
- $headers = ['x-api-key' => $apiKey];
- $responseBody = 'Response body content';
- $statusOk = '200';
- $statusBad = '401';
- $params = [
- 'deployment[app_name]' => $appName,
- 'deployment[application_id]' => $appId,
- 'deployment[description]' => $description,
- 'deployment[changelog]' => $change,
- 'deployment[user]' => $user
- ];
- return ['description' => $description,
- 'change' => $change,
- 'user' => $user,
- 'uri' => $uri,
- 'self_uri' => $selfUri,
- 'api_key' => $apiKey,
- 'app_name' => $appName,
- 'app_id' => $appId,
- 'method' => $method,
- 'headers' => $headers,
- 'status_ok' => $statusOk,
- 'status_bad' => $statusBad,
- 'response_body' => $responseBody,
- 'params' => $params
- ];
- }
- }
|