Skip to content

Commit

Permalink
Fix warning issue for returning entities without results
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuhiko TAMURA committed Jul 6, 2015
1 parent 4a46ba4 commit 57c4d62
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 83 deletions.
1 change: 1 addition & 0 deletions .bzrignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
161 changes: 78 additions & 83 deletions src/Domain/ObjectDomain.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,78 @@
<?php

namespace Omelet\Domain;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class ObjectDomain extends DomainBase
{
/**
* @var string
*/
private $type;
/**
* @var NamedDomain[]
*/
private $fields;

public function __construct($type, array $fields)
{
$this->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']);
}
}
<?php

namespace Omelet\Domain;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class ObjectDomain extends DomainBase {
/**
* @var string
*/
private $type;
/**
* @var NamedDomain[]
*/
private $fields;

public function __construct($type, array $fields) {
$this->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']);
}
}
27 changes: 27 additions & 0 deletions tests/Tests/DaoBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down

0 comments on commit 57c4d62

Please sign in to comment.