generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ParameterDefinitionTest.php
197 lines (166 loc) · 6.41 KB
/
ParameterDefinitionTest.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
declare(strict_types=1);
namespace Yiisoft\Definitions\Tests\Php8;
use PHPUnit\Framework\TestCase;
use Closure;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionClass;
use ReflectionFunction;
use ReflectionParameter;
use RuntimeException;
use stdClass;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Exception\NotInstantiableException;
use Yiisoft\Definitions\ParameterDefinition;
use Yiisoft\Definitions\Tests\Support\CircularReferenceExceptionDependency;
use Yiisoft\Definitions\Tests\Support\GearBox;
use Yiisoft\Definitions\Tests\Support\RuntimeExceptionDependency;
use Yiisoft\Definitions\Tests\Support\UnionBuiltinDependency;
use Yiisoft\Definitions\Tests\Support\UnionCar;
use Yiisoft\Definitions\Tests\Support\UnionOptionalDependency;
use Yiisoft\Definitions\Tests\Support\UnionSelfDependency;
use Yiisoft\Test\Support\Container\Exception\NotFoundException;
use Yiisoft\Test\Support\Container\SimpleContainer;
final class ParameterDefinitionTest extends TestCase
{
public function testResolveUnionType(): void
{
$container = new SimpleContainer([
stdClass::class => new stdClass(),
]);
$definition = new ParameterDefinition(
$this->getFirstParameter(fn (GearBox|stdClass $class) => true)
);
$result = $definition->resolve($container);
$this->assertInstanceOf(stdClass::class, $result);
}
public function testNotInstantiable(): void
{
$definition = new ParameterDefinition(
(new ReflectionClass(UnionCar::class))
->getConstructor()
->getParameters()[0]
);
$container = new SimpleContainer();
$this->expectException(NotFoundExceptionInterface::class);
$definition->resolve($container);
}
public function testResolveRequiredUnionTypeWithIncorrectTypeInContainer(): void
{
$class = GearBox::class . '|' . stdClass::class;
$definition = new ParameterDefinition(
$this->getFirstParameter(fn (GearBox|stdClass $class) => true)
);
$container = new SimpleContainer([
GearBox::class => 7,
stdClass::class => new stdClass(),
]);
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage(
'Container returned incorrect type "integer" for service "' . $class . '".'
);
$definition->resolve($container);
}
public function testResolveOptionalUnionTypeWithIncorrectTypeInContainer(): void
{
$this->markTestSkipped(
'Is there a real case?'
);
$class = stdClass::class . '|' . GearBox::class;
$definition = new ParameterDefinition($this->getFirstParameter(fn (stdClass|GearBox $class) => true));
$container = new SimpleContainer([
stdClass::class => 42,
GearBox::class => 7,
]);
$result = $definition->resolve($container);
$this->assertNull($result);
}
public function testResolveOptionalUnionType(): void
{
$definition = new ParameterDefinition(
$this->getFirstConstructorParameter(UnionOptionalDependency::class)
);
$container = new SimpleContainer();
$this->assertNull($definition->resolve($container));
}
public function testResolveUnionBuiltin(): void
{
$definition = new ParameterDefinition(
$this->getFirstConstructorParameter(UnionBuiltinDependency::class)
);
$container = new SimpleContainer();
$this->expectException(NotInstantiableException::class);
$this->expectExceptionMessage(
'Can not determine value of the "value" parameter of type "string|int" when instantiating '
);
$definition->resolve($container);
}
public function testResolveUnionSelf(): void
{
$definition = new ParameterDefinition(
$this->getFirstConstructorParameter(UnionSelfDependency::class)
);
$container = new SimpleContainer();
$this->expectException(NotFoundException::class);
$definition->resolve($container);
}
public function testResolveOptionalBrokenDependencyWithUnionTypes(): void
{
$container = new SimpleContainer(
[],
static function (string $id) {
if ($id === RuntimeExceptionDependency::class) {
return new RuntimeExceptionDependency();
}
throw new NotFoundException($id);
},
static function (string $id): bool {
return $id === RuntimeExceptionDependency::class;
}
);
$definition = new ParameterDefinition(
$this->getFirstParameter(static fn (RuntimeExceptionDependency|string|null $d = null) => 42),
);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Broken.');
$definition->resolve($container);
}
public function testResolveOptionalCircularDependencyWithUnionTypes(): void
{
$container = new SimpleContainer(
[],
static function (string $id) {
if ($id === CircularReferenceExceptionDependency::class) {
return new CircularReferenceExceptionDependency();
}
throw new NotFoundException($id);
},
static function (string $id): bool {
return $id === CircularReferenceExceptionDependency::class;
}
);
$definition = new ParameterDefinition(
$this->getFirstParameter(static fn (CircularReferenceExceptionDependency|string|null $d = null) => 42),
);
$result = $definition->resolve($container);
$this->assertNull($result);
}
/**
* @return ReflectionParameter[]
*/
private function getParameters(callable $callable): array
{
$closure = $callable instanceof Closure ? $callable : Closure::fromCallable($callable);
return (new ReflectionFunction($closure))->getParameters();
}
private function getFirstParameter(Closure $closure): ReflectionParameter
{
return $this->getParameters($closure)[0];
}
private function getFirstConstructorParameter(string $class): ReflectionParameter
{
return (new ReflectionClass($class))
->getConstructor()
->getParameters()[0];
}
}