File: GH10387Test.php

package info (click to toggle)
doctrine 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,556 kB
  • sloc: php: 108,620; xml: 1,340; makefile: 35; sh: 14
file content (128 lines) | stat: -rw-r--r-- 3,885 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmTestCase;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

use function array_map;

#[Group('GH-10387')]
#[RequiresPhpunit('< 12')]
class GH10387Test extends OrmTestCase
{
    #[DataProvider('classHierachies')]
    public function testSchemaToolCreatesColumnForFieldInTheMiddleClass(array $classes): void
    {
        $em         = $this->getTestEntityManager();
        $schemaTool = new SchemaTool($em);
        $metadata   = array_map(static function (string $class) use ($em) {
            return $em->getClassMetadata($class);
        }, $classes);
        $schema     = $schemaTool->getSchemaFromMetadata([$metadata[0]]);

        self::assertNotNull($schema->getTable('root')->getColumn('middle_class_field'));
        self::assertNotNull($schema->getTable('root')->getColumn('leaf_class_field'));
    }

    public static function classHierachies(): Generator
    {
        yield 'hierarchy with Entity classes only' => [[GH10387EntitiesOnlyRoot::class, GH10387EntitiesOnlyMiddle::class, GH10387EntitiesOnlyLeaf::class]];
        yield 'MappedSuperclass in the middle of the hierarchy' => [[GH10387MappedSuperclassRoot::class, GH10387MappedSuperclassMiddle::class, GH10387MappedSuperclassLeaf::class]];
        yield 'abstract entity the the root and in the middle of the hierarchy' => [[GH10387AbstractEntitiesRoot::class, GH10387AbstractEntitiesMiddle::class, GH10387AbstractEntitiesLeaf::class]];
    }
}

#[ORM\Entity]
#[ORM\Table(name: 'root')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorMap(['A' => GH10387EntitiesOnlyRoot::class, 'B' => GH10387EntitiesOnlyMiddle::class, 'C' => GH10387EntitiesOnlyLeaf::class])]
class GH10387EntitiesOnlyRoot
{
    /** @var string */
    #[ORM\Id]
    #[ORM\Column]
    private $id;
}

#[ORM\Entity]
class GH10387EntitiesOnlyMiddle extends GH10387EntitiesOnlyRoot
{
    /** @var string */
    #[ORM\Column(name: 'middle_class_field')]
    private $parentValue;
}

#[ORM\Entity]
class GH10387EntitiesOnlyLeaf extends GH10387EntitiesOnlyMiddle
{
    /** @var string */
    #[ORM\Column(name: 'leaf_class_field')]
    private $childValue;
}

/** ↓ This DiscriminatorMap contains the Entity classes only, not the Mapped Superclass */
#[ORM\DiscriminatorMap(['A' => GH10387MappedSuperclassRoot::class, 'B' => GH10387MappedSuperclassLeaf::class])]
#[ORM\Entity]
#[ORM\Table(name: 'root')]
#[ORM\InheritanceType('SINGLE_TABLE')]
class GH10387MappedSuperclassRoot
{
    /** @var string */
    #[ORM\Id]
    #[ORM\Column]
    private $id;
}

#[ORM\MappedSuperclass]
class GH10387MappedSuperclassMiddle extends GH10387MappedSuperclassRoot
{
    /** @var string */
    #[ORM\Column(name: 'middle_class_field')]
    private $parentValue;
}

#[ORM\Entity]
class GH10387MappedSuperclassLeaf extends GH10387MappedSuperclassMiddle
{
    /** @var string */
    #[ORM\Column(name: 'leaf_class_field')]
    private $childValue;
}


/** ↓ This DiscriminatorMap contains the single non-abstract Entity class only */
#[ORM\DiscriminatorMap(['A' => GH10387AbstractEntitiesLeaf::class])]
#[ORM\Entity]
#[ORM\Table(name: 'root')]
#[ORM\InheritanceType('SINGLE_TABLE')]
abstract class GH10387AbstractEntitiesRoot
{
    /** @var string */
    #[ORM\Id]
    #[ORM\Column]
    private $id;
}

#[ORM\Entity]
abstract class GH10387AbstractEntitiesMiddle extends GH10387AbstractEntitiesRoot
{
    /** @var string */
    #[ORM\Column(name: 'middle_class_field')]
    private $parentValue;
}

#[ORM\Entity]
class GH10387AbstractEntitiesLeaf extends GH10387AbstractEntitiesMiddle
{
    /** @var string */
    #[ORM\Column(name: 'leaf_class_field')]
    private $childValue;
}