TextDiffTest.php 2.9 KB

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