Completed
Pull Request — master (#6141)
by Luís
10:25
created

testEnumDiscriminatorsShouldBeConvertedToString()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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