diff --git a/PhpUnit/DefinitionHasArgumentConstraint.php b/PhpUnit/DefinitionHasArgumentConstraint.php index 83133b2..5e58d68 100644 --- a/PhpUnit/DefinitionHasArgumentConstraint.php +++ b/PhpUnit/DefinitionHasArgumentConstraint.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\IsEqual; +use SebastianBergmann\Exporter\Exporter; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException; @@ -12,9 +13,10 @@ final class DefinitionHasArgumentConstraint extends Constraint /** * @var int|string */ - private $argumentIndex; - private $expectedValue; - private $checkExpectedValue; + private string|int $argumentIndex; + private mixed $expectedValue; + private bool $checkExpectedValue; + private Exporter $exporter; public function __construct($argumentIndex, $expectedValue, bool $checkExpectedValue = true) { @@ -37,6 +39,7 @@ public function __construct($argumentIndex, $expectedValue, bool $checkExpectedV $this->argumentIndex = $argumentIndex; $this->expectedValue = $expectedValue; $this->checkExpectedValue = $checkExpectedValue; + $this->exporter = new Exporter(); } public function toString(): string @@ -98,6 +101,18 @@ private function evaluateArgumentValue(Definition $definition, bool $returnResul { $actualValue = $definition->getArgument($this->argumentIndex); + if (gettype($actualValue) !== gettype($this->expectedValue)) { + $this->fail( + $definition, + sprintf( + 'The value of argument named "%s" (%s) is not equal to the expected value (%s)', + $this->argumentIndex, + $this->exporter->export($actualValue), + $this->exporter->export($this->expectedValue) + ) + ); + } + $constraint = new IsEqual($this->expectedValue); if (!$constraint->evaluate($actualValue, '', true)) { @@ -109,15 +124,15 @@ private function evaluateArgumentValue(Definition $definition, bool $returnResul $message = sprintf( 'The value of argument named "%s" (%s) is not equal to the expected value (%s)', $this->argumentIndex, - $this->exporter()->export($actualValue), - $this->exporter()->export($this->expectedValue) + $this->exporter->export($actualValue), + $this->exporter->export($this->expectedValue) ); } else { $message = sprintf( 'The value of argument with index %d (%s) is not equal to the expected value (%s)', $this->argumentIndex, - $this->exporter()->export($actualValue), - $this->exporter()->export($this->expectedValue) + $this->exporter->export($actualValue), + $this->exporter->export($this->expectedValue) ); } diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 38f1017..f5e2e1d 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -7,6 +7,7 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Reference; class MatthiasDependencyInjectionTestExtension implements ExtensionInterface { @@ -31,6 +32,11 @@ public function load(array $config, ContainerBuilder $container): void // add an alias to an existing service $container->setAlias('manual_alias', 'service_id'); + + // add a reference to an existing service + $definition = new Definition('manual_with_reference'); + $definition->addArgument(new Reference('manual_service_id')); + $container->setDefinition('manual_with_reference', $definition); } public function getAlias() diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index 0c23943..7f97115 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -178,6 +178,19 @@ public function if_definition_has_argument_but_with_wrong_value_it_fails(): void $this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 1, 'wrong value'); } + /** + * @test + */ + public function if_definition_has_argument_but_with_wrong_value_it_fails1(): void + { + $this->load(); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The value of argument named "0"'); + + $this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_with_reference', 0, 'manual_service_id'); + } + /** * @test */