TextDiffTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests;
  12. use PhpCsFixer\Console\Command\FixCommand;
  13. use PhpCsFixer\Report\ReporterFactory;
  14. use PhpCsFixer\ToolInfo;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. /**
  18. * @author SpacePossum
  19. *
  20. * @internal
  21. *
  22. * @coversNothing
  23. * @group covers-nothing
  24. */
  25. final class TextDiffTest extends TestCase
  26. {
  27. /**
  28. * @param string $expected
  29. * @param string $format
  30. * @param bool $isDecorated
  31. *
  32. * @dataProvider provideDiffReportingCases
  33. */
  34. public function testDiffReportingDecorated($expected, $format, $isDecorated)
  35. {
  36. $command = new FixCommand(new ToolInfo());
  37. $commandTester = new CommandTester($command);
  38. $commandTester->execute(
  39. [
  40. 'path' => [__DIR__.'/Fixtures/FixCommand/TextDiffTestInput.php'],
  41. '--diff' => true,
  42. '--dry-run' => true,
  43. '--format' => $format,
  44. '--rules' => 'cast_spaces',
  45. '--using-cache' => 'no',
  46. ],
  47. [
  48. 'decorated' => $isDecorated,
  49. 'verbosity' => OutputInterface::VERBOSITY_NORMAL,
  50. ]
  51. );
  52. if ($isDecorated !== $commandTester->getOutput()->isDecorated()) {
  53. static::markTestSkipped(sprintf('Output should %sbe decorated.', $isDecorated ? '' : 'not '));
  54. }
  55. if ($isDecorated !== $commandTester->getOutput()->getFormatter()->isDecorated()) {
  56. static::markTestSkipped(sprintf('Formatter should %sbe decorated.', $isDecorated ? '' : 'not '));
  57. }
  58. static::assertStringMatchesFormat($expected, $commandTester->getDisplay(false));
  59. }
  60. public function provideDiffReportingCases()
  61. {
  62. $expected = <<<'TEST'
  63. %A$output->writeln('<error>'.(int)$output.'</error>');%A
  64. %A$output->writeln('<error>'.(int) $output.'</error>');%A
  65. %A$output->writeln('<error> TEST </error>');%A
  66. %A$output->writeln('<error>'.(int)$output.'</error>');%A
  67. %A$output->writeln('<error>'.(int) $output.'</error>');%A
  68. TEST;
  69. $cases = [];
  70. foreach (['txt', 'xml', 'junit'] as $format) {
  71. $cases[] = [$expected, $format, true];
  72. $cases[] = [$expected, $format, false];
  73. }
  74. $expected = substr(json_encode($expected), 1, -1);
  75. $cases[] = [$expected, 'json', true];
  76. $cases[] = [$expected, 'json', false];
  77. return $cases;
  78. }
  79. /**
  80. * Test to make sure @see TextDiffTest::provideDiffReportingCases covers all formats.
  81. */
  82. public function testAllFormatsCovered()
  83. {
  84. $factory = ReporterFactory::create();
  85. $formats = $factory->registerBuiltInReporters()->getFormats();
  86. sort($formats);
  87. static::assertSame(
  88. ['checkstyle', 'gitlab', 'json', 'junit', 'txt', 'xml'],
  89. $formats
  90. );
  91. }
  92. }