Commit 4008b6b6 authored by Bas de Nooijer's avatar Bas de Nooijer

- added arrayaccess to readonly document

- added tests for arrayaccess implementation
parent 6c4d0be6
...@@ -44,7 +44,8 @@ ...@@ -44,7 +44,8 @@
* @package Solarium * @package Solarium
* @subpackage Document * @subpackage Document
*/ */
class Solarium_Document_ReadOnly implements IteratorAggregate, Countable class Solarium_Document_ReadOnly
implements IteratorAggregate, Countable, ArrayAccess
{ {
/** /**
...@@ -129,4 +130,45 @@ class Solarium_Document_ReadOnly implements IteratorAggregate, Countable ...@@ -129,4 +130,45 @@ class Solarium_Document_ReadOnly implements IteratorAggregate, Countable
return count($this->_fields); return count($this->_fields);
} }
/**
* ArrayAccess implementation
*
* @param miex $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset) {
return isset($this->_fields[$offset]);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset) {
$this->__set($offset, null);
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @return mixed|null
*/
public function offsetGet($offset) {
return isset($this->_fields[$offset]) ? $this->_fields[$offset] : null;
}
} }
\ No newline at end of file
...@@ -83,6 +83,48 @@ class Solarium_Document_ReadOnlyTest extends PHPUnit_Framework_TestCase ...@@ -83,6 +83,48 @@ class Solarium_Document_ReadOnlyTest extends PHPUnit_Framework_TestCase
$this->assertEquals($this->_fields, $fields); $this->assertEquals($this->_fields, $fields);
} }
public function testArrayGet()
{
$this->assertEquals(
$this->_fields['categories'],
$this->_doc['categories']
);
}
public function testArrayGetInvalidField()
{
$this->assertEquals(
null,
$this->_doc['invalidfield']
);
}
public function testArrayIsset()
{
$this->assertTrue(
isset($this->_doc['categories'])
);
}
public function testArrayIssetInvalidField()
{
$this->assertFalse(
isset($this->_doc['invalidfield'])
);
}
public function testArraySet()
{
$this->setExpectedException('Solarium_Exception');
$this->_doc['newField'] = 'new value';
}
public function testArrayUnset()
{
$this->setExpectedException('Solarium_Exception');
unset($this->_doc['newField']);
}
public function testCount() public function testCount()
{ {
$this->assertEquals(count($this->_fields), count($this->_doc)); $this->assertEquals(count($this->_fields), count($this->_doc));
......
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