Skip to content
Snippets Groups Projects
Commit 984c54a7 authored by Klaus Purer's avatar Klaus Purer
Browse files

feat(ExpectedExceptionSniff): Add sniff to forbid usage of PHPUnit...

feat(ExpectedExceptionSniff): Add sniff to forbid usage of PHPUnit @expectedException annotation (#2859286)
parent 40708d83
No related branches found
No related tags found
No related merge requests found
<?php
/**
* DrupalPractice_Sniffs_Commenting_ExpectedExceptionSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Checks that the PHPunit @expectedExcpetion tags are not used.
*
* See https://thephp.cc/news/2016/02/questioning-phpunit-best-practices .
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class DrupalPractice_Sniffs_Commenting_ExpectedExceptionSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_DOC_COMMENT_TAG);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$content = $tokens[$stackPtr]['content'];
if ($content === '@expectedException' || $content === '@expectedExceptionCode'
|| $content === '@expectedExceptionMessage'
|| $content === '@expectedExceptionMessageRegExp'
) {
$warning = '%s tags should not be used, use $§this->setExpectedException() or $this->expectException() instead';
$phpcsFile->addWarning($warning, $stackPtr, 'TagFound', [$content]);
}
}//end process()
}//end class
<?php
class ExampleTest extends UnitTestCase {
/**
* Test 1.
*
* @expectedException TestException
* @expectedExceptionCode 100
* @expectedExceptionMessage Message here
* @expectedExceptionMessageRegExp /.+/
*/
public function test1() {
}
}
<?php
class DrupalPractice_Sniffs_Commenting_ExpectedExceptionUnitTest extends CoderSniffUnitTest
{
/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array(int => int)
*/
protected function getErrorList($testFile)
{
return array();
}//end getErrorList()
/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
protected function getWarningList($testFile)
{
return array(
8 => 1,
9 => 1,
10 => 1,
11 => 1,0
);
}//end getWarningList()
}//end class
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment