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

GH6139Person   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 19
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
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\Tests\OrmFunctionalTestCase;
8
9
class GH6139Test extends OrmFunctionalTestCase
10
{
11
    protected function setUp()
12
    {
13
        parent::setUp();
14
15
        Type::addType(GH6139PeopleType::NAME, __NAMESPACE__ . '\\GH6139PeopleType');
16
17
        $this->_schemaTool->createSchema(array(
18
            $this->_em->getClassMetadata(__NAMESPACE__ . '\\GH6139Person'),
19
            $this->_em->getClassMetadata(__NAMESPACE__ . '\\GH6139Boss'),
20
            $this->_em->getClassMetadata(__NAMESPACE__ . '\\GH6139Employee'),
21
        ));
22
    }
23
24
    public function testEnumDiscriminatorsShouldBeConvertedToString()
25
    {
26
        $boss = new GH6139Boss('John');
27
        $employee = new GH6139Employee('Bob');
28
29
        $this->_em->persist($boss);
30
        $this->_em->persist($employee);
31
        $this->_em->flush();
32
        $this->_em->clear();
33
34
        $query = $this->_em->createQueryBuilder()
35
            ->select('person')
36
            ->from(__NAMESPACE__ . '\\GH6139Person', 'person')
37
            ->where('person.name = :name')
38
            ->setMaxResults(1)
39
            ->getQuery();
40
41
        $query->setParameter('name', 'John');
42
        self::assertEquals($boss, $query->getOneOrNullResult());
43
44
        $query->setParameter('name', 'Bob');
45
        self::assertEquals($employee, $query->getOneOrNullResult());
46
    }
47
}
48
49
class GH6139PeopleType extends StringType
50
{
51
    const NAME = 'gh6139people';
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
57
    {
58
        if (!$value instanceof GH6139People) {
59
            $value = GH6139People::get($value);
60
        }
61
62
        return (string) $value;
63
    }
64
65
    public function convertToPhpValue($value, AbstractPlatform $platform)
66
    {
67
        return GH6139People::get($value);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getName()
74
    {
75
        return self::NAME;
76
    }
77
}
78
79
class GH6139People
80
{
81
    const BOSS = 'boss';
82
    const EMPLOYEE = 'employee';
83
84
    private $value;
85
86
    public static function get($value)
87
    {
88
        if (!self::isValid($value)) {
89
            throw new \InvalidArgumentException();
90
        }
91
92
        return new self($value);
93
    }
94
95
    public static function isValid($valid)
96
    {
97
        return in_array($valid, [self::BOSS, self::EMPLOYEE]);
98
    }
99
100
    private function __construct($value)
101
    {
102
        $this->value = $value;
103
    }
104
105
    public function getValue()
106
    {
107
        return $this->value;
108
    }
109
110
    public function __toString()
111
    {
112
        return $this->value;
113
    }
114
}
115
116
/**
117
 * @Entity
118
 * @InheritanceType("JOINED")
119
 * @DiscriminatorColumn(name="discr", type="gh6139people")
120
 * @DiscriminatorMap({
121
 *      "boss"     = "Doctrine\Tests\ORM\Functional\Ticket\GH6139Boss",
122
 *      "employee" = "Doctrine\Tests\ORM\Functional\Ticket\GH6139Employee"
123
 * })
124
 */
125
abstract class GH6139Person
126
{
127
    /**
128
     * @Id
129
     * @Column(type="integer")
130
     * @GeneratedValue(strategy="AUTO")
131
     */
132
    public $id;
133
134
    /**
135
     * @Column(type="string")
136
     */
137
    public $name;
138
139
    public function __construct($name)
140
    {
141
        $this->name = $name;
142
    }
143
}
144
145
/** @Entity */
146
class GH6139Boss extends GH6139Person
147
{
148
}
149
150
/** @Entity */
151
class GH6139Employee extends GH6139Person
152
{
153
}
154