diff --git a/package.xml b/package.xml index 327d75ec97..a6a71598f5 100644 --- a/package.xml +++ b/package.xml @@ -209,6 +209,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -219,6 +220,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -1962,9 +1964,11 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + @@ -1998,8 +2002,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + diff --git a/scripts/ValidatePEAR/ValidatePEARPackageXML.php b/scripts/ValidatePEAR/ValidatePEARPackageXML.php index 266e836d3d..11d73bc87b 100644 --- a/scripts/ValidatePEAR/ValidatePEARPackageXML.php +++ b/scripts/ValidatePEAR/ValidatePEARPackageXML.php @@ -11,6 +11,8 @@ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ +use PHP_CodeSniffer\Tests\FileList; + /** * Validate the PHP_CodeSniffer PEAR package.xml file. */ diff --git a/scripts/validate-pear-package.php b/scripts/validate-pear-package.php index f40a2b5c76..1d373f25f9 100644 --- a/scripts/validate-pear-package.php +++ b/scripts/validate-pear-package.php @@ -12,7 +12,7 @@ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ -require_once __DIR__.'/ValidatePEAR/FileList.php'; +require_once dirname(__DIR__).'/tests/FileList.php'; require_once __DIR__.'/ValidatePEAR/ValidatePEARPackageXML.php'; $validate = new ValidatePEARPackageXML(); diff --git a/tests/Core/AbstractMethodUnitTest.php b/tests/Core/AbstractMethodUnitTest.php new file mode 100644 index 0000000000..4d4f546995 --- /dev/null +++ b/tests/Core/AbstractMethodUnitTest.php @@ -0,0 +1,140 @@ + + * @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved. + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core; + +use PHP_CodeSniffer\Config; +use PHP_CodeSniffer\Ruleset; +use PHP_CodeSniffer\Files\DummyFile; +use PHPUnit\Framework\TestCase; + +abstract class AbstractMethodUnitTest extends TestCase +{ + + /** + * The file extension of the test case file (without leading dot). + * + * This allows child classes to overrule the default `inc` with, for instance, + * `js` or `css` when applicable. + * + * @var string + */ + protected static $fileExtension = 'inc'; + + /** + * The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file. + * + * @var \PHP_CodeSniffer\Files\File + */ + protected static $phpcsFile; + + + /** + * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. + * + * The test case file for a unit test class has to be in the same directory + * directory and use the same file name as the test class, using the .inc extension. + * + * @return void + */ + public static function setUpBeforeClass() + { + $config = new Config(); + $config->standards = ['PSR1']; + + $ruleset = new Ruleset($config); + + // Default to a file with the same name as the test class. Extension is property based. + $relativeCN = str_replace(__NAMESPACE__, '', get_called_class()); + $relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN); + $pathToTestFile = realpath(__DIR__).$relativePath.'.'.static::$fileExtension; + + // Make sure the file gets parsed correctly based on the file type. + $contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL; + $contents .= file_get_contents($pathToTestFile); + + self::$phpcsFile = new DummyFile($contents, $ruleset, $config); + self::$phpcsFile->process(); + + }//end setUpBeforeClass() + + + /** + * Clean up after finished test. + * + * @return void + */ + public static function tearDownAfterClass() + { + self::$phpcsFile = null; + + }//end tearDownAfterClass() + + + /** + * Get the token pointer for a target token based on a specific comment found on the line before. + * + * Note: the test delimiter comment MUST start with "/* test" to allow this function to + * distinguish between comments used *in* a test and test delimiters. + * + * @param string $commentString The delimiter comment to look for. + * @param int|string|array $tokenType The type of token(s) to look for. + * @param string $tokenContent Optional. The token content for the target token. + * + * @return int + */ + public function getTargetToken($commentString, $tokenType, $tokenContent=null) + { + $start = (self::$phpcsFile->numTokens - 1); + $comment = self::$phpcsFile->findPrevious( + T_COMMENT, + $start, + null, + false, + $commentString + ); + + $tokens = self::$phpcsFile->getTokens(); + $end = ($start + 1); + + // Limit the token finding to between this and the next delimiter comment. + for ($i = ($comment + 1); $i < $end; $i++) { + if ($tokens[$i]['code'] !== T_COMMENT) { + continue; + } + + if (stripos($tokens[$i]['content'], '/* test') === 0) { + $end = $i; + break; + } + } + + $target = self::$phpcsFile->findNext( + $tokenType, + ($comment + 1), + $end, + false, + $tokenContent + ); + + if ($target === false) { + $msg = 'Failed to find test target token for comment string: '.$commentString; + if ($tokenContent !== null) { + $msg .= ' With token content: '.$tokenContent; + } + + $this->assertFalse(true, $msg); + } + + return $target; + + }//end getTargetToken() + + +}//end class diff --git a/tests/Core/AllTests.php b/tests/Core/AllTests.php index a9320e36aa..304690eff0 100644 --- a/tests/Core/AllTests.php +++ b/tests/Core/AllTests.php @@ -3,26 +3,17 @@ * A test class for testing the core. * * @author Greg Sherwood - * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) + * @author Juliette Reinders Folmer + * @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ namespace PHP_CodeSniffer\Tests\Core; +use PHP_CodeSniffer\Tests\FileList; use PHPUnit\TextUI\TestRunner; use PHPUnit\Framework\TestSuite; -require_once 'IsCamelCapsTest.php'; -require_once 'ErrorSuppressionTest.php'; -require_once 'File/FindEndOfStatementTest.php'; -require_once 'File/FindExtendedClassNameTest.php'; -require_once 'File/FindImplementedInterfaceNamesTest.php'; -require_once 'File/GetMemberPropertiesTest.php'; -require_once 'File/GetMethodParametersTest.php'; -require_once 'File/GetMethodPropertiesTest.php'; -require_once 'File/IsReferenceTest.php'; -require_once 'Filters/Filter/AcceptTest.php'; - class AllTests { @@ -47,16 +38,23 @@ public static function main() public static function suite() { $suite = new TestSuite('PHP CodeSniffer Core'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\IsCamelCapsTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\ErrorSuppressionTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindEndOfStatementTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindExtendedClassNameTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindImplementedInterfaceNamesTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMemberPropertiesTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodParametersTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodPropertiesTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\IsReferenceTest'); - $suite->addTestSuite('PHP_CodeSniffer\Tests\Core\Filters\Filter\AcceptTest'); + + $testFileIterator = new FileList(__DIR__, '', '`Test\.php$`Di'); + foreach ($testFileIterator->fileIterator as $file) { + if (strpos($file, 'AbstractMethodUnitTest.php') !== false) { + continue; + } + + include_once $file; + + $class = str_replace(__DIR__, '', $file); + $class = str_replace('.php', '', $class); + $class = str_replace('/', '\\', $class); + $class = 'PHP_CodeSniffer\Tests\Core'.$class; + + $suite->addTestSuite($class); + } + return $suite; }//end suite() diff --git a/tests/Core/File/FindEndOfStatementTest.php b/tests/Core/File/FindEndOfStatementTest.php index d3cd104f56..deacd9a49c 100644 --- a/tests/Core/File/FindEndOfStatementTest.php +++ b/tests/Core/File/FindEndOfStatementTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class FindEndOfStatementTest extends TestCase +class FindEndOfStatementTest extends AbstractMethodUnitTest { - /** - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Test a simple assignment. @@ -66,10 +22,10 @@ public function tearDown() */ public function testSimpleAssignment() { - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2); - $found = $this->phpcsFile->findEndOfStatement($start); + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 5)], $tokens[$found]); }//end testSimpleAssignment() @@ -82,10 +38,10 @@ public function testSimpleAssignment() */ public function testControlStructure() { - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2); - $found = $this->phpcsFile->findEndOfStatement($start); + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 6)], $tokens[$found]); }//end testControlStructure() @@ -98,10 +54,10 @@ public function testControlStructure() */ public function testClosureAssignment() { - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2); - $found = $this->phpcsFile->findEndOfStatement($start); + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 13)], $tokens[$found]); }//end testClosureAssignment() @@ -115,24 +71,24 @@ public function testClosureAssignment() public function testHeredocFunctionArg() { // Find the end of the function. - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2); - $found = $this->phpcsFile->findEndOfStatement($start); + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 10)], $tokens[$found]); // Find the end of the heredoc. $start += 2; - $found = $this->phpcsFile->findEndOfStatement($start); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 4)], $tokens[$found]); // Find the end of the last arg. $start = ($found + 2); - $found = $this->phpcsFile->findEndOfStatement($start); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[$start], $tokens[$found]); }//end testHeredocFunctionArg() @@ -146,24 +102,24 @@ public function testHeredocFunctionArg() public function testSwitch() { // Find the end of the switch. - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2); - $found = $this->phpcsFile->findEndOfStatement($start); + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 28)], $tokens[$found]); // Find the end of the case. $start += 9; - $found = $this->phpcsFile->findEndOfStatement($start); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 8)], $tokens[$found]); // Find the end of default case. $start += 11; - $found = $this->phpcsFile->findEndOfStatement($start); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 6)], $tokens[$found]); }//end testSwitch() @@ -177,24 +133,24 @@ public function testSwitch() public function testStatementAsArrayValue() { // Test short array syntax. - $start = ($this->phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7); - $found = $this->phpcsFile->findEndOfStatement($start); + $start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 2)], $tokens[$found]); // Test long array syntax. $start += 12; - $found = $this->phpcsFile->findEndOfStatement($start); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 2)], $tokens[$found]); // Test same statement outside of array. $start += 10; - $found = $this->phpcsFile->findEndOfStatement($start); + $found = self::$phpcsFile->findEndOfStatement($start); - $tokens = $this->phpcsFile->getTokens(); + $tokens = self::$phpcsFile->getTokens(); $this->assertSame($tokens[($start + 3)], $tokens[$found]); }//end testStatementAsArrayValue() diff --git a/tests/Core/File/FindExtendedClassNameTest.php b/tests/Core/File/FindExtendedClassNameTest.php index 5ebb4d9f56..f39377e6c8 100644 --- a/tests/Core/File/FindExtendedClassNameTest.php +++ b/tests/Core/File/FindExtendedClassNameTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class FindExtendedClassNameTest extends TestCase +class FindExtendedClassNameTest extends AbstractMethodUnitTest { - /** - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Test retrieving the name of the class being extended by another class @@ -72,17 +28,8 @@ public function tearDown() */ public function testFindExtendedClassName($identifier, $expected) { - $start = ($this->phpcsFile->numTokens - 1); - $delim = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - $identifier - ); - $OOToken = $this->phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1)); - - $result = $this->phpcsFile->findExtendedClassName($OOToken); + $OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE]); + $result = self::$phpcsFile->findExtendedClassName($OOToken); $this->assertSame($expected, $result); }//end testFindExtendedClassName() diff --git a/tests/Core/File/FindImplementedInterfaceNamesTest.php b/tests/Core/File/FindImplementedInterfaceNamesTest.php index 6a8ee623ae..834e083202 100644 --- a/tests/Core/File/FindImplementedInterfaceNamesTest.php +++ b/tests/Core/File/FindImplementedInterfaceNamesTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class FindImplementedInterfaceNamesTest extends TestCase +class FindImplementedInterfaceNamesTest extends AbstractMethodUnitTest { - /** - * The \PHP_CodeSniffer\Files\File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Test retrieving the name(s) of the interfaces being implemented by a class. @@ -71,17 +27,8 @@ public function tearDown() */ public function testFindImplementedInterfaceNames($identifier, $expected) { - $start = ($this->phpcsFile->numTokens - 1); - $delim = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - $identifier - ); - $OOToken = $this->phpcsFile->findNext([T_CLASS, T_ANON_CLASS, T_INTERFACE], ($delim + 1)); - - $result = $this->phpcsFile->findImplementedInterfaceNames($OOToken); + $OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE]); + $result = self::$phpcsFile->findImplementedInterfaceNames($OOToken); $this->assertSame($expected, $result); }//end testFindImplementedInterfaceNames() diff --git a/tests/Core/File/GetMemberPropertiesTest.php b/tests/Core/File/GetMemberPropertiesTest.php index 6e1ab1b9ce..68ac99eb65 100644 --- a/tests/Core/File/GetMemberPropertiesTest.php +++ b/tests/Core/File/GetMemberPropertiesTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class GetMemberPropertiesTest extends TestCase +class GetMemberPropertiesTest extends AbstractMethodUnitTest { - /** - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize PHP_CodeSniffer_File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Test the getMemberProperties() method. @@ -71,20 +27,10 @@ public function tearDown() */ public function testGetMemberProperties($identifier, $expected) { - $start = ($this->phpcsFile->numTokens - 1); - $delim = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - $identifier - ); - $variable = $this->phpcsFile->findNext(T_VARIABLE, ($delim + 1)); + $variable = $this->getTargetToken($identifier, T_VARIABLE); + $result = self::$phpcsFile->getMemberProperties($variable); - $result = $this->phpcsFile->getMemberProperties($variable); - unset($result['type_token']); - unset($result['type_end_token']); - $this->assertSame($expected, $result); + $this->assertArraySubset($expected, $result, true); }//end testGetMemberProperties() @@ -532,17 +478,8 @@ public function dataGetMemberProperties() */ public function testNotClassPropertyException($identifier) { - $start = ($this->phpcsFile->numTokens - 1); - $delim = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - $identifier - ); - $variable = $this->phpcsFile->findNext(T_VARIABLE, ($delim + 1)); - - $result = $this->phpcsFile->getMemberProperties($variable); + $variable = $this->getTargetToken($identifier, T_VARIABLE); + $result = self::$phpcsFile->getMemberProperties($variable); }//end testNotClassPropertyException() @@ -578,17 +515,8 @@ public function dataNotClassProperty() */ public function testNotAVariableException() { - $start = ($this->phpcsFile->numTokens - 1); - $delim = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNotAVariable */' - ); - $next = $this->phpcsFile->findNext(T_WHITESPACE, ($delim + 1), null, true); - - $result = $this->phpcsFile->getMemberProperties($next); + $next = $this->getTargetToken('/* testNotAVariable */', T_RETURN); + $result = self::$phpcsFile->getMemberProperties($next); }//end testNotAVariableException() diff --git a/tests/Core/File/GetMethodParametersTest.inc b/tests/Core/File/GetMethodParametersTest.inc index a0ae183d42..a2c48b2919 100644 --- a/tests/Core/File/GetMethodParametersTest.inc +++ b/tests/Core/File/GetMethodParametersTest.inc @@ -19,7 +19,8 @@ function defaultValues($var1=1, $var2='value') {} function typeHint(foo $var1, bar $var2) {} class MyClass { -/* testSelfTypeHint */ function typeSelfHint(self $var) {} + /* testSelfTypeHint */ + function typeSelfHint(self $var) {} } /* testNullableTypeHint */ diff --git a/tests/Core/File/GetMethodParametersTest.php b/tests/Core/File/GetMethodParametersTest.php index a9e1e24ce7..57d5e8af6b 100644 --- a/tests/Core/File/GetMethodParametersTest.php +++ b/tests/Core/File/GetMethodParametersTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class GetMethodParametersTest extends TestCase +class GetMethodParametersTest extends AbstractMethodUnitTest { - /** - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize PHP_CodeSniffer_File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Verify pass-by-reference parsing. @@ -76,23 +32,7 @@ public function testPassByReference() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testPassByReference */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testPassByReference() @@ -114,23 +54,7 @@ public function testArrayHint() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testArrayHint */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testArrayHint() @@ -161,29 +85,7 @@ public function testTypeHint() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testTypeHint */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[1]['token']); - unset($found[0]['type_hint_token']); - unset($found[1]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[1]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[1]['comma_token']); - unset($found[0]['reference_token']); - unset($found[1]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[1]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testTypeHint() @@ -205,23 +107,7 @@ public function testSelfTypeHint() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testSelfTypeHint */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testSelfTypeHint() @@ -252,29 +138,7 @@ public function testNullableTypeHint() 'nullable_type' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNullableTypeHint */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[1]['token']); - unset($found[0]['type_hint_token']); - unset($found[1]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[1]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[1]['comma_token']); - unset($found[0]['reference_token']); - unset($found[1]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[1]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testNullableTypeHint() @@ -296,23 +160,7 @@ public function testVariable() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testVariable */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testVariable() @@ -335,25 +183,7 @@ public function testSingleDefaultValue() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testSingleDefaultValue */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[0]['default_token']); - unset($found[0]['default_equal_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testSingleDefaultValue() @@ -385,33 +215,7 @@ public function testDefaultValues() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testDefaultValues */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[1]['token']); - unset($found[0]['type_hint_token']); - unset($found[1]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[1]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[1]['comma_token']); - unset($found[0]['reference_token']); - unset($found[1]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[1]['variadic_token']); - unset($found[0]['default_token']); - unset($found[1]['default_token']); - unset($found[0]['default_equal_token']); - unset($found[1]['default_equal_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testDefaultValues() @@ -434,27 +238,27 @@ public function testBitwiseAndConstantExpressionDefaultValue() 'nullable_type' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testBitwiseAndConstantExpressionDefaultValue */' - ); - - $found = $this->phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[0]['default_token']); - unset($found[0]['default_equal_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testBitwiseAndConstantExpressionDefaultValue() + /** + * Test helper. + * + * @param string $commentString The comment which preceeds the test. + * @param array $expected The expected function output. + * + * @return void + */ + private function getMethodParametersTestHelper($commentString, $expected) + { + $function = $this->getTargetToken($commentString, [T_FUNCTION]); + $found = self::$phpcsFile->getMethodParameters($function); + + $this->assertArraySubset($expected, $found, true); + + }//end getMethodParametersTestHelper() + + }//end class diff --git a/tests/Core/File/GetMethodPropertiesTest.inc b/tests/Core/File/GetMethodPropertiesTest.inc index ced6c13f01..53a412b7bc 100644 --- a/tests/Core/File/GetMethodPropertiesTest.inc +++ b/tests/Core/File/GetMethodPropertiesTest.inc @@ -6,7 +6,8 @@ function myFunction() {} /* testReturnFunction */ function myFunction(array ...$arrays): array { - return array_map(/* testNestedClosure */function(array $array): int { + /* testNestedClosure */ + return array_map(function(array $array): int { return array_sum($array); }, $arrays); } diff --git a/tests/Core/File/GetMethodPropertiesTest.php b/tests/Core/File/GetMethodPropertiesTest.php index 803be5637c..1699a6db43 100644 --- a/tests/Core/File/GetMethodPropertiesTest.php +++ b/tests/Core/File/GetMethodPropertiesTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class GetMethodPropertiesTest extends TestCase +class GetMethodPropertiesTest extends AbstractMethodUnitTest { - /** - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize PHP_CodeSniffer_File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Test a basic function. @@ -77,18 +33,7 @@ public function testBasicFunction() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testBasicFunction */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 2)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testBasicFunction() @@ -111,18 +56,7 @@ public function testReturnFunction() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testReturnFunction */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 2)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testReturnFunction() @@ -145,18 +79,7 @@ public function testNestedClosure() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNestedClosure */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 1)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testNestedClosure() @@ -179,18 +102,7 @@ public function testBasicMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testBasicMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testBasicMethod() @@ -213,18 +125,7 @@ public function testPrivateStaticMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testPrivateStaticMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 7)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testPrivateStaticMethod() @@ -247,18 +148,7 @@ public function testFinalMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testFinalMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 7)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testFinalMethod() @@ -281,18 +171,7 @@ public function testProtectedReturnMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testProtectedReturnMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testProtectedReturnMethod() @@ -315,18 +194,7 @@ public function testPublicReturnMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testPublicReturnMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testPublicReturnMethod() @@ -349,18 +217,7 @@ public function testNullableReturnMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNullableReturnMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testNullableReturnMethod() @@ -383,18 +240,7 @@ public function testMessyNullableReturnMethod() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testMessyNullableReturnMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testMessyNullableReturnMethod() @@ -417,18 +263,7 @@ public function testReturnNamespace() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testReturnNamespace */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testReturnNamespace() @@ -451,18 +286,7 @@ public function testReturnMultilineNamespace() 'has_body' => true, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testReturnMultilineNamespace */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testReturnMultilineNamespace() @@ -485,18 +309,7 @@ public function testAbstractMethod() 'has_body' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testAbstractMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testAbstractMethod() @@ -519,18 +332,7 @@ public function testAbstractReturnMethod() 'has_body' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testAbstractReturnMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 7)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testAbstractReturnMethod() @@ -553,20 +355,27 @@ public function testInterfaceMethod() 'has_body' => false, ]; - $start = ($this->phpcsFile->numTokens - 1); - $function = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testInterfaceMethod */' - ); - - $found = $this->phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testInterfaceMethod() + /** + * Test helper. + * + * @param string $commentString The comment which preceeds the test. + * @param array $expected The expected function output. + * + * @return void + */ + private function getMethodPropertiesTestHelper($commentString, $expected) + { + $function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE]); + $found = self::$phpcsFile->getMethodProperties($function); + + $this->assertArraySubset($expected, $found, true); + + }//end getMethodPropertiesTestHelper() + + }//end class diff --git a/tests/Core/File/IsReferenceTest.inc b/tests/Core/File/IsReferenceTest.inc index c0c867b33e..2aed4eccf7 100644 --- a/tests/Core/File/IsReferenceTest.inc +++ b/tests/Core/File/IsReferenceTest.inc @@ -2,137 +2,137 @@ namespace PHP_CodeSniffer\Tests\Core\File; -/* bitwiseAndA */ +/* testBitwiseAndA */ error_reporting( E_NOTICE & E_STRICT ); -/* bitwiseAndB */ +/* testBitwiseAndB */ $a = [ $something & $somethingElse ]; -/* bitwiseAndC */ +/* testBitwiseAndC */ $a = [ $first, $something & self::$somethingElse ]; -/* bitwiseAndD */ +/* testBitwiseAndD */ $a = array $first, $something & $somethingElse ); -/* bitwiseAndE */ +/* testBitwiseAndE */ $a = [ 'a' => $first, 'b' => $something & $somethingElse ]; -/* bitwiseAndF */ +/* testBitwiseAndF */ $a = array( 'a' => $first, 'b' => $something & \MyClass::$somethingElse ); -/* bitwiseAndG */ +/* testBitwiseAndG */ $a = $something & $somethingElse; -/* bitwiseAndH */ +/* testBitwiseAndH */ function myFunction($a = 10 & 20) {} -/* bitwiseAndI */ +/* testBitwiseAndI */ $closure = function ($a = MY_CONSTANT & parent::OTHER_CONSTANT) {}; -/* functionReturnByReference */ +/* testFunctionReturnByReference */ function &myFunction() {} -/* functionPassByReferenceA */ +/* testFunctionPassByReferenceA */ function myFunction( &$a ) {} -/* functionPassByReferenceB */ +/* testFunctionPassByReferenceB */ function myFunction( $a, &$b ) {} -/* functionPassByReferenceC */ +/* testFunctionPassByReferenceC */ $closure = function ( &$a ) {}; -/* functionPassByReferenceD */ +/* testFunctionPassByReferenceD */ $closure = function ( $a, &$b ) {}; -/* functionPassByReferenceE */ +/* testFunctionPassByReferenceE */ function myFunction(array &$one) {} -/* functionPassByReferenceF */ +/* testFunctionPassByReferenceF */ $closure = function (\MyClass &$one) {}; -/* functionPassByReferenceG */ +/* testFunctionPassByReferenceG */ $closure = function myFunc($param, &...$moreParams) {}; -/* foreachValueByReference */ +/* testForeachValueByReference */ foreach( $array as $key => &$value ) {} -/* foreachKeyByReference */ +/* testForeachKeyByReference */ foreach( $array as &$key => $value ) {} -/* arrayValueByReferenceA */ +/* testArrayValueByReferenceA */ $a = [ 'a' => &$something ]; -/* arrayValueByReferenceB */ +/* testArrayValueByReferenceB */ $a = [ 'a' => $something, 'b' => &$somethingElse ]; -/* arrayValueByReferenceC */ +/* testArrayValueByReferenceC */ $a = [ &$something ]; -/* arrayValueByReferenceD */ +/* testArrayValueByReferenceD */ $a = [ $something, &$somethingElse ]; -/* arrayValueByReferenceE */ +/* testArrayValueByReferenceE */ $a = array( 'a' => &$something ); -/* arrayValueByReferenceF */ +/* testArrayValueByReferenceF */ $a = array( 'a' => $something, 'b' => &$somethingElse ); -/* arrayValueByReferenceG */ +/* testArrayValueByReferenceG */ $a = array( &$something ); -/* arrayValueByReferenceH */ +/* testArrayValueByReferenceH */ $a = array( $something, &$somethingElse ); -/* assignByReferenceA */ +/* testAssignByReferenceA */ $b = &$something; -/* assignByReferenceB */ +/* testAssignByReferenceB */ $b =& $something; -/* assignByReferenceC */ +/* testAssignByReferenceC */ $b .= &$something; -/* assignByReferenceD */ +/* testAssignByReferenceD */ $myValue = &$obj->getValue(); -/* assignByReferenceE */ +/* testAssignByReferenceE */ $collection = &collector(); -/* passByReferenceA */ +/* testPassByReferenceA */ functionCall(&$something, $somethingElse); -/* passByReferenceB */ +/* testPassByReferenceB */ functionCall($something, &$somethingElse); -/* passByReferenceC */ +/* testPassByReferenceC */ functionCall($something, &$this->somethingElse); -/* passByReferenceD */ +/* testPassByReferenceD */ functionCall($something, &self::$somethingElse); -/* passByReferenceE */ +/* testPassByReferenceE */ functionCall($something, &parent::$somethingElse); -/* passByReferenceF */ +/* testPassByReferenceF */ functionCall($something, &static::$somethingElse); -/* passByReferenceG */ +/* testPassByReferenceG */ functionCall($something, &SomeClass::$somethingElse); -/* passByReferenceH */ +/* testPassByReferenceH */ functionCall(&\SomeClass::$somethingElse); -/* passByReferenceI */ +/* testPassByReferenceI */ functionCall($something, &\SomeNS\SomeClass::$somethingElse); -/* passByReferenceJ */ +/* testPassByReferenceJ */ functionCall($something, &namespace\SomeClass::$somethingElse); -/* newByReferenceA */ +/* testNewByReferenceA */ $foobar2 = &new Foobar(); -/* newByReferenceB */ +/* testNewByReferenceB */ functionCall( $something , &new Foobar() ); -/* useByReference */ +/* testUseByReference */ $closure = function() use (&$var){}; diff --git a/tests/Core/File/IsReferenceTest.php b/tests/Core/File/IsReferenceTest.php index a72546a31f..f02f169e8e 100644 --- a/tests/Core/File/IsReferenceTest.php +++ b/tests/Core/File/IsReferenceTest.php @@ -9,55 +9,11 @@ namespace PHP_CodeSniffer\Tests\Core\File; -use PHP_CodeSniffer\Config; -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Files\DummyFile; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; -class IsReferenceTest extends TestCase +class IsReferenceTest extends AbstractMethodUnitTest { - /** - * The PHP_CodeSniffer_File object containing parsed contents of the test case file. - * - * @var \PHP_CodeSniffer\Files\File - */ - private $phpcsFile; - - - /** - * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. - * - * Methods used for these tests can be found in a test case file in the same - * directory and with the same name, using the .inc extension. - * - * @return void - */ - public function setUp() - { - $config = new Config(); - $config->standards = ['Generic']; - - $ruleset = new Ruleset($config); - - $pathToTestFile = dirname(__FILE__).'/'.basename(__FILE__, '.php').'.inc'; - $this->phpcsFile = new DummyFile(file_get_contents($pathToTestFile), $ruleset, $config); - $this->phpcsFile->process(); - - }//end setUp() - - - /** - * Clean up after finished test. - * - * @return void - */ - public function tearDown() - { - unset($this->phpcsFile); - - }//end tearDown() - /** * Test a class that extends another. @@ -71,17 +27,8 @@ public function tearDown() */ public function testIsReference($identifier, $expected) { - $start = ($this->phpcsFile->numTokens - 1); - $delim = $this->phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - $identifier - ); - $bitwiseAnd = $this->phpcsFile->findNext(T_BITWISE_AND, ($delim + 1)); - - $result = $this->phpcsFile->isReference($bitwiseAnd); + $bitwiseAnd = $this->getTargetToken($identifier, T_BITWISE_AND); + $result = self::$phpcsFile->isReference($bitwiseAnd); $this->assertSame($expected, $result); }//end testIsReference() @@ -98,183 +45,183 @@ public function dataIsReference() { return [ [ - '/* bitwiseAndA */', + '/* testBitwiseAndA */', false, ], [ - '/* bitwiseAndB */', + '/* testBitwiseAndB */', false, ], [ - '/* bitwiseAndC */', + '/* testBitwiseAndC */', false, ], [ - '/* bitwiseAndD */', + '/* testBitwiseAndD */', false, ], [ - '/* bitwiseAndE */', + '/* testBitwiseAndE */', false, ], [ - '/* bitwiseAndF */', + '/* testBitwiseAndF */', false, ], [ - '/* bitwiseAndG */', + '/* testBitwiseAndG */', false, ], [ - '/* bitwiseAndH */', + '/* testBitwiseAndH */', false, ], [ - '/* bitwiseAndI */', + '/* testBitwiseAndI */', false, ], [ - '/* functionReturnByReference */', + '/* testFunctionReturnByReference */', true, ], [ - '/* functionPassByReferenceA */', + '/* testFunctionPassByReferenceA */', true, ], [ - '/* functionPassByReferenceB */', + '/* testFunctionPassByReferenceB */', true, ], [ - '/* functionPassByReferenceC */', + '/* testFunctionPassByReferenceC */', true, ], [ - '/* functionPassByReferenceD */', + '/* testFunctionPassByReferenceD */', true, ], [ - '/* functionPassByReferenceE */', + '/* testFunctionPassByReferenceE */', true, ], [ - '/* functionPassByReferenceF */', + '/* testFunctionPassByReferenceF */', true, ], [ - '/* functionPassByReferenceG */', + '/* testFunctionPassByReferenceG */', true, ], [ - '/* foreachValueByReference */', + '/* testForeachValueByReference */', true, ], [ - '/* foreachKeyByReference */', + '/* testForeachKeyByReference */', true, ], [ - '/* arrayValueByReferenceA */', + '/* testArrayValueByReferenceA */', true, ], [ - '/* arrayValueByReferenceB */', + '/* testArrayValueByReferenceB */', true, ], [ - '/* arrayValueByReferenceC */', + '/* testArrayValueByReferenceC */', true, ], [ - '/* arrayValueByReferenceD */', + '/* testArrayValueByReferenceD */', true, ], [ - '/* arrayValueByReferenceE */', + '/* testArrayValueByReferenceE */', true, ], [ - '/* arrayValueByReferenceF */', + '/* testArrayValueByReferenceF */', true, ], [ - '/* arrayValueByReferenceG */', + '/* testArrayValueByReferenceG */', true, ], [ - '/* arrayValueByReferenceH */', + '/* testArrayValueByReferenceH */', true, ], [ - '/* assignByReferenceA */', + '/* testAssignByReferenceA */', true, ], [ - '/* assignByReferenceB */', + '/* testAssignByReferenceB */', true, ], [ - '/* assignByReferenceC */', + '/* testAssignByReferenceC */', true, ], [ - '/* assignByReferenceD */', + '/* testAssignByReferenceD */', true, ], [ - '/* assignByReferenceE */', + '/* testAssignByReferenceE */', true, ], [ - '/* passByReferenceA */', + '/* testPassByReferenceA */', true, ], [ - '/* passByReferenceB */', + '/* testPassByReferenceB */', true, ], [ - '/* passByReferenceC */', + '/* testPassByReferenceC */', true, ], [ - '/* passByReferenceD */', + '/* testPassByReferenceD */', true, ], [ - '/* passByReferenceE */', + '/* testPassByReferenceE */', true, ], [ - '/* passByReferenceF */', + '/* testPassByReferenceF */', true, ], [ - '/* passByReferenceG */', + '/* testPassByReferenceG */', true, ], [ - '/* passByReferenceH */', + '/* testPassByReferenceH */', true, ], [ - '/* passByReferenceI */', + '/* testPassByReferenceI */', true, ], [ - '/* passByReferenceJ */', + '/* testPassByReferenceJ */', true, ], [ - '/* newByReferenceA */', + '/* testNewByReferenceA */', true, ], [ - '/* newByReferenceB */', + '/* testNewByReferenceB */', true, ], [ - '/* useByReference */', + '/* testUseByReference */', true, ], ]; diff --git a/scripts/ValidatePEAR/FileList.php b/tests/FileList.php similarity index 92% rename from scripts/ValidatePEAR/FileList.php rename to tests/FileList.php index 171873dbee..8ef57b7aff 100644 --- a/scripts/ValidatePEAR/FileList.php +++ b/tests/FileList.php @@ -1,19 +1,14 @@ * @copyright 2019 Juliette Reinders Folmer. All rights reserved. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ -/** - * Class to create a file list with filtering. - */ +namespace PHP_CodeSniffer\Tests; + class FileList { @@ -29,7 +24,7 @@ class FileList * * @var \DirectoryIterator */ - protected $fileIterator; + public $fileIterator; /** * Base regex to use if no filter regex is provided.