DiffConsoleFormatterTest.php 3.3 KB

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