Failed Conditions
Pull Request — 2.6 (#7197)
by JHONATAN
09:37
created

testAbstractSchemaNameSupportsClassnamesWithLeadingBackslash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Query;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Query\AST\Functions\ConcatFunction;
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\Parser;
9
use Doctrine\ORM\Query\QueryException;
10
use Doctrine\Tests\Models\CMS\CmsUser;
11
use Doctrine\Tests\OrmTestCase;
12
13
class ParserTest extends OrmTestCase
14
{
15
16
    /**
17
     * @covers \Doctrine\ORM\Query\Parser::AbstractSchemaName
18
     * @group DDC-3715
19
     */
20
    public function testAbstractSchemaNameSupportsFQCN()
21
    {
22
        $parser = $this->createParser(CmsUser::class);
23
24
        $this->assertEquals(CmsUser::class, $parser->AbstractSchemaName());
25
    }
26
27
    /**
28
     * @covers Doctrine\ORM\Query\Parser::AbstractSchemaName
29
     * @group DDC-3715
30
     */
31
    public function testAbstractSchemaNameSupportsClassnamesWithLeadingBackslash()
32
    {
33
        $parser = $this->createParser('\\' . CmsUser::class);
34
35
        $this->assertEquals('\\' . CmsUser::class, $parser->AbstractSchemaName());
36
    }
37
38
    /**
39
     * @covers \Doctrine\ORM\Query\Parser::AbstractSchemaName
40
     * @group DDC-3715
41
     */
42
    public function testAbstractSchemaNameSupportsIdentifier()
43
    {
44
        $parser = $this->createParser(\stdClass::class);
45
46
        $this->assertEquals(\stdClass::class, $parser->AbstractSchemaName());
47
    }
48
49
    /**
50
     * @covers \Doctrine\ORM\Query\Parser::AbstractSchemaName
51
     * @group DDC-3715
52
     */
53
    public function testAbstractSchemaNameSupportsNamespaceAlias()
54
    {
55
        $parser = $this->createParser('CMS:CmsUser');
56
57
        $parser->getEntityManager()->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS');
58
59
        $this->assertEquals(CmsUser::class, $parser->AbstractSchemaName());
60
    }
61
62
    /**
63
     * @covers \Doctrine\ORM\Query\Parser::AbstractSchemaName
64
     * @group DDC-3715
65
     */
66
    public function testAbstractSchemaNameSupportsNamespaceAliasWithRelativeClassname()
67
    {
68
        $parser = $this->createParser('Model:CMS\CmsUser');
69
70
        $parser->getEntityManager()->getConfiguration()->addEntityNamespace('Model', 'Doctrine\Tests\Models');
71
72
        $this->assertEquals(CmsUser::class, $parser->AbstractSchemaName());
73
    }
74
75
    /**
76
     * @dataProvider validMatches
77
     * @covers Doctrine\ORM\Query\Parser::match
78
     * @group DDC-3701
79
     */
80
    public function testMatch($expectedToken, $inputString)
81
    {
82
        $parser = $this->createParser($inputString);
83
84
        $parser->match($expectedToken); // throws exception if not matched
85
86
        $this->addToAssertionCount(1);
87
    }
88
89
    /**
90
     * @dataProvider invalidMatches
91
     * @covers Doctrine\ORM\Query\Parser::match
92
     * @group DDC-3701
93
     */
94
    public function testMatchFailure($expectedToken, $inputString)
95
    {
96
        $this->expectException(QueryException::class);
97
98
        $parser = $this->createParser($inputString);
99
100
        $parser->match($expectedToken);
101
    }
102
    
103
    public function testFunctionsOnWhereClause()
104
    {
105
        $parser = $this->createParser("WHERE SomeFunction('a', 'b')");
106
        $sql    = $parser->WhereClause();
107
        
108
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->simpleConditionalExpression);
0 ignored issues
show
Bug introduced by
The property simpleConditionalExpression does not seem to exist on Doctrine\ORM\Query\AST\ConditionalExpression.
Loading history...
109
        
110
        $parser = $this->createParser("WHERE SomeFunction('a', 'b') AND SomeFunction('a', 'b')");
111
        $sql    = $parser->WhereClause();
112
        
113
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->conditionalFactors[0]->simpleConditionalExpression);
0 ignored issues
show
Bug introduced by
The property conditionalFactors does not exist on Doctrine\ORM\Query\AST\ConditionalExpression. Did you mean conditionalTerms?
Loading history...
114
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->conditionalFactors[1]->simpleConditionalExpression);
115
116
        $parser = $this->createParser("WHERE SomeFunction('a', 'b') OR SomeFunction('a', 'b')");
117
        $sql    = $parser->WhereClause();
118
119
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->conditionalTerms[0]->simpleConditionalExpression);
120
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->conditionalTerms[1]->simpleConditionalExpression);
121
122
        $parser = $this->createParser("WHERE (SomeFunction('a', 'b') OR SomeFunction('a', 'b'))");
123
        $sql    = $parser->WhereClause();
124
        
125
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->conditionalExpression->conditionalTerms[0]->simpleConditionalExpression);
0 ignored issues
show
Bug introduced by
The property conditionalExpression does not exist on Doctrine\ORM\Query\AST\ConditionalExpression. Did you mean conditionalTerms?
Loading history...
126
        $this->assertInstanceOf(ConcatFunction::class, $sql->conditionalExpression->conditionalExpression->conditionalTerms[1]->simpleConditionalExpression);
127
    }
128
129
    public function validMatches()
130
    {
131
        /*
132
         * This only covers the special case handling in the Parser that some
133
         * tokens that are *not* T_IDENTIFIER are accepted as well when matching
134
         * identifiers.
135
         *
136
         * The basic checks that tokens are classified correctly do not belong here
137
         * but in LexerTest.
138
         */
139
        return [
140
            [Lexer::T_WHERE, 'where'], // keyword
141
            [Lexer::T_DOT, '.'], // token that cannot be an identifier
142
            [Lexer::T_IDENTIFIER, 'someIdentifier'],
143
            [Lexer::T_IDENTIFIER, 'from'], // also a terminal string (the "FROM" keyword) as in DDC-505
144
            [Lexer::T_IDENTIFIER, 'comma']
145
            // not even a terminal string, but the name of a constant in the Lexer (whitebox test)
146
        ];
147
    }
148
149
    public function invalidMatches()
150
    {
151
        return [
152
            [Lexer::T_DOT, 'ALL'], // ALL is a terminal string (reserved keyword) and also possibly an identifier
153
            [Lexer::T_DOT, ','], // "," is a token on its own, but cannot be used as identifier
154
            [Lexer::T_WHERE, 'WITH'], // as in DDC-3697
155
            [Lexer::T_WHERE, '.'],
156
157
            // The following are qualified or aliased names and must not be accepted where only an Identifier is expected
158
            [Lexer::T_IDENTIFIER, '\\Some\\Class'],
159
            [Lexer::T_IDENTIFIER, 'Some\\Class'],
160
            [Lexer::T_IDENTIFIER, 'Some:Name'],
161
        ];
162
    }
163
164
    private function createParser($dql)
165
    {
166
        $manager = $this->_getTestEntityManager();
167
        
168
        $manager->getConfiguration()->addCustomBooleanFunction('SomeFunction', function () {
169
            return new ConcatFunction('SomeFunction');
170
        });
171
        
172
        $query = new Query($manager);
173
        $query->setDQL($dql);
174
175
        $parser = new Parser($query);
176
        $parser->getLexer()->moveNext();
177
178
        return $parser;
179
    }
180
}
181