1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Query; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query; |
8
|
|
|
use Doctrine\ORM\Query\Lexer; |
9
|
|
|
use Doctrine\ORM\Query\Parser; |
10
|
|
|
use Doctrine\ORM\Query\QueryException; |
11
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
12
|
|
|
use Doctrine\Tests\OrmTestCase; |
13
|
|
|
|
14
|
|
|
class ParserTest extends OrmTestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @covers \Doctrine\ORM\Query\Parser::AbstractSchemaName |
19
|
|
|
* @group DDC-3715 |
20
|
|
|
*/ |
21
|
|
|
public function testAbstractSchemaNameSupportsFQCN() |
22
|
|
|
{ |
23
|
|
|
$parser = $this->createParser(CmsUser::class); |
24
|
|
|
|
25
|
|
|
self::assertEquals(CmsUser::class, $parser->AbstractSchemaName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @covers Doctrine\ORM\Query\Parser::AbstractSchemaName |
30
|
|
|
* @group DDC-3715 |
31
|
|
|
*/ |
32
|
|
|
public function testAbstractSchemaNameSupportsClassnamesWithLeadingBackslash() |
33
|
|
|
{ |
34
|
|
|
$parser = $this->createParser('\\' . CmsUser::class); |
35
|
|
|
|
36
|
|
|
self::assertEquals('\\' . CmsUser::class, $parser->AbstractSchemaName()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @covers \Doctrine\ORM\Query\Parser::AbstractSchemaName |
41
|
|
|
* @group DDC-3715 |
42
|
|
|
*/ |
43
|
|
|
public function testAbstractSchemaNameSupportsIdentifier() |
44
|
|
|
{ |
45
|
|
|
$parser = $this->createParser(\stdClass::class); |
46
|
|
|
|
47
|
|
|
self::assertEquals(\stdClass::class, $parser->AbstractSchemaName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @dataProvider validMatches |
52
|
|
|
* @covers Doctrine\ORM\Query\Parser::match |
53
|
|
|
* @group DDC-3701 |
54
|
|
|
*/ |
55
|
|
|
public function testMatch($expectedToken, $inputString) |
56
|
|
|
{ |
57
|
|
|
$parser = $this->createParser($inputString); |
58
|
|
|
|
59
|
|
|
$parser->match($expectedToken); // throws exception if not matched |
60
|
|
|
|
61
|
|
|
$this->addToAssertionCount(1); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider invalidMatches |
66
|
|
|
* @covers Doctrine\ORM\Query\Parser::match |
67
|
|
|
* @group DDC-3701 |
68
|
|
|
*/ |
69
|
|
|
public function testMatchFailure($expectedToken, $inputString) |
70
|
|
|
{ |
71
|
|
|
$this->expectException(QueryException::class); |
72
|
|
|
|
73
|
|
|
$parser = $this->createParser($inputString); |
74
|
|
|
|
75
|
|
|
$parser->match($expectedToken); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function validMatches() |
79
|
|
|
{ |
80
|
|
|
/* |
81
|
|
|
* This only covers the special case handling in the Parser that some |
82
|
|
|
* tokens that are *not* T_IDENTIFIER are accepted as well when matching |
83
|
|
|
* identifiers. |
84
|
|
|
* |
85
|
|
|
* The basic checks that tokens are classified correctly do not belong here |
86
|
|
|
* but in LexerTest. |
87
|
|
|
*/ |
88
|
|
|
return [ |
89
|
|
|
[Lexer::T_WHERE, 'where'], // keyword |
90
|
|
|
[Lexer::T_DOT, '.'], // token that cannot be an identifier |
91
|
|
|
[Lexer::T_IDENTIFIER, 'someIdentifier'], |
92
|
|
|
[Lexer::T_IDENTIFIER, 'from'], // also a terminal string (the "FROM" keyword) as in DDC-505 |
93
|
|
|
[Lexer::T_IDENTIFIER, 'comma'] |
94
|
|
|
// not even a terminal string, but the name of a constant in the Lexer (whitebox test) |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function invalidMatches() |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
[Lexer::T_DOT, 'ALL'], // ALL is a terminal string (reserved keyword) and also possibly an identifier |
102
|
|
|
[Lexer::T_DOT, ','], // "," is a token on its own, but cannot be used as identifier |
103
|
|
|
[Lexer::T_WHERE, 'WITH'], // as in DDC-3697 |
104
|
|
|
[Lexer::T_WHERE, '.'], |
105
|
|
|
|
106
|
|
|
// The following are qualified or aliased names and must not be accepted where only an Identifier is expected |
107
|
|
|
[Lexer::T_IDENTIFIER, '\\Some\\Class'], |
108
|
|
|
[Lexer::T_IDENTIFIER, 'Some\\Class'], |
109
|
|
|
[Lexer::T_IDENTIFIER, 'Some:Name'], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function createParser($dql) |
114
|
|
|
{ |
115
|
|
|
$query = new Query($this->getTestEntityManager()); |
116
|
|
|
$query->setDQL($dql); |
117
|
|
|
|
118
|
|
|
$parser = new Parser($query); |
119
|
|
|
$parser->getLexer()->moveNext(); |
120
|
|
|
|
121
|
|
|
return $parser; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|