From 8da71217f67ba0785eff09a3e47a9902f06d5576 Mon Sep 17 00:00:00 2001 From: Boris Avdeev Date: Mon, 7 Apr 2014 15:59:52 +0400 Subject: [PATCH 1/2] Fixed #136 Wrong data value returned in Row in case of non-existent index --- lib/Everyman/Neo4j/Query/Row.php | 17 +++++++++---- .../unit/lib/Everyman/Neo4j/Query/RowTest.php | 25 ++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/Everyman/Neo4j/Query/Row.php b/lib/Everyman/Neo4j/Query/Row.php index 384a43a..03714c5 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..65dfb23 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'); From 9ddbb0e868a416576ec7443beeff6b94ce72ebd1 Mon Sep 17 00:00:00 2001 From: Boris Avdeev Date: Mon, 7 Apr 2014 16:05:51 +0400 Subject: [PATCH 2/2] Spaces to tab correction #136 --- lib/Everyman/Neo4j/Query/Row.php | 10 +++--- .../unit/lib/Everyman/Neo4j/Query/RowTest.php | 34 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/Everyman/Neo4j/Query/Row.php b/lib/Everyman/Neo4j/Query/Row.php index 03714c5..c9414bb 100644 --- a/lib/Everyman/Neo4j/Query/Row.php +++ b/lib/Everyman/Neo4j/Query/Row.php @@ -52,15 +52,15 @@ public function offsetExists($offset) public function offsetGet($offset) { - $offsetInt = $offset; + $offsetInt = $offset; if (!is_integer($offset)) { $offsetInt = array_search($offset, $this->columns); - if ($offsetInt === false) { - trigger_error("Undefined offset: {$offset}", E_USER_NOTICE); - return null; - } + if ($offsetInt === false) { + trigger_error("Undefined offset: {$offset}", E_USER_NOTICE); + return null; + } } if (!isset($this->data[$offsetInt])) { diff --git a/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php b/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php index 65dfb23..dae80a5 100644 --- a/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php +++ b/tests/unit/lib/Everyman/Neo4j/Query/RowTest.php @@ -14,11 +14,11 @@ 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 tearDown() + { + // just in case if testArrayAccessNonExistedValue fails and not set back $enabled to default value + \PHPUnit_Framework_Error_Notice::$enabled = true; + } public function testCount() { @@ -77,22 +77,22 @@ public function testArrayAccess() $this->assertEquals(false, isset($row[3])); } - public function testArrayAccessNonExistedValue() - { - $columns = array('name'); - $data = array('Brenda'); + public function testArrayAccessNonExistedValue() + { + $columns = array('name'); + $data = array('Brenda'); - $row = new Row($this->client, $columns, $data); + $row = new Row($this->client, $columns, $data); - // First check if we have null value + // First check if we have null value - \PHPUnit_Framework_Error_Notice::$enabled = false; - $this->assertSame(null, $row['age']); - \PHPUnit_Framework_Error_Notice::$enabled = true; + \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']); - } + $this->setExpectedException('PHPUnit_Framework_Error_Notice'); + $this->assertSame(null, $row['age']); + } public function testArrayAccess_Set_ThrowsException() {