Skip to content

Commit aeb076d

Browse files
committed
Covering the New Relic plugins by Unit Tests
1 parent e280215 commit aeb076d

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Test\Unit\Plugin;
9+
10+
use Exception;
11+
use Magento\Framework\App\Bootstrap;
12+
use Magento\Framework\App\Http;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\NewRelicReporting\Model\Config as NewRelicConfig;
15+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
16+
use Magento\NewRelicReporting\Plugin\HttpPlugin;
17+
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Framework\MockObject\MockObject as MockObject;
19+
20+
/**
21+
* Test coverage for \Magento\NewRelicReporting\Plugin\HttpPlugin
22+
*/
23+
class HttpPluginTest extends TestCase
24+
{
25+
/**
26+
* @var HttpPlugin
27+
*/
28+
private $httpPlugin;
29+
30+
/**
31+
* @var NewRelicConfig|MockObject
32+
*/
33+
private $configMock;
34+
35+
/**
36+
* @var NewRelicWrapper|MockObject
37+
*/
38+
private $newRelicWrapperMock;
39+
40+
/**
41+
* @var Http|MockObject
42+
*/
43+
private $httpMock;
44+
45+
/**
46+
* @var Bootstrap|MockObject
47+
*/
48+
private $bootstrapMock;
49+
50+
/**
51+
* @var Exception|MockObject
52+
*/
53+
private $exceptionMock;
54+
55+
/**
56+
* Set Up
57+
*/
58+
public function setUp(): void
59+
{
60+
$objectManager = new ObjectManager($this);
61+
$this->configMock = $this->getMockBuilder(NewRelicConfig::class)->disableOriginalConstructor()
62+
->getMock();
63+
$this->newRelicWrapperMock = $this->createMock(NewRelicWrapper::class);
64+
$this->httpMock = $this->createMock(Http::class);
65+
$this->bootstrapMock = $this->createMock(Bootstrap::class);
66+
$this->exceptionMock = $this->createMock(Exception::class);
67+
68+
$this->httpPlugin = $objectManager->getObject(
69+
HttpPlugin::class,
70+
[
71+
'config' => $this->configMock,
72+
'newRelicWrapper' => $this->newRelicWrapperMock,
73+
]
74+
);
75+
}
76+
77+
/**
78+
* Tests the thrown exception is reported to New Relic
79+
*/
80+
public function testSuccessfullyReportingError(): void
81+
{
82+
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true);
83+
$this->newRelicWrapperMock->expects($this->once())->method('reportError');
84+
85+
$this->httpPlugin->beforeCatchException($this->httpMock, $this->bootstrapMock, $this->exceptionMock);
86+
}
87+
88+
/**
89+
* Tests the thrown exception is not reported to New Relic
90+
*/
91+
public function testNotReportingException(): void
92+
{
93+
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(false);
94+
$this->newRelicWrapperMock->expects($this->never())->method('reportError');
95+
96+
$this->httpPlugin->beforeCatchException($this->httpMock, $this->bootstrapMock, $this->exceptionMock);
97+
}
98+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Test\Unit\Plugin;
9+
10+
use Magento\Framework\App\State;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\NewRelicReporting\Model\Config as NewRelicConfig;
13+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
14+
use Magento\NewRelicReporting\Plugin\StatePlugin;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\MockObject\MockObject as MockObject;
17+
use Psr\Log\LoggerInterface;
18+
19+
/**
20+
* Test coverage for \Magento\NewRelicReporting\Plugin\StatePlugin
21+
*/
22+
class StatePluginTest extends TestCase
23+
{
24+
/**
25+
* @var string
26+
*/
27+
private const STUB_APP_NAME = 'app_name';
28+
29+
/**
30+
* @var StatePlugin
31+
*/
32+
private $statePlugin;
33+
34+
/**
35+
* @var NewRelicConfig|MockObject
36+
*/
37+
private $configMock;
38+
39+
/**
40+
* @var NewRelicWrapper|MockObject
41+
*/
42+
private $newRelicWrapperMock;
43+
44+
/**
45+
* @var LoggerInterface|MockObject
46+
*/
47+
private $loggerMock;
48+
49+
/**
50+
* @var State|MockObject
51+
*/
52+
private $stateMock;
53+
54+
/**
55+
* Set Up
56+
*/
57+
public function setUp(): void
58+
{
59+
$objectManager = new ObjectManager($this);
60+
$this->configMock = $this->getMockBuilder(NewRelicConfig::class)->disableOriginalConstructor()
61+
->getMock();
62+
$this->newRelicWrapperMock = $this->createMock(NewRelicWrapper::class);
63+
$this->loggerMock = $this->createMock(LoggerInterface::class);
64+
$this->stateMock = $this->createMock(State::class);
65+
66+
$this->statePlugin = $objectManager->getObject(
67+
StatePlugin::class,
68+
[
69+
'config' => $this->configMock,
70+
'newRelicWrapper' => $this->newRelicWrapperMock,
71+
'logger' => $this->loggerMock,
72+
]
73+
);
74+
75+
}
76+
77+
/**
78+
* Tests setting the new relic app name
79+
*/
80+
public function testSuccessfullySettingAppName(): void
81+
{
82+
$this->configMock->expects($this->once())->method('isSeparateApps')->willReturn(true);
83+
$this->configMock->expects($this->any())->method('getNewRelicAppName')
84+
->willReturn(static::STUB_APP_NAME);
85+
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true);
86+
$this->stateMock->expects($this->once())->method('getAreaCode')->willReturn('frontend');
87+
$this->newRelicWrapperMock->expects($this->once())->method('setAppName');
88+
89+
$this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME);
90+
}
91+
92+
/**
93+
* Tests not being able to set the New Relic app name
94+
*
95+
* @param bool $isSeparateApps
96+
* @param string $newRelicAppName
97+
* @param bool $enabled
98+
*
99+
* @dataProvider newRelicConfigDataProvider
100+
*/
101+
public function testSuccessfullySettingAreaCode(bool $isSeparateApps, string $newRelicAppName, bool $enabled): void
102+
{
103+
$this->configMock->expects($this->any())->method('isSeparateApps')->willReturn($isSeparateApps);
104+
$this->configMock->expects($this->any())->method('getNewRelicAppName')->willReturn($newRelicAppName);
105+
$this->configMock->expects($this->any())->method('isNewRelicEnabled')->willReturn($enabled);
106+
$this->newRelicWrapperMock->expects($this->never())->method('setAppName');
107+
108+
$this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME);
109+
}
110+
111+
/**
112+
* New relic configuration data provider
113+
*
114+
* @return array
115+
*/
116+
public function newRelicConfigDataProvider(): array
117+
{
118+
return [
119+
'Separate apps config is disabled' => [
120+
false,
121+
static::STUB_APP_NAME,
122+
true
123+
],
124+
'Application name is not configured' => [
125+
true,
126+
'',
127+
true
128+
],
129+
'New Relic is disabled' => [
130+
true,
131+
static::STUB_APP_NAME,
132+
false
133+
]
134+
];
135+
}
136+
}

0 commit comments

Comments
 (0)