|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace spec\Knp\DictionaryBundle\Dictionary; |
|
6
|
|
|
|
|
7
|
|
|
use Knp\DictionaryBundle\Dictionary; |
|
8
|
|
|
use Knp\DictionaryBundle\Dictionary\Iterator; |
|
9
|
|
|
use PhpSpec\ObjectBehavior; |
|
10
|
|
|
|
|
11
|
|
|
final class IteratorSpec extends ObjectBehavior |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var bool |
|
15
|
|
|
*/ |
|
16
|
|
|
private $executed; |
|
17
|
|
|
|
|
18
|
|
|
function let() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->executed = false; |
|
21
|
|
|
|
|
22
|
|
|
$this->beConstructedWith('foo', $this->execution()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
function it_is_initializable() |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->shouldHaveType(Iterator::class); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
function it_is_a_dictionary() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->shouldImplement(Dictionary::class); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
function it_supports_some_array_access_functions() |
|
36
|
|
|
{ |
|
37
|
|
|
$dictionary = $this->getWrappedObject(); |
|
38
|
|
|
|
|
39
|
|
|
$this['foo']->shouldBe(0); |
|
40
|
|
|
$this->shouldHaveKey('foo'); |
|
41
|
|
|
|
|
42
|
|
|
$this['foo'] = 'test'; |
|
43
|
|
|
$this['foo']->shouldBe('test'); |
|
44
|
|
|
|
|
45
|
|
|
unset($dictionary['foo']); |
|
46
|
|
|
$this->shouldNotHaveKey('foo'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function it_provides_a_set_of_values() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->getValues()->shouldReturn([ |
|
|
|
|
|
|
52
|
|
|
'foo' => 0, |
|
53
|
|
|
'bar' => 1, |
|
54
|
|
|
'baz' => 2, |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
function it_provides_a_set_of_keys() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->getKeys()->shouldReturn([ |
|
|
|
|
|
|
61
|
|
|
'foo', |
|
62
|
|
|
'bar', |
|
63
|
|
|
'baz', |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
function it_is_hydrated_just_once() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->getValues()->shouldReturn([ |
|
70
|
|
|
'foo' => 0, |
|
71
|
|
|
'bar' => 1, |
|
72
|
|
|
'baz' => 2, |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$this->getValues()->shouldReturn([ |
|
76
|
|
|
'foo' => 0, |
|
77
|
|
|
'bar' => 1, |
|
78
|
|
|
'baz' => 2, |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
function it_access_to_value_like_an_array() |
|
83
|
|
|
{ |
|
84
|
|
|
$this['foo']->shouldReturn(0); |
|
85
|
|
|
$this['bar']->shouldReturn(1); |
|
86
|
|
|
$this['baz']->shouldReturn(2); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
function it_generates_an_iterator() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->shouldIterateLike([ |
|
92
|
|
|
'foo' => 0, |
|
93
|
|
|
'bar' => 1, |
|
94
|
|
|
'baz' => 2, |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
function its_getname_should_return_dictionary_name() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->getName()->shouldReturn('foo'); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function execution(): iterable |
|
104
|
|
|
{ |
|
105
|
|
|
if (!$this->executed) { |
|
106
|
|
|
$this->executed = true; |
|
107
|
|
|
|
|
108
|
|
|
yield 'foo' => 0; |
|
109
|
|
|
|
|
110
|
|
|
yield 'bar' => 1; |
|
111
|
|
|
|
|
112
|
|
|
yield 'baz' => 2; |
|
113
|
|
|
|
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
throw new \Exception('Executed twice.'); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
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.