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

#1336 - class constants over string references in tests - removing useless QueryBuilder usage

This commit is contained in:
Marco Pivetta 2015-03-17 21:21:20 +00:00
parent 40c41857e8
commit 11da060668
4 changed files with 35 additions and 25 deletions

View File

@ -24,6 +24,9 @@ use Doctrine\DBAL\Types\Type;
class CustomIdObjectType extends Type
{
const NAME = 'CustomIdObject';
const CLASSNAME = __CLASS__;
/**
* {@inheritdoc}
*/
@ -55,6 +58,6 @@ class CustomIdObjectType extends Type
*/
public function getName()
{
return 'CustomIdObject';
return self::NAME;
}
}

View File

@ -27,6 +27,8 @@ use Doctrine\Tests\DbalTypes\CustomIdObject;
*/
class CustomIdObjectTypeChild
{
const CLASSNAME = __CLASS__;
/**
* @Id @Column(type="CustomIdObject")
*

View File

@ -28,6 +28,8 @@ use Doctrine\Common\Collections\ArrayCollection;
*/
class CustomIdObjectTypeParent
{
const CLASSNAME = __CLASS__;
/**
* @Id @Column(type="CustomIdObject")
*

View File

@ -19,6 +19,7 @@
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\DbalTypes\CustomIdObjectType;
use Doctrine\Tests\Models\CustomType\CustomIdObjectTypeChild;
use Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent;
use Doctrine\Tests\OrmFunctionalTestCase;
@ -28,13 +29,14 @@ class CustomIdObjectTypeTest extends OrmFunctionalTestCase
{
protected function setUp()
{
if (DBALType::hasType('CustomIdObject')) {
DBALType::overrideType('CustomIdObject', '\Doctrine\Tests\DbalTypes\CustomIdObjectType');
if (DBALType::hasType(CustomIdObjectType::NAME)) {
DBALType::overrideType(CustomIdObjectType::NAME, CustomIdObjectType::CLASSNAME);
} else {
DBALType::addType('CustomIdObject', '\Doctrine\Tests\DbalTypes\CustomIdObjectType');
DBALType::addType(CustomIdObjectType::NAME, CustomIdObjectType::CLASSNAME);
}
$this->useModelSet('custom_id_object_type');
parent::setUp();
}
@ -45,7 +47,7 @@ class CustomIdObjectTypeTest extends OrmFunctionalTestCase
$this->_em->persist($parent);
$this->_em->flush();
$result = $this->_em->find('Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent', $parent->id);
$result = $this->_em->find(CustomIdObjectTypeParent::CLASSNAME, $parent->id);
$this->assertSame($parent, $result);
}
@ -53,20 +55,20 @@ class CustomIdObjectTypeTest extends OrmFunctionalTestCase
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();
$result = $this
->_em
->createQuery(
'SELECT parent, children FROM '
. CustomIdObjectTypeParent::CLASSNAME
. ' parent LEFT JOIN parent.children children'
)
->getResult();
$this->assertCount(1, $result);
$this->assertSame($parent, $result[0]);
@ -75,22 +77,23 @@ class CustomIdObjectTypeTest extends OrmFunctionalTestCase
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();
// note: hydration is willingly broken in this example:
$result = $this
->_em
->createQuery(
'SELECT parent, children FROM '
. CustomIdObjectTypeParent::CLASSNAME
. ' parent LEFT JOIN parent.children children '
. 'WHERE children.id = ?1'
)
->setParameter(1, $parent->children->first()->id)
->getResult();
$this->assertCount(1, $result);
$this->assertSame($parent, $result[0]);