You are here

public function CoderSniffUnitTest::generateFailureMessages in Coder 8.2

Same name and namespace in other branches
  1. 8.3 tests/Drupal/CoderSniffUnitTest.php \Drupal\Test\CoderSniffUnitTest::generateFailureMessages()
  2. 8.3.x tests/Drupal/CoderSniffUnitTest.php \Drupal\Test\CoderSniffUnitTest::generateFailureMessages()

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/Drupal/Test/CoderSniffUnitTest.php
Tests the extending classes Sniff class.

File

coder_sniffer/Drupal/Test/CoderSniffUnitTest.php, line 235

Class

CoderSniffUnitTest

Namespace

Drupal\Test

Code

public function generateFailureMessages(LocalFile $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 RuntimeException('getErrorList() must return an array');
  }
  if (is_array($expectedWarnings) === false) {
    throw new RuntimeException('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();
  $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'] = [];
  $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'] = [];
  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'] . ' (' . $foundError['source'] . ')';
        $source = $foundError['source'];
        if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES']) === false) {
          $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'][] = $source;
        }
        if ($foundError['fixable'] === true && in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES']) === false) {
          $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'][] = $source;
        }
      }
      $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp);
    }

    //end foreach
    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'] . ' (' . $warning['source'] . ')';
      }
      $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp);
    }

    //end foreach
    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'];
    $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;
}