Skip to content

Commit 16aea91

Browse files
committed
PHPLIB-292: Apply type map to offsetGet and throw exceptions for mutate methods in TypeMapArrayIterator
1 parent 4097115 commit 16aea91

File tree

2 files changed

+349
-0
lines changed

2 files changed

+349
-0
lines changed

src/Model/TypeMapArrayIterator.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Model;
1919

2020
use ArrayIterator;
21+
use MongoDB\Exception\BadMethodCallException;
2122

2223
/**
2324
* Iterator for applying a type map to documents in inline command results.
@@ -46,6 +47,16 @@ public function __construct(array $documents = [], array $typeMap)
4647
$this->typeMap = $typeMap;
4748
}
4849

50+
public function append($value)
51+
{
52+
throw BadMethodCallException::classIsImmutable(__CLASS__);
53+
}
54+
55+
public function asort()
56+
{
57+
throw BadMethodCallException::classIsImmutable(__CLASS__);
58+
}
59+
4960
/**
5061
* Return the current element with the type map applied to it.
5162
*
@@ -56,4 +67,50 @@ public function current()
5667
{
5768
return \MongoDB\apply_type_map_to_document(parent::current(), $this->typeMap);
5869
}
70+
71+
public function ksort()
72+
{
73+
throw BadMethodCallException::classIsImmutable(__CLASS__);
74+
}
75+
76+
public function natcasesort()
77+
{
78+
throw BadMethodCallException::classIsImmutable(__CLASS__);
79+
}
80+
81+
public function natsort()
82+
{
83+
throw BadMethodCallException::classIsImmutable(__CLASS__);
84+
}
85+
86+
/**
87+
* Return the value from the provided offset with the type map applied.
88+
*
89+
* @see http://php.net/arrayiterator.offsetget
90+
* @return array|object
91+
*/
92+
public function offsetGet($offset)
93+
{
94+
return \MongoDB\apply_type_map_to_document(parent::offsetGet($offset), $this->typeMap);
95+
}
96+
97+
public function offsetSet($index, $newval)
98+
{
99+
throw BadMethodCallException::classIsImmutable(__CLASS__);
100+
}
101+
102+
public function offsetUnset($index)
103+
{
104+
throw BadMethodCallException::classIsImmutable(__CLASS__);
105+
}
106+
107+
public function uasort($cmp_function)
108+
{
109+
throw BadMethodCallException::classIsImmutable(__CLASS__);
110+
}
111+
112+
public function uksort($cmp_function)
113+
{
114+
throw BadMethodCallException::classIsImmutable(__CLASS__);
115+
}
59116
}

tests/Model/TypeMapArrayIteratorTest.php

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\Model;
44

5+
use MongoDB\Exception\BadMethodCallException;
56
use MongoDB\Model\TypeMapArrayIterator;
67
use MongoDB\Tests\TestCase;
78

@@ -31,4 +32,295 @@ public function testCurrentAppliesTypeMap()
3132

3233
$this->assertEquals($expectedDocument, $iterator->current());
3334
}
35+
36+
public function testOffsetGetAppliesTypeMap()
37+
{
38+
$document = [
39+
'array' => [1, 2, 3],
40+
'object' => ['foo' => 'bar'],
41+
];
42+
43+
$typeMap = [
44+
'root' => 'object',
45+
'document' => 'object',
46+
'array' => 'array',
47+
];
48+
49+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
50+
51+
$expectedDocument = (object) [
52+
'array' => [1, 2, 3],
53+
'object' => (object) ['foo' => 'bar'],
54+
];
55+
56+
$iterator->rewind();
57+
58+
$this->assertEquals($expectedDocument, $iterator->offsetGet(0));
59+
}
60+
61+
/**
62+
* @expectedException MongoDB\Exception\BadMethodCallException
63+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
64+
*/
65+
public function testAppendThrowsException()
66+
{
67+
$document = [
68+
'array' => [1, 2, 3],
69+
'object' => ['foo' => 'bar'],
70+
];
71+
72+
$typeMap = [
73+
'root' => 'object',
74+
'document' => 'object',
75+
'array' => 'array',
76+
];
77+
78+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
79+
80+
$expectedDocument = (object) [
81+
'array' => [1, 2, 3],
82+
'object' => (object) ['foo' => 'bar'],
83+
];
84+
85+
$iterator->rewind();
86+
87+
$iterator->asort();
88+
89+
}
90+
91+
/**
92+
* @expectedException MongoDB\Exception\BadMethodCallException
93+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
94+
*/
95+
public function testAsortThrowsException()
96+
{
97+
$document = [
98+
'array' => [1, 2, 3],
99+
'object' => ['foo' => 'bar'],
100+
];
101+
102+
$typeMap = [
103+
'root' => 'object',
104+
'document' => 'object',
105+
'array' => 'array',
106+
];
107+
108+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
109+
110+
$expectedDocument = (object) [
111+
'array' => [1, 2, 3],
112+
'object' => (object) ['foo' => 'bar'],
113+
];
114+
115+
$iterator->rewind();
116+
117+
$iterator->asort();
118+
}
119+
120+
/**
121+
* @expectedException MongoDB\Exception\BadMethodCallException
122+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
123+
*/
124+
public function testKsortThrowsException()
125+
{
126+
$document = [
127+
'array' => [1, 2, 3],
128+
'object' => ['foo' => 'bar'],
129+
];
130+
131+
$typeMap = [
132+
'root' => 'object',
133+
'document' => 'object',
134+
'array' => 'array',
135+
];
136+
137+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
138+
139+
$expectedDocument = (object) [
140+
'array' => [1, 2, 3],
141+
'object' => (object) ['foo' => 'bar'],
142+
];
143+
144+
$iterator->rewind();
145+
146+
$iterator->ksort();
147+
}
148+
149+
/**
150+
* @expectedException MongoDB\Exception\BadMethodCallException
151+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
152+
*/
153+
public function testNatcasessortThrowsException()
154+
{
155+
$document = [
156+
'array' => [1, 2, 3],
157+
'object' => ['foo' => 'bar'],
158+
];
159+
160+
$typeMap = [
161+
'root' => 'object',
162+
'document' => 'object',
163+
'array' => 'array',
164+
];
165+
166+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
167+
168+
$expectedDocument = (object) [
169+
'array' => [1, 2, 3],
170+
'object' => (object) ['foo' => 'bar'],
171+
];
172+
173+
$iterator->rewind();
174+
175+
$iterator->natcasesort();
176+
}
177+
178+
/**
179+
* @expectedException MongoDB\Exception\BadMethodCallException
180+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
181+
*/
182+
public function testNatsortThrowsException()
183+
{
184+
$document = [
185+
'array' => [1, 2, 3],
186+
'object' => ['foo' => 'bar'],
187+
];
188+
189+
$typeMap = [
190+
'root' => 'object',
191+
'document' => 'object',
192+
'array' => 'array',
193+
];
194+
195+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
196+
197+
$expectedDocument = (object) [
198+
'array' => [1, 2, 3],
199+
'object' => (object) ['foo' => 'bar'],
200+
];
201+
202+
$iterator->rewind();
203+
204+
$iterator->natsort();
205+
}
206+
207+
/**
208+
* @expectedException MongoDB\Exception\BadMethodCallException
209+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
210+
*/
211+
public function testOffsetSetThrowsException()
212+
{
213+
$document = [
214+
'array' => [1, 2, 3],
215+
'object' => ['foo' => 'bar'],
216+
];
217+
218+
$typeMap = [
219+
'root' => 'object',
220+
'document' => 'object',
221+
'array' => 'array',
222+
];
223+
224+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
225+
226+
$expectedDocument = (object) [
227+
'array' => [1, 2, 3],
228+
'object' => (object) ['foo' => 'bar'],
229+
];
230+
231+
$iterator->rewind();
232+
233+
$iterator->offsetSet(0, 3);
234+
}
235+
236+
/**
237+
* @expectedException MongoDB\Exception\BadMethodCallException
238+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
239+
*/
240+
public function testOffsetUnsetThrowsException()
241+
{
242+
$document = [
243+
'array' => [1, 2, 3],
244+
'object' => ['foo' => 'bar'],
245+
];
246+
247+
$typeMap = [
248+
'root' => 'object',
249+
'document' => 'object',
250+
'array' => 'array',
251+
];
252+
253+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
254+
255+
$expectedDocument = (object) [
256+
'array' => [1, 2, 3],
257+
'object' => (object) ['foo' => 'bar'],
258+
];
259+
260+
$iterator->rewind();
261+
262+
$iterator->offsetUnset(0);
263+
}
264+
265+
/**
266+
* @expectedException MongoDB\Exception\BadMethodCallException
267+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
268+
*/
269+
public function testUasortThrowsException()
270+
{
271+
$document = [
272+
'array' => [1, 2, 3],
273+
'object' => ['foo' => 'bar'],
274+
];
275+
276+
$typeMap = [
277+
'root' => 'object',
278+
'document' => 'object',
279+
'array' => 'array',
280+
];
281+
282+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
283+
284+
$expectedDocument = (object) [
285+
'array' => [1, 2, 3],
286+
'object' => (object) ['foo' => 'bar'],
287+
];
288+
289+
$iterator->rewind();
290+
$cmp_function = function($a, $b) {
291+
return $a;
292+
};
293+
$iterator->uasort($cmp_function(0, 1));
294+
}
295+
296+
/**
297+
* @expectedException MongoDB\Exception\BadMethodCallException
298+
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
299+
*/
300+
public function testUksortThrowsException()
301+
{
302+
$document = [
303+
'array' => [1, 2, 3],
304+
'object' => ['foo' => 'bar'],
305+
];
306+
307+
$typeMap = [
308+
'root' => 'object',
309+
'document' => 'object',
310+
'array' => 'array',
311+
];
312+
313+
$iterator = new TypeMapArrayIterator([$document], $typeMap);
314+
315+
$expectedDocument = (object) [
316+
'array' => [1, 2, 3],
317+
'object' => (object) ['foo' => 'bar'],
318+
];
319+
320+
$iterator->rewind();
321+
$cmp_function = function($a, $b) {
322+
return $a;
323+
};
324+
$iterator->uksort($cmp_function(0, 1));
325+
}
34326
}

0 commit comments

Comments
 (0)