Completed
Pull Request — 2.6 (#7296)
by Michael
08:35
created

GH7286Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
6
use Doctrine\ORM\Query\AST\Node;
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\Parser;
9
use Doctrine\ORM\Query\SqlWalker;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
12
final class GH7286Test extends OrmFunctionalTestCase
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    protected function setUp() : void
18
    {
19
        parent::setUp();
20
21
        $this->setUpEntitySchema(
22
            [
23
                GH7286Entity::class,
24
            ]
25
        );
26
27
        $entityA = new GH7286Entity('foo', 1);
28
        $entityB = new GH7286Entity('foo', 2);
29
        $entityC = new GH7286Entity('bar', 3);
30
31
        $this->_em->persist($entityA);
32
        $this->_em->persist($entityB);
33
        $this->_em->persist($entityC);
34
        $this->_em->flush();
35
        $this->_em->clear();
36
    }
37
38
    public function testAggregateExpressionInFunction() : void
39
    {
40
        $query = $this->_em->createQuery(
41
            'SELECT CONCAT(e.type, MIN(e.version)) pair'
42
            . ' FROM ' . GH7286Entity::class . ' e'
43
            . ' GROUP BY e.type'
44
            . ' ORDER BY e.type'
45
        );
46
47
        self::assertSame(
48
            [
49
                ['pair' => 'bar3'],
50
                ['pair' => 'foo1'],
51
            ],
52
            $query->getArrayResult()
53
        );
54
    }
55
56
    /**
57
     * @group DDC-1091
58
     */
59
    public function testAggregateFunctionInCustomFunction()
60
    {
61
        $this->_em->getConfiguration()->addCustomStringFunction('CC', GH7286CustomConcat::class);
62
63
        $entityA = new GH7286Entity('foo', 1);
64
        $this->_em->persist($entityA);
65
        $this->_em->flush();
66
        $this->_em->clear();
67
68
        $query = $this->_em->createQuery(
69
            'SELECT CC(e.type, MIN(e.version)) pair'
70
            . ' FROM ' . GH7286Entity::class . ' e'
71
            . ' WHERE e.type = :type'
72
        );
73
        $query->setParameter('type', 'foo');
74
75
        self::assertSame(
76
            ['pair' => 'foo1'],
77
            $query->getSingleResult()
78
        );
79
    }
80
}
81
82
/**
83
 * @Entity
84
 */
85
class GH7286Entity
86
{
87
    /**
88
     * @Id
89
     * @Column(type="integer")
90
     * @GeneratedValue
91
     * @var int
92
     */
93
    public $id;
94
95
    /**
96
     * @Column
97
     * @var string
98
     */
99
    public $type;
100
101
    /**
102
     * @Column(type="integer")
103
     * @var int
104
     */
105
    public $version;
106
107
    public function __construct(string $type, int $version)
108
    {
109
        $this->type    = $type;
110
        $this->version = $version;
111
    }
112
}
113
114
class GH7286CustomConcat extends FunctionNode
115
{
116
    /** @var Node */
117
    private $first;
118
119
    /** @var Node */
120
    private $second;
121
122
    public function parse(Parser $parser): void
123
    {
124
        $parser->match(Lexer::T_IDENTIFIER);
125
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
126
127
        $this->first = $parser->StringPrimary();
128
        $parser->match(Lexer::T_COMMA);
129
        $this->second = $parser->StringPrimary();
130
131
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
132
    }
133
134
    public function getSql(SqlWalker $walker): string
135
    {
136
        return $walker->getConnection()->getDatabasePlatform()->getConcatExpression(
137
            $this->first->dispatch($walker),
138
            $this->second->dispatch($walker)
139
        );
140
    }
141
}
142