public function CoderSniffUnitTest::generateFailureMessages in Coder 7.2
Generate a list of test failures for a given sniffed file.
Parameters
PHP_CodeSniffer_File $file The file being tested.:
Return value
array
Throws
PHP_CodeSniffer_Exception
1 call to CoderSniffUnitTest::generateFailureMessages()
- CoderSniffUnitTest::testSniff in coder_sniffer/
Test/ CoderSniffUnitTest.php - Tests the extending classes Sniff class.
File
- coder_sniffer/
Test/ CoderSniffUnitTest.php, line 136
Class
- CoderSniffUnitTest
- An abstract class that all sniff unit tests must extend.
Code
public function generateFailureMessages(PHP_CodeSniffer_File $file) {
$testFile = $file
->getFilename();
$foundErrors = $file
->getErrors();
$foundWarnings = $file
->getWarnings();
$expectedErrors = $this
->getErrorList(basename($testFile));
$expectedWarnings = $this
->getWarningList(basename($testFile));
if (is_array($expectedErrors) === false) {
throw new PHP_CodeSniffer_Exception('getErrorList() must return an array');
}
if (is_array($expectedWarnings) === false) {
throw new PHP_CodeSniffer_Exception('getWarningList() must return an array');
}
/*
We merge errors and warnings together to make it easier
to iterate over them and produce the errors string. In this way,
we can report on errors and warnings in the same line even though
it's not really structured to allow that.
*/
$allProblems = array();
$failureMessages = array();
foreach ($foundErrors as $line => $lineErrors) {
foreach ($lineErrors as $column => $errors) {
if (isset($allProblems[$line]) === false) {
$allProblems[$line] = array(
'expected_errors' => 0,
'expected_warnings' => 0,
'found_errors' => array(),
'found_warnings' => array(),
);
}
$foundErrorsTemp = array();
foreach ($allProblems[$line]['found_errors'] as $foundError) {
$foundErrorsTemp[] = $foundError;
}
$errorsTemp = array();
foreach ($errors as $foundError) {
$errorsTemp[] = $foundError['message'];
}
$allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp);
}
if (isset($expectedErrors[$line]) === true) {
$allProblems[$line]['expected_errors'] = $expectedErrors[$line];
}
else {
$allProblems[$line]['expected_errors'] = 0;
}
unset($expectedErrors[$line]);
}
//end foreach
foreach ($expectedErrors as $line => $numErrors) {
if (isset($allProblems[$line]) === false) {
$allProblems[$line] = array(
'expected_errors' => 0,
'expected_warnings' => 0,
'found_errors' => array(),
'found_warnings' => array(),
);
}
$allProblems[$line]['expected_errors'] = $numErrors;
}
foreach ($foundWarnings as $line => $lineWarnings) {
foreach ($lineWarnings as $column => $warnings) {
if (isset($allProblems[$line]) === false) {
$allProblems[$line] = array(
'expected_errors' => 0,
'expected_warnings' => 0,
'found_errors' => array(),
'found_warnings' => array(),
);
}
$foundWarningsTemp = array();
foreach ($allProblems[$line]['found_warnings'] as $foundWarning) {
$foundWarningsTemp[] = $foundWarning;
}
$warningsTemp = array();
foreach ($warnings as $warning) {
$warningsTemp[] = $warning['message'];
}
$allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp);
}
if (isset($expectedWarnings[$line]) === true) {
$allProblems[$line]['expected_warnings'] = $expectedWarnings[$line];
}
else {
$allProblems[$line]['expected_warnings'] = 0;
}
unset($expectedWarnings[$line]);
}
//end foreach
foreach ($expectedWarnings as $line => $numWarnings) {
if (isset($allProblems[$line]) === false) {
$allProblems[$line] = array(
'expected_errors' => 0,
'expected_warnings' => 0,
'found_errors' => array(),
'found_warnings' => array(),
);
}
$allProblems[$line]['expected_warnings'] = $numWarnings;
}
// Order the messages by line number.
ksort($allProblems);
foreach ($allProblems as $line => $problems) {
$numErrors = count($problems['found_errors']);
$numWarnings = count($problems['found_warnings']);
$expectedErrors = $problems['expected_errors'];
$expectedWarnings = $problems['expected_warnings'];
// Uncomment the following generate line error pairs for the bad unit
// test.
/*if ($numErrors) {
print "$line => " . $numErrors . ",\n";
}
if ($numWarnings) {
print "$line => " . $numWarnings . ",\n";
}*/
$errors = '';
$foundString = '';
if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) {
$lineMessage = "[LINE {$line}]";
$expectedMessage = 'Expected ';
$foundMessage = 'in ' . basename($testFile) . ' but found ';
if ($expectedErrors !== $numErrors) {
$expectedMessage .= "{$expectedErrors} error(s)";
$foundMessage .= "{$numErrors} error(s)";
if ($numErrors !== 0) {
$foundString .= 'error(s)';
$errors .= implode(PHP_EOL . ' -> ', $problems['found_errors']);
}
if ($expectedWarnings !== $numWarnings) {
$expectedMessage .= ' and ';
$foundMessage .= ' and ';
if ($numWarnings !== 0) {
if ($foundString !== '') {
$foundString .= ' and ';
}
}
}
}
if ($expectedWarnings !== $numWarnings) {
$expectedMessage .= "{$expectedWarnings} warning(s)";
$foundMessage .= "{$numWarnings} warning(s)";
if ($numWarnings !== 0) {
$foundString .= 'warning(s)';
if (empty($errors) === false) {
$errors .= PHP_EOL . ' -> ';
}
$errors .= implode(PHP_EOL . ' -> ', $problems['found_warnings']);
}
}
$fullMessage = "{$lineMessage} {$expectedMessage} {$foundMessage}.";
if ($errors !== '') {
$fullMessage .= " The {$foundString} found were:" . PHP_EOL . " -> {$errors}";
}
$failureMessages[] = $fullMessage;
}
//end if
}
//end foreach
return $failureMessages;
}