From f8ef292148ebb028cc8430ec051447076e8094d5 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 16 Feb 2021 17:28:59 +0000 Subject: [PATCH] Nullable primitives (#203) * Nullable primitives * Add tests for nullable primitives * Fix precision --- .../InvalidNullablePrimitiveException.php | 6 +++ src/Result/Row.php | 49 ++++++++++++++----- test/phpunit/Migration/MigratorTest.php | 4 +- test/phpunit/Result/RowTest.php | 12 ++++- 4 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 src/Result/InvalidNullablePrimitiveException.php diff --git a/src/Result/InvalidNullablePrimitiveException.php b/src/Result/InvalidNullablePrimitiveException.php new file mode 100644 index 0000000..339661e --- /dev/null +++ b/src/Result/InvalidNullablePrimitiveException.php @@ -0,0 +1,6 @@ + */ protected $data; protected $iterator_index = 0; protected $iterator_data_key_list = []; @@ -23,23 +23,23 @@ public function __isset(string $name):bool { } public function get(string $columnName):?string { - return $this->getString($columnName); + return $this->data[$columnName] ?? null; } public function getString(string $columnName):?string { - return $this->data[$columnName] ?? null; + return $this->getAsNullablePrimitive($columnName, "string"); } public function getInt(string $columnName):?int { - return (int)($this->data[$columnName] ?? null); + return $this->getAsNullablePrimitive($columnName, "int"); } public function getFloat(string $columnName):?float { - return (float)($this->data[$columnName] ?? null); + return $this->getAsNullablePrimitive($columnName, "float"); } public function getBool(string $columnName):?bool { - return (bool)($this->data[$columnName] ?? null); + return $this->getAsNullablePrimitive($columnName, "bool"); } public function getDateTime(string $columnName):?DateTime { @@ -54,12 +54,7 @@ public function getDateTime(string $columnName):?DateTime { return $dateTime; } - $dateTime = new DateTime($dateString); - if(!$dateTime) { - throw new BadlyFormedDataException($columnName); - } - - return $dateTime; + return new DateTime($dateString); } public function asArray():array { @@ -95,4 +90,32 @@ public function current():?string { $key = $this->key(); return $this->$key; } -} \ No newline at end of file + + /** @return mixed */ + private function getAsNullablePrimitive(string $key, string $type) { + $value = $this->get($key); + if(is_null($value)) { + return null; + } + + switch($type) { + case "string": + return $value; + + case "int": + case "integer": + return (int)$value; + + case "float": + case "double": + case "decimal": + return (float)$value; + + case "bool": + case "boolean": + return (bool)$value; + } + + throw new InvalidNullablePrimitiveException($type); + } +} diff --git a/test/phpunit/Migration/MigratorTest.php b/test/phpunit/Migration/MigratorTest.php index f30530e..acc8de7 100644 --- a/test/phpunit/Migration/MigratorTest.php +++ b/test/phpunit/Migration/MigratorTest.php @@ -114,7 +114,7 @@ public function testCheckFileListOrderMissing(array $fileList) { $settings = $this->createSettings($path); $migrator = new Migrator($settings, $path); $actualFileList = $migrator->getMigrationFileList(); - $this->expectException(MigrationSequenceOrderException::class); + self::expectException(MigrationSequenceOrderException::class); $migrator->checkFileListOrder($actualFileList); } @@ -710,4 +710,4 @@ protected function createFiles(array $files, string $path):void { touch($pathName); } } -} \ No newline at end of file +} diff --git a/test/phpunit/Result/RowTest.php b/test/phpunit/Result/RowTest.php index 2f3ab22..508ef48 100644 --- a/test/phpunit/Result/RowTest.php +++ b/test/phpunit/Result/RowTest.php @@ -92,7 +92,7 @@ public function testGetFloat(array $data) { $row = new Row($data); $float = $row->getFloat("exampleFloat"); self::assertIsFloat($float); - self::assertSame((float)$data["exampleFloat"], $float); + self::assertSame(round($data["exampleFloat"], 6), round($float, 6)); } /** @dataProvider data_getTestRow */ @@ -114,6 +114,14 @@ public function testGetDateTime(array $data) { ); } + public function testGetIntNullable() { + $key = uniqid(); + $value = 123; + $row = new Row([$key => $value]); + self::assertEquals($value, $row->getInt($key)); + self::assertNull($row->getInt("does_not_exist")); + } + public function data_getTestRow():array { $data = []; @@ -154,4 +162,4 @@ public function data_getTestRow():array { return $data; } -} \ No newline at end of file +}