Skip to content

Commit

Permalink
Merge branch '2.x' into upmerge-2-x
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/ci.yml
#	CHANGELOG.md
  • Loading branch information
chapterjason committed Dec 17, 2023
2 parents d07b81c + 04a1862 commit ca47064
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 28 deletions.
50 changes: 28 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,46 @@ jobs:
exclude:
- php: '7.2'
symfony: '6.0.*'
- php: '7.3'
symfony: '6.0.*'
- php: '7.4'
symfony: '6.0.*'
- php: '7.2'
symfony: '6.1.*'
- php: '7.3'
symfony: '6.1.*'
- php: '7.4'
symfony: '6.1.*'
- php: '8.0'
symfony: '6.1.*'
- php: '7.2'
symfony: '6.2.*'
- php: '7.3'
symfony: '6.2.*'
- php: '7.4'
symfony: '6.2.*'
- php: '8.0'
symfony: '6.2.*'
- php: '7.2'
symfony: '6.3.*'
- php: '7.3'
symfony: '6.3.*'
- php: '7.4'
symfony: '6.3.*'
- php: '8.0'
symfony: '6.3.*'
- php: '7.2'
symfony: '6.4.*'
- php: '7.3'
symfony: '4.4.*'
- php: '7.3'
symfony: '6.0.*'
- php: '7.3'
symfony: '6.1.*'
- php: '7.3'
symfony: '6.2.*'
- php: '7.3'
symfony: '6.3.*'
- php: '7.3'
symfony: '6.4.*'
- php: '7.4'
symfony: '4.4.*'
- php: '7.4'
symfony: '6.0.*'
- php: '7.4'
symfony: '6.1.*'
- php: '7.4'
symfony: '6.2.*'
- php: '7.4'
symfony: '6.3.*'
- php: '7.4'
symfony: '6.4.*'
- php: '8.0'
symfony: '4.4.*'
- php: '8.0'
symfony: '6.1.*'
- php: '8.0'
symfony: '6.2.*'
- php: '8.0'
symfony: '6.3.*'
- php: '8.0'
symfony: '6.4.*'
- php: '7.2'
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
- Drop support for Symfony 4.4
- Drop support for Symfony 5.3

## 2.1.0

### Added

- Support for Symfony 6.2, 6.3 and 6.4
- Support for PHP 8.2 and 8.3
- Allow to set type and priority for compiler passes in `TestKernel::addTestCompilerPass`

## 2.0.0

### Added
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Using this bundle test together with Matthias Nobacks's
[SymfonyDependencyInjectionTest](https://github.com/SymfonyTest/SymfonyDependencyInjectionTest)
will give you a good base for testing a Symfony bundle.

## Support

Currently supported PHP and Symfony Versions.

| Branch | PHP Version | Symfony Version | Supported |
|--------|-------------------------|-----------------|-----------|
| 2.x | 7.2 - 8.3 | 4.4, 6.3 - 6.4 | Yes |

Please always try to update to the latest version of this package before reporting an issue.

## Install

Via Composer
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "",
"license": "MIT",
"type": "library",
"keywords": ["testing"],
"authors": [
{
"name": "Tobias Nyholm",
Expand Down
17 changes: 11 additions & 6 deletions src/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
Expand Down Expand Up @@ -41,7 +42,7 @@ class TestKernel extends Kernel
private $testProjectDir;

/**
* @var CompilerPassInterface[]
* @var array{CompilerPassInterface, string, int}[]
*/
private $testCompilerPasses = [];

Expand Down Expand Up @@ -125,19 +126,23 @@ protected function buildContainer(): ContainerBuilder
{
$container = parent::buildContainer();

foreach ($this->testCompilerPasses as $pass) {
$container->addCompilerPass($pass);
foreach ($this->testCompilerPasses as $compilerPass) {
$container->addCompilerPass($compilerPass[0], $compilerPass[1], $compilerPass[2]);
}

return $container;
}

/**
* @param CompilerPassInterface $compilerPasses
* @param CompilerPassInterface $compilerPass
* @param string $type
* @param int $priority
*
* @psalm-param PassConfig::TYPE_* $type
*/
public function addTestCompilerPass($compilerPasses): void
public function addTestCompilerPass($compilerPass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, $priority = 0): void
{
$this->testCompilerPasses[] = $compilerPasses;
$this->testCompilerPasses[] = [$compilerPass, $type, $priority];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler;

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

class DeRegisterSomethingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('something')) {
$container->removeDefinition('something');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler;

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

class RegisterSomethingPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('something')) {
return;
}

$definition = new Definition();
$definition->setClass(\stdClass::class);
$definition->setPublic(true);

$container->setDefinition('something', $definition);
}
}
40 changes: 40 additions & 0 deletions tests/Functional/BundleConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Nyholm\BundleTest\TestKernel;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\DeRegisterSomethingPass;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\RegisterSomethingPass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\KernelInterface;

Expand Down Expand Up @@ -61,4 +64,41 @@ public function testBundleWithDifferentConfigurationFormats($config): void
$this->assertEquals('val1', $container->getParameter('app.foo'));
$this->assertEquals(['val2', 'val3'], $container->getParameter('app.bar'));
}

public function testAddCompilerPassPriority(): void
{
// CASE 1: Compiler pass without priority, should be prioritized by order of addition
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
$kernel->addTestCompilerPass(new DeRegisterSomethingPass());
$kernel->addTestCompilerPass(new RegisterSomethingPass());
}]);

$container = $kernel->getContainer();

$this->assertTrue($container->has('something'));

// CASE 2: Compiler pass with priority, should be prioritized by priority
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
$kernel->addTestCompilerPass(new DeRegisterSomethingPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -5);
$kernel->addTestCompilerPass(new RegisterSomethingPass());
}]);

$container = $kernel->getContainer();

$this->assertFalse($container->has('something'));

// CASE 3: Compiler pass without priority, should be prioritized by order of addition
$kernel = self::bootKernel(['config' => function (TestKernel $kernel) {
$kernel->addTestConfig(__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php');
// DeRegisterSomethingPass is now added as second compiler pass
$kernel->addTestCompilerPass(new RegisterSomethingPass());
$kernel->addTestCompilerPass(new DeRegisterSomethingPass());
}]);

$container = $kernel->getContainer();

$this->assertFalse($container->has('something'));
}
}

0 comments on commit ca47064

Please sign in to comment.