Skip to content

Commit 4908f90

Browse files
committed
AC-15461:Migration New Relic from REST v2 to NerdGraph (GraphQL)
1 parent 10a4967 commit 4908f90

File tree

5 files changed

+117
-149
lines changed

5 files changed

+117
-149
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function isNerdGraphMode()
265265
*
266266
* @return string
267267
*/
268-
public function getNerdGraphUrl()
268+
public function getNerdGraphUrl(): string
269269
{
270270
return (string)$this->scopeConfig->getValue(self::XML_PATH_NERD_GRAPH_API_URL);
271271
}

app/code/Magento/NewRelicReporting/Test/Unit/Model/Config/Source/ApiModeTest.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ public function testToOptionArrayReturnsCorrectStructure(): void
4646

4747
// Assert it's an array
4848
$this->assertIsArray($result);
49-
49+
5050
// Assert it has exactly 2 options
5151
$this->assertCount(2, $result);
52-
52+
5353
// Assert structure of first option (v2 REST)
5454
$this->assertArrayHasKey('value', $result[0]);
5555
$this->assertArrayHasKey('label', $result[0]);
5656
$this->assertEquals(ApiMode::MODE_V2_REST, $result[0]['value']);
5757
$this->assertEquals('v2 REST (Legacy)', $result[0]['label']->render());
58-
58+
5959
// Assert structure of second option (NerdGraph)
6060
$this->assertArrayHasKey('value', $result[1]);
6161
$this->assertArrayHasKey('label', $result[1]);
@@ -69,9 +69,9 @@ public function testToOptionArrayReturnsCorrectStructure(): void
6969
public function testToOptionArrayUsesConstants(): void
7070
{
7171
$result = $this->apiMode->toOptionArray();
72-
72+
7373
$values = array_column($result, 'value');
74-
74+
7575
$this->assertContains(ApiMode::MODE_V2_REST, $values);
7676
$this->assertContains(ApiMode::MODE_NERDGRAPH, $values);
7777
}
@@ -85,14 +85,14 @@ public function testToArrayReturnsCorrectStructure(): void
8585

8686
// Assert it's an array
8787
$this->assertIsArray($result);
88-
88+
8989
// Assert it has exactly 2 options
9090
$this->assertCount(2, $result);
91-
91+
9292
// Assert keys match constants
9393
$this->assertArrayHasKey(ApiMode::MODE_V2_REST, $result);
9494
$this->assertArrayHasKey(ApiMode::MODE_NERDGRAPH, $result);
95-
95+
9696
// Assert values are correct
9797
$this->assertEquals('v2 REST (Legacy)', $result[ApiMode::MODE_V2_REST]->render());
9898
$this->assertEquals('NerdGraph (GraphQL) - Recommended', $result[ApiMode::MODE_NERDGRAPH]->render());
@@ -114,7 +114,7 @@ public function testBothMethodsReturnSameOptionCount(): void
114114
{
115115
$optionArray = $this->apiMode->toOptionArray();
116116
$array = $this->apiMode->toArray();
117-
117+
118118
$this->assertCount(count($optionArray), $array);
119119
}
120120

@@ -125,19 +125,19 @@ public function testAllConstantsAreUsedInBothMethods(): void
125125
{
126126
$optionArray = $this->apiMode->toOptionArray();
127127
$array = $this->apiMode->toArray();
128-
128+
129129
// Get values from toOptionArray
130130
$optionValues = array_column($optionArray, 'value');
131-
132-
// Get keys from toArray
131+
132+
// Get keys from toArray
133133
$arrayKeys = array_keys($array);
134-
134+
135135
// Both should contain the same constants
136136
$this->assertEqualsCanonicalizing($optionValues, $arrayKeys);
137-
137+
138138
// Verify specific constants are present
139139
$expectedConstants = [ApiMode::MODE_V2_REST, ApiMode::MODE_NERDGRAPH];
140-
140+
141141
foreach ($expectedConstants as $constant) {
142142
$this->assertContains($constant, $optionValues);
143143
$this->assertArrayHasKey($constant, $array);
@@ -151,19 +151,19 @@ public function testLabelsConsistencyBetweenMethods(): void
151151
{
152152
$optionArray = $this->apiMode->toOptionArray();
153153
$array = $this->apiMode->toArray();
154-
154+
155155
// Create mapping from toOptionArray
156156
$optionLabels = [];
157157
foreach ($optionArray as $option) {
158158
$optionLabels[$option['value']] = $option['label']->render();
159159
}
160-
160+
161161
// Create mapping from toArray
162162
$arrayLabels = [];
163163
foreach ($array as $key => $label) {
164164
$arrayLabels[$key] = $label->render();
165165
}
166-
166+
167167
// Labels should be identical for same keys
168168
$this->assertEquals($optionLabels, $arrayLabels);
169169
}
@@ -175,16 +175,16 @@ public function testMethodReturnTypes(): void
175175
{
176176
$optionArray = $this->apiMode->toOptionArray();
177177
$array = $this->apiMode->toArray();
178-
178+
179179
// Both should return arrays
180180
$this->assertIsArray($optionArray);
181181
$this->assertIsArray($array);
182-
182+
183183
// toOptionArray should return array of arrays
184184
foreach ($optionArray as $option) {
185185
$this->assertIsArray($option);
186186
}
187-
187+
188188
// toArray should have string keys
189189
foreach (array_keys($array) as $key) {
190190
$this->assertIsString($key);
@@ -198,12 +198,12 @@ public function testMethodsAreImmutable(): void
198198
{
199199
$firstCall = $this->apiMode->toOptionArray();
200200
$secondCall = $this->apiMode->toOptionArray();
201-
201+
202202
$this->assertEquals($firstCall, $secondCall);
203-
203+
204204
$firstArrayCall = $this->apiMode->toArray();
205205
$secondArrayCall = $this->apiMode->toArray();
206-
206+
207207
$this->assertEquals($firstArrayCall, $secondArrayCall);
208208
}
209209

@@ -213,26 +213,26 @@ public function testMethodsAreImmutable(): void
213213
public function testSpecificLabelContent(): void
214214
{
215215
$optionArray = $this->apiMode->toOptionArray();
216-
216+
217217
// Find v2 REST option
218218
$v2RestOption = null;
219219
$nerdGraphOption = null;
220-
220+
221221
foreach ($optionArray as $option) {
222222
if ($option['value'] === ApiMode::MODE_V2_REST) {
223223
$v2RestOption = $option;
224224
} elseif ($option['value'] === ApiMode::MODE_NERDGRAPH) {
225225
$nerdGraphOption = $option;
226226
}
227227
}
228-
228+
229229
$this->assertNotNull($v2RestOption);
230230
$this->assertNotNull($nerdGraphOption);
231-
231+
232232
// Test specific label content
233233
$this->assertStringContainsString('Legacy', $v2RestOption['label']->render());
234234
$this->assertStringContainsString('v2 REST', $v2RestOption['label']->render());
235-
235+
236236
$this->assertStringContainsString('Recommended', $nerdGraphOption['label']->render());
237237
$this->assertStringContainsString('NerdGraph', $nerdGraphOption['label']->render());
238238
$this->assertStringContainsString('GraphQL', $nerdGraphOption['label']->render());

app/code/Magento/NewRelicReporting/Test/Unit/Model/ConfigTest.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
use Magento\NewRelicReporting\Model\Config;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
15+
use ReflectionException;
1516

1617
/**
1718
* Test for Config model
19+
*
20+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
1821
*/
1922
class ConfigTest extends TestCase
2023
{
@@ -695,13 +698,13 @@ public function testDisableModule()
695698

696699
/**
697700
* Test disableModule with custom scope and scope ID
701+
* @throws ReflectionException
698702
*/
699703
public function testDisableModuleCustomScope()
700704
{
701705
// We need to use reflection to test the protected setConfigValue method with custom parameters
702706
$reflection = new \ReflectionClass($this->config);
703707
$setConfigValueMethod = $reflection->getMethod('setConfigValue');
704-
$setConfigValueMethod->setAccessible(true);
705708

706709
$this->resourceConfigMock->expects($this->once())
707710
->method('saveConfig')
@@ -782,7 +785,7 @@ public function testGetNewRelicAppIdNull()
782785

783786
/**
784787
* Test all string casting methods with various input types
785-
*
788+
*
786789
* @dataProvider stringCastingProvider
787790
*/
788791
public function testStringCastingMethods($method, $configPath, $inputValue, $expectedOutput)
@@ -798,7 +801,7 @@ public function testStringCastingMethods($method, $configPath, $inputValue, $exp
798801
/**
799802
* Data provider for string casting methods
800803
*/
801-
public function stringCastingProvider(): array
804+
public static function stringCastingProvider(): array
802805
{
803806
return [
804807
// getNewRelicApiUrl tests
@@ -843,7 +846,7 @@ public function stringCastingProvider(): array
843846

844847
/**
845848
* Test integer casting for getNewRelicAppId with various input types
846-
*
849+
*
847850
* @dataProvider integerCastingProvider
848851
*/
849852
public function testGetNewRelicAppIdCasting($inputValue, $expectedOutput)
@@ -859,7 +862,7 @@ public function testGetNewRelicAppIdCasting($inputValue, $expectedOutput)
859862
/**
860863
* Data provider for integer casting
861864
*/
862-
public function integerCastingProvider(): array
865+
public static function integerCastingProvider(): array
863866
{
864867
return [
865868
[null, 0],
@@ -876,7 +879,7 @@ public function integerCastingProvider(): array
876879

877880
/**
878881
* Test boolean casting for isSeparateApps with various input types
879-
*
882+
*
880883
* @dataProvider booleanCastingProvider
881884
*/
882885
public function testIsSeparateAppsCasting($inputValue, $expectedOutput)
@@ -892,7 +895,7 @@ public function testIsSeparateAppsCasting($inputValue, $expectedOutput)
892895
/**
893896
* Data provider for boolean casting
894897
*/
895-
public function booleanCastingProvider(): array
898+
public static function booleanCastingProvider(): array
896899
{
897900
return [
898901
[null, false],
@@ -917,17 +920,14 @@ public function testConstructorAssignment()
917920
{
918921
// Use reflection to verify private properties are set correctly
919922
$reflection = new \ReflectionClass($this->config);
920-
923+
921924
$scopeConfigProperty = $reflection->getProperty('scopeConfig');
922-
$scopeConfigProperty->setAccessible(true);
923925
$this->assertSame($this->scopeConfigMock, $scopeConfigProperty->getValue($this->config));
924-
926+
925927
$encryptorProperty = $reflection->getProperty('encryptor');
926-
$encryptorProperty->setAccessible(true);
927928
$this->assertSame($this->encryptorMock, $encryptorProperty->getValue($this->config));
928-
929+
929930
$resourceConfigProperty = $reflection->getProperty('resourceConfig');
930-
$resourceConfigProperty->setAccessible(true);
931931
$this->assertSame($this->resourceConfigMock, $resourceConfigProperty->getValue($this->config));
932932
}
933933

@@ -981,8 +981,14 @@ public function testComprehensiveConfigurationScenario()
981981

982982
// Test all methods return expected values
983983
$this->assertTrue($this->config->isNewRelicEnabled());
984-
$this->assertEquals('https://api.newrelic.com/v2/applications/%s/deployments.json', $this->config->getNewRelicApiUrl());
985-
$this->assertEquals('https://insights-collector.newrelic.com/v1/accounts/%s/events', $this->config->getInsightsApiUrl());
984+
$this->assertEquals(
985+
'https://api.newrelic.com/v2/applications/%s/deployments.json',
986+
$this->config->getNewRelicApiUrl()
987+
);
988+
$this->assertEquals(
989+
'https://insights-collector.newrelic.com/v1/accounts/%s/events',
990+
$this->config->getInsightsApiUrl()
991+
);
986992
$this->assertEquals('123456', $this->config->getNewRelicAccountId());
987993
$this->assertEquals(789012, $this->config->getNewRelicAppId());
988994
$this->assertEquals('NRAK-DECRYPTED-API-KEY', $this->config->getNewRelicApiKey());

0 commit comments

Comments
 (0)