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

Ticket4646InstanceOfTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

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