You are here

public function CoderSniffUnitTest::generateFailureMessages in Coder 8.3.x

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

Generate a list of test failures for a given sniffed file.

Parameters

\PHP_CodeSniffer\Files\LocalFile $file The file being tested.:

Return value

array<string>

Throws

\PHP_CodeSniffer\Exceptions\RuntimeException

1 call to CoderSniffUnitTest::generateFailureMessages()
CoderSniffUnitTest::testSniff in tests/Drupal/CoderSniffUnitTest.php
Tests the extending classes Sniff class.

File

tests/Drupal/CoderSniffUnitTest.php, line 238

Class

CoderSniffUnitTest

Namespace

Drupal\Test

Code

public function generateFailureMessages(LocalFile $file) : array {
  $testFile = $file
    ->getFilename();
  $foundErrors = $file
    ->getErrors();
  $foundWarnings = $file
    ->getWarnings();
  $expectedErrors = $this
    ->getErrorList(basename($testFile));
  $expectedWarnings = $this
    ->getWarningList(basename($testFile));

  /*
      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 = [];
  $failureMessages = [];
  $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] = [
          'expected_errors' => 0,
          'expected_warnings' => 0,
          'found_errors' => [],
          'found_warnings' => [],
        ];
      }
      $foundErrorsTemp = [];
      foreach ($allProblems[$line]['found_errors'] as $foundError) {
        $foundErrorsTemp[] = $foundError;
      }
      $errorsTemp = [];
      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] = [
        'expected_errors' => 0,
        'expected_warnings' => 0,
        'found_errors' => [],
        'found_warnings' => [],
      ];
    }
    $allProblems[$line]['expected_errors'] = $numErrors;
  }
  foreach ($foundWarnings as $line => $lineWarnings) {
    foreach ($lineWarnings as $column => $warnings) {
      if (isset($allProblems[$line]) === false) {
        $allProblems[$line] = [
          'expected_errors' => 0,
          'expected_warnings' => 0,
          'found_errors' => [],
          'found_warnings' => [],
        ];
      }
      $foundWarningsTemp = [];
      foreach ($allProblems[$line]['found_warnings'] as $foundWarning) {
        $foundWarningsTemp[] = $foundWarning;
      }
      $warningsTemp = [];
      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] = [
        'expected_errors' => 0,
        'expected_warnings' => 0,
        'found_errors' => [],
        'found_warnings' => [],
      ];
    }
    $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;
}