From 18fb7964944f4787bd7578a48512ac9514c2654b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 13 May 2024 12:33:40 +0200 Subject: [PATCH] [squash] Add tests for the changes in "catch" behaviour // Add to original commit message: Includes test for where the behaviour of the functions is now different. --- .../AbstractArrayDeclarationSniffTest.php | 35 ++++++++++++++ .../SplitAndMergeImportUseStatementTest.inc | 3 ++ .../SplitAndMergeImportUseStatementTest.php | 48 +++++++++++++++++-- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/Tests/AbstractSniffs/AbstractArrayDeclaration/AbstractArrayDeclarationSniffTest.php b/Tests/AbstractSniffs/AbstractArrayDeclaration/AbstractArrayDeclarationSniffTest.php index d83bb5c9..343a6258 100644 --- a/Tests/AbstractSniffs/AbstractArrayDeclaration/AbstractArrayDeclarationSniffTest.php +++ b/Tests/AbstractSniffs/AbstractArrayDeclaration/AbstractArrayDeclarationSniffTest.php @@ -40,6 +40,41 @@ final class AbstractArrayDeclarationSniffTest extends PolyfilledTestCase 'processComma', ]; + /** + * Test receiving an expected exception when an invalid token pointer is passed. + * + * @return void + */ + public function testNonExistentToken() + { + $this->expectException('PHPCSUtils\Exceptions\OutOfBoundsStackPtr'); + $this->expectExceptionMessage( + 'Argument #2 ($stackPtr) must be a stack pointer which exists in the $phpcsFile object, 100000 given' + ); + + $mockObj = $this->getMockedClassUnderTest(); + + $mockObj->expects($this->never()) + ->method('processOpenClose'); + + $mockObj->expects($this->never()) + ->method('processKey'); + + $mockObj->expects($this->never()) + ->method('processNoKey'); + + $mockObj->expects($this->never()) + ->method('processArrow'); + + $mockObj->expects($this->never()) + ->method('processValue'); + + $mockObj->expects($this->never()) + ->method('processComma'); + + $mockObj->process(self::$phpcsFile, 100000); + } + /** * Test that the abstract sniff correctly bows out when presented with a token which is not an array. * diff --git a/Tests/Utils/UseStatements/SplitAndMergeImportUseStatementTest.inc b/Tests/Utils/UseStatements/SplitAndMergeImportUseStatementTest.inc index d9bc5bb1..3e8355b4 100644 --- a/Tests/Utils/UseStatements/SplitAndMergeImportUseStatementTest.inc +++ b/Tests/Utils/UseStatements/SplitAndMergeImportUseStatementTest.inc @@ -1,5 +1,8 @@ expectException('PHPCSUtils\Exceptions\OutOfBoundsStackPtr'); + $this->expectExceptionMessage( + 'Argument #2 ($stackPtr) must be a stack pointer which exists in the $phpcsFile object, 100000 given' + ); + + UseStatements::splitAndMergeImportUseStatement(self::$phpcsFile, 100000, []); + } + + /** + * Test receiving an expected exception when a non-supported token is passed. + * + * @return void + */ + public function testInvalidTokenPassed() + { + $this->expectException('PHPCSUtils\Exceptions\UnexpectedTokenType'); + $this->expectExceptionMessage('Argument #2 ($stackPtr) must be of type T_USE;'); + + // 0 = PHP open tag. + UseStatements::splitAndMergeImportUseStatement(self::$phpcsFile, 0, []); + } + /** * Test correctly splitting and merging a import `use` statements. * @@ -53,6 +82,15 @@ public function testSplitAndMergeImportUseStatement($testMarker, $expected, $pre public static function dataSplitAndMergeImportUseStatement() { $data = [ + 'closure-use' => [ + 'testMarker' => '/* testClosureUse */', + // Same as previous, which, as this is the first test case, is an empty statements array. + 'expected' => [ + 'name' => [], + 'function' => [], + 'const' => [], + ], + ], 'name-plain' => [ 'testMarker' => '/* testUseNamePlainAliased */', 'expected' => [ @@ -118,7 +156,11 @@ public static function dataSplitAndMergeImportUseStatement() ], ]; - $previousUse = []; + $previousUse = [ + 'name' => [], + 'function' => [], + 'const' => [], + ]; foreach ($data as $key => $value) { $data[$key]['previousUse'] = $previousUse; $previousUse = $value['expected'];