Passed
Pull Request — master (#6476)
by Luís
15:25
created

CustomFunctionsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
B testCustomFunctionDefinedWithCallback() 0 25 1
A testCustomFunctionOverride() 0 16 1
B testCustomFunctionWithinInExpression() 0 24 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
    public function testCustomFunctionWithinInExpression() : void
69
    {
70
        $user           = new CmsUser();
71
        $user->name     = 'Lo';
72
        $user->username = 'Doe';
73
74
        $this->_em->persist($user);
75
        $this->_em->flush();
76
77
        $this->_em->getConfiguration()->addCustomStringFunction(
78
            'FOO',
79
            function (string $funcName) : NoOp {
80
                return new NoOp($funcName);
81
            }
82
        );
83
84
        $query = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN (FOO(\'Lo\'), \'Lo\', :name)';
85
        $users = $this->_em->createQuery($query)
86
                           ->setParameter('name', 'Lo')
87
                           ->getResult();
88
89
        self::assertCount(1, $users);
90
        self::assertSame($user, $users[0]);
91
    }
92
}
93
94
class NoOp extends FunctionNode
95
{
96
    /**
97
     * @var PathExpression
98
     */
99
    private $field;
100
101
    public function parse(Parser $parser)
102
    {
103
        $parser->match(Lexer::T_IDENTIFIER);
104
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
105
        $this->field = $parser->ArithmeticPrimary();
106
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
107
    }
108
109
    public function getSql(SqlWalker $sqlWalker)
110
    {
111
        return $this->field->dispatch($sqlWalker);
112
    }
113
}
114
115
class CustomCount extends FunctionNode
116
{
117
    /**
118
     * @var Query\AST\AggregateExpression
119
     */
120
    private $aggregateExpression;
121
122
    public function parse(Parser $parser): void
123
    {
124
        $this->aggregateExpression = $parser->AggregateExpression();
125
    }
126
127
    public function getSql(SqlWalker $sqlWalker): string
128
    {
129
        return $this->aggregateExpression->dispatch($sqlWalker);
130
    }
131
}
132