|
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\Invokable; |
|
9
|
|
|
use PhpSpec\ObjectBehavior; |
|
10
|
|
|
|
|
11
|
|
|
final class InvokableSpec extends ObjectBehavior |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var bool |
|
15
|
|
|
*/ |
|
16
|
|
|
private $executed; |
|
17
|
|
|
|
|
18
|
|
|
function let() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->executed = false; |
|
21
|
|
|
|
|
22
|
|
|
$context = $this; |
|
23
|
|
|
|
|
24
|
|
|
$this->beConstructedWith('foo', fn () => $context->execution()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
function it_is_initializable() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$this->shouldHaveType(Invokable::class); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function it_is_a_dictionary() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->shouldImplement(Dictionary::class); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function it_supports_callable_arguments() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->beConstructedWith( |
|
40
|
|
|
'baz', |
|
41
|
|
|
fn () => $this->execution(...\func_get_args()), |
|
42
|
|
|
[['foo' => 2, 'bar' => 1, 'baz' => 0]] |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$this->getValues()->shouldReturn(['foo' => 2, 'bar' => 1, 'baz' => 0]); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
function it_supports_some_array_access_functions() |
|
49
|
|
|
{ |
|
50
|
|
|
$dictionary = $this->getWrappedObject(); |
|
51
|
|
|
|
|
52
|
|
|
$this['foo']->shouldBe(0); |
|
53
|
|
|
$this->shouldHaveKey('foo'); |
|
54
|
|
|
|
|
55
|
|
|
$this['foo'] = 'test'; |
|
56
|
|
|
$this['foo']->shouldBe('test'); |
|
57
|
|
|
|
|
58
|
|
|
unset($dictionary['foo']); |
|
59
|
|
|
$this->shouldNotHaveKey('foo'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
function it_provides_a_set_of_values() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->getValues()->shouldReturn([ |
|
65
|
|
|
'foo' => 0, |
|
66
|
|
|
'bar' => 1, |
|
67
|
|
|
'baz' => 2, |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function it_provides_a_set_of_keys() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->getKeys()->shouldReturn([ |
|
|
|
|
|
|
74
|
|
|
'foo', |
|
75
|
|
|
'bar', |
|
76
|
|
|
'baz', |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
function it_is_hydrated_just_once() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->getValues()->shouldReturn([ |
|
83
|
|
|
'foo' => 0, |
|
84
|
|
|
'bar' => 1, |
|
85
|
|
|
'baz' => 2, |
|
86
|
|
|
]); |
|
87
|
|
|
|
|
88
|
|
|
$this->getValues()->shouldReturn([ |
|
89
|
|
|
'foo' => 0, |
|
90
|
|
|
'bar' => 1, |
|
91
|
|
|
'baz' => 2, |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
function it_access_to_value_like_an_array() |
|
96
|
|
|
{ |
|
97
|
|
|
$this['foo']->shouldReturn(0); |
|
98
|
|
|
$this['bar']->shouldReturn(1); |
|
99
|
|
|
$this['baz']->shouldReturn(2); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
function it_throws_an_exception_if_callable_returns_somthing_else_than_an_array_or_an_array_access($nothing) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
$this->beConstructedWith('foo', function (): void {}); |
|
105
|
|
|
|
|
106
|
|
|
$this |
|
107
|
|
|
->shouldThrow(new \InvalidArgumentException('Dictionary callable must return an array or an instance of ArrayAccess.')) |
|
108
|
|
|
->duringGetValues() |
|
109
|
|
|
; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
function it_generates_an_iterator() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->shouldIterateLike([ |
|
115
|
|
|
'foo' => 0, |
|
116
|
|
|
'bar' => 1, |
|
117
|
|
|
'baz' => 2, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
function its_getname_should_return_dictionary_name() |
|
122
|
|
|
{ |
|
123
|
|
|
$this->getName()->shouldReturn('foo'); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
function it_returns_the_count_of_elements() |
|
127
|
|
|
{ |
|
128
|
|
|
$this->count()->shouldReturn(3); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
private function execution() |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->executed) { |
|
134
|
|
|
$this->executed = true; |
|
135
|
|
|
$args = \func_get_args(); |
|
136
|
|
|
|
|
137
|
|
|
return !empty($args) ? current($args) : [ |
|
138
|
|
|
'foo' => 0, |
|
139
|
|
|
'bar' => 1, |
|
140
|
|
|
'baz' => 2, |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
throw new \Exception('Executed twice.'); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
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.