From 57c4d625064671abb7c7f849af98144611866737 Mon Sep 17 00:00:00 2001 From: Kazuhiko TAMURA Date: Mon, 6 Jul 2015 17:57:46 +0900 Subject: [PATCH] Fix warning issue for returning entities without results --- .bzrignore | 1 + src/Domain/ObjectDomain.php | 161 ++++++++++++++++----------------- tests/Tests/DaoBuilderTest.php | 27 ++++++ 3 files changed, 106 insertions(+), 83 deletions(-) diff --git a/.bzrignore b/.bzrignore index 4b4b0cb..97699a0 100644 --- a/.bzrignore +++ b/.bzrignore @@ -5,3 +5,4 @@ tests/fixtures/todo.sqlite3 tests/fixtures/sql/SwitchDao/findA.sql tests/fixtures/sql/SwitchDao/findB.sql tests/Target/SwitchDao.php +tests/Tests/Target/SwitchDao/SwitchDao.php diff --git a/src/Domain/ObjectDomain.php b/src/Domain/ObjectDomain.php index f50d8f8..199b28b 100644 --- a/src/Domain/ObjectDomain.php +++ b/src/Domain/ObjectDomain.php @@ -1,83 +1,78 @@ -type = $type; - $this->fields = $fields; - } - - public function getChildren() - { - return $this->fields; - } - - protected function expandTypesInternal($name, $val) - { - return $this->expand( - $name, $val, - function ($field, $k, $v) { - return $field->expandTypes($k, $v); - } - ); - } - - protected function expandValuesInternal($name, $val) - { - return $this->expand( - $name, $val, - function ($field, $k, $v) { - return $field->expandValues($k, $v); - } - ); - } - - protected function convertResultsInternal($results, AbstractPlatform $platform) - { - if (is_int(key($results))) { - $results = current($results); - } - - $class = $this->type; - - $obj = new $class(); - - foreach ($this->fields as $name => $domain) { - $obj->{$name} = $domain->convertResults($results, $platform); - } - - return $obj; - } - - private function expand($name, $val, callable $fn) - { - return array_reduce( - array_keys($this->fields), - function (array &$tmp, $k) use ($name, $val, $fn) { - $n = $name !== '' ? "{$name}_{$k}" : $k; - - return $tmp + [$k => $fn($this->fields[$k], $n, $val->{$k})]; - }, - [] - ); - } - - public static function __set_state($values) - { - return new ObjectDomain($values['type'], $values['fields']); - } -} +type = $type; + $this->fields = $fields; + } + + public function getChildren() { + return $this->fields; + } + + protected function expandTypesInternal($name, $val) { + return $this->expand( + $name, $val, + function ($field, $k, $v) { + return $field->expandTypes($k, $v); + } + ); + } + + protected function expandValuesInternal($name, $val) { + return $this->expand( + $name, $val, + function ($field, $k, $v) { + return $field->expandValues($k, $v); + } + ); + } + + protected function convertResultsInternal($results, AbstractPlatform $platform) { + if ($results === false) { + return null; + } + + if (is_int(key($results))) { + $results = current($results); + } + + $class = $this->type; + + $obj = new $class(); + + foreach ($this->fields as $name => $domain) { + $obj->{$name} = $domain->convertResults($results, $platform); + } + + return $obj; + } + + private function expand($name, $val, callable $fn) { + return array_reduce( + array_keys($this->fields), + function (array &$tmp, $k) use($name, $val, $fn) { + $n = $name !== "" ? "{$name}_{$k}" : $k; + return $tmp + [$k => $fn($this->fields[$k], $n, $val->{$k})]; + }, + [] + ); + } + + public static function __set_state($values) { + return new ObjectDomain($values['type'], $values['fields']); + } +} diff --git a/tests/Tests/DaoBuilderTest.php b/tests/Tests/DaoBuilderTest.php index a6abba0..989be55 100644 --- a/tests/Tests/DaoBuilderTest.php +++ b/tests/Tests/DaoBuilderTest.php @@ -501,6 +501,20 @@ public function test_export_select_returning_entity() $this->assertEquals(new \DateTime('2015/05/11'), $results->created); } + /** + * @test + */ + public function test_export_select_returning_entity_but_without_result() + { + $logger = null; +// $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger(); + $dao = $this->exportDao(TodoDao2::class, $logger); + + $results = $dao->findById(new PrimaryKey(987)); + + $this->assertNull($results); + } + /** * @test */ @@ -526,6 +540,19 @@ public function test_export_select_returning_entity_array() $this->assertEquals(new \DateTime('2015/05/11'), $row->created); } + /** + * @test + */ + public function test_export_select_returning_entity_array_but_without_results() + { + $logger = null; +// $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger(); + $dao = $this->exportDao(TodoDao2::class, $logger); + + $results = $dao->listByPub(new \DateTime('2010/4/30'), new \DateTime('2010/5/11')); + $this->assertCount(0, $results); + } + /** * @test */