Skip to content

Commit

Permalink
Tests/GetMethodParametersTest: add extra tests
Browse files Browse the repository at this point in the history
This adds some extra tests which were already in use in PHPCSUtils.
  • Loading branch information
jrfnl committed Jan 2, 2024
1 parent bbe90b0 commit 3c0c0d4
Show file tree
Hide file tree
Showing 6 changed files with 1,617 additions and 114 deletions.
4 changes: 4 additions & 0 deletions tests/Core/File/GetMethodParametersParseError1Test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

/* testParseError */
function missingOpenParens // Intentional parse error.
38 changes: 38 additions & 0 deletions tests/Core/File/GetMethodParametersParseError1Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
*
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2019-2024 PHPCSStandards Contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

/**
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
*
* @covers \PHP_CodeSniffer\Files\File::getMethodParameters
*/
class GetMethodParametersParseError1Test extends AbstractMethodUnitTest
{


/**
* Test receiving an empty array when encountering a specific parse error.
*
* @return void
*/
public function testParseError()
{
$target = $this->getTargetToken('/* testParseError */', [T_FUNCTION, T_CLOSURE, T_FN]);
$result = self::$phpcsFile->getMethodParameters($target);

$this->assertSame([], $result);

}//end testParseError()


}//end class
4 changes: 4 additions & 0 deletions tests/Core/File/GetMethodParametersParseError2Test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

/* testParseError */
function missingCloseParens( // Intentional parse error.
38 changes: 38 additions & 0 deletions tests/Core/File/GetMethodParametersParseError2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
*
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2019-2024 PHPCSStandards Contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

/**
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
*
* @covers \PHP_CodeSniffer\Files\File::getMethodParameters
*/
class GetMethodParametersParseError2Test extends AbstractMethodUnitTest
{


/**
* Test receiving an empty array when encountering a specific parse error.
*
* @return void
*/
public function testParseError()
{
$target = $this->getTargetToken('/* testParseError */', [T_FUNCTION, T_CLOSURE, T_FN]);
$result = self::$phpcsFile->getMethodParameters($target);

$this->assertSame([], $result);

}//end testParseError()


}//end class
149 changes: 147 additions & 2 deletions tests/Core/File/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
<?php

/* testImportUse */
use Vendor\Package\Sub as Alias;

/* testImportGroupUse */
use Vendor\Package\Sub\{
ClassA,
ClassB as BAlias,
};

if ($foo) {}

/* testTraitUse */
class TraitUse {
use ImportedTrait;

function methodName() {}
}

/* testNotAFunction */
interface NotAFunction {};

/* testFunctionNoParams */
function noParams() {}

/* testPassByReference */
function passByReference(&$var) {}

Expand Down Expand Up @@ -32,6 +56,75 @@ function myFunction($a = 10 & 20) {}
/* testArrowFunction */
fn(int $a, ...$b) => $b;

/* testArrowFunctionReturnByRef */
fn&(?string $a) => $b;

/* testArrayDefaultValues */
function arrayDefaultValues($var1 = [], $var2 = array(1, 2, 3) ) {}

/* testConstantDefaultValueSecondParam */
function constantDefaultValueSecondParam($var1, $var2 = M_PI) {}

/* testScalarTernaryExpressionInDefault */
function ternayInDefault( $a = FOO ? 'bar' : 10, ? bool $b ) {}

/* testVariadicFunction */
function variadicFunction( int ... $a ) {}

/* testVariadicByRefFunction */
function variadicByRefFunction( &...$a ) {}

/* testVariadicFunctionClassType */
function variableLengthArgument($unit, DateInterval ...$intervals) {}

/* testNameSpacedTypeDeclaration */
function namespacedClassType( \Package\Sub\ClassName $a, ?Sub\AnotherClass $b ) {}

/* testWithAllTypes */
class testAllTypes {
function allTypes(
?ClassName $a,
self $b,
parent $c,
object $d,
?int $e,
string &$f,
iterable $g,
bool $h = true,
callable $i = 'is_null',
float $j = 1.1,
array ...$k
) {}
}

/* testArrowFunctionWithAllTypes */
$fn = fn(
?ClassName $a,
self $b,
parent $c,
object $d,
?int $e,
string &$f,
iterable $g,
bool $h = true,
callable $i = 'is_null',
float $j = 1.1,
array ...$k
) => $something;

/* testMessyDeclaration */
function messyDeclaration(
// comment
?\MyNS /* comment */
\ SubCat // phpcs:ignore Standard.Cat.Sniff -- for reasons.
\ MyClass $a,
$b /* test */ = /* test */ 'default' /* test*/,
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
? /*comment*/
bool // phpcs:disable Stnd.Cat.Sniff -- For reasons.
& /*test*/ ... /* phpcs:ignore */ $c
) {}

/* testPHP8MixedTypeHint */
function mixedTypeHint(mixed &...$var1) {}

Expand All @@ -46,7 +139,7 @@ function namespaceOperatorTypeHint(?namespace\Name $var1) {}
function unionTypeSimple(int|float $number, self|parent &...$obj) {}

/* testPHP8UnionTypesWithSpreadOperatorAndReference */
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB) {}
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB ) {}

/* testPHP8UnionTypesSimpleWithBitwiseOrInDefault */
$fn = fn(int|float $var = CONSTANT_A | CONSTANT_B) => $var;
Expand Down Expand Up @@ -110,7 +203,13 @@ class ConstructorPropertyPromotionAndNormalParams {

class ConstructorPropertyPromotionWithReadOnly {
/* testPHP81ConstructorPropertyPromotionWithReadOnly */
public function __construct(public readonly ?int $promotedProp, readonly private string|bool &$promotedToo) {}
public function __construct(public readonly ?int $promotedProp, ReadOnly private string|bool &$promotedToo) {}
}

class ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration {
/* testPHP81ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration */
// Intentional fatal error. Readonly properties MUST be typed.
public function __construct(public readonly $promotedProp, ReadOnly private &$promotedToo) {}
}

class ConstructorPropertyPromotionWithOnlyReadOnly {
Expand Down Expand Up @@ -174,3 +273,49 @@ function pseudoTypeTrue(?true $var = true) {}
/* testPHP82PseudoTypeFalseAndTrue */
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
function pseudoTypeFalseAndTrue(true|false $var = true) {}

/* testPHP81NewInInitializers */
function newInInitializers(
TypeA $new = new TypeA(self::CONST_VALUE),
\Package\TypeB $newToo = new \Package\TypeB(10, 'string'),
) {}

/* testFunctionCallFnPHPCS353-354 */
$value = $obj->fn(true);

/* testClosureNoParams */
function() {};

/* testClosure */
function( $a = 'test' ) {};

/* testClosureUseNoParams */
function() use() {};

/* testClosureUse */
function() use( $foo, $bar ) {};

/* testFunctionParamListWithTrailingComma */
function trailingComma(
?string $foo /*comment*/ ,
$bar = 0,
) {}

/* testClosureParamListWithTrailingComma */
function(
$foo,
$bar,
) {};

/* testArrowFunctionParamListWithTrailingComma */
$fn = fn( ?int $a , ...$b, ) => $b;

/* testClosureUseWithTrailingComma */
function() use(
$foo /*comment*/ ,
$bar,
) {};

/* testArrowFunctionLiveCoding */
// Intentional parse error. This has to be the last test in the file.
$fn = fn
Loading

0 comments on commit 3c0c0d4

Please sign in to comment.