/
ContainerTraitTest.php
274 lines (225 loc) · 8.12 KB
/
ContainerTraitTest.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace Atk4\Core\Tests;
use Atk4\Core;
use Atk4\Core\Phpunit\TestCase;
class ContainerTraitTest extends TestCase
{
public function testBasic(): void
{
$m = new ContainerMock();
// add to return object
$tr2 = new \stdClass();
$tr = $m->add($tr2);
static::assertSame($tr, $tr2);
// trackable object can be referenced by name
$tr3 = new TrackableMock();
$m->add($tr3, 'foo');
$tr = $m->getElement('foo');
static::assertSame($tr, $tr3);
}
public function testUniqueNames(): void
{
$m = new ContainerMock();
// two anonymous children should get unique names asigned.
$m->add(new TrackableMock());
$anon = $m->add(new TrackableMock());
$m->add(new TrackableMock(), 'foo bar');
$m->add(new TrackableMock(), '123');
$m->add(new TrackableMock(), 'false');
static::assertTrue($m->hasElement('foo bar'));
static::assertTrue($m->hasElement('123'));
static::assertTrue($m->hasElement('false'));
static::assertSame(5, $m->getElementCount());
$m->getElement('foo bar')->destroy();
static::assertSame(4, $m->getElementCount());
$anon->destroy();
static::assertSame(3, $m->getElementCount());
}
public function testLongNames(): void
{
$app = new ContainerAppMock();
$app->setApp($app);
$app->maxNameLength = 40;
$m = $app->add(new ContainerAppMock(), 'quick-brown-fox');
$m = $m->add(new ContainerAppMock(), 'jumps-over-a-lazy-dog');
$m = $m->add(new ContainerAppMock(), 'then-they-go-out-for-a-pint');
$m = $m->add(new ContainerAppMock(), 'eat-a-stake');
$x = $m->add(new ContainerAppMock(), 'with');
$x = $m->add(new ContainerAppMock(), 'a');
$x = $m->add(new ContainerAppMock(), 'mint');
static::assertSame(
'_quick-brown-fox_jumps-over-a-lazy-dog_then-they-go-out-for-a-pint_eat-a-stake',
$m->unshortenName($this)
);
static::assertLessThan(5, count($app->uniqueNameHashes));
static::assertGreaterThan(2, count($app->uniqueNameHashes));
$m->removeElement($x);
static::assertSame(2, $m->getElementCount());
$m->add(new \stdClass());
static::assertSame(2, $m->getElementCount());
}
public function testLongNames2(): void
{
$app = new ContainerAppMock();
$app->setApp($app);
$app->maxNameLength = 40;
$app->name = 'my-app-name-is-pretty-long';
$minLength = 9999;
$minLengthValue = '';
$maxLength = 0;
$maxLengthValue = '';
for ($x = 1; $x < 100; ++$x) {
$sh = str_repeat('x', $x);
$m = $app->add(new ContainerAppMock(), $sh);
if (strlen($m->name) > $maxLength) {
$maxLength = strlen($m->name);
$maxLengthValue = $m->name;
}
if (strlen($m->name) < $minLength) {
$minLength = strlen($m->name);
$minLengthValue = $m->name;
}
}
// hash is 10 and we want 5 chars minimum for the right side e.g. XYXYXYXY__abcde
static::assertGreaterThanOrEqual(15, $minLength);
// hash is 10 and we want 5 chars minimum for the right side e.g. XYXYXYXY__abcde
static::assertLessThanOrEqual($app->maxNameLength, $maxLength);
}
public function testPreservePresetNames(): void
{
$app = new ContainerAppMock();
$app->setApp($app);
$app->name = 'r';
$app->maxNameLength = 40;
$createTrackableMockFx = function (string $name, bool $isLongName = false) {
return new class($name, $isLongName) extends TrackableMock {
use Core\NameTrait;
public function __construct(string $name, bool $isLongName)
{
if ($isLongName) {
$this->name = $name;
} else {
$this->shortName = $name;
}
}
};
};
static::assertSame('r_foo', $app->add($createTrackableMockFx('foo'))->name);
static::assertSame('r_bar', $app->add($createTrackableMockFx('bar'))->name);
static::assertSame(40, strlen($app->add($createTrackableMockFx(str_repeat('x', 100)))->name));
static::assertSame(40, strlen($app->add($createTrackableMockFx(str_repeat('x', 100)))->name));
static::assertSame('foo', $app->add($createTrackableMockFx('foo', true))->name);
$this->expectException(Core\Exception::class);
static::assertSame(40, strlen($app->add($createTrackableMockFx(str_repeat('x', 100), true))->name));
}
public function testFactoryMock(): void
{
$m = new ContainerFactoryMock();
$m2 = $m->add([ContainerMock::class]);
static::assertSame(ContainerMock::class, get_class($m2));
$m3 = $m->add([TrackableContainerMock::class], 'name');
static::assertSame(TrackableContainerMock::class, get_class($m3));
static::assertSame('name', $m3->shortName);
}
public function testArgs(): void
{
// passing name with array key 'name'
$m = new ContainerMock();
$m2 = $m->add(new class() extends TrackableMock {
use Core\DiContainerTrait;
use Core\NameTrait;
}, ['name' => 'foo']);
static::assertTrue($m->hasElement('foo'));
static::assertSame('foo', $m2->shortName);
}
public function testExceptionExists(): void
{
$this->expectException(Core\Exception::class);
$m = new ContainerMock();
$m->add(new TrackableMock(), 'foo');
$m->add(new TrackableMock(), 'foo');
}
public function testDesiredName(): void
{
$m = new ContainerMock();
$m->add(new TrackableMock(), ['desired_name' => 'foo']);
$m->add(new TrackableMock(), ['desired_name' => 'foo']);
static::assertTrue($m->hasElement('foo'));
}
public function testExceptionShortName(): void
{
$this->expectException(Core\Exception::class);
$m1 = new ContainerMock();
$m2 = new ContainerMock();
$m1foo = $m1->add(new TrackableMock(), 'foo');
$m2foo = $m2->add(new TrackableMock(), 'foo');
// will carry on short name and run into collision.
$m2->add($m1foo);
}
public function testExceptionArg2(): void
{
$m = new ContainerMock();
if (\PHP_MAJOR_VERSION === 7) {
$this->expectWarning();
} else {
$this->expectException(\TypeError::class);
}
$m->add(new TrackableMock(), 123); // @phpstan-ignore-line
}
public function testException3(): void
{
$m = new ContainerMock();
$this->expectException(\Error::class);
$this->expectExceptionMessage(\PHP_MAJOR_VERSION < 8 ? 'Class \'hello\' not found' : 'Class "hello" not found');
$m->add(['hello']);
}
public function testException4(): void
{
$m = new ContainerMock();
$this->expectException(Core\Exception::class);
$m->getElement('dont_exist');
}
public function testException5(): void
{
$this->expectException(Core\Exception::class);
$m = new ContainerMock();
$m->removeElement('dont_exist');
}
}
class TrackableMock
{
use Core\TrackableTrait;
}
class ContainerFactoryMock
{
use Core\ContainerTrait;
use Core\NameTrait;
}
class TrackableContainerMock
{
use Core\ContainerTrait;
use Core\TrackableTrait;
}
class ContainerAppMock
{
use Core\AppScopeTrait;
use Core\ContainerTrait;
use Core\NameTrait;
use Core\TrackableTrait;
public function getElementCount(): int
{
return count($this->elements);
}
public function unshortenName(): string
{
$n = $this->name;
$d = array_flip($this->getApp()->uniqueNameHashes);
for ($x = 0; str_contains($n, '__') && $x < 100; ++$x) {
[$l, $r] = explode('__', $n);
$l = $d[$l];
$n = $l . $r;
}
return $n;
}
}