From 866c58d301999d8968f9d277f24aeb260a8ebcb4 Mon Sep 17 00:00:00 2001 From: Viktor Buzyka Date: Wed, 21 Jun 2023 23:19:33 +0200 Subject: [PATCH] Fixed shopware dependencies removing order, for command plugin:zip:vcs for Shopware6. (This bug led to crushing command for plugins which has not only shopware dependency) --- .../Plugin/Services/DependencyManager.php | 18 ++++++- .../Plugin/Services/DependencyManagerTest.php | 49 +++++++++++++++++++ .../composer.json | 16 ++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Extensions/Shopware/Plugin/Services/DependencyManagerTest.php create mode 100644 tests/Unit/_fixtures/DependencyManager/RemoveShopwareDependencyOrder/composer.json diff --git a/src/Extensions/Shopware/Plugin/Services/DependencyManager.php b/src/Extensions/Shopware/Plugin/Services/DependencyManager.php index 663ed09b..21cc4379 100644 --- a/src/Extensions/Shopware/Plugin/Services/DependencyManager.php +++ b/src/Extensions/Shopware/Plugin/Services/DependencyManager.php @@ -71,7 +71,7 @@ private function manageShopware6Dependencies(string $pathToPlugin): void } // Temporary remove shopware dependencies - foreach (\array_keys($shopwareDependencies) as $dependencyName) { + foreach ($this->getTheRightRemovingOrder(\array_keys($shopwareDependencies)) as $dependencyName) { $this->processExecutor->execute(\sprintf('composer remove %s --update-no-dev', $dependencyName), $pathToPlugin); } @@ -82,4 +82,20 @@ private function manageShopware6Dependencies(string $pathToPlugin): void $this->processExecutor->execute(\sprintf('composer require %s:"%s" --no-update', $dependencyName, $version), $pathToPlugin); } } + + private function getTheRightRemovingOrder(array $dependencies): array + { + usort($dependencies, function (string $a, string $b): int { + switch ('shopware/core') { + case $a: + return 1; + case $b: + return -1; + default: + return 0; + } + }); + + return $dependencies; + } } diff --git a/tests/Unit/Extensions/Shopware/Plugin/Services/DependencyManagerTest.php b/tests/Unit/Extensions/Shopware/Plugin/Services/DependencyManagerTest.php new file mode 100644 index 00000000..62848d8b --- /dev/null +++ b/tests/Unit/Extensions/Shopware/Plugin/Services/DependencyManagerTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Shopware\Plugin\Struct\Plugin; +use ShopwareCli\Extensions\Shopware\Plugin\Services\DependencyManager; +use ShopwareCli\Services\ProcessExecutor; + +class DependencyManagerTest extends TestCase +{ + /** + * @var ProcessExecutor&MockObject + */ + private $processExecutor; + + private $dependencyManager; + + public function setUp(): void + { + $this->processExecutor = $this->createMock(ProcessExecutor::class); + $this->dependencyManager = new DependencyManager($this->processExecutor); + } + + public function testManageDependenciesWithExternalDependenciesWillRemuveShopwareDependenciesInRightOrder(): void + { + $plugin = new Plugin(); + $plugin->isShopware6 = true; + $callNumber = 0; + + $this->processExecutor + ->method('execute') + ->with(static::callback(function ($command) use (&$callNumber) { + if ($callNumber == 2) { + self::assertEquals('composer remove shopware/core --update-no-dev', $command, 'Removing shopware/core should be the last command in removing the packages commands list'); + } + ++$callNumber; + + return true; + })); + $this->dependencyManager->manageDependencies($plugin, __DIR__ . '/../../../../_fixtures/DependencyManager/RemoveShopwareDependencyOrder'); + } +} diff --git a/tests/Unit/_fixtures/DependencyManager/RemoveShopwareDependencyOrder/composer.json b/tests/Unit/_fixtures/DependencyManager/RemoveShopwareDependencyOrder/composer.json new file mode 100644 index 00000000..9d0d693f --- /dev/null +++ b/tests/Unit/_fixtures/DependencyManager/RemoveShopwareDependencyOrder/composer.json @@ -0,0 +1,16 @@ +{ + "name": "shopware/test", + "description": "The Shopware Test", + "type": "shopware-platform-plugin", + "license": "proprietary", + "minimum-stability": "dev", + "prefer-stable": true, + "require": { + "php": "^8.1", + "shopware/core": "~6.5.0", + "shopware/storefront": "~6.5.0", + "shopware/administration": "~6.5.0", + "nikic/fast-route": "^1.0", + "phpoffice/phpspreadsheet": "^1.14" + } +}