2017-03-04 15:09:29 +01:00
|
|
|
<?php
|
2017-08-19 17:05:44 +02:00
|
|
|
declare(strict_types=1);
|
2017-03-04 15:09:29 +01:00
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
|
|
|
|
2017-08-19 16:48:50 +02:00
|
|
|
use Doctrine\ORM\Tools\ToolsException;
|
2017-08-19 16:37:24 +02:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
|
|
|
|
2017-03-04 15:09:29 +01:00
|
|
|
/**
|
2017-08-19 16:37:24 +02:00
|
|
|
* @group #6303
|
2017-03-04 15:09:29 +01:00
|
|
|
*/
|
2017-08-19 16:37:24 +02:00
|
|
|
class DDC6303Test extends OrmFunctionalTestCase
|
2017-03-04 15:09:29 +01:00
|
|
|
{
|
2017-08-19 16:53:41 +02:00
|
|
|
public function setUp() : void
|
2017-03-04 15:09:29 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-08-19 16:48:50 +02:00
|
|
|
|
2017-03-11 20:37:32 +01:00
|
|
|
try {
|
2017-08-19 16:37:24 +02:00
|
|
|
$this->_schemaTool->createSchema([
|
2017-08-19 16:48:50 +02:00
|
|
|
$this->_em->getClassMetadata(DDC6303BaseClass::class),
|
|
|
|
$this->_em->getClassMetadata(DDC6303ChildA::class),
|
|
|
|
$this->_em->getClassMetadata(DDC6303ChildB::class),
|
2017-08-19 16:37:24 +02:00
|
|
|
]);
|
2017-08-19 16:48:50 +02:00
|
|
|
} catch (ToolsException $ignored) {
|
2017-08-19 16:37:24 +02:00
|
|
|
}
|
2017-03-04 15:09:29 +01:00
|
|
|
}
|
2017-08-19 17:47:12 +02:00
|
|
|
|
|
|
|
public function testMixedTypeHydratedCorrectlyInJoinedInheritance() : void
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
$this->assertHydratedEntitiesSameToPersistedOnes([
|
|
|
|
'a' => new DDC6303ChildA('a', 'authorized'),
|
|
|
|
'b' => new DDC6303ChildB('b', ['accepted', 'authorized']),
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-19 16:53:41 +02:00
|
|
|
public function testEmptyValuesInJoinedInheritance() : void
|
2017-03-04 15:27:02 +01:00
|
|
|
{
|
2017-08-19 17:04:26 +02:00
|
|
|
$this->assertHydratedEntitiesSameToPersistedOnes([
|
2017-08-19 17:47:12 +02:00
|
|
|
'stringEmpty' => new DDC6303ChildA('stringEmpty', ''),
|
|
|
|
'stringZero' => new DDC6303ChildA('stringZero', 0),
|
2017-08-19 17:04:26 +02:00
|
|
|
'arrayEmpty' => new DDC6303ChildB('arrayEmpty', []),
|
|
|
|
]);
|
|
|
|
}
|
2017-03-04 15:27:02 +01:00
|
|
|
|
2017-08-19 17:04:26 +02:00
|
|
|
/**
|
2017-08-19 17:49:14 +02:00
|
|
|
* @param DDC6303BaseClass[] $persistedEntities indexed by identifier
|
2017-08-19 17:04:26 +02:00
|
|
|
*
|
|
|
|
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
|
|
|
|
* @throws \Doctrine\ORM\ORMException
|
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException
|
|
|
|
*/
|
|
|
|
private function assertHydratedEntitiesSameToPersistedOnes(array $persistedEntities) : void
|
|
|
|
{
|
|
|
|
array_walk($persistedEntities, [$this->_em, 'persist']);
|
2017-03-04 15:27:02 +01:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2017-08-19 17:49:14 +02:00
|
|
|
/* @var $entities DDC6303BaseClass[] */
|
2017-08-19 17:04:26 +02:00
|
|
|
$entities = $this
|
|
|
|
->_em
|
|
|
|
->getRepository(DDC6303BaseClass::class)
|
2017-08-19 16:37:24 +02:00
|
|
|
->createQueryBuilder('p')
|
2017-03-04 15:27:02 +01:00
|
|
|
->where('p.id IN(:ids)')
|
2017-08-19 17:04:26 +02:00
|
|
|
->setParameter('ids', array_keys($persistedEntities))
|
|
|
|
->getQuery()->getResult();
|
|
|
|
|
|
|
|
self::assertCount(count($persistedEntities), $entities);
|
2017-08-19 16:37:24 +02:00
|
|
|
|
2017-08-19 16:51:31 +02:00
|
|
|
foreach ($entities as $entity) {
|
2017-08-19 17:04:26 +02:00
|
|
|
self::assertEquals($entity, $persistedEntities[$entity->id]);
|
2017-03-04 15:27:02 +01:00
|
|
|
}
|
2017-08-19 16:37:24 +02:00
|
|
|
}
|
2017-03-04 15:09:29 +01:00
|
|
|
}
|
2017-03-11 20:37:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
2017-08-19 16:37:24 +02:00
|
|
|
* @Table
|
2017-03-11 20:37:32 +01:00
|
|
|
* @InheritanceType("JOINED")
|
|
|
|
* @DiscriminatorColumn(name="discr", type="string")
|
|
|
|
* @DiscriminatorMap({
|
2017-08-19 16:48:50 +02:00
|
|
|
* DDC6303ChildA::class = DDC6303ChildA::class,
|
|
|
|
* DDC6303ChildB::class = DDC6303ChildB::class,
|
2017-03-11 20:37:32 +01:00
|
|
|
* })
|
|
|
|
*/
|
2017-08-19 16:48:50 +02:00
|
|
|
abstract class DDC6303BaseClass
|
2017-03-11 20:37:32 +01:00
|
|
|
{
|
2017-08-19 17:04:26 +02:00
|
|
|
/** @Id @Column(type="string") @GeneratedValue(strategy="NONE") */
|
2017-03-11 20:37:32 +01:00
|
|
|
public $id;
|
|
|
|
}
|
|
|
|
|
2017-08-19 16:37:24 +02:00
|
|
|
/** @Entity @Table */
|
2017-08-19 16:48:50 +02:00
|
|
|
class DDC6303ChildA extends DDC6303BaseClass
|
2017-03-11 20:37:32 +01:00
|
|
|
{
|
2017-08-19 17:45:26 +02:00
|
|
|
/** @Column(type="string") */
|
2017-08-19 17:06:27 +02:00
|
|
|
private $originalData;
|
2017-08-19 17:04:26 +02:00
|
|
|
|
2017-08-19 17:05:44 +02:00
|
|
|
public function __construct(string $id, $originalData)
|
2017-08-19 17:04:26 +02:00
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
$this->originalData = $originalData;
|
|
|
|
}
|
2017-03-11 20:37:32 +01:00
|
|
|
}
|
|
|
|
|
2017-08-19 16:37:24 +02:00
|
|
|
/** @Entity @Table */
|
2017-08-19 16:48:50 +02:00
|
|
|
class DDC6303ChildB extends DDC6303BaseClass
|
2017-03-11 20:37:32 +01:00
|
|
|
{
|
2017-08-19 17:45:26 +02:00
|
|
|
/** @Column(type="array") */
|
2017-08-19 17:06:27 +02:00
|
|
|
private $originalData;
|
2017-08-19 17:04:26 +02:00
|
|
|
|
|
|
|
public function __construct(string $id, array $originalData)
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
$this->originalData = $originalData;
|
|
|
|
}
|
2017-03-19 15:05:29 +01:00
|
|
|
}
|