From 11b82a97fa1d8a324d7022c79cc3abb65055100c Mon Sep 17 00:00:00 2001 From: RLysenko96 <117074800+RLysenko96@users.noreply.github.com> Date: Sat, 3 Dec 2022 14:57:30 +0200 Subject: [PATCH] Support for symphony 4+ (#9) Adds support for Symfony 4+ by adding a compiler pass to mark services as public. Also expands composer constraints to support php 8 and phpunit 8 & 9. --- .github/workflows/ci.yml | 17 +++++++--- README.md | 4 --- composer.json | 8 ++--- ...BehatPublicContainerDefinitionCompiler.php | 31 +++++++++++++++++++ src/TestTraits/BehatContainerTrait.php | 7 +++++ 5 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/Compiler/PhpUnitBehatPublicContainerDefinitionCompiler.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00baae8..32d94d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,24 +1,31 @@ name: CI -on: [push] +on: [push, pull_request] jobs: build-test: runs-on: ubuntu-latest strategy: matrix: + phpunit: [6.5.13, 7.5.18, 8.5.31, 9.5.26] php: [7.3, 7.4] - phpunit: [6.5.13, 7.5.18] + include: + - phpunit: 8.5.31 + php: 8.0 + - phpunit: 9.5.26 + php: 8.0 + - phpunit: 9.5.26 + php: 8.1 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - uses: php-actions/composer@v6.0.0 + - uses: php-actions/composer@v6.1.1 with: php_version: ${{ matrix.php }} - name: PHPUnit Tests - uses: php-actions/phpunit@v3.0.0 + uses: php-actions/phpunit@v3.0.2 with: php_version: ${{ matrix.php }} version: ${{ matrix.phpunit }} diff --git a/README.md b/README.md index 1e21b4f..67e5824 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,6 @@ By including these traits in your PHPUnit test classes you can: * Behat 3 * PHPUnit 6+ -This project currently pins symfony/dependency-injection at ^3.0 because the way we are -accessing the Behat container is not currently compatible with Symfony 4. -PRs to fix this are very welcome ... - ## Installation `composer require jonathanjfshaw/phpunitbehat` diff --git a/composer.json b/composer.json index aebb6e5..ab287a1 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,10 @@ } ], "require": { - "php": ">=7.0.0", - "behat/behat": "^3.0", - "phpunit/phpunit": ">=6.0 <8.0", - "symfony/dependency-injection": "^3.0" + "php": "^7.0 || ^8.0", + "phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.0", + "behat/behat": "^3.0.0", + "symfony/dependency-injection": "^4.0 || ^5.0 || ^6.0" }, "autoload": { "psr-4": { diff --git a/src/Compiler/PhpUnitBehatPublicContainerDefinitionCompiler.php b/src/Compiler/PhpUnitBehatPublicContainerDefinitionCompiler.php new file mode 100644 index 0000000..25de4a7 --- /dev/null +++ b/src/Compiler/PhpUnitBehatPublicContainerDefinitionCompiler.php @@ -0,0 +1,31 @@ +getDefinitions() as $definition) { + // Mark all service definitions as public in order to + // be able to get the services directly from the container. + $definition->setPublic(TRUE); + } + } + +} diff --git a/src/TestTraits/BehatContainerTrait.php b/src/TestTraits/BehatContainerTrait.php index e670c61..157922b 100644 --- a/src/TestTraits/BehatContainerTrait.php +++ b/src/TestTraits/BehatContainerTrait.php @@ -4,6 +4,8 @@ use Behat\Testwork\ServiceContainer\ExtensionManager; use Behat\Testwork\ServiceContainer\ContainerLoader; +use PHPUnitBehat\Compiler\PhpUnitBehatPublicContainerDefinitionCompiler; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; @@ -63,6 +65,9 @@ protected function setBehatContainer() { // Provide basic parameters required by Behat, even though they make no sense in PhpUnit. $containerBuilder->setParameter('paths.base', ''); $containerBuilder->set('cli.input', new ArrayInput([])); + // Set default command`s name that require Behat. + // The command`s name can`t be empty. + $containerBuilder->setParameter('cli.command.name', 'PHPUnitBehat'); $containerBuilder->set('cli.output', new NullOutput()); // Add the PhpUnit behat environment handler. @@ -73,6 +78,8 @@ protected function setBehatContainer() { // Finalise the container. $containerLoader->load($containerBuilder, []); $containerBuilder->addObjectResource($containerLoader); + // Added compiler pass in order to process service`s definitions. + $containerBuilder->addCompilerPass(new PhpUnitBehatPublicContainerDefinitionCompiler(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1); $containerBuilder->compile(); self::$behatContainer = $containerBuilder; }