Skip to content

Commit 23e0c46

Browse files
authored
Merge pull request #154 from stloyd/bugfix/issue-141
Fail when comparing arguments with reference
2 parents e0032f5 + d381ca0 commit 23e0c46

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

PhpUnit/DefinitionHasArgumentConstraint.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\Constraint\Constraint;
66
use PHPUnit\Framework\Constraint\IsEqual;
7+
use SebastianBergmann\Exporter\Exporter;
78
use Symfony\Component\DependencyInjection\Definition;
89
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
910

@@ -12,9 +13,10 @@ final class DefinitionHasArgumentConstraint extends Constraint
1213
/**
1314
* @var int|string
1415
*/
15-
private $argumentIndex;
16-
private $expectedValue;
17-
private $checkExpectedValue;
16+
private string|int $argumentIndex;
17+
private mixed $expectedValue;
18+
private bool $checkExpectedValue;
19+
private Exporter $exporter;
1820

1921
public function __construct($argumentIndex, $expectedValue, bool $checkExpectedValue = true)
2022
{
@@ -37,6 +39,7 @@ public function __construct($argumentIndex, $expectedValue, bool $checkExpectedV
3739
$this->argumentIndex = $argumentIndex;
3840
$this->expectedValue = $expectedValue;
3941
$this->checkExpectedValue = $checkExpectedValue;
42+
$this->exporter = new Exporter();
4043
}
4144

4245
public function toString(): string
@@ -98,6 +101,18 @@ private function evaluateArgumentValue(Definition $definition, bool $returnResul
98101
{
99102
$actualValue = $definition->getArgument($this->argumentIndex);
100103

104+
if (gettype($actualValue) !== gettype($this->expectedValue)) {
105+
$this->fail(
106+
$definition,
107+
sprintf(
108+
'The value of argument named "%s" (%s) is not equal to the expected value (%s)',
109+
$this->argumentIndex,
110+
$this->exporter->export($actualValue),
111+
$this->exporter->export($this->expectedValue)
112+
)
113+
);
114+
}
115+
101116
$constraint = new IsEqual($this->expectedValue);
102117

103118
if (!$constraint->evaluate($actualValue, '', true)) {
@@ -109,15 +124,15 @@ private function evaluateArgumentValue(Definition $definition, bool $returnResul
109124
$message = sprintf(
110125
'The value of argument named "%s" (%s) is not equal to the expected value (%s)',
111126
$this->argumentIndex,
112-
$this->exporter()->export($actualValue),
113-
$this->exporter()->export($this->expectedValue)
127+
$this->exporter->export($actualValue),
128+
$this->exporter->export($this->expectedValue)
114129
);
115130
} else {
116131
$message = sprintf(
117132
'The value of argument with index %d (%s) is not equal to the expected value (%s)',
118133
$this->argumentIndex,
119-
$this->exporter()->export($actualValue),
120-
$this->exporter()->export($this->expectedValue)
134+
$this->exporter->export($actualValue),
135+
$this->exporter->export($this->expectedValue)
121136
);
122137
}
123138

Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\DependencyInjection\Definition;
88
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
99
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10+
use Symfony\Component\DependencyInjection\Reference;
1011

1112
class MatthiasDependencyInjectionTestExtension implements ExtensionInterface
1213
{
@@ -31,6 +32,11 @@ public function load(array $config, ContainerBuilder $container): void
3132

3233
// add an alias to an existing service
3334
$container->setAlias('manual_alias', 'service_id');
35+
36+
// add a reference to an existing service
37+
$definition = new Definition('manual_with_reference');
38+
$definition->addArgument(new Reference('manual_service_id'));
39+
$container->setDefinition('manual_with_reference', $definition);
3440
}
3541

3642
public function getAlias()

Tests/PhpUnit/AbstractExtensionTestCaseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ public function if_definition_has_argument_but_with_wrong_value_it_fails(): void
178178
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 1, 'wrong value');
179179
}
180180

181+
/**
182+
* @test
183+
*/
184+
public function if_definition_has_argument_but_with_wrong_value_it_fails1(): void
185+
{
186+
$this->load();
187+
188+
$this->expectException(ExpectationFailedException::class);
189+
$this->expectExceptionMessage('The value of argument named "0"');
190+
191+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_with_reference', 0, 'manual_service_id');
192+
}
193+
181194
/**
182195
* @test
183196
*/

0 commit comments

Comments
 (0)