Skip to content

Commit 2dbcaef

Browse files
author
Stanislav Idolov
authored
ENGCOM-3005: [Backport] Fixes from #15947 #18113
2 parents 017d3c3 + 88ddcaa commit 2dbcaef

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

app/code/Magento/NewRelicReporting/Model/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\NewRelicReporting\Model;
77

8+
/**
9+
* NewRelic configuration model
10+
*/
811
class Config
912
{
1013
/**#@+

app/code/Magento/NewRelicReporting/Plugin/StatePlugin.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\NewRelicReporting\Plugin;
79

810
use Magento\Framework\App\State;
@@ -11,6 +13,9 @@
1113
use Magento\NewRelicReporting\Model\NewRelicWrapper;
1214
use Psr\Log\LoggerInterface;
1315

16+
/**
17+
* Handles setting which, when enabled, reports frontend and adminhtml as separate apps to New Relic.
18+
*/
1419
class StatePlugin
1520
{
1621
/**
@@ -31,6 +36,7 @@ class StatePlugin
3136
/**
3237
* @param Config $config
3338
* @param NewRelicWrapper $newRelicWrapper
39+
* @param LoggerInterface $logger
3440
*/
3541
public function __construct(
3642
Config $config,
@@ -46,32 +52,33 @@ public function __construct(
4652
* Set separate appname
4753
*
4854
* @param State $subject
49-
* @param null $result
50-
* @return void
51-
*
52-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
55+
* @param mixed $result
56+
* @return mixed
5357
*/
54-
public function afterSetAreaCode(State $state, $result)
58+
public function afterSetAreaCode(State $subject, $result)
5559
{
5660
if (!$this->shouldSetAppName()) {
5761
return $result;
5862
}
5963

6064
try {
61-
$this->newRelicWrapper->setAppName($this->appName($state));
65+
$this->newRelicWrapper->setAppName($this->appName($subject));
6266
} catch (LocalizedException $e) {
6367
$this->logger->critical($e);
6468
return $result;
6569
}
70+
71+
return $result;
6672
}
6773

6874
/**
69-
* @param State $state
75+
* Format appName.
7076
*
77+
* @param State $state
7178
* @return string
7279
* @throws LocalizedException
7380
*/
74-
private function appName(State $state)
81+
private function appName(State $state): string
7582
{
7683
$code = $state->getAreaCode();
7784
$current = $this->config->getNewRelicAppName();
@@ -80,22 +87,16 @@ private function appName(State $state)
8087
}
8188

8289
/**
90+
* Check if app name should be set.
91+
*
8392
* @return bool
8493
*/
85-
private function shouldSetAppName()
94+
private function shouldSetAppName(): bool
8695
{
87-
if (!$this->config->isNewRelicEnabled()) {
88-
return false;
89-
}
90-
91-
if (!$this->config->getNewRelicAppName()) {
92-
return false;
93-
}
94-
95-
if (!$this->config->isSeparateApps()) {
96-
return false;
97-
}
98-
99-
return true;
96+
return (
97+
$this->config->isSeparateApps() &&
98+
$this->config->getNewRelicAppName() &&
99+
$this->config->isNewRelicEnabled()
100+
);
100101
}
101102
}

dev/tests/integration/testsuite/Magento/NewRelicReporting/Plugin/SeparateAppsTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\NewRelicReporting\Plugin;
79

810
use Magento\Framework\App\State;
911
use Magento\NewRelicReporting\Model\NewRelicWrapper;
1012
use Magento\TestFramework\ObjectManager;
1113
use Magento\TestFramework\Helper\Bootstrap;
1214

15+
/**
16+
* Class SeparateAppsTest
17+
*/
1318
class SeparateAppsTest extends \PHPUnit\Framework\TestCase
1419
{
1520
/**
1621
* @var ObjectManager
1722
*/
1823
private $objectManager;
1924

25+
/**
26+
* @inheritdoc
27+
*/
2028
protected function setUp()
2129
{
2230
$this->objectManager = Bootstrap::getObjectManager();
@@ -44,4 +52,25 @@ public function testAppNameIsSetWhenConfiguredCorrectly()
4452

4553
$state->setAreaCode('90210');
4654
}
55+
56+
/**
57+
* @magentoConfigFixture default/newrelicreporting/general/enable 1
58+
* @magentoConfigFixture default/newrelicreporting/general/app_name beverly_hills
59+
* @magentoConfigFixture default/newrelicreporting/general/separate_apps 0
60+
*/
61+
public function testAppNameIsNotSetWhenDisabled()
62+
{
63+
$newRelicWrapper = $this->getMockBuilder(NewRelicWrapper::class)
64+
->setMethods(['setAppName'])
65+
->getMock();
66+
67+
$this->objectManager->configure([NewRelicWrapper::class => ['shared' => true]]);
68+
$this->objectManager->addSharedInstance($newRelicWrapper, NewRelicWrapper::class);
69+
70+
$newRelicWrapper->expects($this->never())->method('setAppName');
71+
72+
$state = $this->objectManager->get(State::class);
73+
74+
$state->setAreaCode('90210');
75+
}
4776
}

0 commit comments

Comments
 (0)