1<?php
2/**
3 * Tests for the \PHP_CodeSniffer\Files\File:findEndOfStatement method.
4 *
5 * @author    Greg Sherwood <gsherwood@squiz.net>
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8 */
9
10namespace PHP_CodeSniffer\Tests\Core\File;
11
12use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13
14class FindEndOfStatementTest extends AbstractMethodUnitTest
15{
16
17
18    /**
19     * Test a simple assignment.
20     *
21     * @return void
22     */
23    public function testSimpleAssignment()
24    {
25        $start = $this->getTargetToken('/* testSimpleAssignment */', T_VARIABLE);
26        $found = self::$phpcsFile->findEndOfStatement($start);
27
28        $this->assertSame(($start + 5), $found);
29
30    }//end testSimpleAssignment()
31
32
33    /**
34     * Test a direct call to a control structure.
35     *
36     * @return void
37     */
38    public function testControlStructure()
39    {
40        $start = $this->getTargetToken('/* testControlStructure */', T_WHILE);
41        $found = self::$phpcsFile->findEndOfStatement($start);
42
43        $this->assertSame(($start + 6), $found);
44
45    }//end testControlStructure()
46
47
48    /**
49     * Test the assignment of a closure.
50     *
51     * @return void
52     */
53    public function testClosureAssignment()
54    {
55        $start = $this->getTargetToken('/* testClosureAssignment */', T_VARIABLE, '$a');
56        $found = self::$phpcsFile->findEndOfStatement($start);
57
58        $this->assertSame(($start + 13), $found);
59
60    }//end testClosureAssignment()
61
62
63    /**
64     * Test using a heredoc in a function argument.
65     *
66     * @return void
67     */
68    public function testHeredocFunctionArg()
69    {
70        // Find the end of the function.
71        $start = $this->getTargetToken('/* testHeredocFunctionArg */', T_STRING, 'myFunction');
72        $found = self::$phpcsFile->findEndOfStatement($start);
73
74        $this->assertSame(($start + 10), $found);
75
76        // Find the end of the heredoc.
77        $start += 2;
78        $found  = self::$phpcsFile->findEndOfStatement($start);
79
80        $this->assertSame(($start + 4), $found);
81
82        // Find the end of the last arg.
83        $start = ($found + 2);
84        $found = self::$phpcsFile->findEndOfStatement($start);
85
86        $this->assertSame($start, $found);
87
88    }//end testHeredocFunctionArg()
89
90
91    /**
92     * Test parts of a switch statement.
93     *
94     * @return void
95     */
96    public function testSwitch()
97    {
98        // Find the end of the switch.
99        $start = $this->getTargetToken('/* testSwitch */', T_SWITCH);
100        $found = self::$phpcsFile->findEndOfStatement($start);
101
102        $this->assertSame(($start + 28), $found);
103
104        // Find the end of the case.
105        $start += 9;
106        $found  = self::$phpcsFile->findEndOfStatement($start);
107
108        $this->assertSame(($start + 8), $found);
109
110        // Find the end of default case.
111        $start += 11;
112        $found  = self::$phpcsFile->findEndOfStatement($start);
113
114        $this->assertSame(($start + 6), $found);
115
116    }//end testSwitch()
117
118
119    /**
120     * Test statements that are array values.
121     *
122     * @return void
123     */
124    public function testStatementAsArrayValue()
125    {
126        // Test short array syntax.
127        $start = $this->getTargetToken('/* testStatementAsArrayValue */', T_NEW);
128        $found = self::$phpcsFile->findEndOfStatement($start);
129
130        $this->assertSame(($start + 2), $found);
131
132        // Test long array syntax.
133        $start += 12;
134        $found  = self::$phpcsFile->findEndOfStatement($start);
135
136        $this->assertSame(($start + 2), $found);
137
138        // Test same statement outside of array.
139        $start += 10;
140        $found  = self::$phpcsFile->findEndOfStatement($start);
141
142        $this->assertSame(($start + 3), $found);
143
144    }//end testStatementAsArrayValue()
145
146
147    /**
148     * Test a use group.
149     *
150     * @return void
151     */
152    public function testUseGroup()
153    {
154        $start = $this->getTargetToken('/* testUseGroup */', T_USE);
155        $found = self::$phpcsFile->findEndOfStatement($start);
156
157        $this->assertSame(($start + 23), $found);
158
159    }//end testUseGroup()
160
161
162    /**
163     * Test arrow function as array value.
164     *
165     * @return void
166     */
167    public function testArrowFunctionArrayValue()
168    {
169        $start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_FN);
170        $found = self::$phpcsFile->findEndOfStatement($start);
171
172        $this->assertSame(($start + 9), $found);
173
174    }//end testArrowFunctionArrayValue()
175
176
177    /**
178     * Test static arrow function.
179     *
180     * @return void
181     */
182    public function testStaticArrowFunction()
183    {
184        $static = $this->getTargetToken('/* testStaticArrowFunction */', T_STATIC);
185        $fn     = $this->getTargetToken('/* testStaticArrowFunction */', T_FN);
186
187        $endOfStatementStatic = self::$phpcsFile->findEndOfStatement($static);
188        $endOfStatementFn     = self::$phpcsFile->findEndOfStatement($fn);
189
190        $this->assertSame($endOfStatementFn, $endOfStatementStatic);
191
192    }//end testStaticArrowFunction()
193
194
195    /**
196     * Test arrow function with return value.
197     *
198     * @return void
199     */
200    public function testArrowFunctionReturnValue()
201    {
202        $start = $this->getTargetToken('/* testArrowFunctionReturnValue */', T_FN);
203        $found = self::$phpcsFile->findEndOfStatement($start);
204
205        $this->assertSame(($start + 18), $found);
206
207    }//end testArrowFunctionReturnValue()
208
209
210    /**
211     * Test arrow function used as a function argument.
212     *
213     * @return void
214     */
215    public function testArrowFunctionAsArgument()
216    {
217        $start = $this->getTargetToken('/* testArrowFunctionAsArgument */', T_FN);
218        $found = self::$phpcsFile->findEndOfStatement($start);
219
220        $this->assertSame(($start + 8), $found);
221
222    }//end testArrowFunctionAsArgument()
223
224
225    /**
226     * Test arrow function with arrays used as a function argument.
227     *
228     * @return void
229     */
230    public function testArrowFunctionWithArrayAsArgument()
231    {
232        $start = $this->getTargetToken('/* testArrowFunctionWithArrayAsArgument */', T_FN);
233        $found = self::$phpcsFile->findEndOfStatement($start);
234
235        $this->assertSame(($start + 17), $found);
236
237    }//end testArrowFunctionWithArrayAsArgument()
238
239
240    /**
241     * Test simple match expression case.
242     *
243     * @return void
244     */
245    public function testMatchCase()
246    {
247        $start = $this->getTargetToken('/* testMatchCase */', T_LNUMBER);
248        $found = self::$phpcsFile->findEndOfStatement($start);
249
250        $this->assertSame(($start + 5), $found);
251
252        $start = $this->getTargetToken('/* testMatchCase */', T_CONSTANT_ENCAPSED_STRING);
253        $found = self::$phpcsFile->findEndOfStatement($start);
254
255        $this->assertSame(($start + 1), $found);
256
257    }//end testMatchCase()
258
259
260    /**
261     * Test simple match expression default case.
262     *
263     * @return void
264     */
265    public function testMatchDefault()
266    {
267        $start = $this->getTargetToken('/* testMatchDefault */', T_MATCH_DEFAULT);
268        $found = self::$phpcsFile->findEndOfStatement($start);
269
270        $this->assertSame(($start + 4), $found);
271
272        $start = $this->getTargetToken('/* testMatchDefault */', T_CONSTANT_ENCAPSED_STRING);
273        $found = self::$phpcsFile->findEndOfStatement($start);
274
275        $this->assertSame($start, $found);
276
277    }//end testMatchDefault()
278
279
280    /**
281     * Test multiple comma-separated match expression case values.
282     *
283     * @return void
284     */
285    public function testMatchMultipleCase()
286    {
287        $start = $this->getTargetToken('/* testMatchMultipleCase */', T_LNUMBER);
288        $found = self::$phpcsFile->findEndOfStatement($start);
289        $this->assertSame(($start + 13), $found);
290
291        $start += 6;
292        $found  = self::$phpcsFile->findEndOfStatement($start);
293        $this->assertSame(($start + 7), $found);
294
295    }//end testMatchMultipleCase()
296
297
298    /**
299     * Test match expression default case with trailing comma.
300     *
301     * @return void
302     */
303    public function testMatchDefaultComma()
304    {
305        $start = $this->getTargetToken('/* testMatchDefaultComma */', T_MATCH_DEFAULT);
306        $found = self::$phpcsFile->findEndOfStatement($start);
307
308        $this->assertSame(($start + 5), $found);
309
310    }//end testMatchDefaultComma()
311
312
313    /**
314     * Test match expression with function call.
315     *
316     * @return void
317     */
318    public function testMatchFunctionCall()
319    {
320        $start = $this->getTargetToken('/* testMatchFunctionCall */', T_STRING);
321        $found = self::$phpcsFile->findEndOfStatement($start);
322
323        $this->assertSame(($start + 12), $found);
324
325        $start += 8;
326        $found  = self::$phpcsFile->findEndOfStatement($start);
327
328        $this->assertSame(($start + 1), $found);
329
330    }//end testMatchFunctionCall()
331
332
333    /**
334     * Test match expression with function call in the arm.
335     *
336     * @return void
337     */
338    public function testMatchFunctionCallArm()
339    {
340        // Check the first case.
341        $start = $this->getTargetToken('/* testMatchFunctionCallArm */', T_STRING);
342        $found = self::$phpcsFile->findEndOfStatement($start);
343
344        $this->assertSame(($start + 21), $found);
345
346        // Check the second case.
347        $start += 24;
348        $found  = self::$phpcsFile->findEndOfStatement($start);
349
350        $this->assertSame(($start + 21), $found);
351
352    }//end testMatchFunctionCallArm()
353
354
355    /**
356     * Test match expression with closure.
357     *
358     * @return void
359     */
360    public function testMatchClosure()
361    {
362        $start = $this->getTargetToken('/* testMatchClosure */', T_LNUMBER);
363        $found = self::$phpcsFile->findEndOfStatement($start);
364
365        $this->assertSame(($start + 14), $found);
366
367        $start += 17;
368        $found  = self::$phpcsFile->findEndOfStatement($start);
369
370        $this->assertSame(($start + 14), $found);
371
372    }//end testMatchClosure()
373
374
375    /**
376     * Test match expression with array declaration.
377     *
378     * @return void
379     */
380    public function testMatchArray()
381    {
382        $start = $this->getTargetToken('/* testMatchArray */', T_LNUMBER);
383        $found = self::$phpcsFile->findEndOfStatement($start);
384
385        $this->assertSame(($start + 11), $found);
386
387        $start += 14;
388        $found  = self::$phpcsFile->findEndOfStatement($start);
389
390        $this->assertSame(($start + 22), $found);
391
392    }//end testMatchArray()
393
394
395    /**
396     * Test nested match expressions.
397     *
398     * @return void
399     */
400    public function testNestedMatch()
401    {
402        $start = $this->getTargetToken('/* testNestedMatch */', T_LNUMBER);
403        $found = self::$phpcsFile->findEndOfStatement($start);
404
405        $this->assertSame(($start + 30), $found);
406
407        $start += 21;
408        $found  = self::$phpcsFile->findEndOfStatement($start);
409
410        $this->assertSame(($start + 5), $found);
411
412    }//end testNestedMatch()
413
414
415}//end class
416