1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace spec\Knp\DictionaryBundle\Dictionary; |
6
|
|
|
|
7
|
|
|
use Knp\DictionaryBundle\DataCollector\DictionaryDataCollector; |
8
|
|
|
use Knp\DictionaryBundle\Dictionary; |
9
|
|
|
use Knp\DictionaryBundle\Dictionary\Simple; |
10
|
|
|
use Knp\DictionaryBundle\Dictionary\Traceable; |
11
|
|
|
use PhpSpec\ObjectBehavior; |
12
|
|
|
use Webmozart\Assert\Assert; |
13
|
|
|
|
14
|
|
|
final class TraceableSpec extends ObjectBehavior |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DictionaryDataCollector |
18
|
|
|
*/ |
19
|
|
|
private $collector; |
20
|
|
|
|
21
|
|
|
function let() |
22
|
|
|
{ |
23
|
|
|
$dictionary = new Simple('name', [ |
24
|
|
|
'foo' => 'bar', |
25
|
|
|
'baz' => null, |
26
|
|
|
]); |
27
|
|
|
|
28
|
|
|
$this->collector = new DictionaryDataCollector(); |
29
|
|
|
|
30
|
|
|
$this->beConstructedWith($dictionary, $this->collector); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function it_is_initializable() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->shouldHaveType(Traceable::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function it_is_a_dictionary() |
39
|
|
|
{ |
40
|
|
|
$this->shouldImplement(Dictionary::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function it_has_a_name() |
44
|
|
|
{ |
45
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
46
|
|
|
|
47
|
|
|
$this->getName()->shouldReturn('name'); |
|
|
|
|
48
|
|
|
|
49
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function it_traces_keys() |
53
|
|
|
{ |
54
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
55
|
|
|
|
56
|
|
|
$this->getKeys()->shouldReturn(['foo', 'baz']); |
|
|
|
|
57
|
|
|
|
58
|
|
|
Assert::eq( |
59
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
60
|
|
|
[ |
61
|
|
|
'name' => [ |
62
|
|
|
[ |
63
|
|
|
'key' => 'foo', |
64
|
|
|
'value' => 'bar', |
65
|
|
|
], |
66
|
|
|
[ |
67
|
|
|
'key' => 'baz', |
68
|
|
|
'value' => null, |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function it_traces_values() |
76
|
|
|
{ |
77
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
78
|
|
|
|
79
|
|
|
$this->getValues()->shouldReturn(['foo' => 'bar', 'baz' => null]); |
|
|
|
|
80
|
|
|
|
81
|
|
|
Assert::eq( |
82
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
83
|
|
|
[ |
84
|
|
|
'name' => [ |
85
|
|
|
[ |
86
|
|
|
'key' => 'foo', |
87
|
|
|
'value' => 'bar', |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
'key' => 'baz', |
91
|
|
|
'value' => null, |
92
|
|
|
], |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
function it_traces_values_get() |
99
|
|
|
{ |
100
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
101
|
|
|
|
102
|
|
|
$this['foo']->shouldReturn('bar'); |
103
|
|
|
|
104
|
|
|
Assert::eq( |
105
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
106
|
|
|
[ |
107
|
|
|
'name' => [ |
108
|
|
|
[ |
109
|
|
|
'key' => 'foo', |
110
|
|
|
'value' => 'bar', |
111
|
|
|
], |
112
|
|
|
[ |
113
|
|
|
'key' => 'baz', |
114
|
|
|
'value' => null, |
115
|
|
|
], |
116
|
|
|
], |
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
function it_traces_value_set() |
122
|
|
|
{ |
123
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
124
|
|
|
|
125
|
|
|
$this['yo'] = 'lo'; |
126
|
|
|
|
127
|
|
|
Assert::eq( |
128
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
129
|
|
|
[ |
130
|
|
|
'name' => [ |
131
|
|
|
[ |
132
|
|
|
'key' => 'foo', |
133
|
|
|
'value' => 'bar', |
134
|
|
|
], |
135
|
|
|
[ |
136
|
|
|
'key' => 'baz', |
137
|
|
|
'value' => null, |
138
|
|
|
], |
139
|
|
|
[ |
140
|
|
|
'key' => 'yo', |
141
|
|
|
'value' => 'lo', |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
] |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
function it_traces_value_unset() |
149
|
|
|
{ |
150
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
151
|
|
|
|
152
|
|
|
unset($this['foo']); |
153
|
|
|
|
154
|
|
|
Assert::eq( |
155
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
156
|
|
|
[ |
157
|
|
|
'name' => [ |
158
|
|
|
[ |
159
|
|
|
'key' => 'baz', |
160
|
|
|
'value' => null, |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
function it_traces_key_exists() |
168
|
|
|
{ |
169
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
170
|
|
|
|
171
|
|
|
$this->shouldHaveKey('baz'); |
172
|
|
|
|
173
|
|
|
Assert::eq( |
174
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
175
|
|
|
[ |
176
|
|
|
'name' => [ |
177
|
|
|
[ |
178
|
|
|
'key' => 'foo', |
179
|
|
|
'value' => 'bar', |
180
|
|
|
], |
181
|
|
|
[ |
182
|
|
|
'key' => 'baz', |
183
|
|
|
'value' => null, |
184
|
|
|
], |
185
|
|
|
], |
186
|
|
|
] |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
function it_trace_iteration() |
191
|
|
|
{ |
192
|
|
|
Assert::isEmpty(iterator_to_array($this->collector->getDictionaries())); |
193
|
|
|
|
194
|
|
|
$this->shouldIterateLike([ |
195
|
|
|
'foo' => 'bar', |
196
|
|
|
'baz' => null, |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
Assert::eq( |
200
|
|
|
iterator_to_array($this->collector->getDictionaries()), |
201
|
|
|
[ |
202
|
|
|
'name' => [ |
203
|
|
|
[ |
204
|
|
|
'key' => 'foo', |
205
|
|
|
'value' => 'bar', |
206
|
|
|
], |
207
|
|
|
[ |
208
|
|
|
'key' => 'baz', |
209
|
|
|
'value' => null, |
210
|
|
|
], |
211
|
|
|
], |
212
|
|
|
] |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
function it_delegates_the_count_to_the_other_dictionary() |
217
|
|
|
{ |
218
|
|
|
$this->count()->shouldReturn(2); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.