Skip to content

Commit

Permalink
BCFile::findStartOfStatement(): add tests for upstream bugfix for swi…
Browse files Browse the repository at this point in the history
…tch handling

When inside a `switch` `case`/`default` `break`/`continue`/`return`/`exit`/`throw` statement, the start of the statement was not correctly determined.

This adds unit tests safeguarding the upstream change.

Refs:
* squizlabs/php_codesniffer 3192
* squizlabs/PHP_CodeSniffer 3186
  • Loading branch information
jrfnl committed Oct 15, 2022
1 parent c2a08c5 commit 3c25994
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Tests/BackCompat/BCFile/FindStartOfStatementTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,33 @@ $value = [
Text::make('Title')
]),
];

switch ($foo) {
/* testCaseStatement */
case 1:
/* testInsideCaseStatement */
$var = doSomething();
/* testInsideCaseBreakStatement */
break 2;

case 2:
/* testInsideCaseContinueStatement */
continue 2;

case 3:
/* testInsideCaseReturnStatement */
return false;

case 4:
/* testInsideCaseExitStatement */
exit(1);

case 5:
/* testInsideCaseThrowStatement */
throw new Exception();

/* testDefaultStatement */
default:
/* testInsideDefaultContinueStatement */
continue $var;
}
106 changes: 106 additions & 0 deletions Tests/BackCompat/BCFile/FindStartOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,110 @@ public function testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInAr

$this->assertSame($expected, $found);
}

/**
* Test finding the start of a statement inside a switch control structure case/default statement.
*
* @link https://github.com/squizlabs/php_codesniffer/issues/3192
* @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3186/commits/18a0e54735bb9b3850fec266e5f4c50dacf618ea
*
* @dataProvider dataFindStartInsideSwitchCaseDefaultStatements
*
* @param string $testMarker The comment which prefaces the target token in the test file.
* @param array|string|int $targets The token to search for after the test marker.
* @param string|int $expectedTarget Token code of the expected start of statement stack pointer.
*
* @return void
*/
public function testFindStartInsideSwitchCaseDefaultStatements($testMarker, $targets, $expectedTarget)
{
$testToken = $this->getTargetToken($testMarker, $targets);
$expected = $this->getTargetToken($testMarker, $expectedTarget);

$found = BCFile::findStartOfStatement(self::$phpcsFile, $testToken);

$this->assertSame($expected, $found);
}

/**
* Data provider.
*
* @return array
*/
public static function dataFindStartInsideSwitchCaseDefaultStatements()
{
return [
'Case keyword should be start of case statement - case itself' => [
'testMarker' => '/* testCaseStatement */',
'targets' => \T_CASE,
'expectedTarget' => \T_CASE,
],
'Case keyword should be start of case statement - number (what\'s being compared)' => [
'testMarker' => '/* testCaseStatement */',
'targets' => \T_LNUMBER,
'expectedTarget' => \T_CASE,
],
'Variable should be start of arbitrary assignment statement - variable itself' => [
'testMarker' => '/* testInsideCaseStatement */',
'targets' => \T_VARIABLE,
'expectedTarget' => \T_VARIABLE,
],
'Variable should be start of arbitrary assignment statement - equal sign' => [
'testMarker' => '/* testInsideCaseStatement */',
'targets' => \T_EQUAL,
'expectedTarget' => \T_VARIABLE,
],
'Variable should be start of arbitrary assignment statement - function call' => [
'testMarker' => '/* testInsideCaseStatement */',
'targets' => \T_STRING,
'expectedTarget' => \T_VARIABLE,
],
'Break should be start for contents of the break statement - contents' => [
'testMarker' => '/* testInsideCaseBreakStatement */',
'targets' => \T_LNUMBER,
'expectedTarget' => \T_BREAK,
],
'Continue should be start for contents of the continue statement - contents' => [
'testMarker' => '/* testInsideCaseContinueStatement */',
'targets' => \T_LNUMBER,
'expectedTarget' => \T_CONTINUE,
],
'Return should be start for contents of the return statement - contents' => [
'testMarker' => '/* testInsideCaseReturnStatement */',
'targets' => \T_FALSE,
'expectedTarget' => \T_RETURN,
],
'Exit should be start for contents of the exit statement - close parenthesis' => [
// Note: not sure if this is actually correct - should this be the open parenthesis ?
'testMarker' => '/* testInsideCaseExitStatement */',
'targets' => \T_CLOSE_PARENTHESIS,
'expectedTarget' => \T_EXIT,
],
'Throw should be start for contents of the throw statement - new keyword' => [
'testMarker' => '/* testInsideCaseThrowStatement */',
'targets' => \T_NEW,
'expectedTarget' => \T_THROW,
],
'Throw should be start for contents of the throw statement - exception name' => [
'testMarker' => '/* testInsideCaseThrowStatement */',
'targets' => \T_STRING,
'expectedTarget' => \T_THROW,
],
'Throw should be start for contents of the throw statement - close parenthesis' => [
'testMarker' => '/* testInsideCaseThrowStatement */',
'targets' => \T_CLOSE_PARENTHESIS,
'expectedTarget' => \T_THROW,
],
'Default keyword should be start of default statement - default itself' => [
'testMarker' => '/* testDefaultStatement */',
'targets' => \T_DEFAULT,
'expectedTarget' => \T_DEFAULT,
],
'Return should be start for contents of the return statement (inside default) - variable' => [
'testMarker' => '/* testInsideDefaultContinueStatement */',
'targets' => \T_VARIABLE,
'expectedTarget' => \T_CONTINUE,
],
];
}
}

0 comments on commit 3c25994

Please sign in to comment.