1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\DbalTypes\CustomIdObject;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
use function str_replace;
/**
* Functional tests for the Class Table Inheritance mapping strategy with custom id object types.
*/
#[Group('GH5988')]
final class GH5988Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
if (! DBALType::hasType(GH5988CustomIdObjectHashType::class)) {
DBALType::addType(GH5988CustomIdObjectHashType::class, GH5988CustomIdObjectHashType::class);
}
$this->setUpEntitySchema([GH5988CustomIdObjectTypeParent::class, GH5988CustomIdObjectTypeChild::class]);
}
public function testDelete(): void
{
$object = new GH5988CustomIdObjectTypeChild(new CustomIdObject('foo'), 'Test');
$this->_em->persist($object);
$this->_em->flush();
$id = $object->id;
$object2 = $this->_em->find(GH5988CustomIdObjectTypeChild::class, $id);
$this->_em->remove($object2);
$this->_em->flush();
self::assertNull($this->_em->find(GH5988CustomIdObjectTypeChild::class, $id));
}
}
class GH5988CustomIdObjectHashType extends DBALType
{
/**
* {@inheritDoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): string
{
return $value->id . '_test';
}
/**
* {@inheritDoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform): CustomIdObject
{
return new CustomIdObject(str_replace('_test', '', $value));
}
/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getStringTypeDeclarationSQL($column);
}
public function getName(): string
{
return self::class;
}
}
#[Table]
#[Entity]
#[InheritanceType('JOINED')]
#[DiscriminatorColumn(name: 'type', type: 'string')]
#[DiscriminatorMap(['child' => GH5988CustomIdObjectTypeChild::class])]
abstract class GH5988CustomIdObjectTypeParent
{
/** @var CustomIdObject */
#[Id]
#[Column(type: 'Doctrine\Tests\ORM\Functional\Ticket\GH5988CustomIdObjectHashType', length: 255)]
public $id;
}
#[Table]
#[Entity]
class GH5988CustomIdObjectTypeChild extends GH5988CustomIdObjectTypeParent
{
public function __construct(CustomIdObject $id, public string $name)
{
$this->id = $id;
}
}
|