-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-container.spec.php
More file actions
118 lines (92 loc) 路 3.56 KB
/
Copy patharray-container.spec.php
File metadata and controls
118 lines (92 loc) 路 3.56 KB
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
<?php
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Technically\ArrayContainer\Exceptions\ServiceNotFound;
use Technically\ArrayContainer\ArrayContainer;
describe('ArrayContainer', function() {
it('should instantiate a new instance', function() {
$container = new ArrayContainer();
assert($container instanceof ArrayContainer);
});
it('should implement PSR container interface', function() {
$container = new ArrayContainer();
assert($container instanceof ContainerInterface);
});
it('should instantiate a new instance with array of entries', function() {
$container = new ArrayContainer([
'a' => 'A',
'b' => 'B',
]);
assert($container->has('a'));
assert($container->has('b'));
assert($container->toArray() === [
'a' => 'A',
'b' => 'B',
]);
});
describe('->has()', function() {
it('should return true for defined entries', function () {
$container = new ArrayContainer([
'a' => 'A',
'b' => 'B',
]);
assert($container->has('a') === true);
assert($container->has('b') === true);
});
it('should return false for entries not defined', function () {
$container = new ArrayContainer([
'a' => 'A',
'b' => 'B',
]);
assert($container->has('A') === false);
assert($container->has('B') === false);
});
});
describe('->get()', function() {
it('should return entries by identifier', function () {
$now = new DateTime('now');
$container = new ArrayContainer([
'greeting' => 'Hello',
'date' => $now,
]);
assert($container->get('greeting') === 'Hello');
assert($container->get('date') === $now);
});
it('should throw ServiceNotFound exception for entries not defined', function () {
$container = new ArrayContainer(['a' => 'A']);
assert($container->has('b') === false);
try {
$container->get('b');
} catch (ServiceNotFound $exception) {
// passthru
}
assert(isset($exception));
assert($exception instanceof ServiceNotFound);
assert($exception instanceof NotFoundExceptionInterface);
});
});
describe('->set()', function() {
it('should set entries for identifier', function () {
$now = new DateTime('now');
$container = new ArrayContainer();
assert($container->has('date') === false);
$container->set('date', $now);
assert($container->has('date') === true);
assert($container->get('date') === $now);
});
it('should overwrite previously defined entries for the same identifier', function () {
$container = new ArrayContainer(['a' => 'A']);
assert($container->get('a') === 'A');
$container->set('a', 'B');
assert($container->get('a') === 'B');
});
});
describe('->toArray()', function() {
it('should return all container entries in array', function () {
$container = new ArrayContainer(['a' => 'A']);
assert($container->toArray() === ['a' => 'A']);
$container->set('b', 'B');
assert($container->toArray() === ['a' => 'A', 'b' => 'B']);
});
});
});