ErrorOutputTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Console\Output;
  13. use PhpCsFixer\Console\Output\ErrorOutput;
  14. use PhpCsFixer\Error\Error;
  15. use PhpCsFixer\Linter\LintingException;
  16. use PhpCsFixer\Tests\TestCase;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Output\StreamOutput;
  19. /**
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Console\Output\ErrorOutput
  23. */
  24. final class ErrorOutputTest extends TestCase
  25. {
  26. /**
  27. * @param OutputInterface::VERBOSITY_* $verbosityLevel
  28. *
  29. * @dataProvider provideErrorOutputCases
  30. */
  31. public function testErrorOutput(Error $error, int $verbosityLevel, int $lineNumber, int $exceptionLineNumber, string $process): void
  32. {
  33. $source = $error->getSource();
  34. $output = $this->createStreamOutput($verbosityLevel);
  35. $errorOutput = new ErrorOutput($output);
  36. $errorOutput->listErrors($process, [$error]);
  37. $displayed = $this->readFullStreamOutput($output);
  38. $startWith = \sprintf(
  39. '
  40. Files that were not fixed due to errors reported during %s:
  41. 1) %s',
  42. $process,
  43. __FILE__
  44. );
  45. if ($verbosityLevel >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
  46. $startWith .= \sprintf(
  47. '
  48. '.'
  49. [%s] '.'
  50. %s (%d) '.'
  51. '.'
  52. ',
  53. \get_class($source),
  54. $source->getMessage(),
  55. $source->getCode()
  56. );
  57. }
  58. if ($verbosityLevel >= OutputInterface::VERBOSITY_DEBUG) {
  59. $startWith .= \sprintf(
  60. '
  61. PhpCsFixer\Tests\Console\Output\ErrorOutputTest::getErrorAndLineNumber()
  62. in %s at line %d
  63. PhpCsFixer\Tests\Console\Output\ErrorOutputTest::provideErrorOutputCases()
  64. ',
  65. __FILE__,
  66. $lineNumber
  67. );
  68. }
  69. self::assertStringStartsWith($startWith, $displayed);
  70. }
  71. /**
  72. * @return iterable<array{Error, int, int, int, string}>
  73. */
  74. public static function provideErrorOutputCases(): iterable
  75. {
  76. $lineNumber = __LINE__;
  77. [$exceptionLineNumber, $error] = self::getErrorAndLineNumber(); // note: keep call and __LINE__ separated with one line break
  78. ++$lineNumber;
  79. yield [$error, OutputInterface::VERBOSITY_NORMAL, $lineNumber, $exceptionLineNumber, 'VN'];
  80. yield [$error, OutputInterface::VERBOSITY_VERBOSE, $lineNumber, $exceptionLineNumber, 'VV'];
  81. yield [$error, OutputInterface::VERBOSITY_VERY_VERBOSE, $lineNumber, $exceptionLineNumber, 'VVV'];
  82. yield [$error, OutputInterface::VERBOSITY_DEBUG, $lineNumber, $exceptionLineNumber, 'DEBUG'];
  83. }
  84. public function testLintingExceptionOutputsAppliedFixersAndDiff(): void
  85. {
  86. $fixerName = uniqid('braces_');
  87. $diffSpecificContext = uniqid('added_');
  88. $diff = <<<EOT
  89. --- Original
  90. +++ New
  91. @@ @@
  92. same line
  93. -deleted
  94. +{$diffSpecificContext}
  95. EOT;
  96. $lintError = new Error(Error::TYPE_LINT, __FILE__, new LintingException(), [$fixerName], $diff);
  97. $noDiffLintFixerName = uniqid('no_diff_');
  98. $noDiffLintError = new Error(Error::TYPE_LINT, __FILE__, new LintingException(), [$noDiffLintFixerName]);
  99. $invalidErrorFixerName = uniqid('line_ending_');
  100. $invalidDiff = uniqid('invalid_diff_');
  101. $invalidError = new Error(Error::TYPE_INVALID, __FILE__, new LintingException(), [$invalidErrorFixerName], $invalidDiff);
  102. $output = $this->createStreamOutput(OutputInterface::VERBOSITY_VERY_VERBOSE);
  103. $errorOutput = new ErrorOutput($output);
  104. $errorOutput->listErrors(uniqid('process_'), [
  105. $lintError,
  106. $noDiffLintError,
  107. $invalidError,
  108. ]);
  109. $displayed = $this->readFullStreamOutput($output);
  110. self::assertStringContainsString($fixerName, $displayed);
  111. self::assertStringContainsString($diffSpecificContext, $displayed);
  112. self::assertStringContainsString($noDiffLintFixerName, $displayed);
  113. self::assertStringNotContainsString($invalidErrorFixerName, $displayed);
  114. self::assertStringNotContainsString($invalidDiff, $displayed);
  115. }
  116. /**
  117. * @param OutputInterface::VERBOSITY_* $verbosityLevel
  118. */
  119. private function createStreamOutput(int $verbosityLevel): StreamOutput
  120. {
  121. $output = new StreamOutput(fopen('php://memory', 'w', false));
  122. $output->setDecorated(false);
  123. $output->setVerbosity($verbosityLevel);
  124. return $output;
  125. }
  126. private function readFullStreamOutput(StreamOutput $output): string
  127. {
  128. rewind($output->getStream());
  129. $displayed = stream_get_contents($output->getStream());
  130. // normalize line breaks,
  131. // as we output using SF `writeln` we are not sure what line ending has been used as it is
  132. // based on the platform/console/terminal used
  133. return str_replace(PHP_EOL, "\n", $displayed);
  134. }
  135. /**
  136. * @return array{int, Error}
  137. */
  138. private static function getErrorAndLineNumber(): array
  139. {
  140. $lineNumber = __LINE__;
  141. $exception = new \RuntimeException(// note: keep exception constructor and __LINE__ separated with one line break
  142. 'PHPUnit RT',
  143. 888,
  144. new \InvalidArgumentException('PHPUnit IAE')
  145. );
  146. return [$lineNumber + 1, new Error(Error::TYPE_EXCEPTION, __FILE__, $exception)];
  147. }
  148. }