Completed
Pull Request — master (#7405)
by Michael
68:49 queued 63:08
created

GH7286Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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