TextDiffTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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;
  13. use PhpCsFixer\Console\Command\FixCommand;
  14. use PhpCsFixer\Console\Report\FixReport\ReporterFactory;
  15. use PhpCsFixer\ToolInfo;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. /**
  19. * @internal
  20. *
  21. * @coversNothing
  22. *
  23. * @group covers-nothing
  24. */
  25. final class TextDiffTest extends TestCase
  26. {
  27. /**
  28. * @dataProvider provideDiffReportingDecoratedCases
  29. */
  30. public function testDiffReportingDecorated(string $expected, string $format, bool $isDecorated): void
  31. {
  32. $command = new FixCommand(new ToolInfo());
  33. $commandTester = new CommandTester($command);
  34. $commandTester->execute(
  35. [
  36. 'path' => [__DIR__.'/Fixtures/FixCommand/TextDiffTestInput.php'],
  37. '--diff' => true,
  38. '--dry-run' => true,
  39. '--format' => $format,
  40. '--rules' => 'cast_spaces',
  41. '--using-cache' => 'no',
  42. ],
  43. [
  44. 'decorated' => $isDecorated,
  45. 'verbosity' => OutputInterface::VERBOSITY_NORMAL,
  46. ]
  47. );
  48. if ($isDecorated !== $commandTester->getOutput()->isDecorated()) {
  49. self::markTestSkipped(\sprintf('Output should %sbe decorated.', $isDecorated ? '' : 'not '));
  50. }
  51. if ($isDecorated !== $commandTester->getOutput()->getFormatter()->isDecorated()) {
  52. self::markTestSkipped(\sprintf('Formatter should %sbe decorated.', $isDecorated ? '' : 'not '));
  53. }
  54. self::assertStringMatchesFormat($expected, $commandTester->getDisplay(false));
  55. }
  56. /**
  57. * @return iterable<array{string, string, bool}>
  58. */
  59. public static function provideDiffReportingDecoratedCases(): iterable
  60. {
  61. $expected = <<<'TEST'
  62. %A$output->writeln('<error>'.(int)$output.'</error>');%A
  63. %A$output->writeln('<error>'.(int) $output.'</error>');%A
  64. %A$output->writeln('<error> TEST </error>');%A
  65. %A$output->writeln('<error>'.(int)$output.'</error>');%A
  66. %A$output->writeln('<error>'.(int) $output.'</error>');%A
  67. TEST;
  68. foreach (['txt', 'xml', 'junit'] as $format) {
  69. yield [$expected, $format, true];
  70. yield [$expected, $format, false];
  71. }
  72. $expected = substr(json_encode($expected, JSON_THROW_ON_ERROR), 1, -1);
  73. yield [$expected, 'json', true];
  74. yield [$expected, 'json', false];
  75. }
  76. /**
  77. * Test to make sure @see TextDiffTest::provideDiffReportingCases covers all formats.
  78. */
  79. public function testAllFormatsCovered(): void
  80. {
  81. $factory = new ReporterFactory();
  82. $formats = $factory->registerBuiltInReporters()->getFormats();
  83. sort($formats);
  84. self::assertSame(
  85. ['checkstyle', 'gitlab', 'json', 'junit', 'txt', 'xml'],
  86. $formats
  87. );
  88. }
  89. }