Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to define Area for dev:di:info CLI command #38758 #38759

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
292c9b6
Merge pull request #1 from magento/2.4-develop
rostilos Oct 13, 2023
7fa4832
Merge branch 'magento:2.4-develop' into 2.4-develop
rostilos May 26, 2024
b3804fa
[issue #38758] Area code parameter for dev:di:info CLI command
rostilos May 26, 2024
f969a17
[issue #38758] Area code parameter for dev:di:info CLI command ( fixes )
rostilos May 26, 2024
b8060ac
Merge branch 'magento:2.4-develop' into 2.4-develop
rostilos Jul 12, 2024
2553d94
Merge remote-tracking branch 'refs/remotes/origin/2.4-develop' into f…
Aug 11, 2024
691b517
Merge branch '2.4-develop' into fix-for-issue-38758
engcom-Charlie Aug 13, 2024
07a2784
Merge branch '2.4-develop' into fix-for-issue-38758
engcom-Hotel Aug 19, 2024
c90d2dd
issue-38758 added new constructor param as null and handle with Objec…
Aug 30, 2024
d72c7ae
issue-38758 code refactoring ( dev:di:info )
Aug 30, 2024
ed345a1
issue-38703 codestyle issues fix
Sep 4, 2024
6447c54
issue-38703 codestyle ( prettify )
Sep 4, 2024
0a20ef5
issue-38758 added test coverage ( integration tests )
Sep 4, 2024
2735499
Merge branch '2.4-develop' into fix-for-issue-38758
engcom-Hotel Sep 9, 2024
5b29414
issue-38758 codestyle fixes ( static tests )
Sep 11, 2024
e81b4de
Merge remote-tracking branch 'origin/fix-for-issue-38758' into fix-fo…
Sep 11, 2024
671583d
issue-38758 codestyle fixes ( strict_types and copyright )
Sep 11, 2024
21e3f48
Merge branch '2.4-develop' into fix-for-issue-38758
engcom-Bravo Sep 12, 2024
60c3032
Merge branch '2.4-develop' into fix-for-issue-38758
engcom-Charlie Sep 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 74 additions & 7 deletions app/code/Magento/Developer/Console/Command/DiInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@
namespace Magento\Developer\Console\Command;

use Magento\Developer\Model\Di\Information;
use Magento\Framework\ObjectManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\App\AreaList;
use Magento\Framework\App\Area;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DiInfoCommand extends Command
{
/**
* @var ObjectManagerInterface
*/
private ObjectManagerInterface $objectManager;

/**
* Command name
*/
Expand All @@ -26,18 +37,34 @@ class DiInfoCommand extends Command
*/
public const CLASS_NAME = 'class';

/**
* Area name
*/
public const AREA_CODE = 'area';

/**
* @var Information
*/
private $diInformation;
private Information $diInformation;

/**
* @var AreaList
*/
private AreaList $areaList;

/**
* @param Information $diInformation
* @param ObjectManagerInterface $objectManager
* @param AreaList|null $areaList
*/
public function __construct(
Information $diInformation
Information $diInformation,
ObjectManagerInterface $objectManager,
?AreaList $areaList = null
) {
$this->diInformation = $diInformation;
$this->objectManager = $objectManager;
$this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class);
parent::__construct();
}

Expand All @@ -49,10 +76,11 @@ public function __construct(
protected function configure()
{
$this->setName(self::COMMAND_NAME)
->setDescription('Provides information on Dependency Injection configuration for the Command.')
->setDefinition([
new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name')
]);
->setDescription('Provides information on Dependency Injection configuration for the Command.')
->setDefinition([
new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name'),
new InputArgument(self::AREA_CODE, InputArgument::OPTIONAL, 'Area Code')
]);

parent::configure();
}
Expand Down Expand Up @@ -154,10 +182,14 @@ private function printPlugins($className, $output, $label)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$area = $input->getArgument(self::AREA_CODE) ?? Area::AREA_GLOBAL;
if ($area !== Area::AREA_GLOBAL) {
$this->setDiArea($area);
}
$className = $input->getArgument(self::CLASS_NAME);
$output->setDecorated(true);
$output->writeln('');
$output->writeln(sprintf('DI configuration for the class %s in the GLOBAL area', $className));
$output->writeln(sprintf('DI configuration for the class %s in the %s area', $className, strtoupper($area)));

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

return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}

/**
* Set Area for DI Configuration
*
* @param string $area
* @return void
* @throws \InvalidArgumentException
*/
private function setDiArea(string $area): void
{
if ($this->validateAreaCodeFromInput($area)) {
$areaOmConfiguration = $this->objectManager
->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class)
->load($area);

$this->objectManager->configure($areaOmConfiguration);

$this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class)
->setCurrentScope($area);
} else {
throw new InvalidArgumentException(sprintf('The "%s" area code does not exist', $area));
}
}

/**
* Validate Input
*
* @param string $area
* @return bool
*/
private function validateAreaCodeFromInput($area): bool
{
$availableAreaCodes = $this->areaList->getCodes();
return in_array($area, $availableAreaCodes, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

engcom-Hotel marked this conversation as resolved.
Show resolved Hide resolved
namespace Magento\Developer\Console\Command;

use Magento\Developer\Model\Di\Information;
use Magento\Framework\App\AreaList;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Magento\Framework\ObjectManagerInterface;

class DiInfoCommandTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private ObjectManagerInterface $objectManager;

/**
* @var Information|MockObject
*/
private Information|MockObject $informationMock;

/**
* @var AreaList|MockObject
*/
private AreaList|MockObject $areaListMock;

/**
* @var DiInfoCommand
*/
private DiInfoCommand $command;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->informationMock = $this->createMock(Information::class);
$this->areaListMock = $this->createMock(AreaList::class);
$this->command = new DiInfoCommand($this->informationMock, $this->objectManager, $this->areaListMock);
}

/**
* @return void
*/
public function testExecuteWithGlobalArea(): void
{
$this->informationMock->expects($this->any())
->method('getPreference')
->with('Magento\Framework\App\RouterList')
->willReturn('Magento\Framework\App\RouterList');

$this->informationMock->expects($this->any())
->method('getParameters')
->with('Magento\Framework\App\RouterList')
->willReturn([
['objectManager', 'Magento\Framework\ObjectManagerInterface', null],
['routerList', null, null]
]);

$this->informationMock->expects($this->once())
->method('getVirtualTypes')
->with('Magento\Framework\App\RouterList')
->willReturn([]);

$this->informationMock->expects($this->any())
->method('getPlugins')
->with('Magento\Framework\App\RouterList')
->willReturn([
'before' => [],
'around' => [],
'after' => []
]);

$commandTester = new CommandTester($this->command);
$commandTester->execute(
[
DiInfoCommand::CLASS_NAME => "Magento\Framework\App\RouterList",
DiInfoCommand::AREA_CODE => null
],
);
$this->assertStringContainsString(
'DI configuration for the class Magento\Framework\App\RouterList in the GLOBAL area',
$commandTester->getDisplay()
);
}

/**
* @return void
*/
public function testExecuteWithAreaCode(): void
{
$className = "Magento\Framework\App\RouterList";
$this->informationMock->expects($this->any())
->method('getPreference')
->with($className)
->willReturn($className);

$this->informationMock->expects($this->any())
->method('getParameters')
->with($className)
->willReturn([
['objectManager', 'Magento\Framework\ObjectManagerInterface', null],
['routerList', null, null]
]);

$this->informationMock->expects($this->once())
->method('getVirtualTypes')
->with($className)
->willReturn([]);

$this->informationMock->expects($this->any())
->method('getPlugins')
->with($className)
->willReturn([
'before' => [],
'around' => [],
'after' => []
]);

$this->areaListMock->expects($this->once())
->method('getCodes')
->willReturn(['frontend', 'adminhtml']);

$commandTester = new CommandTester($this->command);
$commandTester->execute(
[
DiInfoCommand::CLASS_NAME => "$className",
DiInfoCommand::AREA_CODE => "adminhtml"
],
);

$this->assertStringContainsString(
"DI configuration for the class $className in the ADMINHTML area",
$commandTester->getDisplay()
);
}
}