DiffConsoleFormatterTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. public static function provideDiffConsoleFormatterCases(): iterable
  35. {
  36. yield [
  37. sprintf(
  38. '<comment> ---------- begin diff ----------</comment>
  39. '.'
  40. <fg=cyan>%s</fg=cyan>
  41. no change
  42. <fg=red>%s</fg=red>
  43. <fg=green>%s</fg=green>
  44. <fg=green>%s</fg=green>
  45. '.'
  46. <comment> ----------- end diff -----------</comment>',
  47. OutputFormatter::escape('@@ -12,51 +12,151 @@'),
  48. OutputFormatter::escape('-/**\\'),
  49. OutputFormatter::escape('+/*\\'),
  50. OutputFormatter::escape('+A')
  51. ),
  52. true,
  53. sprintf(
  54. '<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>',
  55. PHP_EOL,
  56. PHP_EOL
  57. ),
  58. '
  59. @@ -12,51 +12,151 @@
  60. no change
  61. -/**\
  62. +/*\
  63. +A
  64. ',
  65. ' %s',
  66. ];
  67. yield [
  68. '[start]
  69. | '.'
  70. | @@ -12,51 +12,151 @@
  71. | no change
  72. | '.'
  73. | -/**\
  74. | +/*\
  75. | +A
  76. | '.'
  77. [end]',
  78. false,
  79. sprintf('[start]%s%%s%s[end]', PHP_EOL, PHP_EOL),
  80. '
  81. @@ -12,51 +12,151 @@
  82. no change
  83. '.'
  84. -/**\
  85. +/*\
  86. +A
  87. ',
  88. '| %s',
  89. ];
  90. yield [
  91. mb_convert_encoding("<fg=red>--- Original</fg=red>\n<fg=green>+ausgefüllt</fg=green>", 'ISO-8859-1'),
  92. true,
  93. '%s',
  94. mb_convert_encoding("--- Original\n+ausgefüllt", 'ISO-8859-1'),
  95. '%s',
  96. ];
  97. yield [
  98. 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'),
  99. true,
  100. '%s',
  101. mb_convert_encoding("--- Original\n+++ New\n@@ @@\n-ausgefüllt", 'ISO-8859-1'),
  102. '%s',
  103. ];
  104. yield [
  105. mb_convert_encoding("--- Original\n+++ New\n@@ @@\n-ausgefüllt", 'ISO-8859-1'),
  106. false,
  107. '%s',
  108. mb_convert_encoding("--- Original\n+++ New\n@@ @@\n-ausgefüllt", 'ISO-8859-1'),
  109. '%s',
  110. ];
  111. }
  112. }