add failing functional test for custom id object types
This commit is contained in:
parent
32137c72e4
commit
41e873bd72
32
tests/Doctrine/Tests/DbalTypes/CustomIdObject.php
Normal file
32
tests/Doctrine/Tests/DbalTypes/CustomIdObject.php
Normal 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;
|
||||
}
|
||||
}
|
49
tests/Doctrine/Tests/DbalTypes/CustomIdObjectType.php
Normal file
49
tests/Doctrine/Tests/DbalTypes/CustomIdObjectType.php
Normal 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';
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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]);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user