Skip to content

Commit

Permalink
Merge pull request #29 from IonBazan/feature/symfony-7
Browse files Browse the repository at this point in the history
Symfony 7 support
  • Loading branch information
IonBazan authored Dec 29, 2023
2 parents 972f371 + 005967f commit 57c9f41
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 32 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
include:
- php-versions: '5.3'
dependencies: 'lowest'
Expand All @@ -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:
Expand All @@ -57,15 +58,15 @@ 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: |
composer config --no-plugins allow-plugins.infection/extension-installer true
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"source": {
"directories": [
"src"
],
"excludes": [
"src/Command/BaseNotTypedCommand.php"
]
},
"logs": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ parameters:
- src
checkGenericClassInNonGenericObjectType: true
checkMissingIterableValueType: true
bootstrapFiles:
- src/Command/DiffCommand.php # contains class alias
25 changes: 25 additions & 0 deletions src/Command/BaseNotTypedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace IonBazan\ComposerDiff\Command;

use Composer\Command\BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @codeCoverageIgnore
*
* This class contains a non-typed version of execute() method (for PHP 5).
*/
abstract class BaseNotTypedCommand extends BaseCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->handle($input, $output);
}

/**
* @return int
*/
abstract protected function handle(InputInterface $input, OutputInterface $output);
}
25 changes: 25 additions & 0 deletions src/Command/BaseTypedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace IonBazan\ComposerDiff\Command;

use Composer\Command\BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @codeCoverageIgnore
*
* This class contains a typed version of execute() method (PHP 7+).
*/
abstract class BaseTypedCommand extends BaseCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
return $this->handle($input, $output);
}

/**
* @return int
*/
abstract protected function handle(InputInterface $input, OutputInterface $output);
}
18 changes: 14 additions & 4 deletions src/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -252,7 +251,7 @@ public function outputDataProvider()
'packages-dev' => array(
),
),
128
128
).PHP_EOL,
array(
'--no-dev' => null,
Expand Down
21 changes: 2 additions & 19 deletions tests/Integration/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
25 changes: 25 additions & 0 deletions tests/Util/ComposerApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace IonBazan\ComposerDiff\Tests\Util;

use Composer\Composer;
use Composer\Console\Application;
use Composer\IO\IOInterface;

class ComposerApplication extends Application
{
public function setIO(IOInterface $io)
{
$this->io = $io;
}

public function setComposer(Composer $composer)
{
$this->composer = $composer;
}

protected function getDefaultCommands()
{
return array();
}
}
25 changes: 25 additions & 0 deletions tests/Util/TypedComposerApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace IonBazan\ComposerDiff\Tests\Util;

use Composer\Composer;
use Composer\Console\Application;
use Composer\IO\IOInterface;

class TypedComposerApplication extends Application
{
public function setIO(IOInterface $io)
{
$this->io = $io;
}

public function setComposer(Composer $composer)
{
$this->composer = $composer;
}

protected function getDefaultCommands(): array
{
return array();
}
}

0 comments on commit 57c9f41

Please sign in to comment.