-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathDefinitionHasArgumentConstraint.php
142 lines (118 loc) · 4.62 KB
/
DefinitionHasArgumentConstraint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Util\Exporter;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
final class DefinitionHasArgumentConstraint extends Constraint
{
/**
* @var int|string
*/
private string|int $argumentIndex;
private mixed $expectedValue;
private bool $checkExpectedValue;
public function __construct($argumentIndex, $expectedValue, bool $checkExpectedValue = true)
{
if (!(is_string($argumentIndex) || (is_int($argumentIndex) && $argumentIndex >= 0))) {
throw new \InvalidArgumentException('Expected either a string or a positive integer for $argumentIndex.');
}
if (is_string($argumentIndex)) {
if ('' === $argumentIndex) {
throw new \InvalidArgumentException('A named argument must begin with a "$".');
}
if ('$' !== $argumentIndex[0]) {
throw new \InvalidArgumentException(
sprintf('Unknown argument "%s". Did you mean "$%s"?', $argumentIndex, $argumentIndex)
);
}
}
$this->argumentIndex = $argumentIndex;
$this->expectedValue = $expectedValue;
$this->checkExpectedValue = $checkExpectedValue;
}
public function toString(): string
{
if (is_string($this->argumentIndex)) {
return sprintf(
'has an argument named "%s" with the given value',
$this->argumentIndex
);
}
return sprintf(
'has an argument with index %d with the given value',
$this->argumentIndex
);
}
public function evaluate($other, string $description = '', bool $returnResult = false): bool
{
if (!($other instanceof Definition)) {
throw new \InvalidArgumentException(
'Expected an instance of Symfony\Component\DependencyInjection\Definition'
);
}
if (!$this->evaluateArgumentIndex($other, $returnResult)) {
return false;
}
if ($this->checkExpectedValue && !$this->evaluateArgumentValue($other, $returnResult)) {
return false;
}
return true;
}
private function evaluateArgumentIndex(Definition $definition, bool $returnResult): bool
{
try {
$definition->getArgument($this->argumentIndex);
} catch (OutOfBoundsException $exception) {
if ($returnResult) {
return false;
}
if (is_string($this->argumentIndex)) {
$message = sprintf('The definition has no argument named "%s"', $this->argumentIndex);
} else {
$message = sprintf('The definition has no argument with index %d', $this->argumentIndex);
}
$this->fail($definition, $message);
}
return true;
}
private function evaluateArgumentValue(Definition $definition, bool $returnResult): bool
{
$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,
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
)
);
}
$constraint = new IsEqual($this->expectedValue);
if (!$constraint->evaluate($actualValue, '', true)) {
if ($returnResult) {
return false;
}
if (is_string($this->argumentIndex)) {
$message = sprintf(
'The value of argument named "%s" (%s) is not equal to the expected value (%s)',
$this->argumentIndex,
Exporter::export($actualValue),
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,
Exporter::export($actualValue),
Exporter::export($this->expectedValue)
);
}
$this->fail($definition, $message);
}
return true;
}
}