Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace spec\Prophecy\Argument\Token;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpSpec\ObjectBehavior;
|
Chris@0
|
6 use Prophecy\Argument;
|
Chris@0
|
7 use Prophecy\Argument\Token\ExactValueToken;
|
Chris@0
|
8 use Prophecy\Argument\Token\TokenInterface;
|
Chris@0
|
9 use Prophecy\Exception\InvalidArgumentException;
|
Chris@0
|
10
|
Chris@0
|
11 class ArrayEntryTokenSpec extends ObjectBehavior
|
Chris@0
|
12 {
|
Chris@0
|
13 function let(TokenInterface $key, TokenInterface $value)
|
Chris@0
|
14 {
|
Chris@0
|
15 $this->beConstructedWith($key, $value);
|
Chris@0
|
16 }
|
Chris@0
|
17
|
Chris@0
|
18 function it_implements_TokenInterface()
|
Chris@0
|
19 {
|
Chris@0
|
20 $this->shouldBeAnInstanceOf('Prophecy\Argument\Token\TokenInterface');
|
Chris@0
|
21 }
|
Chris@0
|
22
|
Chris@0
|
23 function it_is_not_last()
|
Chris@0
|
24 {
|
Chris@0
|
25 $this->shouldNotBeLast();
|
Chris@0
|
26 }
|
Chris@0
|
27
|
Chris@0
|
28 function it_holds_key_and_value($key, $value)
|
Chris@0
|
29 {
|
Chris@0
|
30 $this->getKey()->shouldBe($key);
|
Chris@0
|
31 $this->getValue()->shouldBe($value);
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 function its_string_representation_tells_that_its_an_array_containing_the_key_value_pair($key, $value)
|
Chris@0
|
35 {
|
Chris@0
|
36 $key->__toString()->willReturn('key');
|
Chris@0
|
37 $value->__toString()->willReturn('value');
|
Chris@0
|
38 $this->__toString()->shouldBe('[..., key => value, ...]');
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 function it_wraps_non_token_value_into_ExactValueToken(TokenInterface $key, \stdClass $object)
|
Chris@0
|
42 {
|
Chris@0
|
43 $this->beConstructedWith($key, $object);
|
Chris@0
|
44 $this->getValue()->shouldHaveType('\Prophecy\Argument\Token\ExactValueToken');
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 function it_wraps_non_token_key_into_ExactValueToken(\stdClass $object, TokenInterface $value)
|
Chris@0
|
48 {
|
Chris@0
|
49 $this->beConstructedWith($object, $value);
|
Chris@0
|
50 $this->getKey()->shouldHaveType('\Prophecy\Argument\Token\ExactValueToken');
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 function it_scores_array_half_of_combined_scores_from_key_and_value_tokens($key, $value)
|
Chris@0
|
54 {
|
Chris@0
|
55 $key->scoreArgument('key')->willReturn(4);
|
Chris@0
|
56 $value->scoreArgument('value')->willReturn(6);
|
Chris@0
|
57 $this->scoreArgument(array('key'=>'value'))->shouldBe(5);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 function it_scores_traversable_object_half_of_combined_scores_from_key_and_value_tokens(
|
Chris@0
|
61 TokenInterface $key,
|
Chris@0
|
62 TokenInterface $value,
|
Chris@0
|
63 \Iterator $object
|
Chris@0
|
64 ) {
|
Chris@0
|
65 $object->current()->will(function () use ($object) {
|
Chris@0
|
66 $object->valid()->willReturn(false);
|
Chris@0
|
67
|
Chris@0
|
68 return 'value';
|
Chris@0
|
69 });
|
Chris@0
|
70 $object->key()->willReturn('key');
|
Chris@0
|
71 $object->rewind()->willReturn(null);
|
Chris@0
|
72 $object->next()->willReturn(null);
|
Chris@0
|
73 $object->valid()->willReturn(true);
|
Chris@0
|
74 $key->scoreArgument('key')->willReturn(6);
|
Chris@0
|
75 $value->scoreArgument('value')->willReturn(2);
|
Chris@0
|
76 $this->scoreArgument($object)->shouldBe(4);
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 function it_throws_exception_during_scoring_of_array_accessible_object_if_key_is_not_ExactValueToken(
|
Chris@0
|
80 TokenInterface $key,
|
Chris@0
|
81 TokenInterface $value,
|
Chris@0
|
82 \ArrayAccess $object
|
Chris@0
|
83 ) {
|
Chris@0
|
84 $key->__toString()->willReturn('any_token');
|
Chris@0
|
85 $this->beConstructedWith($key,$value);
|
Chris@0
|
86 $errorMessage = 'You can only use exact value tokens to match key of ArrayAccess object'.PHP_EOL.
|
Chris@0
|
87 'But you used `any_token`.';
|
Chris@0
|
88 $this->shouldThrow(new InvalidArgumentException($errorMessage))->duringScoreArgument($object);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 function it_scores_array_accessible_object_half_of_combined_scores_from_key_and_value_tokens(
|
Chris@0
|
92 ExactValueToken $key,
|
Chris@0
|
93 TokenInterface $value,
|
Chris@0
|
94 \ArrayAccess $object
|
Chris@0
|
95 ) {
|
Chris@0
|
96 $object->offsetExists('key')->willReturn(true);
|
Chris@0
|
97 $object->offsetGet('key')->willReturn('value');
|
Chris@0
|
98 $key->getValue()->willReturn('key');
|
Chris@0
|
99 $key->scoreArgument('key')->willReturn(3);
|
Chris@0
|
100 $value->scoreArgument('value')->willReturn(1);
|
Chris@0
|
101 $this->scoreArgument($object)->shouldBe(2);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 function it_accepts_any_key_token_type_to_score_object_that_is_both_traversable_and_array_accessible(
|
Chris@0
|
105 TokenInterface $key,
|
Chris@0
|
106 TokenInterface $value,
|
Chris@0
|
107 \ArrayIterator $object
|
Chris@0
|
108 ) {
|
Chris@0
|
109 $this->beConstructedWith($key, $value);
|
Chris@0
|
110 $object->current()->will(function () use ($object) {
|
Chris@0
|
111 $object->valid()->willReturn(false);
|
Chris@0
|
112
|
Chris@0
|
113 return 'value';
|
Chris@0
|
114 });
|
Chris@0
|
115 $object->key()->willReturn('key');
|
Chris@0
|
116 $object->rewind()->willReturn(null);
|
Chris@0
|
117 $object->next()->willReturn(null);
|
Chris@0
|
118 $object->valid()->willReturn(true);
|
Chris@0
|
119 $this->shouldNotThrow(new InvalidArgumentException)->duringScoreArgument($object);
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 function it_does_not_score_if_argument_is_neither_array_nor_traversable_nor_array_accessible()
|
Chris@0
|
123 {
|
Chris@0
|
124 $this->scoreArgument('string')->shouldBe(false);
|
Chris@0
|
125 $this->scoreArgument(new \stdClass)->shouldBe(false);
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 function it_does_not_score_empty_array()
|
Chris@0
|
129 {
|
Chris@0
|
130 $this->scoreArgument(array())->shouldBe(false);
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 function it_does_not_score_array_if_key_and_value_tokens_do_not_score_same_entry($key, $value)
|
Chris@0
|
134 {
|
Chris@0
|
135 $argument = array(1 => 'foo', 2 => 'bar');
|
Chris@0
|
136 $key->scoreArgument(1)->willReturn(true);
|
Chris@0
|
137 $key->scoreArgument(2)->willReturn(false);
|
Chris@0
|
138 $value->scoreArgument('foo')->willReturn(false);
|
Chris@0
|
139 $value->scoreArgument('bar')->willReturn(true);
|
Chris@0
|
140 $this->scoreArgument($argument)->shouldBe(false);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 function it_does_not_score_traversable_object_without_entries(\Iterator $object)
|
Chris@0
|
144 {
|
Chris@0
|
145 $object->rewind()->willReturn(null);
|
Chris@0
|
146 $object->next()->willReturn(null);
|
Chris@0
|
147 $object->valid()->willReturn(false);
|
Chris@0
|
148 $this->scoreArgument($object)->shouldBe(false);
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 function it_does_not_score_traversable_object_if_key_and_value_tokens_do_not_score_same_entry(
|
Chris@0
|
152 TokenInterface $key,
|
Chris@0
|
153 TokenInterface $value,
|
Chris@0
|
154 \Iterator $object
|
Chris@0
|
155 ) {
|
Chris@0
|
156 $object->current()->willReturn('foo');
|
Chris@0
|
157 $object->current()->will(function () use ($object) {
|
Chris@0
|
158 $object->valid()->willReturn(false);
|
Chris@0
|
159
|
Chris@0
|
160 return 'bar';
|
Chris@0
|
161 });
|
Chris@0
|
162 $object->key()->willReturn(1);
|
Chris@0
|
163 $object->key()->willReturn(2);
|
Chris@0
|
164 $object->rewind()->willReturn(null);
|
Chris@0
|
165 $object->next()->willReturn(null);
|
Chris@0
|
166 $object->valid()->willReturn(true);
|
Chris@0
|
167 $key->scoreArgument(1)->willReturn(true);
|
Chris@0
|
168 $key->scoreArgument(2)->willReturn(false);
|
Chris@0
|
169 $value->scoreArgument('foo')->willReturn(false);
|
Chris@0
|
170 $value->scoreArgument('bar')->willReturn(true);
|
Chris@0
|
171 $this->scoreArgument($object)->shouldBe(false);
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 function it_does_not_score_array_accessible_object_if_it_has_no_offset_with_key_token_value(
|
Chris@0
|
175 ExactValueToken $key,
|
Chris@0
|
176 \ArrayAccess $object
|
Chris@0
|
177 ) {
|
Chris@0
|
178 $object->offsetExists('key')->willReturn(false);
|
Chris@0
|
179 $key->getValue()->willReturn('key');
|
Chris@0
|
180 $this->scoreArgument($object)->shouldBe(false);
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 function it_does_not_score_array_accessible_object_if_key_and_value_tokens_do_not_score_same_entry(
|
Chris@0
|
184 ExactValueToken $key,
|
Chris@0
|
185 TokenInterface $value,
|
Chris@0
|
186 \ArrayAccess $object
|
Chris@0
|
187 ) {
|
Chris@0
|
188 $object->offsetExists('key')->willReturn(true);
|
Chris@0
|
189 $object->offsetGet('key')->willReturn('value');
|
Chris@0
|
190 $key->getValue()->willReturn('key');
|
Chris@0
|
191 $value->scoreArgument('value')->willReturn(false);
|
Chris@0
|
192 $key->scoreArgument('key')->willReturn(true);
|
Chris@0
|
193 $this->scoreArgument($object)->shouldBe(false);
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 function its_score_is_capped_at_8($key, $value)
|
Chris@0
|
197 {
|
Chris@0
|
198 $key->scoreArgument('key')->willReturn(10);
|
Chris@0
|
199 $value->scoreArgument('value')->willReturn(10);
|
Chris@0
|
200 $this->scoreArgument(array('key'=>'value'))->shouldBe(8);
|
Chris@0
|
201 }
|
Chris@0
|
202 }
|