From 005967ff40ae46c7d6c47b615b46f96931ef7167 Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Mon, 23 Oct 2023 15:00:38 +0800 Subject: [PATCH] Symfony 7 support --- .github/workflows/test.yml | 7 ++++--- composer.json | 4 ++-- infection.json.dist | 3 +++ phpstan.neon | 2 ++ src/Command/BaseNotTypedCommand.php | 25 +++++++++++++++++++++++++ src/Command/BaseTypedCommand.php | 25 +++++++++++++++++++++++++ src/Command/DiffCommand.php | 18 ++++++++++++++---- tests/Command/DiffCommandTest.php | 7 +++---- tests/Integration/DiffCommandTest.php | 21 ++------------------- tests/TestCase.php | 10 ++++++++++ tests/Util/ComposerApplication.php | 25 +++++++++++++++++++++++++ tests/Util/TypedComposerApplication.php | 25 +++++++++++++++++++++++++ 12 files changed, 140 insertions(+), 32 deletions(-) create mode 100644 src/Command/BaseNotTypedCommand.php create mode 100644 src/Command/BaseTypedCommand.php create mode 100644 tests/Util/ComposerApplication.php create mode 100644 tests/Util/TypedComposerApplication.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 32d4071..b3a537a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,6 +24,7 @@ jobs: - '8.1' - '8.2' - '8.3' + - '8.4' include: - php-versions: '5.3' dependencies: 'lowest' @@ -38,7 +39,7 @@ jobs: operating-system: windows-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP, with composer and extensions uses: shivammathur/setup-php@v2 with: @@ -57,7 +58,7 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 - name: Run mutation tests - if: ${{ matrix.php-versions == 8.2 && matrix.operating-system == 'ubuntu-latest' }} + if: ${{ matrix.php-versions == 8.3 && matrix.operating-system == 'ubuntu-latest' }} env: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} run: | @@ -65,7 +66,7 @@ jobs: composer req infection/infection -W vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4 - name: Run phpstan - if: ${{ matrix.php-versions == 8.2 && matrix.operating-system == 'ubuntu-latest' }} + if: ${{ matrix.php-versions == 8.3 && matrix.operating-system == 'ubuntu-latest' }} run: | composer req phpstan/phpstan vendor/bin/phpstan diff --git a/composer.json b/composer.json index 3947560..54e7497 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ }, "require-dev": { "composer/composer": "^1.1 || ^2.0", - "symfony/console": "^2.3 || ^3.0 || ^4.0 || ^5.0 || ^6.0", - "symfony/phpunit-bridge": "^4.2 || ^5.0 || ^6.0" + "symfony/console": "^2.3 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0", + "symfony/phpunit-bridge": "^4.2 || ^5.0 || ^6.0 || ^7.0" }, "suggest": { "composer/composer": "To use the binary without composer runtime", diff --git a/infection.json.dist b/infection.json.dist index 30616e6..e91cf76 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -2,6 +2,9 @@ "source": { "directories": [ "src" + ], + "excludes": [ + "src/Command/BaseNotTypedCommand.php" ] }, "logs": { diff --git a/phpstan.neon b/phpstan.neon index 99b1de4..9bf6e70 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,3 +4,5 @@ parameters: - src checkGenericClassInNonGenericObjectType: true checkMissingIterableValueType: true + bootstrapFiles: + - src/Command/DiffCommand.php # contains class alias diff --git a/src/Command/BaseNotTypedCommand.php b/src/Command/BaseNotTypedCommand.php new file mode 100644 index 0000000..908af86 --- /dev/null +++ b/src/Command/BaseNotTypedCommand.php @@ -0,0 +1,25 @@ +handle($input, $output); + } + + /** + * @return int + */ + abstract protected function handle(InputInterface $input, OutputInterface $output); +} diff --git a/src/Command/BaseTypedCommand.php b/src/Command/BaseTypedCommand.php new file mode 100644 index 0000000..c4ce397 --- /dev/null +++ b/src/Command/BaseTypedCommand.php @@ -0,0 +1,25 @@ +handle($input, $output); + } + + /** + * @return int + */ + abstract protected function handle(InputInterface $input, OutputInterface $output); +} diff --git a/src/Command/DiffCommand.php b/src/Command/DiffCommand.php index d3daa79..6c66691 100644 --- a/src/Command/DiffCommand.php +++ b/src/Command/DiffCommand.php @@ -2,7 +2,6 @@ namespace IonBazan\ComposerDiff\Command; -use Composer\Command\BaseCommand; use IonBazan\ComposerDiff\Diff\DiffEntries; use IonBazan\ComposerDiff\Diff\DiffEntry; use IonBazan\ComposerDiff\Formatter\Formatter; @@ -17,6 +16,17 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +/* + * This is a trick to maintain compatibility with both PHP 5 and 7 with Symfony 2.3 all the way to 7 with typed returns. + * This is only needed when using this package as a dependency with Symfony 7+, not when using as Composer plugin. + */ +class_alias( + PHP_VERSION_ID >= 70000 + ? 'IonBazan\ComposerDiff\Command\BaseTypedCommand' + : 'IonBazan\ComposerDiff\Command\BaseNotTypedCommand', + 'IonBazan\ComposerDiff\Command\BaseCommand' +); + class DiffCommand extends BaseCommand { const CHANGES_PROD = 2; @@ -122,9 +132,9 @@ protected function configure() } /** - * {@inheritdoc} + * @return int */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function handle(InputInterface $input, OutputInterface $output) { $base = null !== $input->getArgument('base') ? $input->getArgument('base') : $input->getOption('base'); $target = null !== $input->getArgument('target') ? $input->getArgument('target') : $input->getOption('target'); @@ -205,7 +215,7 @@ private function getFormatter(InputInterface $input, OutputInterface $output) return new MarkdownListFormatter($output, $urlGenerators); case 'github': return new GitHubFormatter($output, $urlGenerators); - // case 'mdtable': + // case 'mdtable': default: return new MarkdownTableFormatter($output, $urlGenerators); } diff --git a/tests/Command/DiffCommandTest.php b/tests/Command/DiffCommandTest.php index ad40b31..a66ab29 100644 --- a/tests/Command/DiffCommandTest.php +++ b/tests/Command/DiffCommandTest.php @@ -7,7 +7,6 @@ use Composer\DependencyResolver\Operation\UninstallOperation; use Composer\DependencyResolver\Operation\UpdateOperation; use IonBazan\ComposerDiff\Command\DiffCommand; -use IonBazan\ComposerDiff\Tests\Integration\ComposerApplication; use IonBazan\ComposerDiff\Tests\TestCase; use Symfony\Component\Console\Tester\CommandTester; @@ -21,7 +20,7 @@ class DiffCommandTest extends TestCase public function testItGeneratesReportInGivenFormat($expectedOutput, array $options) { $diff = $this->getMockBuilder('IonBazan\ComposerDiff\PackageDiff')->getMock(); - $application = new ComposerApplication(); + $application = $this->getComposerApplication(); $command = new DiffCommand($diff, array('gitlab2.org')); $command->setApplication($application); $tester = new CommandTester($command); @@ -53,7 +52,7 @@ public function testItGeneratesReportInGivenFormat($expectedOutput, array $optio public function testStrictMode($exitCode, array $prodOperations, array $devOperations) { $diff = $this->getMockBuilder('IonBazan\ComposerDiff\PackageDiff')->getMock(); - $application = new ComposerApplication(); + $application = $this->getComposerApplication(); $command = new DiffCommand($diff, array('gitlab2.org')); $command->setApplication($application); $tester = new CommandTester($command); @@ -252,7 +251,7 @@ public function outputDataProvider() 'packages-dev' => array( ), ), - 128 + 128 ).PHP_EOL, array( '--no-dev' => null, diff --git a/tests/Integration/DiffCommandTest.php b/tests/Integration/DiffCommandTest.php index 08981b6..fc641bf 100644 --- a/tests/Integration/DiffCommandTest.php +++ b/tests/Integration/DiffCommandTest.php @@ -2,10 +2,7 @@ namespace IonBazan\ComposerDiff\Tests\Integration; -use Composer\Composer; -use Composer\Console\Application; use Composer\Factory; -use Composer\IO\IOInterface; use Composer\IO\NullIO; use Composer\Package\Package; use Composer\Plugin\PluginManager; @@ -25,9 +22,8 @@ class DiffCommandTest extends TestCase */ public function testCommand($expectedOutput, array $input) { - $application = new ComposerApplication(); $command = new DiffCommand(new PackageDiff()); - $command->setApplication($application); + $command->setApplication($this->getComposerApplication()); $tester = new CommandTester($command); $result = $tester->execute($input); $this->assertSame(0, $result); @@ -44,7 +40,7 @@ public function testCommand($expectedOutput, array $input) public function testComposerApplication($expectedOutput, array $input) { $input = array_merge(array('command' => 'diff'), $input); - $app = new ComposerApplication(); + $app = $this->getComposerApplication(); $app->setIO(new NullIO()); // For Composer v1 $app->setAutoExit(false); $plugin = $this->getPluginPackage(); @@ -247,16 +243,3 @@ private function getPluginPackage() return $plugin; } } - -class ComposerApplication extends Application -{ - public function setIO(IOInterface $io) - { - $this->io = $io; - } - - public function setComposer(Composer $composer) - { - $this->composer = $composer; - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 55ac762..f628742 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,8 @@ use Composer\Package\PackageInterface; use IonBazan\ComposerDiff\Diff\DiffEntries; use IonBazan\ComposerDiff\Diff\DiffEntry; +use IonBazan\ComposerDiff\Tests\Util\ComposerApplication; +use IonBazan\ComposerDiff\Tests\Util\TypedComposerApplication; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -73,4 +75,12 @@ protected function getEntries(array $operations) return new DiffEntry($operation); }, $operations)); } + + /** + * @return ComposerApplication|TypedComposerApplication + */ + protected function getComposerApplication() + { + return PHP_VERSION_ID >= 70000 ? new TypedComposerApplication() : new ComposerApplication(); + } } diff --git a/tests/Util/ComposerApplication.php b/tests/Util/ComposerApplication.php new file mode 100644 index 0000000..e792340 --- /dev/null +++ b/tests/Util/ComposerApplication.php @@ -0,0 +1,25 @@ +io = $io; + } + + public function setComposer(Composer $composer) + { + $this->composer = $composer; + } + + protected function getDefaultCommands() + { + return array(); + } +} diff --git a/tests/Util/TypedComposerApplication.php b/tests/Util/TypedComposerApplication.php new file mode 100644 index 0000000..9132bd5 --- /dev/null +++ b/tests/Util/TypedComposerApplication.php @@ -0,0 +1,25 @@ +io = $io; + } + + public function setComposer(Composer $composer) + { + $this->composer = $composer; + } + + protected function getDefaultCommands(): array + { + return array(); + } +}