Skip to content

Commit

Permalink
[squash] Add tests for the changes in "catch" behaviour
Browse files Browse the repository at this point in the history
// Add to original commit message:

Includes test for where the behaviour of the functions is now different.
  • Loading branch information
jrfnl committed May 14, 2024
1 parent 8597920 commit 5680552
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

/* testClosureUse */
$closure = function() use($bar) {};

/* testUseNamePlainAliased */
use MyNamespace \ YourClass as ClassAlias;

Expand Down
48 changes: 45 additions & 3 deletions Tests/Utils/UseStatements/SplitAndMergeImportUseStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace PHPCSUtils\Tests\Utils\UseStatements;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\UseStatements;

/**
Expand All @@ -22,9 +22,38 @@
*
* @since 1.0.0
*/
final class SplitAndMergeImportUseStatementTest extends UtilityMethodTestCase
final class SplitAndMergeImportUseStatementTest extends PolyfilledTestCase
{

/**
* Test passing a non-existent token pointer.
*
* @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'
);

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.
*
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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'];
Expand Down

0 comments on commit 5680552

Please sign in to comment.