generated from yiisoft/package-template
/
InjectorTest.php
225 lines (188 loc) · 6.44 KB
/
InjectorTest.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace Yiisoft\Injector\Tests\Php8;
use ArrayAccess;
use ArrayIterator;
use Countable;
use DateTimeImmutable;
use DateTimeInterface;
use Psr\Container\NotFoundExceptionInterface;
use stdClass;
use Yiisoft\Injector\Injector;
use Yiisoft\Injector\MissingRequiredArgumentException;
use Yiisoft\Injector\Tests\Common\BaseInjectorTest;
use Yiisoft\Injector\Tests\Php8\Support\IntEnum;
use Yiisoft\Injector\Tests\Php8\Support\NonBackedEnum;
use Yiisoft\Injector\Tests\Php8\Support\StrEnum;
use Yiisoft\Injector\Tests\Php8\Support\TimerUnionTypes;
use Yiisoft\Injector\Tests\Php8\Support\TypesIntersection;
use Yiisoft\Injector\Tests\Php8\Support\TypesIntersectionReferencedConstructor;
class InjectorTest extends BaseInjectorTest
{
public function testMakeInternalClass(): void
{
$container = $this->getContainer();
$object = (new Injector($container))->make(\SplFileObject::class, [
'filename' => __FILE__,
// second parameter skipped
// third parameter skipped
'context' => null,
'other-parameter' => true,
]);
$this->assertSame(basename(__FILE__), $object->getFilename());
}
public function testInvokeUnionTypes(): void
{
$time = new DateTimeImmutable();
$container = $this->getContainer([DateTimeInterface::class => $time]);
$object = (new Injector($container))
->make(TimerUnionTypes::class);
$this->assertSame($object->getTime(), $time);
}
/**
* @requires PHP >= 8.1
*/
public function testTypeIntersection(): void
{
$argument = new ArrayIterator();
$container = $this->getContainer();
$object = (new Injector($container))
->make(TypesIntersection::class, [$argument]);
$this->assertSame($argument, $object->collection);
}
/**
* @requires PHP >= 8.1
*/
public function testTypeIntersectionReferenced(): void
{
$obj1 = new ArrayIterator();
$obj2 = new ArrayIterator();
$container = $this->getContainer();
$object = (new Injector($container))
->make(TypesIntersectionReferencedConstructor::class, [&$obj1]);
$object->collection = $obj2;
$this->assertSame($obj1, $obj2);
}
/**
* @requires PHP >= 8.1
*/
public function testTypeIntersectionVariadic(): void
{
$obj1 = new ArrayIterator();
$obj2 = new ArrayIterator();
$obj3 = new ArrayIterator();
$obj4 = new ArrayIterator();
$container = $this->getContainer();
$result = (new Injector($container))
->invoke([new TypesIntersection($obj1), 'getVariadic'], [
new stdClass(),
$obj1,
new stdClass(),
$obj2,
new stdClass(),
$obj3,
new stdClass(),
$obj4,
new stdClass(),
]);
$this->assertSame([$obj1, $obj2, $obj3, $obj4], $result);
}
/**
* @requires PHP >= 8.1
*/
public function testResolveMultiple(): void
{
$obj1 = new ArrayIterator();
$obj2 = new ArrayIterator();
$obj3 = new ArrayIterator();
$obj4 = new ArrayIterator();
$container = $this->getContainer();
$result = (new Injector($container))
->invoke([new TypesIntersection($obj1), 'getMultiple'], [
new stdClass(),
$obj1,
new stdClass(),
$obj2,
new stdClass(),
$obj3,
new stdClass(),
$obj4,
new stdClass(),
]);
$this->assertSame($obj1, $result[0]);
$this->assertSame($obj2, $result[1]);
$this->assertSame($obj3, $result[2]);
$this->assertSame($obj4, $result[3]);
}
/**
* @requires PHP >= 8.1
*/
public function testTypeIntersectionMustNotBePulledFromContainer(): void
{
$collection = new ArrayIterator();
$container = $this->getContainer([
ArrayAccess::class => $collection,
Countable::class => $collection,
]);
$this->expectException(MissingRequiredArgumentException::class);
(new Injector($container))
->make(TypesIntersection::class);
}
/**
* @requires PHP >= 8.1
*/
public function testTypeIntersectionNotResolved(): void
{
$container = $this->getContainer();
$this->expectException(MissingRequiredArgumentException::class);
(new Injector($container))->make(TypesIntersection::class);
}
/**
* @requires PHP >= 8.1
*/
public function testResolveEnumFromContainer(): void
{
$str = $this->createEnumValue(StrEnum::class, 'Bar');
$int = $this->createEnumValue(IntEnum::class, 'Bar');
$nb = $this->createEnumValue(NonBackedEnum::class, 'Bar');
$container = $this->getContainer([
StrEnum::class => $str,
IntEnum::class => $int,
NonBackedEnum::class => $nb,
]);
$result = (new Injector($container))
->invoke(static fn (StrEnum $arg1, IntEnum $arg2, NonBackedEnum $arg3) => [$arg1, $arg2, $arg3]);
$this->assertSame([$str, $int, $nb], $result);
}
/**
* @requires PHP >= 8.1
*/
public function testResolveEnumFromArguments(): void
{
$str = $this->createEnumValue(StrEnum::class, 'Bar');
$int = $this->createEnumValue(IntEnum::class, 'Bar');
$nb = $this->createEnumValue(NonBackedEnum::class, 'Bar');
$container = $this->getContainer();
$result = (new Injector($container))
->invoke(
static fn (StrEnum $arg1, IntEnum $arg2, NonBackedEnum $arg3) => [$arg1, $arg2, $arg3],
[$nb, $int, $str]
);
$this->assertSame([$str, $int, $nb], $result);
}
/**
* @requires PHP >= 8.1
*/
public function testEnumCanNotBeAutowired(): void
{
$container = $this->getContainer();
$this->expectException(NotFoundExceptionInterface::class);
(new Injector($container))
->invoke(static fn (StrEnum $arg1, IntEnum $arg2) => [$arg1, $arg2]);
}
private function createEnumValue(string $enumClass, string $case)
{
$reflection = new \ReflectionEnum($enumClass);
return $reflection->getCase($case)->getValue();
}
}