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