Skip to content

Commit d15db21

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

23 files changed

+2002
-13
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Test\Unit\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Model\AbstractModel;
12+
use Magento\Framework\Model\Context;
13+
use Magento\Framework\Registry;
14+
use Magento\NewRelicReporting\Model\Counts;
15+
use Magento\NewRelicReporting\Model\ResourceModel\Counts as CountsResource;
16+
use PHPUnit\Framework\MockObject\Exception;
17+
use PHPUnit\Framework\TestCase;
18+
use ReflectionClass;
19+
20+
/**
21+
* Test for Counts model
22+
*
23+
* @covers \Magento\NewRelicReporting\Model\Counts
24+
*/
25+
class CountsTest extends TestCase
26+
{
27+
/**
28+
* Create Counts instance with minimal required dependencies
29+
*
30+
* @return Counts
31+
* @throws LocalizedException|Exception
32+
*/
33+
private function createCounts(): Counts
34+
{
35+
$context = $this->createMock(Context::class);
36+
$registry = $this->createMock(Registry::class);
37+
$resource = $this->createMock(CountsResource::class);
38+
39+
return new Counts($context, $registry, $resource);
40+
}
41+
42+
/**
43+
* @return void
44+
* @throws Exception|LocalizedException
45+
*/
46+
public function testItExtendsAbstractModel()
47+
{
48+
$counts = $this->createCounts();
49+
$this->assertInstanceOf(AbstractModel::class, $counts);
50+
}
51+
52+
/**
53+
* Test that Counts initializes the correct resource model
54+
* @return void
55+
* @throws Exception|LocalizedException
56+
*/
57+
public function testItInitializesResourceModel()
58+
{
59+
$counts = $this->createCounts();
60+
61+
$reflection = new ReflectionClass($counts);
62+
$resourceNameProperty = $reflection->getProperty('_resourceName');
63+
$resourceNameProperty->setAccessible(true);
64+
$this->assertEquals(
65+
CountsResource::class,
66+
$resourceNameProperty->getValue($counts)
67+
);
68+
}
69+
}

app/code/Magento/NewRelicReporting/Test/Unit/Model/Cron/ReportNewRelicCronTest.php

Lines changed: 247 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2018 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -17,6 +17,8 @@
1717
use Magento\NewRelicReporting\Model\Module\Collect;
1818
use PHPUnit\Framework\MockObject\MockObject;
1919
use PHPUnit\Framework\TestCase;
20+
use ReflectionClass;
21+
use ReflectionException;
2022

2123
class ReportNewRelicCronTest extends TestCase
2224
{
@@ -26,37 +28,37 @@ class ReportNewRelicCronTest extends TestCase
2628
protected $model;
2729

2830
/**
29-
* @var Config|MockObject
31+
* @var MockObject&Config
3032
*/
3133
protected $config;
3234

3335
/**
34-
* @var Collect|MockObject
36+
* @var MockObject&Collect
3537
*/
3638
protected $collect;
3739

3840
/**
39-
* @var Counter|MockObject
41+
* @var MockObject&Counter
4042
*/
4143
protected $counter;
4244

4345
/**
44-
* @var CronEventFactory|MockObject
46+
* @var MockObject&CronEventFactory
4547
*/
4648
protected $cronEventFactory;
4749

4850
/**
49-
* @var CronEvent|MockObject
51+
* @var MockObject&CronEvent
5052
*/
5153
protected $cronEventModel;
5254

5355
/**
54-
* @var DeploymentsFactory|MockObject
56+
* @var MockObject&DeploymentsFactory
5557
*/
5658
protected $deploymentsFactory;
5759

5860
/**
59-
* @var Deployments|MockObject
61+
* @var MockObject&Deployments
6062
*/
6163
protected $deploymentsModel;
6264

@@ -213,4 +215,240 @@ public function testReportNewRelicCronRequestFailed()
213215

214216
$this->model->report();
215217
}
218+
219+
/**
220+
* Test reportModules method with module changes
221+
*
222+
* @return void
223+
* @throws ReflectionException
224+
*/
225+
public function testReportModulesWithChanges()
226+
{
227+
$moduleData = [
228+
'changes' => [
229+
[
230+
'type' => Config::ENABLED,
231+
'name' => 'Test_Module1',
232+
'setup_version' => '1.0.0'
233+
],
234+
[
235+
'type' => Config::DISABLED,
236+
'name' => 'Test_Module2',
237+
'setup_version' => '1.1.0'
238+
],
239+
[
240+
'type' => Config::INSTALLED,
241+
'name' => 'Test_Module3',
242+
'setup_version' => '2.0.0'
243+
],
244+
[
245+
'type' => Config::UNINSTALLED,
246+
'name' => 'Test_Module4',
247+
'setup_version' => '1.5.0'
248+
]
249+
],
250+
Config::ENABLED => 10,
251+
Config::DISABLED => 5,
252+
Config::INSTALLED => 15
253+
];
254+
255+
$this->collect->expects($this->once())
256+
->method('getModuleData')
257+
->with(false)
258+
->willReturn($moduleData);
259+
260+
// Expect deployment calls for each change type
261+
$this->deploymentsModel->expects($this->exactly(4))
262+
->method('setDeployment');
263+
264+
// Use reflection to call protected method
265+
$reflection = new ReflectionClass($this->model);
266+
$method = $reflection->getMethod('reportModules');
267+
$method->setAccessible(true);
268+
$method->invoke($this->model);
269+
270+
// Verify custom parameters were added
271+
$property = $reflection->getProperty('customParameters');
272+
$property->setAccessible(true);
273+
$customParams = $property->getValue($this->model);
274+
275+
$this->assertEquals(10, $customParams[Config::MODULES_ENABLED]);
276+
$this->assertEquals(5, $customParams[Config::MODULES_DISABLED]);
277+
$this->assertEquals(15, $customParams[Config::MODULES_INSTALLED]);
278+
}
279+
280+
/**
281+
* Test reportModules method with no changes
282+
*
283+
* @return void
284+
* @throws ReflectionException
285+
*/
286+
public function testReportModulesWithNoChanges()
287+
{
288+
$moduleData = [
289+
'changes' => [],
290+
Config::ENABLED => 8,
291+
Config::DISABLED => 3,
292+
Config::INSTALLED => 11
293+
];
294+
295+
$this->collect->expects($this->once())
296+
->method('getModuleData')
297+
->with(false)
298+
->willReturn($moduleData);
299+
300+
// Should not call setDeployment when no changes
301+
$this->deploymentsModel->expects($this->never())
302+
->method('setDeployment');
303+
304+
// Use reflection to call protected method
305+
$reflection = new ReflectionClass($this->model);
306+
$method = $reflection->getMethod('reportModules');
307+
$method->setAccessible(true);
308+
$method->invoke($this->model);
309+
310+
// Verify custom parameters were still added
311+
$property = $reflection->getProperty('customParameters');
312+
$property->setAccessible(true);
313+
$customParams = $property->getValue($this->model);
314+
315+
$this->assertEquals(8, $customParams[Config::MODULES_ENABLED]);
316+
$this->assertEquals(3, $customParams[Config::MODULES_DISABLED]);
317+
$this->assertEquals(11, $customParams[Config::MODULES_INSTALLED]);
318+
}
319+
320+
/**
321+
* Test setModuleChangeStatusDeployment with changes
322+
*
323+
* @return void
324+
* @throws ReflectionException
325+
*/
326+
public function testSetModuleChangeStatusDeploymentWithChanges()
327+
{
328+
$changesArray = ['Module1-1.0.0', 'Module2-2.0.0'];
329+
$deploymentText = 'Test Deployment';
330+
331+
$this->deploymentsModel->expects($this->exactly(2))
332+
->method('setDeployment');
333+
334+
// Use reflection to call protected method
335+
$reflection = new ReflectionClass($this->model);
336+
$method = $reflection->getMethod('setModuleChangeStatusDeployment');
337+
$method->setAccessible(true);
338+
$method->invoke($this->model, $changesArray, $deploymentText);
339+
}
340+
341+
/**
342+
* Test setModuleChangeStatusDeployment with empty changes
343+
*
344+
* @return void
345+
* @throws ReflectionException
346+
*/
347+
public function testSetModuleChangeStatusDeploymentWithEmptyChanges()
348+
{
349+
$changesArray = [];
350+
$deploymentText = 'Test Deployment';
351+
352+
// Should not call setDeployment when array is empty
353+
$this->deploymentsModel->expects($this->never())
354+
->method('setDeployment');
355+
356+
// Use reflection to call protected method
357+
$reflection = new ReflectionClass($this->model);
358+
$method = $reflection->getMethod('setModuleChangeStatusDeployment');
359+
$method->setAccessible(true);
360+
$method->invoke($this->model, $changesArray, $deploymentText);
361+
}
362+
363+
/**
364+
* Data provider for module change types
365+
*
366+
* @return array
367+
*/
368+
public static function moduleChangeTypesDataProvider(): array
369+
{
370+
return [
371+
'enabled_modules' => [Config::ENABLED, 'Test_Module_Enabled', '1.0.0'],
372+
'disabled_modules' => [Config::DISABLED, 'Test_Module_Disabled', '1.1.0'],
373+
'installed_modules' => [Config::INSTALLED, 'Test_Module_Installed', '2.0.0'],
374+
'uninstalled_modules' => [Config::UNINSTALLED, 'Test_Module_Uninstalled', '1.5.0']
375+
];
376+
}
377+
378+
/**
379+
* Test reportModules handles different module change types correctly
380+
*
381+
* @param string $changeType
382+
* @param string $moduleName
383+
* @param string $version
384+
* @return void
385+
* @dataProvider moduleChangeTypesDataProvider
386+
* @throws ReflectionException
387+
*/
388+
public function testReportModulesHandlesChangeTypes(string $changeType, string $moduleName, string $version)
389+
{
390+
$moduleData = [
391+
'changes' => [
392+
[
393+
'type' => $changeType,
394+
'name' => $moduleName,
395+
'setup_version' => $version
396+
]
397+
],
398+
Config::ENABLED => 1,
399+
Config::DISABLED => 1,
400+
Config::INSTALLED => 1
401+
];
402+
403+
$this->collect->expects($this->once())
404+
->method('getModuleData')
405+
->with(false)
406+
->willReturn($moduleData);
407+
408+
$this->deploymentsModel->expects($this->once())
409+
->method('setDeployment');
410+
411+
// Use reflection to call protected method
412+
$reflection = new ReflectionClass($this->model);
413+
$method = $reflection->getMethod('reportModules');
414+
$method->setAccessible(true);
415+
$method->invoke($this->model);
416+
}
417+
418+
/**
419+
* Test reportModules switch statement coverage for all cases
420+
*
421+
* @return void
422+
* @throws ReflectionException
423+
*/
424+
public function testReportModulesAllSwitchCases()
425+
{
426+
$moduleData = [
427+
'changes' => [
428+
['type' => Config::ENABLED, 'name' => 'EnabledModule', 'setup_version' => '1.0.0'],
429+
['type' => Config::DISABLED, 'name' => 'DisabledModule', 'setup_version' => '1.0.0'],
430+
['type' => Config::INSTALLED, 'name' => 'InstalledModule', 'setup_version' => '1.0.0'],
431+
['type' => Config::UNINSTALLED, 'name' => 'UninstalledModule', 'setup_version' => '1.0.0'],
432+
['type' => 'unknown_type', 'name' => 'UnknownModule', 'setup_version' => '1.0.0'] // Test default case
433+
],
434+
Config::ENABLED => 1,
435+
Config::DISABLED => 1,
436+
Config::INSTALLED => 1
437+
];
438+
439+
$this->collect->expects($this->once())
440+
->method('getModuleData')
441+
->with(false)
442+
->willReturn($moduleData);
443+
444+
// Expect 4 deployment calls (unknown_type should not trigger deployment)
445+
$this->deploymentsModel->expects($this->exactly(4))
446+
->method('setDeployment');
447+
448+
// Use reflection to call protected method
449+
$reflection = new ReflectionClass($this->model);
450+
$method = $reflection->getMethod('reportModules');
451+
$method->setAccessible(true);
452+
$method->invoke($this->model);
453+
}
216454
}

0 commit comments

Comments
 (0)