Skip to content

Commit

Permalink
Test new type-safe get functionality, closes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Aug 21, 2019
1 parent df0e5aa commit 44c9843
Showing 1 changed file with 97 additions and 6 deletions.
103 changes: 97 additions & 6 deletions test/unit/Result/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace unit\Result;

use DateTime;
use Gt\Database\Result\NoSuchColumnException;
use Gt\Database\Result\Row;
use PHPUnit\Framework\TestCase;
Expand All @@ -12,7 +13,12 @@ public function testFieldAccess(array $data) {
$row = new Row($data);

foreach($data as $key => $value) {
self::assertEquals($data[$key], $row->$key);
if(is_float($data[$key])) {
self::assertEqualsWithDelta($data[$key], $value, 0.0001);
}
else {
self::assertEquals($data[$key], $value);
}
}
}

Expand Down Expand Up @@ -57,14 +63,99 @@ public function testIteration(array $data) {
$row = new Row($data);

foreach($row as $colName => $value) {
self::assertEquals($data[$colName], $value);
if(is_float($data[$colName])) {
self::assertEqualsWithDelta($data[$colName], $value, 0.0001);
}
else {
self::assertEquals($data[$colName], $value);
}
}
}

/** @dataProvider data_getTestRow */
public function testGetString(array $data) {
$row = new Row($data);
foreach($data as $key => $expected) {
$actual = $row->getString($key);
self::assertIsString($actual);
self::assertSame((string)$expected, $actual);
}
}

/** @dataProvider data_getTestRow */
public function testGetInt(array $data) {
$row = new Row($data);
$id = $row->getInt("id");
self::assertIsInt($id);
self::assertSame((int)$data["id"], $id);
}

/** @dataProvider data_getTestRow */
public function testGetFloat(array $data) {
$row = new Row($data);
$float = $row->getFloat("exampleFloat");
self::assertIsFloat($float);
self::assertSame((float)$data["exampleFloat"], $float);
}

/** @dataProvider data_getTestRow */
public function testGetBool(array $data) {
$row = new Row($data);
$bool = $row->getBool("exampleBool");
self::assertIsBool($bool);
self::assertSame((bool)$data["exampleBool"], $bool);
}

/** @dataProvider data_getTestRow */
public function testGetDateTime(array $data) {
$row = new Row($data);
$dateTime = $row->getDateTime("exampleDateTime");
self::assertInstanceOf(DateTime::class, $dateTime);
self::assertEquals(
$dateTime->format("Y-m-d H:i:s"),
$data["exampleDateTime"]
);
}

public function data_getTestRow():array {
return [
[["id" => 1, "name" => "Alice"]],
[["col1" => "binky", "col2" => "boo", "col3", "dah"]],
];
$data = [];

$columns = ["id", "name", "example", "exampleFloat", "exampleDateTime", "exampleBool"];
$rows = [];
$rowNum = rand(2, 50);
for($i = 0; $i < $rowNum; $i++) {
$row = [];
foreach($columns as $columnIndex => $columnName) {
switch($columnName) {
case "id":
$value = $columnIndex;
break;

case "exampleFloat":
$value = rand(100, 1000000) / 3.141;
break;

case "exampleDateTime":
$timestamp = rand(0, 4260560700);
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);
$value = $dateTime->format("Y-m-d H:i:s");
break;

case "exampleBool":
$value = rand(0, 1);
break;

default:
$value = uniqid();
}

$row[$columnName] = $value;
}

$data []= [$row];
}

return $data;
}
}

0 comments on commit 44c9843

Please sign in to comment.