Skip to content

Commit f1c890f

Browse files
Indrani SonawaneIndrani Sonawane
authored andcommitted
Merge remote-tracking branch '38759/fix-for-issue-38758' into compr_author
2 parents 86c715f + 60c3032 commit f1c890f

File tree

2 files changed

+220
-7
lines changed

2 files changed

+220
-7
lines changed

app/code/Magento/Developer/Console/Command/DiInfoCommand.php

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77
namespace Magento\Developer\Console\Command;
88

99
use Magento\Developer\Model\Di\Information;
10+
use Magento\Framework\ObjectManagerInterface;
1011
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\Console\Exception\InvalidArgumentException;
1213
use Symfony\Component\Console\Helper\Table;
1314
use Symfony\Component\Console\Input\InputArgument;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Output\OutputInterface;
17+
use Magento\Framework\App\AreaList;
18+
use Magento\Framework\App\Area;
1619

20+
/**
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
1723
class DiInfoCommand extends Command
1824
{
25+
/**
26+
* @var ObjectManagerInterface
27+
*/
28+
private ObjectManagerInterface $objectManager;
29+
1930
/**
2031
* Command name
2132
*/
@@ -26,18 +37,34 @@ class DiInfoCommand extends Command
2637
*/
2738
public const CLASS_NAME = 'class';
2839

40+
/**
41+
* Area name
42+
*/
43+
public const AREA_CODE = 'area';
44+
2945
/**
3046
* @var Information
3147
*/
32-
private $diInformation;
48+
private Information $diInformation;
49+
50+
/**
51+
* @var AreaList
52+
*/
53+
private AreaList $areaList;
3354

3455
/**
3556
* @param Information $diInformation
57+
* @param ObjectManagerInterface $objectManager
58+
* @param AreaList|null $areaList
3659
*/
3760
public function __construct(
38-
Information $diInformation
61+
Information $diInformation,
62+
ObjectManagerInterface $objectManager,
63+
?AreaList $areaList = null
3964
) {
4065
$this->diInformation = $diInformation;
66+
$this->objectManager = $objectManager;
67+
$this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class);
4168
parent::__construct();
4269
}
4370

@@ -49,10 +76,11 @@ public function __construct(
4976
protected function configure()
5077
{
5178
$this->setName(self::COMMAND_NAME)
52-
->setDescription('Provides information on Dependency Injection configuration for the Command.')
53-
->setDefinition([
54-
new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name')
55-
]);
79+
->setDescription('Provides information on Dependency Injection configuration for the Command.')
80+
->setDefinition([
81+
new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name'),
82+
new InputArgument(self::AREA_CODE, InputArgument::OPTIONAL, 'Area Code')
83+
]);
5684

5785
parent::configure();
5886
}
@@ -154,10 +182,14 @@ private function printPlugins($className, $output, $label)
154182
*/
155183
protected function execute(InputInterface $input, OutputInterface $output)
156184
{
185+
$area = $input->getArgument(self::AREA_CODE) ?? Area::AREA_GLOBAL;
186+
if ($area !== Area::AREA_GLOBAL) {
187+
$this->setDiArea($area);
188+
}
157189
$className = $input->getArgument(self::CLASS_NAME);
158190
$output->setDecorated(true);
159191
$output->writeln('');
160-
$output->writeln(sprintf('DI configuration for the class %s in the GLOBAL area', $className));
192+
$output->writeln(sprintf('DI configuration for the class %s in the %s area', $className, strtoupper($area)));
161193

162194
if ($this->diInformation->isVirtualType($className)) {
163195
$output->writeln(
@@ -173,4 +205,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
173205

174206
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
175207
}
208+
209+
/**
210+
* Set Area for DI Configuration
211+
*
212+
* @param string $area
213+
* @return void
214+
* @throws \InvalidArgumentException
215+
*/
216+
private function setDiArea(string $area): void
217+
{
218+
if ($this->validateAreaCodeFromInput($area)) {
219+
$areaOmConfiguration = $this->objectManager
220+
->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class)
221+
->load($area);
222+
223+
$this->objectManager->configure($areaOmConfiguration);
224+
225+
$this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class)
226+
->setCurrentScope($area);
227+
} else {
228+
throw new InvalidArgumentException(sprintf('The "%s" area code does not exist', $area));
229+
}
230+
}
231+
232+
/**
233+
* Validate Input
234+
*
235+
* @param string $area
236+
* @return bool
237+
*/
238+
private function validateAreaCodeFromInput($area): bool
239+
{
240+
$availableAreaCodes = $this->areaList->getCodes();
241+
return in_array($area, $availableAreaCodes, true);
242+
}
176243
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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\Developer\Console\Command;
9+
10+
use Magento\Developer\Model\Di\Information;
11+
use Magento\Framework\App\AreaList;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Tester\CommandTester;
16+
use Magento\Framework\ObjectManagerInterface;
17+
18+
class DiInfoCommandTest extends TestCase
19+
{
20+
/**
21+
* @var ObjectManagerInterface
22+
*/
23+
private ObjectManagerInterface $objectManager;
24+
25+
/**
26+
* @var Information|MockObject
27+
*/
28+
private Information|MockObject $informationMock;
29+
30+
/**
31+
* @var AreaList|MockObject
32+
*/
33+
private AreaList|MockObject $areaListMock;
34+
35+
/**
36+
* @var DiInfoCommand
37+
*/
38+
private DiInfoCommand $command;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->informationMock = $this->createMock(Information::class);
47+
$this->areaListMock = $this->createMock(AreaList::class);
48+
$this->command = new DiInfoCommand($this->informationMock, $this->objectManager, $this->areaListMock);
49+
}
50+
51+
/**
52+
* @return void
53+
*/
54+
public function testExecuteWithGlobalArea(): void
55+
{
56+
$this->informationMock->expects($this->any())
57+
->method('getPreference')
58+
->with('Magento\Framework\App\RouterList')
59+
->willReturn('Magento\Framework\App\RouterList');
60+
61+
$this->informationMock->expects($this->any())
62+
->method('getParameters')
63+
->with('Magento\Framework\App\RouterList')
64+
->willReturn([
65+
['objectManager', 'Magento\Framework\ObjectManagerInterface', null],
66+
['routerList', null, null]
67+
]);
68+
69+
$this->informationMock->expects($this->once())
70+
->method('getVirtualTypes')
71+
->with('Magento\Framework\App\RouterList')
72+
->willReturn([]);
73+
74+
$this->informationMock->expects($this->any())
75+
->method('getPlugins')
76+
->with('Magento\Framework\App\RouterList')
77+
->willReturn([
78+
'before' => [],
79+
'around' => [],
80+
'after' => []
81+
]);
82+
83+
$commandTester = new CommandTester($this->command);
84+
$commandTester->execute(
85+
[
86+
DiInfoCommand::CLASS_NAME => "Magento\Framework\App\RouterList",
87+
DiInfoCommand::AREA_CODE => null
88+
],
89+
);
90+
$this->assertStringContainsString(
91+
'DI configuration for the class Magento\Framework\App\RouterList in the GLOBAL area',
92+
$commandTester->getDisplay()
93+
);
94+
}
95+
96+
/**
97+
* @return void
98+
*/
99+
public function testExecuteWithAreaCode(): void
100+
{
101+
$className = "Magento\Framework\App\RouterList";
102+
$this->informationMock->expects($this->any())
103+
->method('getPreference')
104+
->with($className)
105+
->willReturn($className);
106+
107+
$this->informationMock->expects($this->any())
108+
->method('getParameters')
109+
->with($className)
110+
->willReturn([
111+
['objectManager', 'Magento\Framework\ObjectManagerInterface', null],
112+
['routerList', null, null]
113+
]);
114+
115+
$this->informationMock->expects($this->once())
116+
->method('getVirtualTypes')
117+
->with($className)
118+
->willReturn([]);
119+
120+
$this->informationMock->expects($this->any())
121+
->method('getPlugins')
122+
->with($className)
123+
->willReturn([
124+
'before' => [],
125+
'around' => [],
126+
'after' => []
127+
]);
128+
129+
$this->areaListMock->expects($this->once())
130+
->method('getCodes')
131+
->willReturn(['frontend', 'adminhtml']);
132+
133+
$commandTester = new CommandTester($this->command);
134+
$commandTester->execute(
135+
[
136+
DiInfoCommand::CLASS_NAME => "$className",
137+
DiInfoCommand::AREA_CODE => "adminhtml"
138+
],
139+
);
140+
141+
$this->assertStringContainsString(
142+
"DI configuration for the class $className in the ADMINHTML area",
143+
$commandTester->getDisplay()
144+
);
145+
}
146+
}

0 commit comments

Comments
 (0)