Completed
Push — master ( 86fd0c...7789df )
by Marco
12s
created

GH6141Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Doctrine\Tests\ORM\Functional\Ticket;
3
4
use Doctrine\DBAL\Platforms\AbstractPlatform;
5
use Doctrine\DBAL\Types\StringType;
6
use Doctrine\DBAL\Types\Type;
7
use Doctrine\ORM\AbstractQuery;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
class GH6141Test extends OrmFunctionalTestCase
11
{
12
    protected function setUp()
13
    {
14
        parent::setUp();
15
16
        Type::addType(GH6141PeopleType::NAME, GH6141PeopleType::class);
17
18
        $this->_schemaTool->createSchema(
19
            [
20
                $this->_em->getClassMetadata(GH6141Person::class),
21
                $this->_em->getClassMetadata(GH6141Boss::class),
22
                $this->_em->getClassMetadata(GH6141Employee::class),
23
            ]
24
        );
25
    }
26
27
    public function testEnumDiscriminatorsShouldBeConvertedToString()
28
    {
29
        $boss = new GH6141Boss('John');
30
        $employee = new GH6141Employee('Bob');
31
32
        $this->_em->persist($boss);
33
        $this->_em->persist($employee);
34
        $this->_em->flush();
35
        $this->_em->clear();
36
37
        // Using DQL here to make sure that we'll use ObjectHydrator instead of SimpleObjectHydrator
38
        $query = $this->_em->createQueryBuilder()
39
            ->select('person')
40
            ->from(GH6141Person::class, 'person')
41
            ->where('person.name = :name')
42
            ->setMaxResults(1)
43
            ->getQuery();
44
45
        $query->setParameter('name', 'John');
46
        self::assertEquals($boss, $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT));
47
        self::assertEquals(
48
            GH6141People::get(GH6141People::BOSS),
49
            $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY)['discr']
50
        );
51
52
        $query->setParameter('name', 'Bob');
53
        self::assertEquals($employee, $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT));
54
        self::assertEquals(
55
            GH6141People::get(GH6141People::EMPLOYEE),
56
            $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY)['discr']
57
        );
58
    }
59
}
60
61
class GH6141PeopleType extends StringType
62
{
63
    const NAME = 'gh6141people';
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
69
    {
70
        if (!$value instanceof GH6141People) {
71
            $value = GH6141People::get($value);
72
        }
73
74
        return (string) $value;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function convertToPhpValue($value, AbstractPlatform $platform)
81
    {
82
        return GH6141People::get($value);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getName()
89
    {
90
        return self::NAME;
91
    }
92
}
93
94
class GH6141People
95
{
96
    const BOSS = 'boss';
97
    const EMPLOYEE = 'employee';
98
99
    /**
100
     * @var string
101
     */
102
    private $value;
103
104
    /**
105
     * @param string $value
106
     *
107
     * @return GH6141People
108
     *
109
     * @throws \InvalidArgumentException
110
     */
111
    public static function get($value)
112
    {
113
        if (!self::isValid($value)) {
114
            throw new \InvalidArgumentException();
115
        }
116
117
        return new self($value);
118
    }
119
120
    /**
121
     * @param string $valid
122
     *
123
     * @return bool
124
     */
125
    private static function isValid($valid)
126
    {
127
        return in_array($valid, [self::BOSS, self::EMPLOYEE]);
128
    }
129
130
    /**
131
     * @param string $value
132
     */
133
    private function __construct($value)
134
    {
135
        $this->value = $value;
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function getValue()
142
    {
143
        return $this->value;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function __toString()
150
    {
151
        return $this->value;
152
    }
153
}
154
155
/**
156
 * @Entity
157
 * @InheritanceType("JOINED")
158
 * @DiscriminatorColumn(name="discr", type="gh6141people")
159
 * @DiscriminatorMap({
160
 *      "boss"     = GH6141Boss::class,
161
 *      "employee" = GH6141Employee::class
162
 * })
163
 */
164
abstract class GH6141Person
165
{
166
    /**
167
     * @Id
168
     * @Column(type="integer")
169
     * @GeneratedValue(strategy="AUTO")
170
     */
171
    public $id;
172
173
    /**
174
     * @Column(type="string")
175
     */
176
    public $name;
177
178
    /**
179
     * @param string $name
180
     */
181
    public function __construct($name)
182
    {
183
        $this->name = $name;
184
    }
185
}
186
187
/** @Entity */
188
class GH6141Boss extends GH6141Person
189
{
190
}
191
192
/** @Entity */
193
class GH6141Employee extends GH6141Person
194
{
195
}
196