|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests that scope owner (scope_condition, conditions) is set correctly |
| 4 | + * |
| 5 | + * @author Dan Wallis <[email protected]> |
| 6 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 7 | + */ |
| 8 | + |
| 9 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 10 | + |
| 11 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 12 | + |
| 13 | +final class ScopeOwnerTest extends AbstractMethodUnitTest |
| 14 | +{ |
| 15 | + |
| 16 | + |
| 17 | + /** |
| 18 | + * Test that a basic 'if' condition gets scope opener/closer set as expected. |
| 19 | + * |
| 20 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 21 | + * @param int $conditionWidth How many tokens wide the 'if' condition should be. |
| 22 | + * |
| 23 | + * @dataProvider dataIfCondition |
| 24 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function testIfCondition($testMarker, $conditionWidth) |
| 29 | + { |
| 30 | + $tokens = self::$phpcsFile->getTokens(); |
| 31 | + $targetIf = $this->getTargetToken($testMarker, T_IF); |
| 32 | + |
| 33 | + $this->assertSame($targetIf, $tokens[$targetIf]['parenthesis_owner'], 'parenthesis owner is self for if'); |
| 34 | + $this->assertSame(($targetIf + 2), $tokens[$targetIf]['parenthesis_opener'], 'expected parenthesis opener'); |
| 35 | + $this->assertSame(($targetIf + 2 + $conditionWidth), $tokens[$targetIf]['parenthesis_closer'], 'expected parenthesis closer'); |
| 36 | + $this->assertSame($targetIf, $tokens[$targetIf]['scope_condition']); |
| 37 | + |
| 38 | + $targetOpenCurly = self::$phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $targetIf); |
| 39 | + $this->assertSame($targetIf, $tokens[$targetOpenCurly]['scope_condition'], 'scope_condition set on open curly'); |
| 40 | + |
| 41 | + $targetCloseCurly = $tokens[$targetOpenCurly]['bracket_closer']; |
| 42 | + $this->assertSame($targetIf, $tokens[$targetCloseCurly]['scope_condition'], 'scope_condition set on close curly'); |
| 43 | + |
| 44 | + $targetReturn = self::$phpcsFile->findNext(T_RETURN, $targetIf); |
| 45 | + $this->assertSame([$targetIf => T_IF], $tokens[$targetReturn]['conditions'], 'conditions set on if statement body'); |
| 46 | + |
| 47 | + }//end testIfCondition() |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Data provider. |
| 52 | + * |
| 53 | + * @see testIfCondition() |
| 54 | + * |
| 55 | + * @return array<string, array<string, int>> |
| 56 | + */ |
| 57 | + public static function dataIfCondition() |
| 58 | + { |
| 59 | + return [ |
| 60 | + 'Basic if' => [ |
| 61 | + 'testMarker' => '/* testNormalIfCondition */', |
| 62 | + 'conditionWidth' => 2, |
| 63 | + ], |
| 64 | + 'Function call in if condition' => [ |
| 65 | + 'testMarker' => '/* testFunctionCallInIfCondition */', |
| 66 | + 'conditionWidth' => 5, |
| 67 | + ], |
| 68 | + 'Heredoc in if condition' => [ |
| 69 | + 'testMarker' => '/* testHeredocInIfCondition */', |
| 70 | + 'conditionWidth' => 8, |
| 71 | + ], |
| 72 | + ]; |
| 73 | + |
| 74 | + }//end dataIfCondition() |
| 75 | + |
| 76 | + |
| 77 | +}//end class |
0 commit comments