Skip to content

Commit

Permalink
Merge branch 'master' into check-identity-of-parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Dec 13, 2023
2 parents 7a3a569 + 23e0c46 commit 29d8638
Show file tree
Hide file tree
Showing 31 changed files with 609 additions and 193 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
70 changes: 70 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Tests

on:
push:
pull_request:

jobs:
tests:
name: PHPUnit PHP ${{ matrix.php }} ${{ matrix.dependency }} (Symfony ${{ matrix.symfony }})
runs-on: ubuntu-latest
strategy:
matrix:
php:
- '8.1'
- '8.2'
- '8.3'
dependency:
- ''
symfony:
- '5.4.*'
- '6.3.*'
- '6.4.*'
- '7.0.*'
include:
- php: '8.1'
symfony: '5.4.*'
dependency: 'lowest'
exclude:
- php: '8.1'
symfony: '7.0.*'
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pcov
tools: flex

- name: Get Composer Cache Directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ matrix.php }}-composer-

- name: Update project dependencies
if: matrix.dependency == ''
run: composer update --no-progress --ansi --prefer-stable
env:
SYMFONY_REQUIRE: ${{ matrix.symfony }}

- name: Update project dependencies lowest
if: matrix.dependency == 'lowest'
run: composer update --no-progress --ansi --prefer-stable --prefer-lowest
env:
SYMFONY_REQUIRE: ${{ matrix.symfony }}

- name: Validate composer
run: composer validate --strict --no-check-lock

- name: Run tests
run: vendor/bin/phpunit
48 changes: 0 additions & 48 deletions .travis.yml

This file was deleted.

15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## v4.3.0

- Added support for Symfony 6,
- Removed support for Symfony <4.4 & >5.0 - <5.3

## v4.2.1

- Support for PHP8

## v4.2.0

- Fix: PSR-4 autoloading (#132)
- Add `@coversNothing` annotation to prevent raising risky warnings in PHPUnit 9
- Fix: run `prepend()` for all extensions first, then `load()` then extensions (#126)

## v4.1.0

- Support for Symfony 5.
Expand Down
6 changes: 0 additions & 6 deletions Loader/LoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
Expand Down Expand Up @@ -56,9 +55,4 @@ public function createPhpFileLoader(ContainerBuilder $container): PhpFileLoader
{
return new PhpFileLoader($container, new FileLocator());
}

public function createIniFileLoader(ContainerBuilder $container): IniFileLoader
{
return new IniFileLoader($container, new FileLocator());
}
}
2 changes: 2 additions & 0 deletions PhpUnit/AbstractCompilerPassTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ abstract protected function registerCompilerPass(ContainerBuilder $container): v
* This test will run the compile method.
*
* @test
*
* @coversNothing
*/
final public function compilation_should_not_fail_with_empty_container(): void
{
Expand Down
11 changes: 10 additions & 1 deletion PhpUnit/AbstractContainerBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

abstract class AbstractContainerBuilderTestCase extends TestCase
{
Expand Down Expand Up @@ -250,4 +249,14 @@ final protected function assertContainerBuilderHasServiceLocator(

self::assertThat($definition, new DefinitionEqualsServiceLocatorConstraint($expectedServiceMap));
}

final protected function assertContainerBuilderServiceDecoration(
string $serviceId,
string $decoratedServiceId,
?string $renamedId = null,
int $priority = 0,
?int $invalidBehavior = null
): void {
self::assertThat($this->container, new DefinitionDecoratesConstraint($serviceId, $decoratedServiceId, $renamedId, $priority, $invalidBehavior));
}
}
2 changes: 2 additions & 0 deletions PhpUnit/AbstractExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ final protected function load(array $configurationValues = []): void
if ($extension instanceof PrependExtensionInterface) {
$extension->prepend($this->container);
}
}

foreach ($this->container->getExtensions() as $extension) {
$extension->load($configs, $this->container);
}
}
Expand Down
153 changes: 153 additions & 0 deletions PhpUnit/DefinitionDecoratesConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class DefinitionDecoratesConstraint extends Constraint
{
private const INVALID_BEHAVIORS = [
ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE => 'RUNTIME_EXCEPTION_ON_INVALID_REFERENCE',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE => 'EXCEPTION_ON_INVALID_REFERENCE',
ContainerInterface::NULL_ON_INVALID_REFERENCE => 'NULL_ON_INVALID_REFERENCE',
ContainerInterface::IGNORE_ON_INVALID_REFERENCE => 'IGNORE_ON_INVALID_REFERENCE',
ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE => 'IGNORE_ON_UNINITIALIZED_REFERENCE',
];

private string $serviceId;
private string $decoratedServiceId;
private ?string $renamedId;
private int $priority;
private ?int $invalidBehavior;

public function __construct(string $serviceId, string $decoratedServiceId, ?string $renamedId = null, int $priority = 0, ?int $invalidBehavior = null)
{
$this->serviceId = $serviceId;
$this->decoratedServiceId = $decoratedServiceId;
$this->renamedId = $renamedId;
$this->priority = $priority;
$this->invalidBehavior = $invalidBehavior;
}

public function toString(): string
{
return sprintf(
'"%s" decorates service "%s"%s with priority "%d" and "%s" behavior.',
$this->serviceId,
$this->decoratedServiceId,
$this->renamedId !== null ? sprintf(' and renames it to "%s"', $this->renamedId) : '',
$this->priority,
self::INVALID_BEHAVIORS[$this->invalidBehavior ?? 0]
);
}

public function evaluate($other, string $description = '', bool $returnResult = false): bool
{
if (!($other instanceof ContainerBuilder)) {
throw new \InvalidArgumentException(
'Expected an instance of Symfony\Component\DependencyInjection\ContainerBuilder'
);
}

return $this->evaluateServiceDefinition($other, $returnResult);
}

private function evaluateServiceDefinition(ContainerBuilder $containerBuilder, bool $returnResult): bool
{
if (!$containerBuilder->has($this->serviceId)) {
if ($returnResult) {
return false;
}

$this->fail(
$this->serviceId,
sprintf(
'The container builder has no service "%s"',
$this->serviceId
)
);
}

$definition = $containerBuilder->findDefinition($this->serviceId);

$decorated = $definition->getDecoratedService();

if ($decorated === null) {
if ($returnResult) {
return false;
}

$this->fail(
$this->serviceId,
sprintf(
'The container builder has a service "%s", but it does not decorate any service',
$this->serviceId
)
);
}

if ($decorated[0] !== $this->decoratedServiceId) {
if ($returnResult) {
return false;
}

$this->fail(
$this->serviceId,
sprintf(
'The container builder has a decorator service "%s", but it does decorate service "%s".',
$this->serviceId,
$decorated[0]
)
);
}

if ($decorated[1] !== $this->renamedId) {
if ($returnResult) {
return false;
}

$this->fail(
$this->serviceId,
sprintf(
'The container builder has a decorator service "%s", but it does not rename decorated service to "%s".',
$this->serviceId,
$this->renamedId
)
);
}

if ($decorated[2] !== $this->priority) {
if ($returnResult) {
return false;
}

$this->fail(
$this->serviceId,
sprintf(
'The container builder has a decorator service "%s", but it does not decorate at expected "%d" priority.',
$this->serviceId,
$this->priority
)
);
}

if (($decorated[3] ?? null) !== $this->invalidBehavior) {
if ($returnResult) {
return false;
}

$this->fail(
$this->serviceId,
sprintf(
'The container builder has a decorator service "%s", but it does not decorate with expected "%s" behavior.',
$this->serviceId,
self::INVALID_BEHAVIORS[$this->invalidBehavior]
)
);
}

return true;
}
}
Loading

0 comments on commit 29d8638

Please sign in to comment.