Skip to content

Commit 529f43e

Browse files
committed
Tests/BCFile::findStartOfStatement(): sync in new tests from upstream [2]
See PR PHPCSStandards/PHP_CodeSniffer 502 / PHPCSStandards/PHP_CodeSniffer@b82438f
1 parent e70fa6e commit 529f43e

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

Tests/BackCompat/BCFile/FindStartOfStatementTest.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,23 @@ match ($var) {
172172
},
173173
default => false
174174
};
175+
176+
match ($var) {
177+
/* test437NestedLongArrayWithinMatch */
178+
'a' => array( 1, 2.5, $var),
179+
/* test437NestedFunctionCallWithinMatch */
180+
'b' => functionCall( 11, $var, 50.50),
181+
/* test437NestedArrowFunctionWithinMatch */
182+
'c' => fn($p1, /* test437FnSecondParamWithinMatch */ $p2) => $p1 + $p2,
183+
default => false
184+
};
185+
186+
callMe($paramA, match ($var) {
187+
/* test437NestedLongArrayWithinNestedMatch */
188+
'a' => array( 1, 2.5, $var),
189+
/* test437NestedFunctionCallWithinNestedMatch */
190+
'b' => functionCall( 11, $var, 50.50),
191+
/* test437NestedArrowFunctionWithinNestedMatch */
192+
'c' => fn($p1, /* test437FnSecondParamWithinNestedMatch */ $p2) => $p1 + $p2,
193+
default => false
194+
});

Tests/BackCompat/BCFile/FindStartOfStatementTest.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,157 @@ public static function dataFindStartInsideClosedScopeNestedWithinMatch()
660660
],
661661
];
662662
}
663+
664+
/**
665+
* Test finding the start of a statement for a token within a set of parentheses within a match expressions.
666+
*
667+
* @param string $testMarker The comment which prefaces the target token in the test file.
668+
* @param int|string $target The token to search for after the test marker.
669+
* @param int|string $expectedTarget Token code of the expected start of statement stack pointer.
670+
*
671+
* @link https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/437
672+
*
673+
* @dataProvider dataFindStartInsideParenthesesNestedWithinMatch
674+
*
675+
* @return void
676+
*/
677+
public function testFindStartInsideParenthesesNestedWithinMatch($testMarker, $target, $expectedTarget)
678+
{
679+
$testToken = $this->getTargetToken($testMarker, $target);
680+
$expected = $this->getTargetToken($testMarker, $expectedTarget);
681+
682+
$found = BCFile::findStartOfStatement(self::$phpcsFile, $testToken);
683+
684+
$this->assertSame($expected, $found);
685+
}
686+
687+
/**
688+
* Data provider.
689+
*
690+
* @return array<string, array<string, int|string>>
691+
*/
692+
public static function dataFindStartInsideParenthesesNestedWithinMatch()
693+
{
694+
return [
695+
'Array item itself should be start for first array item' => [
696+
'testMarker' => '/* test437NestedLongArrayWithinMatch */',
697+
'target' => T_LNUMBER,
698+
'expectedTarget' => T_LNUMBER,
699+
],
700+
'Array item itself should be start for second array item' => [
701+
'testMarker' => '/* test437NestedLongArrayWithinMatch */',
702+
'target' => T_DNUMBER,
703+
'expectedTarget' => T_DNUMBER,
704+
],
705+
'Array item itself should be start for third array item' => [
706+
'testMarker' => '/* test437NestedLongArrayWithinMatch */',
707+
'target' => T_VARIABLE,
708+
'expectedTarget' => T_VARIABLE,
709+
],
710+
711+
'Parameter itself should be start for first param passed to function call' => [
712+
'testMarker' => '/* test437NestedFunctionCallWithinMatch */',
713+
'target' => T_LNUMBER,
714+
'expectedTarget' => T_LNUMBER,
715+
],
716+
'Parameter itself should be start for second param passed to function call' => [
717+
'testMarker' => '/* test437NestedFunctionCallWithinMatch */',
718+
'target' => T_VARIABLE,
719+
'expectedTarget' => T_VARIABLE,
720+
],
721+
'Parameter itself should be start for third param passed to function call' => [
722+
'testMarker' => '/* test437NestedFunctionCallWithinMatch */',
723+
'target' => T_DNUMBER,
724+
'expectedTarget' => T_DNUMBER,
725+
],
726+
727+
'Parameter itself should be start for first param declared in arrow function' => [
728+
'testMarker' => '/* test437NestedArrowFunctionWithinMatch */',
729+
'target' => T_VARIABLE,
730+
'expectedTarget' => T_VARIABLE,
731+
],
732+
'Parameter itself should be start for second param declared in arrow function' => [
733+
'testMarker' => '/* test437FnSecondParamWithinMatch */',
734+
'target' => T_VARIABLE,
735+
'expectedTarget' => T_VARIABLE,
736+
],
737+
];
738+
}
739+
740+
/**
741+
* Test finding the start of a statement for a token within a set of parentheses within a match expressions,
742+
* which itself is nested within parentheses.
743+
*
744+
* @param string $testMarker The comment which prefaces the target token in the test file.
745+
* @param int|string $target The token to search for after the test marker.
746+
* @param int|string $expectedTarget Token code of the expected start of statement stack pointer.
747+
*
748+
* @link https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/437
749+
*
750+
* @dataProvider dataFindStartInsideParenthesesNestedWithinNestedMatch
751+
*
752+
* @return void
753+
*/
754+
public function testFindStartInsideParenthesesNestedWithinNestedMatch($testMarker, $target, $expectedTarget)
755+
{
756+
$testToken = $this->getTargetToken($testMarker, $target);
757+
$expected = $this->getTargetToken($testMarker, $expectedTarget);
758+
759+
$found = BCFile::findStartOfStatement(self::$phpcsFile, $testToken);
760+
761+
$this->assertSame($expected, $found);
762+
}
763+
764+
/**
765+
* Data provider.
766+
*
767+
* @return array<string, array<string, int|string>>
768+
*/
769+
public static function dataFindStartInsideParenthesesNestedWithinNestedMatch()
770+
{
771+
return [
772+
'Array item itself should be start for first array item' => [
773+
'testMarker' => '/* test437NestedLongArrayWithinNestedMatch */',
774+
'target' => T_LNUMBER,
775+
'expectedTarget' => T_LNUMBER,
776+
],
777+
'Array item itself should be start for second array item' => [
778+
'testMarker' => '/* test437NestedLongArrayWithinNestedMatch */',
779+
'target' => T_DNUMBER,
780+
'expectedTarget' => T_DNUMBER,
781+
],
782+
'Array item itself should be start for third array item' => [
783+
'testMarker' => '/* test437NestedLongArrayWithinNestedMatch */',
784+
'target' => T_VARIABLE,
785+
'expectedTarget' => T_VARIABLE,
786+
],
787+
788+
'Parameter itself should be start for first param passed to function call' => [
789+
'testMarker' => '/* test437NestedFunctionCallWithinNestedMatch */',
790+
'target' => T_LNUMBER,
791+
'expectedTarget' => T_LNUMBER,
792+
],
793+
'Parameter itself should be start for second param passed to function call' => [
794+
'testMarker' => '/* test437NestedFunctionCallWithinNestedMatch */',
795+
'target' => T_VARIABLE,
796+
'expectedTarget' => T_VARIABLE,
797+
],
798+
'Parameter itself should be start for third param passed to function call' => [
799+
'testMarker' => '/* test437NestedFunctionCallWithinNestedMatch */',
800+
'target' => T_DNUMBER,
801+
'expectedTarget' => T_DNUMBER,
802+
],
803+
804+
'Parameter itself should be start for first param declared in arrow function' => [
805+
'testMarker' => '/* test437NestedArrowFunctionWithinNestedMatch */',
806+
'target' => T_VARIABLE,
807+
'expectedTarget' => T_VARIABLE,
808+
],
809+
'Parameter itself should be start for second param declared in arrow function' => [
810+
'testMarker' => '/* test437FnSecondParamWithinNestedMatch */',
811+
'target' => T_VARIABLE,
812+
'expectedTarget' => T_VARIABLE,
813+
],
814+
];
815+
}
663816
}

0 commit comments

Comments
 (0)