1
0
mirror of synced 2024-12-05 03:06:05 +03:00

add failing functional test for custom id object types

This commit is contained in:
Stefano Torresi 2015-03-17 17:58:59 +01:00 committed by Marco Pivetta
parent 32137c72e4
commit 41e873bd72
6 changed files with 263 additions and 1 deletions

View File

@ -0,0 +1,32 @@
<?php
/**
* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/
namespace Doctrine\Tests\DbalTypes;
class CustomIdObject
{
/**
* @var string
*/
public $id;
/**
* @param string $id
*/
public function __construct($id)
{
$this->id = (string) $id;
}
/**
* @return string
*/
public function __toString()
{
return $this->id;
}
}

View File

@ -0,0 +1,49 @@
<?php
/**
* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/
namespace Doctrine\Tests\DbalTypes;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
class CustomIdObjectType extends Type
{
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value->id;
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$idObject = new CustomIdObject($value);
return $idObject;
}
/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'CustomIdObject';
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/
namespace Doctrine\Tests\Models\CustomType;
use Doctrine\Tests\DbalTypes\CustomIdObject;
/**
* @Entity
* @Table(name="custom_id_type_child")
*/
class CustomIdObjectTypeChild
{
/**
* @Id @Column(type="CustomIdObject")
*
* @var CustomIdObject
*/
public $id;
/**
* @ManyToOne(targetEntity="Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent", inversedBy="children")
*/
public $parent;
/**
* @param CustomIdObject|string $id
* @param CustomIdObjectTypeParent $parent
*/
public function __construct($id, CustomIdObjectTypeParent $parent)
{
if (! $id instanceof CustomIdObject) {
$id = new CustomIdObject($id);
}
$this->id = $id;
$this->parent = $parent;
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/
namespace Doctrine\Tests\Models\CustomType;
use Doctrine\Tests\DbalTypes\CustomIdObject;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="custom_id_type_parent")
*/
class CustomIdObjectTypeParent
{
/**
* @Id @Column(type="CustomIdObject")
*
* @var CustomIdObject
*/
public $id;
/**
* @OneToMany(targetEntity="Doctrine\Tests\Models\CustomType\CustomIdObjectTypeChild", cascade={"persist", "remove"}, mappedBy="parent")
*/
public $children;
/**
* @param CustomIdObject|string $id
*/
public function __construct($id)
{
if (! $id instanceof CustomIdObject) {
$id = new CustomIdObject($id);
}
$this->id = $id;
$this->children = new ArrayCollection();
}
}

View File

@ -0,0 +1,86 @@
<?php
/**
* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\CustomType\CustomIdObjectTypeChild;
use Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\DBAL\Types\Type as DBALType;
class CustomIdObjectTypeTest extends OrmFunctionalTestCase
{
protected function setUp()
{
if (DBALType::hasType('CustomIdObject')) {
DBALType::overrideType('CustomIdObject', '\Doctrine\Tests\DbalTypes\CustomIdObjectType');
} else {
DBALType::addType('CustomIdObject', '\Doctrine\Tests\DbalTypes\CustomIdObjectType');
}
$this->useModelSet('custom_id_object_type');
parent::setUp();
}
public function testFindByCustomIdObject()
{
$parent = new CustomIdObjectTypeParent('foo');
$this->_em->persist($parent);
$this->_em->flush();
$result = $this->_em->find('Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent', $parent->id);
$this->assertSame($parent, $result);
}
public function testFetchJoinCustomIdObject()
{
$parent = new CustomIdObjectTypeParent('foo');
$parent->children->add(new CustomIdObjectTypeChild('bar', $parent));
$this->_em->persist($parent);
$this->_em->flush();
$qb = $this->_em->createQueryBuilder();
$qb
->select('parent')
->from('Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent', 'parent')
->addSelect('children')
->leftJoin('parent.children', 'children')
;
$result = $qb->getQuery()->getResult();
$this->assertCount(1, $result);
$this->assertSame($parent, $result[0]);
}
public function testFetchJoinWhereCustomIdObject()
{
$parent = new CustomIdObjectTypeParent('foo');
$parent->children->add(new CustomIdObjectTypeChild('bar', $parent));
$this->_em->persist($parent);
$this->_em->flush();
$qb = $this->_em->createQueryBuilder();
$qb
->select('parent')
->from('Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent', 'parent')
->addSelect('children')
->leftJoin('parent.children', 'children')
->where('children.id = ?1')
->setParameter(1, $parent->children->first()->id);
;
$result = $qb->getQuery()->getResult();
$this->assertCount(1, $result);
$this->assertSame($parent, $result[0]);
}
}

View File

@ -257,7 +257,11 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\GeoNames\Admin1',
'Doctrine\Tests\Models\GeoNames\Admin1AlternateName',
'Doctrine\Tests\Models\GeoNames\City'
)
),
'custom_id_object_type' => array(
'Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent',
'Doctrine\Tests\Models\CustomType\CustomIdObjectTypeChild',
),
);
/**
@ -496,6 +500,11 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
$conn->executeUpdate('DELETE FROM geonames_country');
}
if (isset($this->_usedModelSets['custom_id_object_type'])) {
$conn->executeUpdate('DELETE FROM custom_id_type_parent');
$conn->executeUpdate('DELETE FROM custom_id_type_child');
}
$this->_em->clear();
}