1
0
mirror of synced 2025-02-09 16:59:26 +03:00

151 lines
4.3 KiB
PHP
Raw Normal View History

2017-03-04 15:09:29 +01:00
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\Tools\ToolsException;
use Doctrine\Tests\OrmFunctionalTestCase;
2017-03-04 15:09:29 +01:00
/**
* @group #6303
2017-03-04 15:09:29 +01:00
*/
class DDC6303Test extends OrmFunctionalTestCase
2017-03-04 15:09:29 +01:00
{
public function setUp() : void
2017-03-04 15:09:29 +01:00
{
parent::setUp();
2017-03-11 20:37:32 +01:00
try {
$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(DDC6303BaseClass::class),
$this->_em->getClassMetadata(DDC6303ChildA::class),
$this->_em->getClassMetadata(DDC6303ChildB::class),
]);
} catch (ToolsException $ignored) {
}
2017-03-04 15:09:29 +01:00
}
public function testMixedTypeHydratedCorrectlyInJoinedInheritance() : void
2017-03-04 15:09:29 +01:00
{
$a = new DDC6303ChildA();
$b = new DDC6303ChildB();
$aData = 'authorized';
$bData = ['accepted', 'authorized'];
2017-03-04 15:09:29 +01:00
// DDC6303ChildA and DDC6303ChildB have an inheritance from DDC6303BaseClass,
// but one has a string originalData and the second has an array, since the fields
// are mapped differently
$a->originalData = $aData;
$b->originalData = $bData;
2017-03-04 15:09:29 +01:00
$this->_em->persist($a);
$this->_em->persist($b);
2017-03-04 15:09:29 +01:00
$this->_em->flush();
2017-03-04 15:09:29 +01:00
// clear entity manager so that $repository->find actually fetches them and uses the hydrator
// instead of just returning the existing managed entities
$this->_em->clear();
$repository = $this->_em->getRepository(DDC6303BaseClass::class);
2017-03-04 15:09:29 +01:00
$dataMap = [
$a->id => $aData,
$b->id => $bData,
];
/* @var $entities DDC6303ChildA[]|DDC6303ChildB[] */
$entities = $repository
->createQueryBuilder('p')
->where('p.id IN(:ids)')
->setParameter('ids', array_keys($dataMap))
->getQuery()->getResult();
2017-03-04 15:09:29 +01:00
foreach ($entities as $entity) {
self::assertEquals(
$entity->originalData,
$dataMap[$entity->id],
get_class($entity) . ' not equals to original'
);
2017-03-04 15:09:29 +01:00
}
}
2017-03-04 15:27:02 +01:00
public function testEmptyValuesInJoinedInheritance() : void
2017-03-04 15:27:02 +01:00
{
$stringEmptyData = '';
$stringZeroData = 0;
$arrayEmptyData = [];
2017-03-04 15:27:02 +01:00
$stringEmpty = new DDC6303ChildA();
$stringZero = new DDC6303ChildA();
$arrayEmpty = new DDC6303ChildB();
2017-03-04 15:27:02 +01:00
$stringEmpty->originalData = $stringEmptyData;
$stringZero->originalData = $stringZeroData;
$arrayEmpty->originalData = $arrayEmptyData;
2017-03-04 15:27:02 +01:00
$this->_em->persist($stringZero);
$this->_em->persist($stringEmpty);
$this->_em->persist($arrayEmpty);
2017-03-04 15:27:02 +01:00
$this->_em->flush();
2017-03-04 15:27:02 +01:00
// clear entity manager so that $repository->find actually fetches them and uses the hydrator
// instead of just returning the existing managed entities
$this->_em->clear();
$repository = $this->_em->getRepository(DDC6303BaseClass::class);
$dataMap = [
$stringZero->id => $stringZeroData,
$stringEmpty->id => $stringEmptyData,
$arrayEmpty->id => $arrayEmptyData,
];
2017-03-04 15:27:02 +01:00
/* @var $entities DDC6303ChildA[]|DDC6303ChildB[] */
$entities = $repository
->createQueryBuilder('p')
2017-03-04 15:27:02 +01:00
->where('p.id IN(:ids)')
->setParameter('ids', array_keys($dataMap))
->getQuery()
->getResult();
foreach ($entities as $entity) {
self::assertEquals(
$entity->originalData,
$dataMap[$entity->id],
get_class($entity) . ' not equals to original'
);
2017-03-04 15:27:02 +01:00
}
}
2017-03-04 15:09:29 +01:00
}
2017-03-11 20:37:32 +01:00
/**
* @Entity
* @Table
2017-03-11 20:37:32 +01:00
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({
* DDC6303ChildA::class = DDC6303ChildA::class,
* DDC6303ChildB::class = DDC6303ChildB::class,
2017-03-11 20:37:32 +01:00
* })
*/
abstract class DDC6303BaseClass
2017-03-11 20:37:32 +01:00
{
/** @Id @Column(type="integer") @GeneratedValue */
2017-03-11 20:37:32 +01:00
public $id;
}
/** @Entity @Table */
class DDC6303ChildA extends DDC6303BaseClass
2017-03-11 20:37:32 +01:00
{
/** @Column(type="string", nullable=true) */
2017-03-11 20:37:32 +01:00
public $originalData;
}
/** @Entity @Table */
class DDC6303ChildB extends DDC6303BaseClass
2017-03-11 20:37:32 +01:00
{
/** @Column(type="simple_array", nullable=true) */
2017-03-11 20:37:32 +01:00
public $originalData;
}