Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
some Unit tests for handleTypeConversions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Mar 29, 2018
1 parent 68e32d6 commit e82f6ff
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ function ($item) {
/**
* Handle various type conversions that should be supported natively by Doctrine (like DateTime)
* See Documentation of Doctrine Mapping Types for defaults
*
*
* @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#doctrine-mapping-types
* @param mixed $value
* @param string $typeOfField
Expand Down Expand Up @@ -497,13 +497,17 @@ protected function handleTypeConversions($value, $typeOfField)
case 'datetime':
case 'time':
case 'date':
if ('' === $value) {
if ($value === '') {
return null;
}

if ($value instanceof Datetime) {
return $value;
}

if (is_int($value)) {
$dateTime = new DateTime();
$dateTime->setTimestamp($value);
$dateTime->setTimestamp((int)$value);
$value = $dateTime;
} elseif (is_string($value)) {
$value = new DateTime($value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public function isActive()
{
return $this->isActive;
}

public function getIsActive()
{
return $this->isActive();
}
}
82 changes: 82 additions & 0 deletions tests/DoctrineModuleTest/Stdlib/Hydrator/DoctrineObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DoctrineModuleTest\Stdlib\Hydrator;

use Datetime;
use DoctrineModuleTest\Stdlib\Hydrator\Asset\ContextStrategy;
use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionClass;
Expand Down Expand Up @@ -2287,6 +2288,87 @@ public function testCanLookupsForEmptyIdentifiers()
$this->assertSame($entityInDatabaseWithEmptyId, $entity);
}

public function testHandleTypeConversionsDatetime()
{
// When using hydration by value, it will use the public API of the entity to set values (setters)
$this->configureObjectManagerForSimpleEntityWithDateTime();

$entity = new Asset\SimpleEntityWithDateTime();
$now = new DateTime();
$now->setTimestamp(1522353676);
$data = ['date' => 1522353676];

$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertInstanceOf('DateTime', $entity->getDate());
$this->assertEquals($now, $entity->getDate());

$entity = $this->hydratorByReference->hydrate($data, $entity);

$this->assertInstanceOf('DateTime', $entity->getDate());
$this->assertEquals($now, $entity->getDate());

$entity = new Asset\SimpleEntityWithDateTime();
$now = new DateTime();
$data = ['date' => $now->format('Y-m-d\TH:i:s')];

$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertInstanceOf('DateTime', $entity->getDate());
$this->assertEquals($now->format('Y-m-d\TH:i:s'), $entity->getDate()->format('Y-m-d\TH:i:s'));

$entity = $this->hydratorByReference->hydrate($data, $entity);

$this->assertInstanceOf('DateTime', $entity->getDate());
$this->assertEquals($now->format('Y-m-d\TH:i:s'), $entity->getDate()->format('Y-m-d\TH:i:s'));
}

public function testHandleTypeConversionsInteger()
{
// When using hydration by value, it will use the public API of the entity to set values (setters)
$this->configureObjectManagerForSimpleEntity();

$entity = new Asset\SimpleEntity();
$value = 123465;
$data = ['id' => '123465'];

$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertTrue(is_integer($entity->getId()));
$this->assertEquals($value, $entity->getId());

$entity = new Asset\SimpleEntity();
$value = 123465;
$data = ['id' => '123465'];

$entity = $this->hydratorByReference->hydrate($data, $entity);

$this->assertTrue(is_integer($entity->getId()));
$this->assertEquals($value, $entity->getId());
}

public function testHandleTypeConversionsBoolean()
{
// When using hydration by value, it will use the public API of the entity to set values (setters)
$this->configureObjectManagerForSimpleEntityWithIsBoolean();

$entity = new Asset\SimpleEntityWithIsBoolean();
$data = ['isActive' => true];

$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertTrue(is_bool($entity->getIsActive()));
$this->assertEquals(true, $entity->getIsActive());

$entity = new Asset\SimpleEntityWithIsBoolean();
$data = ['isActive' => true];

$entity = $this->hydratorByReference->hydrate($data, $entity);

$this->assertTrue(is_bool($entity->getIsActive()));
$this->assertEquals(true, $entity->getIsActive());
}

public function testHandleDateTimeConversionUsingByValue()
{
// When using hydration by value, it will use the public API of the entity to set values (setters)
Expand Down

0 comments on commit e82f6ff

Please sign in to comment.