-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[Performance] Implement command loader to reduce initialization time of Magento CLI #29355
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
Merged
magento-devops-reposync-svc
merged 7 commits into
magento:2.4-develop
from
mattwellss:implement-command-loader
Dec 4, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f162173
Implement command loader
mattwellss 764a2c6
Implement command loader
ihor-sviziev 428a5b2
Merge branch '2.4-develop' into implement-command-loader
ihor-sviziev f6152ed
Implement command loader
ihor-sviziev fa9a597
Merge branch '2.4-develop' into implement-command-loader
engcom-Echo 5735e04
Merge branch '2.4-develop' into implement-command-loader
engcom-Charlie 7d4d669
29355: Fix static failure related to copyright tag
engcom-Dash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
lib/internal/Magento/Framework/App/Test/Unit/Console/CommandLoader/AggregateTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* Copyright 2021 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\App\Test\Unit\Console\CommandLoader; | ||
|
||
use Magento\Framework\Console\CommandLoader\Aggregate; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; | ||
use Symfony\Component\Console\Exception\CommandNotFoundException; | ||
|
||
/** | ||
* Tests the "aggregate" command loader | ||
* @see Aggregate | ||
*/ | ||
class AggregateTest extends TestCase | ||
{ | ||
/** @var CommandLoaderInterface|MockObject */ | ||
private MockObject|CommandLoaderInterface $firstMockCommandLoader; | ||
|
||
/** @var CommandLoaderInterface|MockObject */ | ||
private MockObject|CommandLoaderInterface $secondMockCommandLoader; | ||
|
||
/** @var Aggregate */ | ||
private Aggregate $aggregateCommandLoader; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->firstMockCommandLoader = $this->getMockBuilder(CommandLoaderInterface::class)->getMock(); | ||
$this->secondMockCommandLoader = $this->getMockBuilder(CommandLoaderInterface::class)->getMock(); | ||
$this->aggregateCommandLoader = new Aggregate([$this->firstMockCommandLoader, $this->secondMockCommandLoader]); | ||
} | ||
|
||
/** | ||
* Test the various cases of `has` for the aggregate command loader: | ||
* - When at least one "internal" command loader has a command, the aggregate does as well | ||
* - When none of the "internal" command loaders has a command, neither does the aggregate | ||
* | ||
* @dataProvider provideTestCasesForHas | ||
*/ | ||
public function testHas(bool $firstResult, bool $secondResult, bool $overallResult): void | ||
{ | ||
$this->firstMockCommandLoader->method('has')->with('foo')->willReturn($firstResult); | ||
$this->secondMockCommandLoader->method('has')->with('foo')->willReturn($secondResult); | ||
|
||
$this->assertEquals($overallResult, $this->aggregateCommandLoader->has('foo')); | ||
} | ||
|
||
public function provideTestCasesForHas(): array | ||
{ | ||
return [ | ||
[true, false, true], | ||
[false, true, true], | ||
[false, false, false] | ||
]; | ||
} | ||
|
||
/** | ||
* Test the various cases of `get` for the aggregate command loader. Similar to `has`, | ||
* the return value of `Aggregate::get` mirrors its internal command loaders. | ||
* | ||
* For simplicity, this test does not cover the "no results" case. @see testGetThrow | ||
* | ||
* @dataProvider provideTestCasesForGet | ||
*/ | ||
public function testGet(?Command $firstCmd, ?Command $secondCmd): void | ||
{ | ||
$firstHas = (bool)$firstCmd; | ||
$secondHas = (bool)$secondCmd; | ||
|
||
$this->firstMockCommandLoader->method('has')->with('foo')->willReturn($firstHas); | ||
if ($firstHas) { | ||
$this->firstMockCommandLoader->method('get')->with('foo')->willReturn($firstCmd); | ||
} | ||
|
||
$this->secondMockCommandLoader->method('has')->with('foo')->willReturn($secondHas); | ||
if ($secondHas) { | ||
$this->secondMockCommandLoader->method('get')->with('foo')->willReturn($secondCmd); | ||
} | ||
|
||
$this->assertInstanceOf(Command::class, $this->aggregateCommandLoader->get('foo')); | ||
} | ||
|
||
public function provideTestCasesForGet(): array | ||
{ | ||
return [ | ||
[ | ||
new Command(), | ||
null | ||
], | ||
[ | ||
null, | ||
new Command() | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* When none of the internal command loaders have matching commands, the aggregate command loader | ||
* will throw an exception. @see CommandNotFoundException | ||
*/ | ||
public function testGetThrow(): void | ||
{ | ||
$this->firstMockCommandLoader->method('has')->with('foo')->willReturn(false); | ||
$this->secondMockCommandLoader->method('has')->with('foo')->willReturn(false); | ||
|
||
$this->expectException(CommandNotFoundException::class); | ||
$this->aggregateCommandLoader->get('foo'); | ||
} | ||
|
||
/** | ||
* An aggregate command loader's `getNames` method returns the merged array of the `getNames` | ||
* return values of all its internal command loaders | ||
*/ | ||
public function testGetNames(): void | ||
{ | ||
$this->firstMockCommandLoader->method('getNames')->willReturn(['foo', 'bar']); | ||
$this->secondMockCommandLoader->method('getNames')->willReturn(['baz', 'qux']); | ||
|
||
$this->assertEquals(['foo', 'bar', 'baz', 'qux'], $this->aggregateCommandLoader->getNames()); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
lib/internal/Magento/Framework/App/Test/Unit/Console/CommandLoaderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* Copyright 2021 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\App\Test\Unit\Console; | ||
|
||
use Magento\Framework\Console\CommandLoader; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\CommandNotFoundException; | ||
|
||
class CommandLoaderTest extends TestCase | ||
{ | ||
/** @var MockObject|ObjectManagerInterface */ | ||
private ObjectManagerInterface|MockObject $objectManagerMock; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)->getMock(); | ||
} | ||
|
||
/** | ||
* Test that the command loader, when provided zero commands, does not have a command named "foo" | ||
*/ | ||
public function testHasWithZeroCommands(): void | ||
{ | ||
$subj = new CommandLoader($this->objectManagerMock, []); | ||
|
||
$this->assertFalse($subj->has('foo')); | ||
} | ||
|
||
/** | ||
* Test that the command loader will return true when provided with a command "foo" | ||
*/ | ||
public function testHasWithAtLeastOneCommand(): void | ||
{ | ||
$subj = new CommandLoader($this->objectManagerMock, [ | ||
[ | ||
'name' => 'foo', | ||
'class' => FooCommand::class | ||
] | ||
]); | ||
|
||
$this->assertTrue($subj->has('foo')); | ||
} | ||
|
||
/** | ||
* Test that the command loader will throw a CommandNotFoundException when it does not have the requested command | ||
*/ | ||
public function testGetWithZeroCommands(): void | ||
{ | ||
$subj = new CommandLoader($this->objectManagerMock, []); | ||
|
||
$this->expectException(CommandNotFoundException::class); | ||
|
||
$subj->get('foo'); | ||
} | ||
|
||
/** | ||
* Test that the command loader returns a command when one it has is requested | ||
*/ | ||
public function testGetWithAtLeastOneCommand(): void | ||
{ | ||
$this->objectManagerMock | ||
->method('create') | ||
->with(FooCommand::class) | ||
->willReturn(new FooCommand()); | ||
|
||
$subj = new CommandLoader($this->objectManagerMock, [ | ||
[ | ||
'name' => 'foo', | ||
'class' => FooCommand::class | ||
] | ||
]); | ||
|
||
$this->assertInstanceOf(FooCommand::class, $subj->get('foo')); | ||
} | ||
|
||
/** | ||
* Test that the command loader will return an empty "names" array when it has none | ||
*/ | ||
public function testGetNamesWithZeroCommands(): void | ||
{ | ||
$subj = new CommandLoader($this->objectManagerMock, []); | ||
|
||
$this->assertEquals([], $subj->getNames()); | ||
} | ||
|
||
/** | ||
* Test that the command loader returns an array of its command names when `getNames` is called | ||
*/ | ||
public function testGetNames(): void | ||
{ | ||
$subj = new CommandLoader($this->objectManagerMock, [ | ||
[ | ||
'name' => 'foo', | ||
'class' => FooCommand::class | ||
], | ||
[ | ||
'name' => 'bar', | ||
'class' => 'BarCommand' | ||
] | ||
]); | ||
|
||
$this->assertEquals(['foo', 'bar'], $subj->getNames()); | ||
} | ||
} | ||
|
||
// phpcs:ignore PSR1.Classes.ClassDeclaration | ||
class FooCommand extends Command | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.