TextDiffTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. public static function provideDiffReportingDecoratedCases(): iterable
  57. {
  58. $expected = <<<'TEST'
  59. %A$output->writeln('<error>'.(int)$output.'</error>');%A
  60. %A$output->writeln('<error>'.(int) $output.'</error>');%A
  61. %A$output->writeln('<error> TEST </error>');%A
  62. %A$output->writeln('<error>'.(int)$output.'</error>');%A
  63. %A$output->writeln('<error>'.(int) $output.'</error>');%A
  64. TEST;
  65. foreach (['txt', 'xml', 'junit'] as $format) {
  66. yield [$expected, $format, true];
  67. yield [$expected, $format, false];
  68. }
  69. $expected = substr(json_encode($expected, JSON_THROW_ON_ERROR), 1, -1);
  70. yield [$expected, 'json', true];
  71. yield [$expected, 'json', false];
  72. }
  73. /**
  74. * Test to make sure @see TextDiffTest::provideDiffReportingCases covers all formats.
  75. */
  76. public function testAllFormatsCovered(): void
  77. {
  78. $factory = new ReporterFactory();
  79. $formats = $factory->registerBuiltInReporters()->getFormats();
  80. sort($formats);
  81. self::assertSame(
  82. ['checkstyle', 'gitlab', 'json', 'junit', 'txt', 'xml'],
  83. $formats
  84. );
  85. }
  86. }