Skip to content

Commit acdca48

Browse files
committed
Tests/FindStartOfStatementTest: add extra tests
This adds some extra tests which were already in use in PHPCSUtils.
1 parent a6bed33 commit acdca48

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

tests/Core/File/FindStartOfStatementTest.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,44 @@ return 0;
121121
?>
122122
<h1>Test</h1>
123123
<?= '<h2>', foo(), '</h2>';
124+
125+
$value = [
126+
/* testPrecededByArrowFunctionInArray - Expected */
127+
Url::make('View Song', fn($song) => $song->url())
128+
/* testPrecededByArrowFunctionInArray */
129+
->onlyOnDetail(),
130+
131+
new Panel('Information', [
132+
Text::make('Title')
133+
]),
134+
];
135+
136+
switch ($foo) {
137+
/* testCaseStatement */
138+
case 1:
139+
/* testInsideCaseStatement */
140+
$var = doSomething();
141+
/* testInsideCaseBreakStatement */
142+
break 2;
143+
144+
case 2:
145+
/* testInsideCaseContinueStatement */
146+
continue 2;
147+
148+
case 3:
149+
/* testInsideCaseReturnStatement */
150+
return false;
151+
152+
case 4:
153+
/* testInsideCaseExitStatement */
154+
exit(1);
155+
156+
case 5:
157+
/* testInsideCaseThrowStatement */
158+
throw new Exception();
159+
160+
/* testDefaultStatement */
161+
default:
162+
/* testInsideDefaultContinueStatement */
163+
continue $var;
164+
}

tests/Core/File/FindStartOfStatementTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
44
*
55
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
67
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
8+
* @copyright 2019-2024 PHPCSStandards Contributors
79
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
810
*/
911

@@ -505,4 +507,134 @@ public function testOpenTagWithEcho()
505507
}//end testOpenTagWithEcho()
506508

507509

510+
/**
511+
* Test object call on result of static function call with arrow function as parameter and wrapped within an array.
512+
*
513+
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2849
514+
* @link https://github.com/squizlabs/PHP_CodeSniffer/commit/fbf67efc3fc0c2a355f5585d49f4f6fe160ff2f9
515+
*
516+
* @return void
517+
*/
518+
public function testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInArray()
519+
{
520+
$expected = $this->getTargetToken('/* testPrecededByArrowFunctionInArray - Expected */', T_STRING, 'Url');
521+
522+
$start = $this->getTargetToken('/* testPrecededByArrowFunctionInArray */', T_STRING, 'onlyOnDetail');
523+
$found = self::$phpcsFile->findStartOfStatement($start);
524+
525+
$this->assertSame($expected, $found);
526+
527+
}//end testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInArray()
528+
529+
530+
/**
531+
* Test finding the start of a statement inside a switch control structure case/default statement.
532+
*
533+
* @param string $testMarker The comment which prefaces the target token in the test file.
534+
* @param int|string $targets The token to search for after the test marker.
535+
* @param string|int $expectedTarget Token code of the expected start of statement stack pointer.
536+
*
537+
* @link https://github.com/squizlabs/php_codesniffer/issues/3192
538+
* @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3186/commits/18a0e54735bb9b3850fec266e5f4c50dacf618ea
539+
*
540+
* @dataProvider dataFindStartInsideSwitchCaseDefaultStatements
541+
*
542+
* @return void
543+
*/
544+
public function testFindStartInsideSwitchCaseDefaultStatements($testMarker, $targets, $expectedTarget)
545+
{
546+
$testToken = $this->getTargetToken($testMarker, $targets);
547+
$expected = $this->getTargetToken($testMarker, $expectedTarget);
548+
549+
$found = self::$phpcsFile->findStartOfStatement($testToken);
550+
551+
$this->assertSame($expected, $found);
552+
553+
}//end testFindStartInsideSwitchCaseDefaultStatements()
554+
555+
556+
/**
557+
* Data provider.
558+
*
559+
* @return array<string, array<string, int|string>>
560+
*/
561+
public static function dataFindStartInsideSwitchCaseDefaultStatements()
562+
{
563+
return [
564+
'Case keyword should be start of case statement - case itself' => [
565+
'testMarker' => '/* testCaseStatement */',
566+
'targets' => T_CASE,
567+
'expectedTarget' => T_CASE,
568+
],
569+
'Case keyword should be start of case statement - number (what\'s being compared)' => [
570+
'testMarker' => '/* testCaseStatement */',
571+
'targets' => T_LNUMBER,
572+
'expectedTarget' => T_CASE,
573+
],
574+
'Variable should be start of arbitrary assignment statement - variable itself' => [
575+
'testMarker' => '/* testInsideCaseStatement */',
576+
'targets' => T_VARIABLE,
577+
'expectedTarget' => T_VARIABLE,
578+
],
579+
'Variable should be start of arbitrary assignment statement - equal sign' => [
580+
'testMarker' => '/* testInsideCaseStatement */',
581+
'targets' => T_EQUAL,
582+
'expectedTarget' => T_VARIABLE,
583+
],
584+
'Variable should be start of arbitrary assignment statement - function call' => [
585+
'testMarker' => '/* testInsideCaseStatement */',
586+
'targets' => T_STRING,
587+
'expectedTarget' => T_VARIABLE,
588+
],
589+
'Break should be start for contents of the break statement - contents' => [
590+
'testMarker' => '/* testInsideCaseBreakStatement */',
591+
'targets' => T_LNUMBER,
592+
'expectedTarget' => T_BREAK,
593+
],
594+
'Continue should be start for contents of the continue statement - contents' => [
595+
'testMarker' => '/* testInsideCaseContinueStatement */',
596+
'targets' => T_LNUMBER,
597+
'expectedTarget' => T_CONTINUE,
598+
],
599+
'Return should be start for contents of the return statement - contents' => [
600+
'testMarker' => '/* testInsideCaseReturnStatement */',
601+
'targets' => T_FALSE,
602+
'expectedTarget' => T_RETURN,
603+
],
604+
'Exit should be start for contents of the exit statement - close parenthesis' => [
605+
// Note: not sure if this is actually correct - should this be the open parenthesis ?
606+
'testMarker' => '/* testInsideCaseExitStatement */',
607+
'targets' => T_CLOSE_PARENTHESIS,
608+
'expectedTarget' => T_EXIT,
609+
],
610+
'Throw should be start for contents of the throw statement - new keyword' => [
611+
'testMarker' => '/* testInsideCaseThrowStatement */',
612+
'targets' => T_NEW,
613+
'expectedTarget' => T_THROW,
614+
],
615+
'Throw should be start for contents of the throw statement - exception name' => [
616+
'testMarker' => '/* testInsideCaseThrowStatement */',
617+
'targets' => T_STRING,
618+
'expectedTarget' => T_THROW,
619+
],
620+
'Throw should be start for contents of the throw statement - close parenthesis' => [
621+
'testMarker' => '/* testInsideCaseThrowStatement */',
622+
'targets' => T_CLOSE_PARENTHESIS,
623+
'expectedTarget' => T_THROW,
624+
],
625+
'Default keyword should be start of default statement - default itself' => [
626+
'testMarker' => '/* testDefaultStatement */',
627+
'targets' => T_DEFAULT,
628+
'expectedTarget' => T_DEFAULT,
629+
],
630+
'Return should be start for contents of the return statement (inside default) - variable' => [
631+
'testMarker' => '/* testInsideDefaultContinueStatement */',
632+
'targets' => T_VARIABLE,
633+
'expectedTarget' => T_CONTINUE,
634+
],
635+
];
636+
637+
}//end dataFindStartInsideSwitchCaseDefaultStatements()
638+
639+
508640
}//end class

0 commit comments

Comments
 (0)