|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Matthias\SymfonyDependencyInjectionTest\Tests\PhpUnit; |
| 4 | + |
| 5 | +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\DefinitionDecoratesConstraint; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 9 | +use Symfony\Component\DependencyInjection\Definition; |
| 10 | + |
| 11 | +class DefinitionDecoratesConstraintTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @test |
| 15 | + * @dataProvider containerBuilderProvider |
| 16 | + */ |
| 17 | + public function match(ContainerBuilder $containerBuilder, bool $expectedToMatch, string $serviceId, string $decoratedServiceId, ?string $renamedId, int $priority, ?int $invalidBehavior): void |
| 18 | + { |
| 19 | + $constraint = new DefinitionDecoratesConstraint($serviceId, $decoratedServiceId, $renamedId, $priority, $invalidBehavior); |
| 20 | + |
| 21 | + $this->assertSame($expectedToMatch, $constraint->evaluate($containerBuilder, '', true)); |
| 22 | + } |
| 23 | + |
| 24 | + public function containerBuilderProvider(): iterable |
| 25 | + { |
| 26 | + $containerBuilder = new ContainerBuilder(); |
| 27 | + $containerBuilder->setDefinition('decorated1', new Definition('DecoratedClass1')); |
| 28 | + $containerBuilder->setDefinition('decorated2', new Definition('DecoratedClass2')); |
| 29 | + $containerBuilder->setDefinition('decorated3', new Definition('DecoratedClass3')); |
| 30 | + $containerBuilder->setDefinition('decorated4', new Definition('DecoratedClass4')); |
| 31 | + $containerBuilder->setDefinition('decorated5', new Definition('DecoratedClass5')); |
| 32 | + |
| 33 | + $containerBuilder->setDefinition('decorator1', (new Definition('DecoratorClass1'))->setDecoratedService('decorated1')); |
| 34 | + $containerBuilder->setDefinition('decorator2', (new Definition('DecoratorClass2'))->setDecoratedService('decorated2', 'decorated2_0')); |
| 35 | + $containerBuilder->setDefinition('decorator3', (new Definition('DecoratorClass3'))->setDecoratedService('decorated3', null, 10)); |
| 36 | + $containerBuilder->setDefinition('decorator4', (new Definition('DecoratorClass4'))->setDecoratedService('decorated4', null, 0, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)); |
| 37 | + $containerBuilder->setDefinition('decorator5', (new Definition('DecoratorClass5'))->setDecoratedService('decorated5', 'decorated5_0', -3, ContainerInterface::NULL_ON_INVALID_REFERENCE)); |
| 38 | + |
| 39 | + yield [$containerBuilder, false, 'not_decorator', 'decorated', null, 0, null]; |
| 40 | + yield [$containerBuilder, true, 'decorator1', 'decorated1', null, 0, null]; |
| 41 | + yield [$containerBuilder, true, 'decorator2', 'decorated2', 'decorated2_0', 0, null]; |
| 42 | + yield [$containerBuilder, true, 'decorator3', 'decorated3', null, 10, null]; |
| 43 | + yield [$containerBuilder, true, 'decorator4', 'decorated4', null, 0, 3]; |
| 44 | + yield [$containerBuilder, true, 'decorator5', 'decorated5', 'decorated5_0', -3, 2]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @test |
| 49 | + * @dataProvider stringRepresentationProvider |
| 50 | + */ |
| 51 | + public function it_has_a_string_representation(DefinitionDecoratesConstraint $constraint, string $expectedRepresentation): void |
| 52 | + { |
| 53 | + $this->assertSame($expectedRepresentation, $constraint->toString()); |
| 54 | + } |
| 55 | + |
| 56 | + public function stringRepresentationProvider(): iterable |
| 57 | + { |
| 58 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated'), '"decorator" decorates service "decorated" with priority "0" and "RUNTIME_EXCEPTION_ON_INVALID_REFERENCE" behavior.']; |
| 59 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0'), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "0" and "RUNTIME_EXCEPTION_ON_INVALID_REFERENCE" behavior.']; |
| 60 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0', -3), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "-3" and "RUNTIME_EXCEPTION_ON_INVALID_REFERENCE" behavior.']; |
| 61 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0', -3, 0), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "-3" and "RUNTIME_EXCEPTION_ON_INVALID_REFERENCE" behavior.']; |
| 62 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0', -3, 1), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "-3" and "EXCEPTION_ON_INVALID_REFERENCE" behavior.']; |
| 63 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0', -3, 2), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "-3" and "NULL_ON_INVALID_REFERENCE" behavior.']; |
| 64 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0', -3, 3), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "-3" and "IGNORE_ON_INVALID_REFERENCE" behavior.']; |
| 65 | + yield [new DefinitionDecoratesConstraint('decorator', 'decorated', 'decorated_0', -3, 4), '"decorator" decorates service "decorated" and renames it to "decorated_0" with priority "-3" and "IGNORE_ON_UNINITIALIZED_REFERENCE" behavior.']; |
| 66 | + } |
| 67 | +} |
0 commit comments