DiffConsoleFormatterTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Differ;
  13. use PhpCsFixer\Differ\DiffConsoleFormatter;
  14. use PhpCsFixer\Tests\TestCase;
  15. use Symfony\Component\Console\Formatter\OutputFormatter;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Differ\DiffConsoleFormatter
  20. */
  21. final class DiffConsoleFormatterTest extends TestCase
  22. {
  23. /**
  24. * @dataProvider provideDiffConsoleFormatterCases
  25. */
  26. public function testDiffConsoleFormatter(string $expected, bool $isDecoratedOutput, string $template, string $diff, string $lineTemplate): void
  27. {
  28. $diffFormatter = new DiffConsoleFormatter($isDecoratedOutput, $template);
  29. self::assertSame(
  30. str_replace(PHP_EOL, "\n", $expected),
  31. str_replace(PHP_EOL, "\n", $diffFormatter->format($diff, $lineTemplate))
  32. );
  33. }
  34. /**
  35. * @return iterable<array{string, bool, string, string, string}>
  36. */
  37. public static function provideDiffConsoleFormatterCases(): iterable
  38. {
  39. yield [
  40. \sprintf(
  41. '<comment> ---------- begin diff ----------</comment>
  42. '.'
  43. <fg=cyan>%s</fg=cyan>
  44. no change
  45. <fg=red>%s</fg=red>
  46. <fg=green>%s</fg=green>
  47. <fg=green>%s</fg=green>
  48. '.'
  49. <comment> ----------- end diff -----------</comment>',
  50. OutputFormatter::escape('@@ -12,51 +12,151 @@'),
  51. OutputFormatter::escape('-/**\\'),
  52. OutputFormatter::escape('+/*\\'),
  53. OutputFormatter::escape('+A')
  54. ),
  55. true,
  56. \sprintf(
  57. '<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>',
  58. PHP_EOL,
  59. PHP_EOL
  60. ),
  61. '
  62. @@ -12,51 +12,151 @@
  63. no change
  64. -/**\
  65. +/*\
  66. +A
  67. ',
  68. ' %s',
  69. ];
  70. yield [
  71. '[start]
  72. | '.'
  73. | @@ -12,51 +12,151 @@
  74. | no change
  75. | '.'
  76. | -/**\
  77. | +/*\
  78. | +A
  79. | '.'
  80. [end]',
  81. false,
  82. \sprintf('[start]%s%%s%s[end]', PHP_EOL, PHP_EOL),
  83. '
  84. @@ -12,51 +12,151 @@
  85. no change
  86. '.'
  87. -/**\
  88. +/*\
  89. +A
  90. ',
  91. '| %s',
  92. ];
  93. yield [
  94. mb_convert_encoding("<fg=red>--- Original</fg=red>\n<fg=green>+ausgefüllt</fg=green>", 'ISO-8859-1'),
  95. true,
  96. '%s',
  97. mb_convert_encoding("--- Original\n+ausgefüllt", 'ISO-8859-1'),
  98. '%s',
  99. ];
  100. yield [
  101. mb_convert_encoding("<fg=red>--- Original</fg=red>\n<fg=green>+++ New</fg=green>\n<fg=cyan>@@ @@</fg=cyan>\n<fg=red>-ausgefüllt</fg=red>", 'ISO-8859-1'),
  102. true,
  103. '%s',
  104. mb_convert_encoding("--- Original\n+++ New\n@@ @@\n-ausgefüllt", 'ISO-8859-1'),
  105. '%s',
  106. ];
  107. yield [
  108. mb_convert_encoding("--- Original\n+++ New\n@@ @@\n-ausgefüllt", 'ISO-8859-1'),
  109. false,
  110. '%s',
  111. mb_convert_encoding("--- Original\n+++ New\n@@ @@\n-ausgefüllt", 'ISO-8859-1'),
  112. '%s',
  113. ];
  114. }
  115. }