Completed
Pull Request — 2.7 (#8012)
by Stefan
08:57
created

testOrderWithArithmeticExpressionWithResultVariableAndLiteral()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 12
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Tests\Models\Company\CompanyEmployee;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
/**
11
 * Functional tests for ordering with arithmetic expression.
12
 *
13
 * @group GH8011
14
 */
15
class GH8011Test extends OrmFunctionalTestCase
16
{
17
    protected function setUp()
18
    {
19
        $this->useModelSet('company');
20
        parent::setUp();
21
22
        $this->generateFixture();
23
    }
24
25
    public function testOrderWithArithmeticExpressionWithSingleValuedPathExpression()
26
    {
27
        $dql = 'SELECT p ' .
28
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
29
            'ORDER BY p.id + p.id ASC';
30
31
        /** @var CompanyEmployee[] $result */
32
        $result = $this->_em->createQuery($dql)->getResult();
33
34
        $this->assertEquals(2, count($result));
35
        $this->assertEquals('Benjamin E.', $result[0]->getName());
36
        $this->assertEquals('Guilherme B.', $result[1]->getName());
37
    }
38
39
    public function testOrderWithArithmeticExpressionWithLiteralAndSingleValuedPathExpression()
40
    {
41
        $dql = 'SELECT p ' .
42
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
43
            'ORDER BY 1 + p.id ASC';
44
45
        /** @var CompanyEmployee[] $result */
46
        $result = $this->_em->createQuery($dql)->getResult();
47
48
        $this->assertEquals(2, count($result));
49
        $this->assertEquals('Benjamin E.', $result[0]->getName());
50
        $this->assertEquals('Guilherme B.', $result[1]->getName());
51
    }
52
53
    public function testOrderWithArithmeticExpressionWithSingleValuedPathExpressionAndLiteral()
54
    {
55
        $dql = 'SELECT p ' .
56
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
57
            'ORDER BY p.id + 1 ASC';
58
59
        /** @var CompanyEmployee[] $result */
60
        $result = $this->_em->createQuery($dql)->getResult();
61
62
        $this->assertEquals(2, count($result));
63
        $this->assertEquals('Benjamin E.', $result[0]->getName());
64
        $this->assertEquals('Guilherme B.', $result[1]->getName());
65
    }
66
67
    public function testOrderWithArithmeticExpressionWithResultVariableAndLiteral()
68
    {
69
        $dql = 'SELECT p,  p.salary AS HIDDEN s ' .
70
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
71
            'ORDER BY s + 1 DESC';
72
73
        /** @var CompanyEmployee[] $result */
74
        $result = $this->_em->createQuery($dql)->getResult();
75
76
        $this->assertEquals(2, count($result));
77
        $this->assertEquals('Guilherme B.', $result[0]->getName());
78
        $this->assertEquals('Benjamin E.', $result[1]->getName());
79
    }
80
81
    public function testOrderWithArithmeticExpressionWithLiteralAndResultVariable()
82
    {
83
        $dql = 'SELECT p,  p.salary AS HIDDEN s ' .
84
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
85
            'ORDER BY 1 + s DESC';
86
87
        /** @var CompanyEmployee[] $result */
88
        $result = $this->_em->createQuery($dql)->getResult();
89
90
        $this->assertEquals(2, count($result));
91
        $this->assertEquals('Guilherme B.', $result[0]->getName());
92
        $this->assertEquals('Benjamin E.', $result[1]->getName());
93
    }
94
95
    public function testOrderWithArithmeticExpressionWithResultVariableAndSingleValuedPathExpression()
96
    {
97
        $dql = 'SELECT p,  p.salary AS HIDDEN s ' .
98
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
99
            'ORDER BY s + p.id DESC';
100
101
        /** @var CompanyEmployee[] $result */
102
        $result = $this->_em->createQuery($dql)->getResult();
103
104
        $this->assertEquals(2, count($result));
105
        $this->assertEquals('Guilherme B.', $result[0]->getName());
106
        $this->assertEquals('Benjamin E.', $result[1]->getName());
107
    }
108
109
    public function testOrderWithArithmeticExpressionWithSingleValuedPathExpressionAndResultVariable()
110
    {
111
        $dql = 'SELECT p,  p.salary AS HIDDEN s ' .
112
            'FROM Doctrine\Tests\Models\Company\CompanyEmployee p ' .
113
            'ORDER BY p.id + s DESC';
114
115
        /** @var CompanyEmployee[] $result */
116
        $result = $this->_em->createQuery($dql)->getResult();
117
118
        $this->assertEquals(2, count($result));
119
        $this->assertEquals('Guilherme B.', $result[0]->getName());
120
        $this->assertEquals('Benjamin E.', $result[1]->getName());
121
    }
122
123
    public function generateFixture()
124
    {
125
        $person1 = new CompanyEmployee();
126
        $person1->setName('Benjamin E.');
127
        $person1->setDepartment('IT');
128
        $person1->setSalary(200000);
129
130
        $person2 = new CompanyEmployee();
131
        $person2->setName('Guilherme B.');
132
        $person2->setDepartment('IT2');
133
        $person2->setSalary(400000);
134
135
        $this->_em->persist($person1);
136
        $this->_em->persist($person2);
137
        $this->_em->flush();
138
        $this->_em->clear();
139
    }
140
}
141