Skip to content

Commit

Permalink
Support for symphony 4+ (#9)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RLysenko96 authored Dec 3, 2022
1 parent 1f0763b commit 11b82a9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected].0
uses: php-actions/[email protected].2
with:
php_version: ${{ matrix.php }}
version: ${{ matrix.phpunit }}
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
31 changes: 31 additions & 0 deletions src/Compiler/PhpUnitBehatPublicContainerDefinitionCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace PHPUnitBehat\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Mark all service definitions as public in order to
* be able to get the services directly from the container.
*/
class PhpUnitBehatPublicContainerDefinitionCompiler implements CompilerPassInterface {

/**
* Process service`s definitions.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* The service`s container.
*
* @return void
*/
public function process(ContainerBuilder $container)
{
foreach ($container->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);
}
}

}
7 changes: 7 additions & 0 deletions src/TestTraits/BehatContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down

0 comments on commit 11b82a9

Please sign in to comment.