Failed Conditions
Push — 2.7 ( aeef8f...195140 )
by Michael
08:10
created

GH7286CustomConcat::getSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
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
        $this->_em->persist(new GH7286Entity('foo', 1));
28
        $this->_em->persist(new GH7286Entity('foo', 2));
29
        $this->_em->persist(new GH7286Entity('bar', 3));
30
        $this->_em->persist(new GH7286Entity(null, 4));
31
        $this->_em->flush();
32
        $this->_em->clear();
33
    }
34
35
    public function testAggregateExpressionInFunction() : void
36
    {
37
        $query = $this->_em->createQuery(
38
            'SELECT CONCAT(e.type, MIN(e.version)) pair'
39
            . ' FROM ' . GH7286Entity::class . ' e'
40
            . ' WHERE e.type IS NOT NULL'
41
            . ' GROUP BY e.type'
42
            . ' ORDER BY e.type'
43
        );
44
45
        self::assertSame(
46
            [
47
                ['pair' => 'bar3'],
48
                ['pair' => 'foo1'],
49
            ],
50
            $query->getArrayResult()
51
        );
52
    }
53
54
    /**
55
     * @group DDC-1091
56
     */
57
    public function testAggregateFunctionInCustomFunction() : void
58
    {
59
        $this->_em->getConfiguration()->addCustomStringFunction('CC', GH7286CustomConcat::class);
60
61
        $query = $this->_em->createQuery(
62
            'SELECT CC(e.type, MIN(e.version)) pair'
63
            . ' FROM ' . GH7286Entity::class . ' e'
64
            . ' WHERE e.type IS NOT NULL AND e.type != :type'
65
            . ' GROUP BY e.type'
66
        );
67
        $query->setParameter('type', 'bar');
68
69
        self::assertSame(
70
            ['pair' => 'foo1'],
71
            $query->getSingleResult()
72
        );
73
    }
74
}
75
76
/**
77
 * @Entity
78
 */
79
class GH7286Entity
80
{
81
    /**
82
     * @Id
83
     * @Column(type="integer")
84
     * @GeneratedValue
85
     * @var int
86
     */
87
    public $id;
88
89
    /**
90
     * @Column(nullable=true)
91
     * @var string|null
92
     */
93
    public $type;
94
95
    /**
96
     * @Column(type="integer")
97
     * @var int
98
     */
99
    public $version;
100
101
    public function __construct(?string $type, int $version)
102
    {
103
        $this->type    = $type;
104
        $this->version = $version;
105
    }
106
}
107
108
class GH7286CustomConcat extends FunctionNode
109
{
110
    /** @var Node */
111
    private $first;
112
113
    /** @var Node */
114
    private $second;
115
116
    public function parse(Parser $parser) : void
117
    {
118
        $parser->match(Lexer::T_IDENTIFIER);
119
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
120
121
        $this->first = $parser->StringPrimary();
122
        $parser->match(Lexer::T_COMMA);
123
        $this->second = $parser->StringPrimary();
124
125
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
126
    }
127
128
    public function getSql(SqlWalker $walker) : string
129
    {
130
        return $walker->getConnection()->getDatabasePlatform()->getConcatExpression(
131
            $this->first->dispatch($walker),
132
            $this->second->dispatch($walker)
133
        );
134
    }
135
}
136