Commit 16aea91d authored by Katherine Walker's avatar Katherine Walker

PHPLIB-292: Apply type map to offsetGet and throw exceptions for mutate methods…

PHPLIB-292: Apply type map to offsetGet and throw exceptions for mutate methods in TypeMapArrayIterator
parent 40971158
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
namespace MongoDB\Model; namespace MongoDB\Model;
use ArrayIterator; use ArrayIterator;
use MongoDB\Exception\BadMethodCallException;
/** /**
* Iterator for applying a type map to documents in inline command results. * Iterator for applying a type map to documents in inline command results.
...@@ -46,6 +47,16 @@ class TypeMapArrayIterator extends ArrayIterator ...@@ -46,6 +47,16 @@ class TypeMapArrayIterator extends ArrayIterator
$this->typeMap = $typeMap; $this->typeMap = $typeMap;
} }
public function append($value)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
public function asort()
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
/** /**
* Return the current element with the type map applied to it. * Return the current element with the type map applied to it.
* *
...@@ -56,4 +67,50 @@ class TypeMapArrayIterator extends ArrayIterator ...@@ -56,4 +67,50 @@ class TypeMapArrayIterator extends ArrayIterator
{ {
return \MongoDB\apply_type_map_to_document(parent::current(), $this->typeMap); return \MongoDB\apply_type_map_to_document(parent::current(), $this->typeMap);
} }
public function ksort()
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
public function natcasesort()
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
public function natsort()
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
/**
* Return the value from the provided offset with the type map applied.
*
* @see http://php.net/arrayiterator.offsetget
* @return array|object
*/
public function offsetGet($offset)
{
return \MongoDB\apply_type_map_to_document(parent::offsetGet($offset), $this->typeMap);
}
public function offsetSet($index, $newval)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
public function offsetUnset($index)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
public function uasort($cmp_function)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
public function uksort($cmp_function)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
}
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Model; namespace MongoDB\Tests\Model;
use MongoDB\Exception\BadMethodCallException;
use MongoDB\Model\TypeMapArrayIterator; use MongoDB\Model\TypeMapArrayIterator;
use MongoDB\Tests\TestCase; use MongoDB\Tests\TestCase;
...@@ -31,4 +32,295 @@ class TypeMapArrayIteratorTest extends TestCase ...@@ -31,4 +32,295 @@ class TypeMapArrayIteratorTest extends TestCase
$this->assertEquals($expectedDocument, $iterator->current()); $this->assertEquals($expectedDocument, $iterator->current());
} }
public function testOffsetGetAppliesTypeMap()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$this->assertEquals($expectedDocument, $iterator->offsetGet(0));
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testAppendThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->asort();
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testAsortThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->asort();
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testKsortThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->ksort();
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testNatcasessortThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->natcasesort();
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testNatsortThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->natsort();
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testOffsetSetThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->offsetSet(0, 3);
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testOffsetUnsetThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$iterator->offsetUnset(0);
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testUasortThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$cmp_function = function($a, $b) {
return $a;
};
$iterator->uasort($cmp_function(0, 1));
}
/**
* @expectedException MongoDB\Exception\BadMethodCallException
* @expectedExceptionMessage MongoDB\Model\TypeMapArrayIterator is immutable
*/
public function testUksortThrowsException()
{
$document = [
'array' => [1, 2, 3],
'object' => ['foo' => 'bar'],
];
$typeMap = [
'root' => 'object',
'document' => 'object',
'array' => 'array',
];
$iterator = new TypeMapArrayIterator([$document], $typeMap);
$expectedDocument = (object) [
'array' => [1, 2, 3],
'object' => (object) ['foo' => 'bar'],
];
$iterator->rewind();
$cmp_function = function($a, $b) {
return $a;
};
$iterator->uksort($cmp_function(0, 1));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment