diff --git a/coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/WaitForTextSniff.php b/coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/WaitForTextSniff.php new file mode 100644 index 00000000..7af46be8 --- /dev/null +++ b/coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/WaitForTextSniff.php @@ -0,0 +1,83 @@ + + */ + public function registerFunctionNames() + { + return ['waitForText']; + + }//end registerFunctionNames() + + + /** + * Processes this function call. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the function call in + * the stack. + * @param int $openBracket The position of the opening + * parenthesis in the stack. + * @param int $closeBracket The position of the closing + * parenthesis in the stack. + * + * @return void|int + */ + public function processFunctionCall( + File $phpcsFile, + $stackPtr, + $openBracket, + $closeBracket + ) { + $tokens = $phpcsFile->getTokens(); + + $start = $phpcsFile->findStartOfStatement($stackPtr); + $end = $phpcsFile->findEndOfStatement($start); + // We are assigning to a variable, all is well. + if ($tokens[$start]['code'] === T_VARIABLE && $phpcsFile->findNext(T_EQUAL, $start, $end) !== false) { + return; + } + + $function = ($start - 2); + if ($tokens[$function]['content'] === 'assertTrue' || $tokens[$function]['content'] === 'assertFalse') { + return; + } + + $phpcsFile->addWarning('waitForText functions do not self assert and must be asserted manually', $start, 'WaitForText'); + + }//end processFunctionCall() + + +}//end class diff --git a/tests/DrupalPractice/FunctionCalls/WaitForTextUnitTest.inc b/tests/DrupalPractice/FunctionCalls/WaitForTextUnitTest.inc new file mode 100644 index 00000000..34771d92 --- /dev/null +++ b/tests/DrupalPractice/FunctionCalls/WaitForTextUnitTest.inc @@ -0,0 +1,15 @@ +assertTrue($this->assertSession()->waitForText('Example Text')); +$this->assertFalse($this->assertSession()->waitForText('Example Text')); + +// Assigned to a variable which we assume will be asserted later +$wait = $this->assertSession()->waitForText('Example Text'); + +// Incorrect assertion, waitForText only returns true or false and should be asserted as such +$this->assertEmpty($this->assertSession()->waitForText('Example Text')); + +// No Assertion, failure +$this->assertSession()->waitForText('Example Text'); + diff --git a/tests/DrupalPractice/FunctionCalls/WaitForTextUnitTest.php b/tests/DrupalPractice/FunctionCalls/WaitForTextUnitTest.php new file mode 100644 index 00000000..f8e3428d --- /dev/null +++ b/tests/DrupalPractice/FunctionCalls/WaitForTextUnitTest.php @@ -0,0 +1,57 @@ + + */ + protected function getErrorList(string $testFile): array + { + return []; + + }//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. + * + * @param string $testFile The name of the file being tested. + * + * @return array + */ + protected function getWarningList(string $testFile): array + { + return [ + 11 => 1, + 14 => 1, + ]; + + }//end getWarningList() + + +}//end class