Failed Conditions
Push — master ( 388afb...c32ba8 )
by Luís
01:26
created

CustomCount::getSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
7
use Doctrine\ORM\Query\AST\PathExpression;
8
use Doctrine\ORM\Query\Lexer;
9
use Doctrine\ORM\Query\Parser;
10
use Doctrine\ORM\Query\SqlWalker;
11
use Doctrine\Tests\Models\CMS\CmsUser;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
14
require_once __DIR__ . '/../../TestInit.php';
15
16
class CustomFunctionsTest extends OrmFunctionalTestCase
17
{
18
    protected function setUp()
19
    {
20
        $this->useModelSet('cms');
21
22
        parent::setUp();
23
    }
24
25
    public function testCustomFunctionDefinedWithCallback()
26
    {
27
        $user = new CmsUser();
28
        $user->name = 'Bob';
29
        $user->username = 'Dylan';
30
        $this->_em->persist($user);
31
        $this->_em->flush();
32
33
        // Instead of defining the function with the class name, we use a callback
34
        $this->_em->getConfiguration()->addCustomStringFunction('FOO', function($funcName) {
35
            return new NoOp($funcName);
36
        });
37
        $this->_em->getConfiguration()->addCustomNumericFunction('BAR', function($funcName) {
38
            return new NoOp($funcName);
39
        });
40
41
        $query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u'
42
            . ' WHERE FOO(u.name) = \'Bob\''
43
            . ' AND BAR(1) = 1');
44
45
        $users = $query->getResult();
46
47
        $this->assertEquals(1, count($users));
48
        $this->assertSame($user, $users[0]);
49
    }
50
51
    public function testCustomFunctionOverride()
52
    {
53
        $user = new CmsUser();
54
        $user->name = 'Bob';
55
        $user->username = 'Dylan';
56
        $this->_em->persist($user);
57
        $this->_em->flush();
58
59
        $this->_em->getConfiguration()->addCustomStringFunction('COUNT', 'Doctrine\Tests\ORM\Functional\CustomCount');
60
61
        $query = $this->_em->createQuery('SELECT COUNT(DISTINCT u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u');
62
63
        $usersCount = $query->getSingleScalarResult();
64
65
        $this->assertEquals(1, $usersCount);
66
    }
67
}
68
69
class NoOp extends FunctionNode
70
{
71
    /**
72
     * @var PathExpression
73
     */
74
    private $field;
75
76
    public function parse(Parser $parser)
77
    {
78
        $parser->match(Lexer::T_IDENTIFIER);
79
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
80
        $this->field = $parser->ArithmeticPrimary();
81
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
82
    }
83
84
    public function getSql(SqlWalker $sqlWalker)
85
    {
86
        return $this->field->dispatch($sqlWalker);
87
    }
88
}
89
90
class CustomCount extends FunctionNode
91
{
92
    /**
93
     * @var Query\AST\AggregateExpression
94
     */
95
    private $aggregateExpression;
96
97
    public function parse(Parser $parser): void
98
    {
99
        $this->aggregateExpression = $parser->AggregateExpression();
100
    }
101
102
    public function getSql(SqlWalker $sqlWalker): string
103
    {
104
        return $this->aggregateExpression->dispatch($sqlWalker);
105
    }
106
}
107