Skip to content

Commit

Permalink
Merge pull request #154 from stloyd/bugfix/issue-141
Browse files Browse the repository at this point in the history
Fail when comparing arguments with reference
  • Loading branch information
stloyd authored Dec 13, 2023
2 parents e0032f5 + d381ca0 commit 23e0c46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
29 changes: 22 additions & 7 deletions PhpUnit/DefinitionHasArgumentConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)
);
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions Tests/PhpUnit/AbstractExtensionTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 23e0c46

Please sign in to comment.