├── .github └── workflows │ └── php.yml ├── .gitignore ├── LICENSE.txt ├── LICENSE_AFL.txt ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── ConsoleArrayInputFactory.php ├── InfoCommand.php ├── MagentoComposerApplication.php └── RequireUpdateDryRunCommand.php └── tests └── Composer ├── ConsoleArrayInputFactoryTest.php ├── InfoCommandTest.php ├── MagentoComposerApplicationTest.php └── RequireUpdateDryRunCommandTest.php /.github/workflows/php.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ develop ] 6 | pull_request: 7 | branches: [ develop ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | php-version: 16 | - "7.4" 17 | - "8.1" 18 | - "8.2" 19 | - "8.3" 20 | - "8.4" 21 | dependencies: 22 | - "lowest" 23 | - "highest" 24 | 25 | name: Tests with PHP ${{ matrix.php-version }} and ${{ matrix.dependencies }} dependencies 26 | 27 | steps: 28 | - uses: actions/checkout@v2 29 | 30 | - name: Setup PHP 31 | uses: shivammathur/setup-php@v2 32 | with: 33 | php-version: ${{ matrix.php-version }} 34 | env: 35 | COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | 37 | - name: Validate composer 38 | run: composer validate 39 | 40 | - name: Composer install 41 | uses: "ramsey/composer-install@v1" 42 | with: 43 | dependency-versions: "${{ matrix.dependencies }}" 44 | composer-options: "${{ matrix.composer-options }}" 45 | 46 | - name: Run unit tests suite 47 | run: vendor/bin/phpunit 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.cache 3 | /.metadata 4 | /.project 5 | /.settings 6 | atlassian* 7 | /.idea 8 | /.gitattributes 9 | /composer.lock 10 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Open Software License ("OSL") v. 3.0 3 | 4 | This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: 5 | 6 | Licensed under the Open Software License version 3.0 7 | 8 | 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: 9 | 10 | 1. to reproduce the Original Work in copies, either alone or as part of a collective work; 11 | 12 | 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; 13 | 14 | 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; 15 | 16 | 4. to perform the Original Work publicly; and 17 | 18 | 5. to display the Original Work publicly. 19 | 20 | 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 21 | 22 | 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 23 | 24 | 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 25 | 26 | 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 27 | 28 | 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 29 | 30 | 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 31 | 32 | 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 33 | 34 | 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 35 | 36 | 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 37 | 38 | 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 39 | 40 | 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 41 | 42 | 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 43 | 44 | 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 45 | 46 | 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 47 | 48 | 16. Modification of This License. This License is Copyright (C) 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. -------------------------------------------------------------------------------- /LICENSE_AFL.txt: -------------------------------------------------------------------------------- 1 | 2 | Academic Free License ("AFL") v. 3.0 3 | 4 | This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: 5 | 6 | Licensed under the Academic Free License version 3.0 7 | 8 | 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: 9 | 10 | 1. to reproduce the Original Work in copies, either alone or as part of a collective work; 11 | 12 | 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; 13 | 14 | 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor's reserved rights and remedies, in this Academic Free License; 15 | 16 | 4. to perform the Original Work publicly; and 17 | 18 | 5. to display the Original Work publicly. 19 | 20 | 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 21 | 22 | 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 23 | 24 | 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 25 | 26 | 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 27 | 28 | 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 29 | 30 | 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 31 | 32 | 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 33 | 34 | 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 35 | 36 | 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 37 | 38 | 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 39 | 40 | 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 41 | 42 | 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 43 | 44 | 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 45 | 46 | 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 47 | 48 | 16. Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Magento composer library helps to instantiate Composer application and run composer commands. 2 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "magento/composer", 3 | "description": "Magento composer library helps to instantiate Composer application and run composer commands.", 4 | "type": "library", 5 | "version": "1.10.1", 6 | "license": [ 7 | "OSL-3.0", 8 | "AFL-3.0" 9 | ], 10 | "require": { 11 | "php": "~7.4.0||~8.1.0||~8.2.0||~8.3.0||~8.4.0", 12 | "composer/composer": "^2.1.13", 13 | "symfony/console": "~4.4.0||~5.4.0||~6.4.0" 14 | }, 15 | "require-dev": { 16 | "phpunit/phpunit": "^9.5.10 || ^10" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Magento\\Composer\\": "src" 21 | } 22 | }, 23 | "archive": { 24 | "exclude": [".github", ".gitignore"] 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | ./tests/ 13 | 14 | 15 | 16 | 17 | ./ 18 | 19 | 20 | ./tests 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/ConsoleArrayInputFactory.php: -------------------------------------------------------------------------------- 1 | magentoComposerApplication = $magentoComposerApplication; 50 | } 51 | 52 | /** 53 | * Runs composer info command 54 | * 55 | * @param string $package 56 | * @param bool $installed 57 | * @return array|bool 58 | */ 59 | public function run($package, $installed = false) 60 | { 61 | $showAllPackages = !$installed; 62 | $commandParameters = [ 63 | 'command' => 'info', 64 | 'package' => $package, 65 | '-i' => $installed, 66 | '--all' => $showAllPackages, 67 | ]; 68 | 69 | try { 70 | $output = $this->magentoComposerApplication->runComposerCommand($commandParameters); 71 | } catch (\RuntimeException $e) { 72 | return false; 73 | } 74 | 75 | $rawLines = explode("\n", str_replace("\r\n", "\n", $output)); 76 | $result = []; 77 | 78 | foreach ($rawLines as $line) { 79 | $chunk = explode(':', $line); 80 | if (count($chunk) === 2) { 81 | $result[trim($chunk[0])] = trim($chunk[1]); 82 | } 83 | } 84 | 85 | $result = $this->extractVersions($result); 86 | 87 | if (!isset($result[self::NAME]) && isset($result[self::CURRENT_VERSION])) { 88 | $result[self::NAME] = $package; 89 | } 90 | 91 | return $result; 92 | } 93 | 94 | /** 95 | * Extracts package versions info 96 | * 97 | * @param array $packageInfo 98 | * @return array 99 | */ 100 | private function extractVersions($packageInfo) 101 | { 102 | $versions = explode(', ', $packageInfo[self::VERSIONS]); 103 | $packageInfo[self::NEW_VERSIONS] = []; 104 | $packageInfo[self::AVAILABLE_VERSIONS] = []; 105 | 106 | if (count($versions) === 1) { 107 | $packageInfo[self::CURRENT_VERSION] = str_replace('* ', '', $packageInfo[self::VERSIONS]); 108 | } else { 109 | $currentVersion = array_values(preg_grep("/^\*.*/", $versions)); 110 | if ($currentVersion) { 111 | $packageInfo[self::CURRENT_VERSION] = str_replace('* ', '', $currentVersion[0]); 112 | } else { 113 | $packageInfo[self::CURRENT_VERSION] = ''; 114 | } 115 | 116 | $packageInfo[self::AVAILABLE_VERSIONS] = array_values(preg_grep("/^\*.*/", $versions, PREG_GREP_INVERT)); 117 | } 118 | 119 | if (count($packageInfo[self::AVAILABLE_VERSIONS]) > 0) { 120 | if ($packageInfo[self::CURRENT_VERSION]) { 121 | foreach ($packageInfo[self::AVAILABLE_VERSIONS] as $version) { 122 | if (version_compare($packageInfo[self::CURRENT_VERSION], $version, '<')) { 123 | $packageInfo[self::NEW_VERSIONS][] = $version; 124 | } 125 | } 126 | } else { 127 | $packageInfo[self::NEW_VERSIONS] = $packageInfo[self::AVAILABLE_VERSIONS]; 128 | } 129 | } 130 | 131 | return $packageInfo; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/MagentoComposerApplication.php: -------------------------------------------------------------------------------- 1 | consoleApplication = $consoleApplication ? $consoleApplication : new Application(); 73 | $this->consoleArrayInputFactory = $consoleArrayInputFactory ? $consoleArrayInputFactory 74 | : new ConsoleArrayInputFactory(); 75 | $this->consoleOutput = $consoleOutput ? $consoleOutput : new BufferedOutput(); 76 | 77 | $this->composerJson = $pathToComposerJson; 78 | $this->composerHome = $pathToComposerHome; 79 | 80 | putenv('COMPOSER_HOME=' . $pathToComposerHome); 81 | 82 | $this->consoleApplication->setAutoExit(false); 83 | } 84 | 85 | /** 86 | * Creates composer object 87 | * 88 | * @return \Composer\Composer 89 | * @throws \Exception 90 | */ 91 | public function createComposer() 92 | { 93 | return ComposerFactory::create(new BufferIO(), $this->composerJson); 94 | } 95 | 96 | /** 97 | * Runs composer command 98 | * 99 | * @param array $commandParams 100 | * @param string|null $workingDir 101 | * @return bool 102 | * @throws \RuntimeException 103 | */ 104 | public function runComposerCommand(array $commandParams, $workingDir = null) 105 | { 106 | $this->consoleApplication->resetComposer(); 107 | 108 | if ($workingDir) { 109 | $commandParams[self::COMPOSER_WORKING_DIR] = $workingDir; 110 | } else { 111 | $commandParams[self::COMPOSER_WORKING_DIR] = dirname($this->composerJson); 112 | } 113 | 114 | $input = $this->consoleArrayInputFactory->create($commandParams); 115 | 116 | $exitCode = $this->consoleApplication->run($input, $this->consoleOutput); 117 | 118 | if ($exitCode) { 119 | throw new \RuntimeException( 120 | sprintf('Command "%s" failed: %s', $commandParams['command'], $this->consoleOutput->fetch()) 121 | ); 122 | } 123 | 124 | return $this->consoleOutput->fetch(); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/RequireUpdateDryRunCommand.php: -------------------------------------------------------------------------------- 1 | magentoComposerApplication = $magentoComposerApplication; 35 | $this->infoCommand = $infoCommand; 36 | } 37 | 38 | /** 39 | * Runs composer update --dry-run command 40 | * 41 | * @param array $packages 42 | * @param string|null $workingDir 43 | * @return string 44 | * @throws \RuntimeException 45 | */ 46 | public function run($packages, $workingDir = null) 47 | { 48 | try { 49 | // run require 50 | $this->magentoComposerApplication->runComposerCommand( 51 | ['command' => 'require', 'packages' => $packages, '--no-update' => true], 52 | $workingDir 53 | ); 54 | 55 | $output = $this->magentoComposerApplication->runComposerCommand( 56 | ['command' => 'update', '--dry-run' => true], 57 | $workingDir 58 | ); 59 | } catch (\RuntimeException $e) { 60 | $errorMessage = $this->generateAdditionalErrorMessage($e->getMessage(), $packages); 61 | if ($errorMessage) { 62 | throw new \RuntimeException($errorMessage, $e->getCode(), $e); 63 | } else { 64 | throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); 65 | } 66 | 67 | } 68 | 69 | return $output; 70 | } 71 | 72 | /** 73 | * Generates additional explanation for error message 74 | * 75 | * @param string $message 76 | * @param array $inputPackages 77 | * @return string 78 | */ 79 | protected function generateAdditionalErrorMessage($message, $inputPackages) 80 | { 81 | $matches = []; 82 | $errorMessage = ''; 83 | $packages = []; 84 | $rawLines = explode(PHP_EOL, $message); 85 | 86 | foreach ($rawLines as $line) { 87 | if (preg_match('/- (.*) requires (.*) -> no matching package/', $line, $matches)) { 88 | $packages[] = $matches[1]; 89 | $packages[] = $matches[2]; 90 | } 91 | } 92 | 93 | if (!empty($packages)) { 94 | $packages = array_unique($packages); 95 | $packages = $this->explodePackagesAndVersions($packages); 96 | $inputPackages = $this->explodePackagesAndVersions($inputPackages); 97 | 98 | $update = []; 99 | $conflicts = []; 100 | 101 | foreach ($inputPackages as $package => $version) { 102 | if (isset($packages[$package])) { 103 | $update[] = $package . ' to ' . $version; 104 | } 105 | } 106 | 107 | foreach (array_diff_key($packages, $inputPackages) as $package => $version) { 108 | 109 | if (!$packageInfo = $this->infoCommand->run($package, true)) { 110 | return false; 111 | } 112 | 113 | $currentVersion = $packageInfo[InfoCommand::CURRENT_VERSION]; 114 | 115 | if (empty($packageInfo[InfoCommand::AVAILABLE_VERSIONS])) { 116 | $packageInfo = $this->infoCommand->run($package); 117 | if (empty($packageInfo[InfoCommand::AVAILABLE_VERSIONS])) { 118 | return false; 119 | } 120 | } 121 | 122 | $conflicts[] = ' - ' . $package . ' version ' . $currentVersion . '. ' 123 | . 'Please try to update it to one of the following package versions: ' 124 | . implode(', ', $packageInfo['available_versions']); 125 | } 126 | 127 | $errorMessage = 'You are trying to update package(s) ' 128 | . implode(', ', $update) . PHP_EOL 129 | . "We've detected conflicts with the following packages:" . PHP_EOL 130 | . implode(PHP_EOL, $conflicts) 131 | . PHP_EOL; 132 | } 133 | 134 | return $errorMessage; 135 | } 136 | 137 | /** 138 | * Returns array that contains package as key and version as value 139 | * 140 | * @param array $packages 141 | * @return array 142 | */ 143 | protected function explodePackagesAndVersions($packages) 144 | { 145 | $packagesAndVersions = []; 146 | foreach ($packages as $package) { 147 | $package = explode(' ', $package); 148 | $packagesAndVersions[$package[0]] = $package[1]; 149 | } 150 | 151 | return $packagesAndVersions; 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /tests/Composer/ConsoleArrayInputFactoryTest.php: -------------------------------------------------------------------------------- 1 | factory = new ConsoleArrayInputFactory(); 20 | } 21 | 22 | public function testCreate() 23 | { 24 | $this->assertInstanceOf(\Symfony\Component\Console\Input\ArrayInput::class, $this->factory->create([])); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Composer/InfoCommandTest.php: -------------------------------------------------------------------------------- 1 | =5.4.11 23 | 3rdp/c 1.1.0'; 24 | 25 | /** 26 | * @var MagentoComposerApplication|MockObject 27 | */ 28 | protected $application; 29 | 30 | /** 31 | * @var InfoCommand 32 | */ 33 | protected $infoCommand; 34 | 35 | protected function setUp(): void 36 | { 37 | $this->application = $this->createMock(\Magento\Composer\MagentoComposerApplication::class); 38 | 39 | $this->infoCommand = new InfoCommand($this->application); 40 | } 41 | 42 | /** 43 | * @dataProvider getCommandOutputDataProvider 44 | */ 45 | public function testRun($input, $output) 46 | { 47 | $this->application->expects($this->once())->method('runComposerCommand')->willReturn($input); 48 | $result = $this->infoCommand->run('3rdp/a'); 49 | $this->assertEquals($output, $result); 50 | } 51 | 52 | public function testRunInstalled() 53 | { 54 | $this->application->expects($this->once())->method('runComposerCommand')->willReturn($this->installedOutput); 55 | $result = $this->infoCommand->run('3rdp/a', true); 56 | $this->assertEquals( 57 | [ 58 | 'name' => '3rdp/a', 59 | 'descrip.' => 'Plugin project A', 60 | 'versions' => '* 1.0.0', 61 | 'keywords' => '', 62 | 'type' => 'library', 63 | 'names' => '3rdp/a', 64 | 'current_version' => '1.0.0', 65 | 'available_versions' => [], 66 | 'new_versions' => [] 67 | ], 68 | $result 69 | ); 70 | } 71 | 72 | /** 73 | * Data provider that returns different input and output for composer info command. 74 | * 75 | * @return array 76 | */ 77 | public static function getCommandOutputDataProvider() 78 | { 79 | return [ 80 | 'Package not installed' => [ 81 | 'name : 3rdp/a 82 | descrip. : Plugin project A 83 | keywords : 84 | versions : 1.0.0, 1.1.0 85 | type : library 86 | names : 3rdp/a 87 | 88 | requires 89 | php >=5.4.11 90 | 3rdp/c 1.1.0', 91 | [ 92 | 'name' => '3rdp/a', 93 | 'descrip.' => 'Plugin project A', 94 | 'versions' => '1.0.0, 1.1.0', 95 | 'keywords' => '', 96 | 'type' => 'library', 97 | 'names' => '3rdp/a', 98 | 'current_version' => '', 99 | 'available_versions' => [ 100 | '1.0.0', 101 | '1.1.0' 102 | ], 103 | 'new_versions' => [ 104 | '1.0.0', 105 | '1.1.0' 106 | ] 107 | ] 108 | ], 109 | 'Package installed' => [ 110 | 'name : 3rdp/a 111 | descrip. : Plugin project A 112 | keywords : 113 | versions : 1.0.0, 1.1.0, * 1.1.2, 1.2.0 114 | type : library 115 | names : 3rdp/a 116 | 117 | requires 118 | php >=5.4.11 119 | 3rdp/c 1.1.0', 120 | [ 121 | 'name' => '3rdp/a', 122 | 'descrip.' => 'Plugin project A', 123 | 'versions' => '1.0.0, 1.1.0, * 1.1.2, 1.2.0', 124 | 'keywords' => '', 125 | 'type' => 'library', 126 | 'names' => '3rdp/a', 127 | 'current_version' => '1.1.2', 128 | 'available_versions' => [ 129 | '1.0.0', 130 | '1.1.0', 131 | '1.2.0' 132 | ], 133 | 'new_versions' => [ 134 | '1.2.0' 135 | ] 136 | ] 137 | ], 138 | ]; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /tests/Composer/MagentoComposerApplicationTest.php: -------------------------------------------------------------------------------- 1 | composerApplication = $this->getMockBuilder('Composer\Console\Application') 38 | ->getMock(); 39 | $this->inputFactory = $this->createMock(\Magento\Composer\ConsoleArrayInputFactory::class); 40 | $this->consoleOutput = $this->createMock(\Symfony\Component\Console\Output\BufferedOutput::class); 41 | 42 | $this->application = new MagentoComposerApplication( 43 | 'path1', 44 | 'path2', 45 | $this->composerApplication, 46 | $this->inputFactory, 47 | $this->consoleOutput 48 | ); 49 | } 50 | 51 | function testWrongExitCode() 52 | { 53 | $this->composerApplication->expects($this->once())->method('run')->willReturn(1); 54 | $this->expectException(\RuntimeException::class); 55 | $this->expectExceptionMessage('Command "update" failed'); 56 | 57 | $this->application->runComposerCommand(['command'=>'update']); 58 | } 59 | 60 | function testRunCommand() 61 | { 62 | $inputData = ['command' => 'update', MagentoComposerApplication::COMPOSER_WORKING_DIR => '.']; 63 | 64 | $this->composerApplication->expects($this->once())->method('resetComposer'); 65 | 66 | $this->inputFactory->expects($this->once())->method('create')->with($inputData); 67 | 68 | $this->consoleOutput->expects($this->once())->method('fetch')->willReturn('Nothing to update'); 69 | 70 | $this->composerApplication->expects($this->once())->method('run')->willReturn(0); 71 | 72 | $message = $this->application->runComposerCommand($inputData); 73 | $this->assertEquals('Nothing to update', $message); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/Composer/RequireUpdateDryRunCommandTest.php: -------------------------------------------------------------------------------- 1 | no matching package found. 38 | - 3rdp/e 1.0.0 requires 3rdp/d 1.0.0 -> no matching package found. 39 | - 3rdp/e 1.0.0 requires 3rdp/d 1.0.0 -> no matching package found. 40 | - Installation request for 3rdp/e 1.0.0 -> satisfiable by 3rdp/e[1.0.0]. 41 | 42 | Potential causes: 43 | - A typo in the package name 44 | - The package is not available in a stable-enough version according to your minimum-stability setting 45 | see for more details. 46 | 47 | Read for further common problems.'; 48 | 49 | /** 50 | * @var string 51 | */ 52 | private $packageInfo = [ 53 | 'name' => '3rdp/d', 54 | 'descrip.' => 'Plugin project A', 55 | 'versions' => '* 1.0.0, 1.1.0, 1.2.0', 56 | 'keywords' => '', 57 | 'type' => 'library', 58 | 'names' => '3rdp/d', 59 | 'current_version' => '1.0.0', 60 | 'available_versions' => [ 61 | '1.1.0', 62 | '1.2.0' 63 | ] 64 | ]; 65 | 66 | protected function setUp(): void 67 | { 68 | $this->application = $this->createMock(\Magento\Composer\MagentoComposerApplication::class); 69 | $this->infoCommand = $this->createMock(\Magento\Composer\InfoCommand::class); 70 | 71 | $this->requireUpdateDryRunCommand = new RequireUpdateDryRunCommand( 72 | $this->application, 73 | $this->infoCommand 74 | ); 75 | } 76 | 77 | public function testRun() 78 | { 79 | $this->application->expects($this->exactly(2))->method('runComposerCommand'); 80 | $this->requireUpdateDryRunCommand->run([], ''); 81 | } 82 | 83 | public function testRunException() 84 | { 85 | $this->application->expects($this->once()) 86 | ->method('runComposerCommand') 87 | ->willThrowException(new \RuntimeException($this->errorMessage)); 88 | $this->expectException(\RuntimeException::class); 89 | $this->infoCommand->expects($this->once())->method('run')->willReturn($this->packageInfo); 90 | $this->requireUpdateDryRunCommand->run(['3rdp/e 1.2.0'], ''); 91 | } 92 | 93 | } 94 | --------------------------------------------------------------------------------