Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Query / LexerTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Query;
4
5 use Doctrine\ORM\Query\Lexer;
6
7 require_once __DIR__ . '/../../TestInit.php';
8
9 class LexerTest extends \Doctrine\Tests\OrmTestCase
10 {
11     //private $_lexer;
12
13     protected function setUp() {
14     }
15
16     public function testScannerRecognizesIdentifierWithLengthOfOneCharacter()
17     {
18         $lexer = new Lexer('u');
19
20         $lexer->moveNext();
21         $token = $lexer->lookahead;
22
23         $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
24         $this->assertEquals('u', $token['value']);
25     }
26
27     public function testScannerRecognizesIdentifierConsistingOfLetters()
28     {
29         $lexer = new Lexer('someIdentifier');
30
31         $lexer->moveNext();
32         $token = $lexer->lookahead;
33         $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
34         $this->assertEquals('someIdentifier', $token['value']);
35     }
36
37     public function testScannerRecognizesIdentifierIncludingDigits()
38     {
39         $lexer = new Lexer('s0m31d3nt1f13r');
40
41         $lexer->moveNext();
42         $token = $lexer->lookahead;
43         $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
44         $this->assertEquals('s0m31d3nt1f13r', $token['value']);
45     }
46
47     public function testScannerRecognizesIdentifierIncludingUnderscore()
48     {
49         $lexer = new Lexer('some_identifier');
50         $lexer->moveNext();
51         $token = $lexer->lookahead;
52         $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
53         $this->assertEquals('some_identifier', $token['value']);
54     }
55
56     public function testScannerRecognizesDecimalInteger()
57     {
58         $lexer = new Lexer('1234');
59         $lexer->moveNext();
60         $token = $lexer->lookahead;
61         $this->assertEquals(Lexer::T_INTEGER, $token['type']);
62         $this->assertEquals(1234, $token['value']);
63     }
64
65     public function testScannerRecognizesFloat()
66     {
67         $lexer = new Lexer('1.234');
68         $lexer->moveNext();
69         $token = $lexer->lookahead;
70         $this->assertEquals(Lexer::T_FLOAT, $token['type']);
71         $this->assertEquals(1.234, $token['value']);
72     }
73
74     public function testScannerRecognizesFloatWithExponent()
75     {
76         $lexer = new Lexer('1.2e3');
77         $lexer->moveNext();
78         $token = $lexer->lookahead;
79         $this->assertEquals(Lexer::T_FLOAT, $token['type']);
80         $this->assertEquals(1.2e3, $token['value']);
81     }
82
83     public function testScannerRecognizesFloatWithExponent2()
84     {
85         $lexer = new Lexer('0.2e3');
86         $lexer->moveNext();
87         $token = $lexer->lookahead;
88         $this->assertEquals(Lexer::T_FLOAT, $token['type']);
89         $this->assertEquals(.2e3, $token['value']);
90     }
91
92     public function testScannerRecognizesFloatWithNegativeExponent()
93     {
94         $lexer = new Lexer('7E-10');
95         $lexer->moveNext();
96         $token = $lexer->lookahead;
97         $this->assertEquals(Lexer::T_FLOAT, $token['type']);
98         $this->assertEquals(7E-10, $token['value']);
99     }
100
101     public function testScannerRecognizesFloatBig()
102     {
103         $lexer = new Lexer('123456789.01');
104         $lexer->moveNext();
105         $token = $lexer->lookahead;
106         $this->assertEquals(Lexer::T_FLOAT, $token['type']);
107         $this->assertEquals(1.2345678901e8, $token['value']);
108     }
109
110     public function testScannerRecognizesFloatContainingWhitespace()
111     {
112         $lexer = new Lexer('-   1.234e2');
113         $lexer->moveNext();
114         $token = $lexer->lookahead;
115         $this->assertEquals(Lexer::T_MINUS, $token['type']);
116         $this->assertEquals('-', $token['value']);
117
118         $lexer->moveNext();
119         $token = $lexer->lookahead;
120         $this->assertEquals(Lexer::T_FLOAT, $token['type']);
121         $this->assertNotEquals(-1.234e2, $token['value']);
122         $this->assertEquals(1.234e2, $token['value']);
123     }
124
125     public function testScannerRecognizesStringContainingWhitespace()
126     {
127         $lexer = new Lexer("'This is a string.'");
128         $lexer->moveNext();
129         $token = $lexer->lookahead;
130         $this->assertEquals(Lexer::T_STRING, $token['type']);
131         $this->assertEquals("This is a string.", $token['value']);
132     }
133
134     public function testScannerRecognizesStringContainingSingleQuotes()
135     {
136         $lexer = new Lexer("'abc''defg'''");
137         $lexer->moveNext();
138         $token = $lexer->lookahead;
139         $this->assertEquals(Lexer::T_STRING, $token['type']);
140         $this->assertEquals("abc'defg'", $token['value']);
141     }
142
143     public function testScannerRecognizesInputParameter()
144     {
145         $lexer = new Lexer('?1');
146         $lexer->moveNext();
147         $token = $lexer->lookahead;
148         $this->assertEquals(Lexer::T_INPUT_PARAMETER, $token['type']);
149         $this->assertEquals('?1', $token['value']);
150     }
151
152     public function testScannerRecognizesNamedInputParameter()
153     {
154         $lexer = new Lexer(':name');
155         $lexer->moveNext();
156         $token = $lexer->lookahead;
157         $this->assertEquals(Lexer::T_INPUT_PARAMETER, $token['type']);
158         $this->assertEquals(':name', $token['value']);
159     }
160
161     public function testScannerTokenizesASimpleQueryCorrectly()
162     {
163         $dql = "SELECT u FROM My\Namespace\User u WHERE u.name = 'Jack O''Neil'";
164         $lexer = new Lexer($dql);
165
166         $tokens = array(
167             array(
168                 'value' => 'SELECT',
169                 'type'  => Lexer::T_SELECT,
170                 'position' => 0
171             ),
172             array(
173                 'value' => 'u',
174                 'type'  => Lexer::T_IDENTIFIER,
175                 'position' => 7
176             ),
177             array(
178                 'value' => 'FROM',
179                 'type'  => Lexer::T_FROM,
180                 'position' => 9
181             ),
182             array(
183                 'value' => 'My\Namespace\User',
184                 'type'  => Lexer::T_IDENTIFIER,
185                 'position' => 14
186             ),
187             array(
188                 'value' => 'u',
189                 'type'  => Lexer::T_IDENTIFIER,
190                 'position' => 32
191             ),
192             array(
193                 'value' => 'WHERE',
194                 'type'  => Lexer::T_WHERE,
195                 'position' => 34
196             ),
197             array(
198                 'value' => 'u',
199                 'type'  => Lexer::T_IDENTIFIER,
200                 'position' => 40
201             ),
202             array(
203                 'value' => '.',
204                 'type'  => Lexer::T_DOT,
205                 'position' => 41
206             ),
207             array(
208                 'value' => 'name',
209                 'type'  => Lexer::T_IDENTIFIER,
210                 'position' => 42
211             ),
212             array(
213                 'value' => '=',
214                 'type'  => Lexer::T_EQUALS,
215                 'position' => 47
216             ),
217             array(
218                 'value' => "Jack O'Neil",
219                 'type'  => Lexer::T_STRING,
220                 'position' => 49
221             )
222         );
223
224         foreach ($tokens as $expected) {
225             $lexer->moveNext();
226             $actual = $lexer->lookahead;
227             $this->assertEquals($expected['value'], $actual['value']);
228             $this->assertEquals($expected['type'], $actual['type']);
229             $this->assertEquals($expected['position'], $actual['position']);
230         }
231
232         $this->assertFalse($lexer->moveNext());
233     }
234 }