Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NewRelicReporting] Covering the New Relic plugins by Unit Tests #26878

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\NewRelicReporting\Test\Unit\Plugin;

use Exception;
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Http;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\NewRelicReporting\Model\Config as NewRelicConfig;
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Magento\NewRelicReporting\Plugin\HttpPlugin;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject as MockObject;

/**
* Test coverage for \Magento\NewRelicReporting\Plugin\HttpPlugin
*/
class HttpPluginTest extends TestCase
{
/**
* @var HttpPlugin
*/
private $httpPlugin;

/**
* @var NewRelicConfig|MockObject
*/
private $configMock;

/**
* @var NewRelicWrapper|MockObject
*/
private $newRelicWrapperMock;

/**
* @var Http|MockObject
*/
private $httpMock;

/**
* @var Bootstrap|MockObject
*/
private $bootstrapMock;

/**
* @var Exception|MockObject
*/
private $exceptionMock;

/**
* Set Up
*/
public function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->configMock = $this->getMockBuilder(NewRelicConfig::class)->disableOriginalConstructor()
->getMock();
$this->newRelicWrapperMock = $this->createMock(NewRelicWrapper::class);
$this->httpMock = $this->createMock(Http::class);
$this->bootstrapMock = $this->createMock(Bootstrap::class);
$this->exceptionMock = $this->createMock(Exception::class);

$this->httpPlugin = $objectManager->getObject(
HttpPlugin::class,
[
'config' => $this->configMock,
'newRelicWrapper' => $this->newRelicWrapperMock,
]
);
}

/**
* Tests the thrown exception is reported to New Relic
*/
public function testSuccessfullyReportingError(): void
{
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true);
$this->newRelicWrapperMock->expects($this->once())->method('reportError');

$this->httpPlugin->beforeCatchException($this->httpMock, $this->bootstrapMock, $this->exceptionMock);
}

/**
* Tests the thrown exception is not reported to New Relic
*/
public function testNotReportingException(): void
{
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(false);
$this->newRelicWrapperMock->expects($this->never())->method('reportError');

$this->httpPlugin->beforeCatchException($this->httpMock, $this->bootstrapMock, $this->exceptionMock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\NewRelicReporting\Test\Unit\Plugin;

use Magento\Framework\App\State;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\NewRelicReporting\Model\Config as NewRelicConfig;
use Magento\NewRelicReporting\Model\NewRelicWrapper;
use Magento\NewRelicReporting\Plugin\StatePlugin;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject as MockObject;
use Psr\Log\LoggerInterface;

/**
* Test coverage for \Magento\NewRelicReporting\Plugin\StatePlugin
*/
class StatePluginTest extends TestCase
{
/**
* @var string
*/
private const STUB_APP_NAME = 'app_name';

/**
* @var StatePlugin
*/
private $statePlugin;

/**
* @var NewRelicConfig|MockObject
*/
private $configMock;

/**
* @var NewRelicWrapper|MockObject
*/
private $newRelicWrapperMock;

/**
* @var LoggerInterface|MockObject
*/
private $loggerMock;

/**
* @var State|MockObject
*/
private $stateMock;

/**
* Set Up
*/
public function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->configMock = $this->getMockBuilder(NewRelicConfig::class)->disableOriginalConstructor()
->getMock();
$this->newRelicWrapperMock = $this->createMock(NewRelicWrapper::class);
$this->loggerMock = $this->createMock(LoggerInterface::class);
$this->stateMock = $this->createMock(State::class);

$this->statePlugin = $objectManager->getObject(
StatePlugin::class,
[
'config' => $this->configMock,
'newRelicWrapper' => $this->newRelicWrapperMock,
'logger' => $this->loggerMock,
]
);
}

/**
* Tests setting the new relic app name
*/
public function testSuccessfullySettingAppName(): void
{
$this->configMock->expects($this->once())->method('isSeparateApps')->willReturn(true);
$this->configMock->expects($this->any())->method('getNewRelicAppName')
->willReturn(static::STUB_APP_NAME);
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true);
$this->stateMock->expects($this->once())->method('getAreaCode')->willReturn('frontend');
$this->newRelicWrapperMock->expects($this->once())->method('setAppName');

$this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME);
}

/**
* Tests not being able to set the New Relic app name
*
* @param bool $isSeparateApps
* @param string $newRelicAppName
* @param bool $enabled
*
* @dataProvider newRelicConfigDataProvider
*/
public function testSuccessfullySettingAreaCode(bool $isSeparateApps, string $newRelicAppName, bool $enabled): void
{
$this->configMock->expects($this->any())->method('isSeparateApps')->willReturn($isSeparateApps);
$this->configMock->expects($this->any())->method('getNewRelicAppName')->willReturn($newRelicAppName);
$this->configMock->expects($this->any())->method('isNewRelicEnabled')->willReturn($enabled);
$this->newRelicWrapperMock->expects($this->never())->method('setAppName');

$this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME);
}

/**
* New relic configuration data provider
*
* @return array
*/
public function newRelicConfigDataProvider(): array
{
return [
'Separate apps config is disabled' => [
false,
static::STUB_APP_NAME,
true
],
'Application name is not configured' => [
true,
'',
true
],
'New Relic is disabled' => [
true,
static::STUB_APP_NAME,
false
]
];
}
}