LineEndingFixerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Fixer\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\LineEndingFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\LineEndingFixer>
  23. */
  24. final class LineEndingFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<int|string, array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield from self::provideCommonCases();
  39. yield [
  40. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  41. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\n", // both cases
  42. ];
  43. yield [
  44. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  45. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \n |\r\nTEST;\r\n", // both cases
  46. ];
  47. yield 'T_INLINE_HTML' => [
  48. "<?php ?>\nZ\r\n<?php ?>\nZ\r\n",
  49. ];
  50. yield '!T_CONSTANT_ENCAPSED_STRING' => [
  51. "<?php \$a=\"a\r\n\";",
  52. ];
  53. yield [
  54. "<?php echo 'foo',\n\n'bar';",
  55. "<?php echo 'foo',\r\r\n'bar';",
  56. ];
  57. yield 'T_CLOSE_TAG' => [
  58. "<?php\n?>\n<?php\n",
  59. "<?php\n?>\r\n<?php\n",
  60. ];
  61. yield 'T_CLOSE_TAG II' => [
  62. "<?php\n?>\n<?php\n?>\n<?php\n",
  63. "<?php\n?>\r\n<?php\n?>\r\n<?php\n",
  64. ];
  65. }
  66. /**
  67. * @dataProvider provideWithWhitespacesConfigCases
  68. */
  69. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  70. {
  71. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  72. $this->doTest($expected, $input);
  73. }
  74. /**
  75. * @return iterable<int|string, array{string, string}>
  76. */
  77. public static function provideWithWhitespacesConfigCases(): iterable
  78. {
  79. yield from array_map(static fn (array $case): array => array_reverse($case), self::provideCommonCases());
  80. yield [
  81. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  82. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\r\n", // both types
  83. ];
  84. yield [
  85. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  86. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \r\n |\nTEST;\n", // both types
  87. ];
  88. }
  89. /**
  90. * @return array<int|string, array{string, string}>
  91. */
  92. private static function provideCommonCases(): array
  93. {
  94. return [
  95. 'T_OPEN_TAG' => [
  96. "<?php\n \$a = 1;",
  97. "<?php\r\n \$a = 1;",
  98. ],
  99. 'T_WHITESPACE' => [
  100. "<?php \n \$a\n= 1;\n",
  101. "<?php \r\n \$a\r\n= 1;\r\n",
  102. ],
  103. 'T_COMMENT' => [
  104. "<?php /*\n*/",
  105. "<?php /*\r\n*/",
  106. ],
  107. 'T_DOC_COMMENT' => [
  108. "<?php /**\n*/",
  109. "<?php /**\r\n*/",
  110. ],
  111. 'T_START_HEREDOC' => [
  112. "<?php \$a = <<<'TEST'\nAA\nTEST;\n",
  113. "<?php \$a = <<<'TEST'\r\nAA\r\nTEST;\r\n",
  114. ],
  115. [
  116. "<?php \$a = <<<TEST\nAAA\nTEST;\n",
  117. "<?php \$a = <<<TEST\r\nAAA\r\nTEST;\r\n",
  118. ],
  119. 'T_ENCAPSED_AND_WHITESPACE' => [
  120. "<?php \$a = <<<'TEST'\nAAAA 1\n \$b\nTEST;\n",
  121. "<?php \$a = <<<'TEST'\r\nAAAA 1\r\n \$b\r\nTEST;\r\n",
  122. ],
  123. [
  124. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  125. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  126. ],
  127. ];
  128. }
  129. }