Completed
Pull Request — master (#6392)
by Alessandro
13:46
created

Employee   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDepartement() 0 4 1
A setDepartement() 0 4 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket {
4
5
    use Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Employee;
6
    use Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Engineer;
7
    use Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person;
8
    use Doctrine\Tests\OrmFunctionalTestCase;
9
10
    class Ticket4646InstanceOfMultiLevelTest extends OrmFunctionalTestCase
11
    {
12
        protected function setUp()
13
        {
14
            parent::setUp();
15
16
            $this->_schemaTool->createSchema([
17
                $this->_em->getClassMetadata(Person::class),
18
                $this->_em->getClassMetadata(Employee::class),
19
                $this->_em->getClassMetadata(Engineer::class),
20
            ]);
21
        }
22
23
        public function testInstanceOf()
24
        {
25
            $this->loadData();
26
27
            $dql = 'SELECT p FROM Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person p
28
                    WHERE p INSTANCE OF Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person';
29
            $query = $this->_em->createQuery($dql);
30
            $result = $query->getResult();
31
32
            $this->assertCount(3, $result);
33
34
            foreach ($result as $r) {
35
                $this->assertInstanceOf(Person::class, $r);
36
                if ($r instanceof Engineer) {
37
                    $this->assertEquals('foobar', $r->getName());
38
                    $this->assertEquals('doctrine', $r->getSpecialization());
39
                } elseif ($r instanceof Employee) {
40
                    $this->assertEquals('bar', $r->getName());
41
                    $this->assertEquals('qux', $r->getDepartement());
42
                } else {
43
                    $this->assertEquals('foo', $r->getName());
44
                }
45
            }
46
        }
47
48
        private function loadData()
49
        {
50
            $person = new Person();
51
            $person->setName('foo');
52
53
            $employee = new Employee();
54
            $employee->setName('bar');
55
            $employee->setDepartement('qux');
56
57
            $engineer = new Engineer();
58
            $engineer->setName('foobar');
59
            $engineer->setDepartement('dep');
60
            $engineer->setSpecialization('doctrine');
61
62
            $this->_em->persist($person);
63
            $this->_em->persist($employee);
64
            $this->_em->persist($engineer);
65
66
            $this->_em->flush(array($person, $employee, $engineer));
67
        }
68
    }
69
}
70
71
namespace Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest {
72
    /**
73
     * @Entity()
74
     * @Table(name="instance_of_multi_level_test_person")
75
     * @InheritanceType(value="JOINED")
76
     * @DiscriminatorColumn(name="kind", type="string")
77
     * @DiscriminatorMap(value={
78
     *     "person": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Person",
79
     *     "employee": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Employee",
80
     *     "engineer": "Doctrine\Tests\ORM\Functional\InstanceOfMultiLevelTest\Engineer",
81
     * })
82
     */
83
    class Person
84
    {
85
        /**
86
         * @Id()
87
         * @GeneratedValue()
88
         * @Column(type="integer")
89
         */
90
        private $id;
91
92
        /**
93
         * @Column(type="string")
94
         */
95
        private $name;
96
97
        public function getId()
98
        {
99
            return $this->id;
100
        }
101
102
        public function getName()
103
        {
104
            return $this->name;
105
        }
106
107
        public function setName($name)
108
        {
109
            $this->name = $name;
110
        }
111
    }
112
113
    /**
114
     * @Entity()
115
     * @Table(name="instance_of_multi_level_employee")
116
     */
117
    class Employee extends Person
118
    {
119
        /**
120
         * @Column(type="string")
121
         */
122
        private $departement;
123
124
        public function getDepartement()
125
        {
126
            return $this->departement;
127
        }
128
129
        public function setDepartement($departement)
130
        {
131
            $this->departement = $departement;
132
        }
133
    }
134
135
    /**
136
     * @Entity()
137
     * @Table(name="instance_of_multi_level_engineer")
138
     */
139
    class Engineer extends Employee
140
    {
141
        /**
142
         * @Column(type="string")
143
         */
144
        private $specialization;
145
146
        public function getSpecialization()
147
        {
148
            return $this->specialization;
149
        }
150
151
        public function setSpecialization($specialization)
152
        {
153
            $this->specialization = $specialization;
154
        }
155
    }
156
}
157