-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContainerServiceCacheTest.php
69 lines (56 loc) · 2.17 KB
/
ContainerServiceCacheTest.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
<?php
namespace PHPWatch\SimpleContainer\Tests;
use PHPWatch\SimpleContainer\Container;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use const PHP_INT_MAX;
use const PHP_INT_MIN;
class ContainerServiceCacheTest extends TestCase {
public function testStandardServiceResolutionInInit(): void {
$container = new Container(
[
'foo' => fn() => bin2hex(random_bytes(16)),
]
);
$value = $container['foo'];
$this->assertIsString($value);
$this->assertSame($value, $container['foo']);
$this->assertSame($value, $container['foo']);
$this->assertSame($value, $container['foo']);
}
public function testStandardServiceResolution(): void {
$container = new Container();
$random = fn() => random_int(PHP_INT_MIN, PHP_INT_MAX);
$container->set('bar', $random);
$value = $container['bar'];
$this->assertIsInt($value);
$this->assertSame($value, $container['bar']);
$this->assertSame($value, $container['bar']);
$container->setFactory('foo', $random);
$container->setFactory('bar');
$this->assertNotSame($container['foo'], $container['foo']);
$this->assertNotSame($container['bar'], $container['bar']);
}
public function testServiceOverrides(): void {
$random = static fn() => random_int(43, PHP_INT_MAX);
$static = 42;
$container = new Container(['foo' => $random]);
$this->assertNotSame($static, $container['foo']);
$container->set('foo', $static);
$this->assertSame($static, $container['foo']);
$this->assertSame($static, $container->get('foo'));
}
public function testCreateFromArray(): void {
$services = [
'database' => [
'dsn' => 'sqlite...'
],
'prefix' => 'Foo',
'csprng' => static function (ContainerInterface $container) {
return $container->get('prefix') . bin2hex(random_bytes(16));
}
];
$container = new Container($services);
$this->assertStringStartsWith('Foo', $container->get('csprng'));
}
}