diff --git a/lib/Everyman/Neo4j/Query/Row.php b/lib/Everyman/Neo4j/Query/Row.php index 384a43a..c9414bb 100644 --- a/lib/Everyman/Neo4j/Query/Row.php +++ b/lib/Everyman/Neo4j/Query/Row.php @@ -52,20 +52,27 @@ public function offsetExists($offset) public function offsetGet($offset) { + $offsetInt = $offset; + if (!is_integer($offset)) { - $offset = array_search($offset, $this->columns); + $offsetInt = array_search($offset, $this->columns); + + if ($offsetInt === false) { + trigger_error("Undefined offset: {$offset}", E_USER_NOTICE); + return null; + } } - if (!isset($this->data[$offset])) { - $raw = $this->raw[$offset]; + if (!isset($this->data[$offsetInt])) { + $raw = $this->raw[$offsetInt]; $data = $this->client->getEntityMapper()->getEntityFor($raw); if (is_array($data)) { $data = new Row($this->client, array_keys($raw), array_values($raw)); } - $this->data[$offset] = $data; + $this->data[$offsetInt] = $data; } - return $this->data[$offset]; + return $this->data[$offsetInt]; } public function offsetSet($offset, $value) diff --git a/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php b/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php index 6982612..dae80a5 100644 --- a/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php +++ b/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php @@ -13,7 +13,13 @@ public function setUp() { $this->client = new Client($this->getMock('Everyman\Neo4j\Transport', array(), array(), '', false)); } - + + public function tearDown() + { + // just in case if testArrayAccessNonExistedValue fails and not set back $enabled to default value + \PHPUnit_Framework_Error_Notice::$enabled = true; + } + public function testCount() { $columns = array('name','age'); @@ -71,6 +77,23 @@ public function testArrayAccess() $this->assertEquals(false, isset($row[3])); } + public function testArrayAccessNonExistedValue() + { + $columns = array('name'); + $data = array('Brenda'); + + $row = new Row($this->client, $columns, $data); + + // First check if we have null value + + \PHPUnit_Framework_Error_Notice::$enabled = false; + $this->assertSame(null, $row['age']); + \PHPUnit_Framework_Error_Notice::$enabled = true; + + $this->setExpectedException('PHPUnit_Framework_Error_Notice'); + $this->assertSame(null, $row['age']); + } + public function testArrayAccess_Set_ThrowsException() { $columns = array('name','age');