diff --git a/src/Fetchable.php b/src/Fetchable.php index f8e5d65..0899b4c 100644 --- a/src/Fetchable.php +++ b/src/Fetchable.php @@ -130,7 +130,7 @@ protected function fetchAllTyped( } protected function castRow(string $type, Row $row) { - $assocArray = $row->toArray(); + $assocArray = $row->asArray(); reset($assocArray); $key = key($assocArray); $value = $assocArray[$key]; diff --git a/src/NoSuchTableException.php b/src/NoSuchTableException.php deleted file mode 100644 index 9929be4..0000000 --- a/src/NoSuchTableException.php +++ /dev/null @@ -1,4 +0,0 @@ -toArray(); + return $element->asArray(); }, $this->fetchAll() ); diff --git a/src/Result/Row.php b/src/Result/Row.php index bf54119..b47f525 100644 --- a/src/Result/Row.php +++ b/src/Result/Row.php @@ -62,7 +62,7 @@ public function getDateTime(string $columnName):?DateTime { return $dateTime; } - public function toArray():array { + public function asArray():array { return $this->data; } diff --git a/test/unit/Result/ResultSetTest.php b/test/unit/Result/ResultSetTest.php index d3337e6..e8e5555 100644 --- a/test/unit/Result/ResultSetTest.php +++ b/test/unit/Result/ResultSetTest.php @@ -120,6 +120,25 @@ public function testFetchAll() { } } + public function testAsArray() { + $resultSet = new ResultSet($this->getStatementMock()); + $array = $resultSet->asArray(); + foreach($array as $i => $rowArray) { + self::assertIsArray($rowArray); + self::assertArrayHasKey("id", $rowArray); + self::assertArrayHasKey("name", $rowArray); + self::assertIsInt($rowArray["id"]); + } + } + + public function testAsArrayNoMap() { + $resultSet = new ResultSet($this->getStatementMock()); + $array = $resultSet->asArray(false); + foreach($array as $i => $row) { + self::assertInstanceOf(Row::class, $row); + } + } + private function getStatementMock():PDOStatement { $statement = $this->createMock(PDOStatement::class); $statement->method("fetch") diff --git a/test/unit/Result/RowTest.php b/test/unit/Result/RowTest.php index 0e038ca..8cc8964 100644 --- a/test/unit/Result/RowTest.php +++ b/test/unit/Result/RowTest.php @@ -3,7 +3,6 @@ namespace unit\Result; use DateTime; -use Gt\Database\Result\NoSuchColumnException; use Gt\Database\Result\Row; use PHPUnit\Framework\TestCase; @@ -12,12 +11,12 @@ class RowTest extends TestCase { public function testFieldAccess(array $data) { $row = new Row($data); - foreach($data as $key => $value) { - if(is_float($data[$key])) { - self::assertEqualsWithDelta($data[$key], $value, 0.0001); + foreach($row as $key => $value) { + if(is_float($row->$key)) { + self::assertEqualsWithDelta($row->$key, $value, 0.0001); } else { - self::assertEquals($data[$key], $value); + self::assertEquals($row->$key, $value); } } } @@ -34,7 +33,7 @@ public function testIsNotSet() { public function testGetNonExistentProperty() { $row = new Row(["col1" => "item"]); - self::assertNull($row->doink); + self::assertNull($row->get("doink")); } public function testEmpty() { @@ -120,7 +119,6 @@ public function data_getTestRow():array { $data = []; $columns = ["id", "name", "example", "exampleFloat", "exampleDateTime", "exampleBool"]; - $rows = []; $rowNum = rand(2, 50); for($i = 0; $i < $rowNum; $i++) { $row = [];