Passed
Pull Request — 2.6 (#7322)
by Luís
08:57
created

GH5988Test::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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