-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContainerStaticValuesTest.php
86 lines (64 loc) · 2.6 KB
/
ContainerStaticValuesTest.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
<?php
namespace PHPWatch\SimpleContainer\Tests;
use Exception;
use PHPWatch\SimpleContainer\Container;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
class ContainerStaticValuesTest extends TestCase {
public function testStringValues(): void {
$container = new Container();
$value = bin2hex(random_bytes(16));
$container['foo'] = $value;
$this->assertSame($value, $container['foo']);
$container->set('foo', $value);
$this->assertSame($value, $container['foo']);
}
public function testValuesAreOverwritten(): void {
$container = new Container();
$value = bin2hex(random_bytes(16));
$value2 = bin2hex(random_bytes(16));
$container['foo'] = $value;
$this->assertSame($value, $container['foo']);
$container->set('foo', $value2);
$container['foo'] = $value2;
$this->assertSame($value2, $container['foo']);
}
public function testStandardReferenceRules(): void {
$container = new Container();
$value = bin2hex(random_bytes(16));
$object = new stdClass();
$object->bar = $value;
$container['foo-val'] = $value;
$container['foo-obj'] = $object;
$this->assertSame($value, $container['foo-val']);
$this->assertSame($object, $container['foo-obj']);
$val_obtained = $container->get('foo-val');
$obj_obtained = $container->get('foo-obj');
$val_obtained .= 'baz';
$this->assertNotSame($val_obtained, $container['foo-val']);
$obj_obtained->bar .= 'baz';
$this->assertSame($obj_obtained, $container['foo-obj']);
$obj_obtained_new = $container['foo-obj'];
unset($obj_obtained_new); // Delete the reference
$this->assertSame($object, $container->get('foo-obj'));
}
public function testIssetWorks(): void {
$container = new Container(['foo' => 1, 'bar' => null]);
$this->assertTrue(isset($container['foo']));
$this->assertTrue(isset($container['bar'])); // Evaluate null as exists.
$this->assertTrue($container->has('bar'));
$this->assertFalse(isset($container['xyz']));
$container['xyz'] = false;
$this->assertTrue(isset($container['xyz']));
}
public function testIssetDoesNotExecute(): void {
$container = new Container();
$container->set('kill', static function() {
throw new RuntimeException('Must not execute');
});
$this->assertTrue(isset($container['kill']));
$this->expectException(Exception::class);
$container->get('kill');
}
}