1<?php 2/** 3 * Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement 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 FindStartOfStatementTest 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_SEMICOLON); 26 $found = self::$phpcsFile->findStartOfStatement($start); 27 28 $this->assertSame(($start - 5), $found); 29 30 }//end testSimpleAssignment() 31 32 33 /** 34 * Test a function call. 35 * 36 * @return void 37 */ 38 public function testFunctionCall() 39 { 40 $start = $this->getTargetToken('/* testFunctionCall */', T_CLOSE_PARENTHESIS); 41 $found = self::$phpcsFile->findStartOfStatement($start); 42 43 $this->assertSame(($start - 6), $found); 44 45 }//end testFunctionCall() 46 47 48 /** 49 * Test a function call. 50 * 51 * @return void 52 */ 53 public function testFunctionCallArgument() 54 { 55 $start = $this->getTargetToken('/* testFunctionCallArgument */', T_VARIABLE, '$b'); 56 $found = self::$phpcsFile->findStartOfStatement($start); 57 58 $this->assertSame($start, $found); 59 60 }//end testFunctionCallArgument() 61 62 63 /** 64 * Test a direct call to a control structure. 65 * 66 * @return void 67 */ 68 public function testControlStructure() 69 { 70 $start = $this->getTargetToken('/* testControlStructure */', T_CLOSE_CURLY_BRACKET); 71 $found = self::$phpcsFile->findStartOfStatement($start); 72 73 $this->assertSame(($start - 6), $found); 74 75 }//end testControlStructure() 76 77 78 /** 79 * Test the assignment of a closure. 80 * 81 * @return void 82 */ 83 public function testClosureAssignment() 84 { 85 $start = $this->getTargetToken('/* testClosureAssignment */', T_CLOSE_CURLY_BRACKET); 86 $found = self::$phpcsFile->findStartOfStatement($start); 87 88 $this->assertSame(($start - 12), $found); 89 90 }//end testClosureAssignment() 91 92 93 /** 94 * Test using a heredoc in a function argument. 95 * 96 * @return void 97 */ 98 public function testHeredocFunctionArg() 99 { 100 // Find the start of the function. 101 $start = $this->getTargetToken('/* testHeredocFunctionArg */', T_SEMICOLON); 102 $found = self::$phpcsFile->findStartOfStatement($start); 103 104 $this->assertSame(($start - 10), $found); 105 106 // Find the start of the heredoc. 107 $start -= 4; 108 $found = self::$phpcsFile->findStartOfStatement($start); 109 110 $this->assertSame(($start - 4), $found); 111 112 // Find the start of the last arg. 113 $start += 2; 114 $found = self::$phpcsFile->findStartOfStatement($start); 115 116 $this->assertSame($start, $found); 117 118 }//end testHeredocFunctionArg() 119 120 121 /** 122 * Test parts of a switch statement. 123 * 124 * @return void 125 */ 126 public function testSwitch() 127 { 128 // Find the start of the switch. 129 $start = $this->getTargetToken('/* testSwitch */', T_CLOSE_CURLY_BRACKET); 130 $found = self::$phpcsFile->findStartOfStatement($start); 131 132 $this->assertSame(($start - 47), $found); 133 134 // Find the start of default case. 135 $start -= 5; 136 $found = self::$phpcsFile->findStartOfStatement($start); 137 138 $this->assertSame(($start - 6), $found); 139 140 // Find the start of the second case. 141 $start -= 12; 142 $found = self::$phpcsFile->findStartOfStatement($start); 143 144 $this->assertSame(($start - 5), $found); 145 146 // Find the start of the first case. 147 $start -= 13; 148 $found = self::$phpcsFile->findStartOfStatement($start); 149 150 $this->assertSame(($start - 8), $found); 151 152 // Test inside the first case. 153 $start--; 154 $found = self::$phpcsFile->findStartOfStatement($start); 155 156 $this->assertSame(($start - 1), $found); 157 158 }//end testSwitch() 159 160 161 /** 162 * Test statements that are array values. 163 * 164 * @return void 165 */ 166 public function testStatementAsArrayValue() 167 { 168 // Test short array syntax. 169 $start = $this->getTargetToken('/* testStatementAsArrayValue */', T_STRING, 'Datetime'); 170 $found = self::$phpcsFile->findStartOfStatement($start); 171 172 $this->assertSame(($start - 2), $found); 173 174 // Test long array syntax. 175 $start += 12; 176 $found = self::$phpcsFile->findStartOfStatement($start); 177 178 $this->assertSame(($start - 2), $found); 179 180 // Test same statement outside of array. 181 $start++; 182 $found = self::$phpcsFile->findStartOfStatement($start); 183 184 $this->assertSame(($start - 9), $found); 185 186 // Test with an array index. 187 $start += 17; 188 $found = self::$phpcsFile->findStartOfStatement($start); 189 190 $this->assertSame(($start - 5), $found); 191 192 }//end testStatementAsArrayValue() 193 194 195 /** 196 * Test a use group. 197 * 198 * @return void 199 */ 200 public function testUseGroup() 201 { 202 $start = $this->getTargetToken('/* testUseGroup */', T_SEMICOLON); 203 $found = self::$phpcsFile->findStartOfStatement($start); 204 205 $this->assertSame(($start - 23), $found); 206 207 }//end testUseGroup() 208 209 210 /** 211 * Test arrow function as array value. 212 * 213 * @return void 214 */ 215 public function testArrowFunctionArrayValue() 216 { 217 $start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_COMMA); 218 $found = self::$phpcsFile->findStartOfStatement($start); 219 220 $this->assertSame(($start - 9), $found); 221 222 }//end testArrowFunctionArrayValue() 223 224 225 /** 226 * Test static arrow function. 227 * 228 * @return void 229 */ 230 public function testStaticArrowFunction() 231 { 232 $start = $this->getTargetToken('/* testStaticArrowFunction */', T_SEMICOLON); 233 $found = self::$phpcsFile->findStartOfStatement($start); 234 235 $this->assertSame(($start - 11), $found); 236 237 }//end testStaticArrowFunction() 238 239 240 /** 241 * Test arrow function with return value. 242 * 243 * @return void 244 */ 245 public function testArrowFunctionReturnValue() 246 { 247 $start = $this->getTargetToken('/* testArrowFunctionReturnValue */', T_SEMICOLON); 248 $found = self::$phpcsFile->findStartOfStatement($start); 249 250 $this->assertSame(($start - 18), $found); 251 252 }//end testArrowFunctionReturnValue() 253 254 255 /** 256 * Test arrow function used as a function argument. 257 * 258 * @return void 259 */ 260 public function testArrowFunctionAsArgument() 261 { 262 $start = $this->getTargetToken('/* testArrowFunctionAsArgument */', T_FN); 263 $start += 8; 264 $found = self::$phpcsFile->findStartOfStatement($start); 265 266 $this->assertSame(($start - 8), $found); 267 268 }//end testArrowFunctionAsArgument() 269 270 271 /** 272 * Test arrow function with arrays used as a function argument. 273 * 274 * @return void 275 */ 276 public function testArrowFunctionWithArrayAsArgument() 277 { 278 $start = $this->getTargetToken('/* testArrowFunctionWithArrayAsArgument */', T_FN); 279 $start += 17; 280 $found = self::$phpcsFile->findStartOfStatement($start); 281 282 $this->assertSame(($start - 17), $found); 283 284 }//end testArrowFunctionWithArrayAsArgument() 285 286 287 /** 288 * Test simple match expression case. 289 * 290 * @return void 291 */ 292 public function testMatchCase() 293 { 294 $start = $this->getTargetToken('/* testMatchCase */', T_COMMA); 295 $found = self::$phpcsFile->findStartOfStatement($start); 296 297 $this->assertSame(($start - 1), $found); 298 299 }//end testMatchCase() 300 301 302 /** 303 * Test simple match expression default case. 304 * 305 * @return void 306 */ 307 public function testMatchDefault() 308 { 309 $start = $this->getTargetToken('/* testMatchDefault */', T_CONSTANT_ENCAPSED_STRING, "'bar'"); 310 $found = self::$phpcsFile->findStartOfStatement($start); 311 312 $this->assertSame($start, $found); 313 314 }//end testMatchDefault() 315 316 317 /** 318 * Test multiple comma-separated match expression case values. 319 * 320 * @return void 321 */ 322 public function testMatchMultipleCase() 323 { 324 $start = $this->getTargetToken('/* testMatchMultipleCase */', T_MATCH_ARROW); 325 $found = self::$phpcsFile->findStartOfStatement($start); 326 327 $this->assertSame(($start - 6), $found); 328 329 $start += 6; 330 $found = self::$phpcsFile->findStartOfStatement($start); 331 332 $this->assertSame(($start - 4), $found); 333 334 }//end testMatchMultipleCase() 335 336 337 /** 338 * Test match expression default case with trailing comma. 339 * 340 * @return void 341 */ 342 public function testMatchDefaultComma() 343 { 344 $start = $this->getTargetToken('/* testMatchDefaultComma */', T_MATCH_ARROW); 345 $found = self::$phpcsFile->findStartOfStatement($start); 346 347 $this->assertSame(($start - 3), $found); 348 349 $start += 2; 350 $found = self::$phpcsFile->findStartOfStatement($start); 351 352 $this->assertSame($start, $found); 353 354 }//end testMatchDefaultComma() 355 356 357 /** 358 * Test match expression with function call. 359 * 360 * @return void 361 */ 362 public function testMatchFunctionCall() 363 { 364 $start = $this->getTargetToken('/* testMatchFunctionCall */', T_CLOSE_PARENTHESIS); 365 $found = self::$phpcsFile->findStartOfStatement($start); 366 367 $this->assertSame(($start - 6), $found); 368 369 }//end testMatchFunctionCall() 370 371 372 /** 373 * Test match expression with function call in the arm. 374 * 375 * @return void 376 */ 377 public function testMatchFunctionCallArm() 378 { 379 // Check the first case. 380 $start = $this->getTargetToken('/* testMatchFunctionCallArm */', T_MATCH_ARROW); 381 $found = self::$phpcsFile->findStartOfStatement($start); 382 383 $this->assertSame(($start - 18), $found); 384 385 // Check the second case. 386 $start += 24; 387 $found = self::$phpcsFile->findStartOfStatement($start); 388 389 $this->assertSame(($start - 18), $found); 390 391 }//end testMatchFunctionCallArm() 392 393 394 /** 395 * Test match expression with closure. 396 * 397 * @return void 398 */ 399 public function testMatchClosure() 400 { 401 $start = $this->getTargetToken('/* testMatchClosure */', T_LNUMBER); 402 $start += 14; 403 $found = self::$phpcsFile->findStartOfStatement($start); 404 405 $this->assertSame(($start - 10), $found); 406 407 $start += 17; 408 $found = self::$phpcsFile->findStartOfStatement($start); 409 410 $this->assertSame(($start - 10), $found); 411 412 }//end testMatchClosure() 413 414 415 /** 416 * Test match expression with array declaration. 417 * 418 * @return void 419 */ 420 public function testMatchArray() 421 { 422 // Start of first case statement. 423 $start = $this->getTargetToken('/* testMatchArray */', T_LNUMBER); 424 $found = self::$phpcsFile->findStartOfStatement($start); 425 $this->assertSame($start, $found); 426 427 // Comma after first statement. 428 $start += 11; 429 $found = self::$phpcsFile->findStartOfStatement($start); 430 $this->assertSame(($start - 7), $found); 431 432 // Start of second case statement. 433 $start += 3; 434 $found = self::$phpcsFile->findStartOfStatement($start); 435 $this->assertSame($start, $found); 436 437 // Comma after first statement. 438 $start += 30; 439 $found = self::$phpcsFile->findStartOfStatement($start); 440 $this->assertSame(($start - 26), $found); 441 442 }//end testMatchArray() 443 444 445 /** 446 * Test nested match expressions. 447 * 448 * @return void 449 */ 450 public function testNestedMatch() 451 { 452 $start = $this->getTargetToken('/* testNestedMatch */', T_LNUMBER); 453 $start += 30; 454 $found = self::$phpcsFile->findStartOfStatement($start); 455 456 $this->assertSame(($start - 26), $found); 457 458 $start -= 4; 459 $found = self::$phpcsFile->findStartOfStatement($start); 460 461 $this->assertSame(($start - 1), $found); 462 463 $start -= 3; 464 $found = self::$phpcsFile->findStartOfStatement($start); 465 466 $this->assertSame(($start - 2), $found); 467 468 }//end testNestedMatch() 469 470 471 /** 472 * Test nested match expressions. 473 * 474 * @return void 475 */ 476 public function testOpenTag() 477 { 478 $start = $this->getTargetToken('/* testOpenTag */', T_OPEN_TAG); 479 $start += 2; 480 $found = self::$phpcsFile->findStartOfStatement($start); 481 482 $this->assertSame(($start - 1), $found); 483 484 }//end testOpenTag() 485 486 487 /** 488 * Test nested match expressions. 489 * 490 * @return void 491 */ 492 public function testOpenTagWithEcho() 493 { 494 $start = $this->getTargetToken('/* testOpenTagWithEcho */', T_OPEN_TAG_WITH_ECHO); 495 $start += 3; 496 $found = self::$phpcsFile->findStartOfStatement($start); 497 498 $this->assertSame(($start - 1), $found); 499 500 }//end testOpenTagWithEcho() 501 502 503}//end class 504