Skip to content

Commit 22fcb70

Browse files
ENGCOM-6913: [NewRelicReporting] Covering the New Relic plugins by Unit Tests #26878
2 parents 34d93e5 + 1840cf3 commit 22fcb70

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
* Tests setting the new relic app name
78+
*/
79+
public function testSuccessfullySettingAppName(): void
80+
{
81+
$this->configMock->expects($this->once())->method('isSeparateApps')->willReturn(true);
82+
$this->configMock->expects($this->any())->method('getNewRelicAppName')
83+
->willReturn(static::STUB_APP_NAME);
84+
$this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true);
85+
$this->stateMock->expects($this->once())->method('getAreaCode')->willReturn('frontend');
86+
$this->newRelicWrapperMock->expects($this->once())->method('setAppName');
87+
88+
$this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME);
89+
}
90+
91+
/**
92+
* Tests not being able to set the New Relic app name
93+
*
94+
* @param bool $isSeparateApps
95+
* @param string $newRelicAppName
96+
* @param bool $enabled
97+
*
98+
* @dataProvider newRelicConfigDataProvider
99+
*/
100+
public function testSuccessfullySettingAreaCode(bool $isSeparateApps, string $newRelicAppName, bool $enabled): void
101+
{
102+
$this->configMock->expects($this->any())->method('isSeparateApps')->willReturn($isSeparateApps);
103+
$this->configMock->expects($this->any())->method('getNewRelicAppName')->willReturn($newRelicAppName);
104+
$this->configMock->expects($this->any())->method('isNewRelicEnabled')->willReturn($enabled);
105+
$this->newRelicWrapperMock->expects($this->never())->method('setAppName');
106+
107+
$this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME);
108+
}
109+
110+
/**
111+
* New relic configuration data provider
112+
*
113+
* @return array
114+
*/
115+
public function newRelicConfigDataProvider(): array
116+
{
117+
return [
118+
'Separate apps config is disabled' => [
119+
false,
120+
static::STUB_APP_NAME,
121+
true
122+
],
123+
'Application name is not configured' => [
124+
true,
125+
'',
126+
true
127+
],
128+
'New Relic is disabled' => [
129+
true,
130+
static::STUB_APP_NAME,
131+
false
132+
]
133+
];
134+
}
135+
}

0 commit comments

Comments
 (0)