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

Commit

Permalink
Unit tests for all data types
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Mar 29, 2018
1 parent dbce785 commit 159c9d6
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 214 deletions.
1 change: 0 additions & 1 deletion src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ protected function handleTypeConversions($value, $typeOfField)
case 'string':
case 'text':
case 'bigint':
case 'decimal':
case 'decimal':
$value = (string)$value;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DoctrineModuleTest\Stdlib\Hydrator\Asset;

class SimpleEntityWithFloat
class SimpleEntityWithGenericField
{
/**
* @var int
Expand All @@ -12,7 +12,7 @@ class SimpleEntityWithFloat
/**
* @var float
*/
protected $floatField;
protected $genericField;

public function setId($id)
{
Expand All @@ -24,15 +24,15 @@ public function getId()
return $this->id;
}

public function setFloatField($value)
public function setGenericField($value)
{
$this->floatField = $value;
$this->genericField = $value;

return $this;
}

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

namespace DoctrineModuleTest\Stdlib\Hydrator;

use Datetime;
use DoctrineModuleTest\Stdlib\Hydrator\Asset\ContextStrategy;
use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionClass;
Expand Down Expand Up @@ -310,74 +309,6 @@ function ($arg) {
);
}

public function configureObjectManagerForSimpleEntityWithFloat()
{
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithFloat');

$this
->metadata
->expects($this->any())
->method('getName')
->will($this->returnValue('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntityWithFloat'));
$this
->metadata
->expects($this->any())
->method('getAssociationNames')
->will($this->returnValue([]));

$this
->metadata
->expects($this->any())
->method('getFieldNames')
->will($this->returnValue(['id', 'floatField']));

$this
->metadata
->expects($this->any())
->method('getTypeOfField')
->with($this->logicalOr($this->equalTo('id'), $this->equalTo('floatField')))
->will(
$this->returnCallback(
function ($arg) {
if ('id' === $arg) {
return 'integer';
} elseif ('floatField' === $arg) {
return 'float';
}

throw new \InvalidArgumentException();
}
)
);

$this
->metadata
->expects($this->any())
->method('hasAssociation')
->will($this->returnValue(false));

$this
->metadata
->expects($this->any())
->method('getIdentifierFieldNames')
->will($this->returnValue(['id']));

$this
->metadata
->expects($this->any())
->method('getReflectionClass')
->will($this->returnValue($refl));

$this->hydratorByValue = new DoctrineObjectHydrator(
$this->objectManager,
true
);
$this->hydratorByReference = new DoctrineObjectHydrator(
$this->objectManager,
false
);
}

public function configureObjectManagerForSimpleEntityWithStringId()
{
$refl = new ReflectionClass('DoctrineModuleTest\Stdlib\Hydrator\Asset\SimpleEntity');
Expand Down Expand Up @@ -2356,144 +2287,6 @@ 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\.u')];

$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' => clone $now];

$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());
}

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 testHandleTypeConversionsFloat()
{
// When using hydration by value, it will use the public API of the entity to set values (setters)
$this->configureObjectManagerForSimpleEntityWithFloat();

$entity = new Asset\SimpleEntityWithFloat();
$value = 123.465;
$data = ['floatField' => '123.465'];

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

$this->assertTrue(is_float($entity->getFloatField()));
$this->assertEquals($value, $entity->getFloatField());

$entity = new Asset\SimpleEntityWithFloat();
$value = 123.465;
$data = ['floatField' => '123.465'];

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

$this->assertTrue(is_float($entity->getFloatField()));
$this->assertEquals($value, $entity->getFloatField());
}

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());


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

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

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

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

$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
Loading

0 comments on commit 159c9d6

Please sign in to comment.