Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisenderink authored and lcobucci committed Sep 20, 2019
1 parent d7a537c commit 37d1d57
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/DbalTypes/CustomIdObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class CustomIdObjectType extends Type
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->id;
return $value->id . '_test';
}

/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$idObject = new CustomIdObject($value);
$idObject = new CustomIdObject(str_replace('_test', '', $value));

return $idObject;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Doctrine\Tests\Models\ClassTableInheritanceCustomType;

/**
* @Entity
* @Table(name="class_table_inheritance_custom_id_type_child")
*/
class CustomIdObjectTypeChild extends CustomIdObjectTypeParent
{
/**
* @var string
*/
public $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\Tests\Models\ClassTableInheritanceCustomType;

use Doctrine\Tests\DbalTypes\CustomIdObject;

/**
* @Entity
* @Table(name="class_table_inheritance_custom_id_type_parent")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"child" = "CustomIdObjectTypeChild"})
*/
abstract class CustomIdObjectTypeParent
{
/**
* @Id
* @Column(type="CustomIdObject")
*
* @var CustomIdObject
*/
public $id;

/**
* @var string
*/
public $type;

/**
* CustomIdObjectTypeParent constructor.
* @param CustomIdObject $id
* @param string $type
*/
public function __construct(CustomIdObject $id, $type = 'child')
{
$this->id = $id;
$this->type = $type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\Tests\DbalTypes\CustomIdObject;
use Doctrine\Tests\DbalTypes\CustomIdObjectType;
use Doctrine\Tests\Models\ClassTableInheritanceCustomType\CustomIdObjectTypeChild;
use Doctrine\Tests\Models\ClassTableInheritanceCustomType\CustomIdObjectTypeParent;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* Functional tests for the Class Table Inheritance mapping strategy with custom id object types.
*/
class ClassTableInheritanceCustomIdObjectTypeTest extends OrmFunctionalTestCase
{
protected function setUp()
{
if (DBALType::hasType(CustomIdObjectType::NAME)) {
DBALType::overrideType(CustomIdObjectType::NAME, CustomIdObjectType::class);
} else {
DBALType::addType(CustomIdObjectType::NAME, CustomIdObjectType::class);
}

$this->useModelSet('class_table_inheritance_custom_id_object_type');

parent::setUp();
}

public function testDelete()
{
$object = new CustomIdObjectTypeChild(new CustomIdObject('foo'));
$object->name = 'Test';

// persist parent
$this->_em->persist($object);
$this->_em->flush();

// save id for later use
$id = $object->id;

// get child
$object2 = $this->_em->find(CustomIdObjectTypeChild::class, $id);

// remove child
$this->_em->remove($object2);
$this->_em->flush();

$this->assertNull($this->_em->find(CustomIdObjectTypeChild::class, $id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testFetchJoinWhereCustomIdObject()
. ' parent LEFT JOIN parent.children children '
. 'WHERE children.id = ?1'
)
->setParameter(1, $parent->children->first()->id)
->setParameter(1, $parent->children->first()->id, CustomIdObjectType::NAME)
->getResult();

$this->assertCount(1, $result);
Expand Down
9 changes: 9 additions & 0 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
* @var array
*/
protected static $_modelSets = [
'class_table_inheritance_custom_id_object_type' => [
Models\ClassTableInheritanceCustomType\CustomIdObjectTypeParent::class,
Models\ClassTableInheritanceCustomType\CustomIdObjectTypeChild::class,
],
'cms' => [
Models\CMS\CmsUser::class,
Models\CMS\CmsPhonenumber::class,
Expand Down Expand Up @@ -343,6 +347,11 @@ protected function tearDown()

$this->_sqlLoggerStack->enabled = false;

if (isset($this->_usedModelSets['class_table_inheritance_custom_id_object_type'])) {
$conn->executeUpdate('DELETE FROM class_table_inheritance_custom_id_type_child');
$conn->executeUpdate('DELETE FROM class_table_inheritance_custom_id_type_parent');
}

if (isset($this->_usedModelSets['cms'])) {
$conn->executeUpdate('DELETE FROM cms_users_groups');
$conn->executeUpdate('DELETE FROM cms_groups');
Expand Down

0 comments on commit 37d1d57

Please sign in to comment.