LineEndingFixerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public static function provideFixCases(): iterable
  34. {
  35. yield from self::provideCommonCases();
  36. yield [
  37. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  38. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\n", // both cases
  39. ];
  40. yield [
  41. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  42. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \n |\r\nTEST;\r\n", // both cases
  43. ];
  44. yield 'T_INLINE_HTML' => [
  45. "<?php ?>\nZ\r\n<?php ?>\nZ\r\n",
  46. ];
  47. yield '!T_CONSTANT_ENCAPSED_STRING' => [
  48. "<?php \$a=\"a\r\n\";",
  49. ];
  50. yield [
  51. "<?php echo 'foo',\n\n'bar';",
  52. "<?php echo 'foo',\r\r\n'bar';",
  53. ];
  54. yield 'T_CLOSE_TAG' => [
  55. "<?php\n?>\n<?php\n",
  56. "<?php\n?>\r\n<?php\n",
  57. ];
  58. yield 'T_CLOSE_TAG II' => [
  59. "<?php\n?>\n<?php\n?>\n<?php\n",
  60. "<?php\n?>\r\n<?php\n?>\r\n<?php\n",
  61. ];
  62. }
  63. /**
  64. * @dataProvider provideWithWhitespacesConfigCases
  65. */
  66. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  67. {
  68. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  69. $this->doTest($expected, $input);
  70. }
  71. public static function provideWithWhitespacesConfigCases(): iterable
  72. {
  73. yield from array_map(static fn (array $case): array => array_reverse($case), self::provideCommonCases());
  74. yield [
  75. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  76. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\r\n", // both types
  77. ];
  78. yield [
  79. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  80. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \r\n |\nTEST;\n", // both types
  81. ];
  82. }
  83. /**
  84. * @return array<int|string, array{string, string}>
  85. */
  86. private static function provideCommonCases(): array
  87. {
  88. return [
  89. 'T_OPEN_TAG' => [
  90. "<?php\n \$a = 1;",
  91. "<?php\r\n \$a = 1;",
  92. ],
  93. 'T_WHITESPACE' => [
  94. "<?php \n \$a\n= 1;\n",
  95. "<?php \r\n \$a\r\n= 1;\r\n",
  96. ],
  97. 'T_COMMENT' => [
  98. "<?php /*\n*/",
  99. "<?php /*\r\n*/",
  100. ],
  101. 'T_DOC_COMMENT' => [
  102. "<?php /**\n*/",
  103. "<?php /**\r\n*/",
  104. ],
  105. 'T_START_HEREDOC' => [
  106. "<?php \$a = <<<'TEST'\nAA\nTEST;\n",
  107. "<?php \$a = <<<'TEST'\r\nAA\r\nTEST;\r\n",
  108. ],
  109. [
  110. "<?php \$a = <<<TEST\nAAA\nTEST;\n",
  111. "<?php \$a = <<<TEST\r\nAAA\r\nTEST;\r\n",
  112. ],
  113. 'T_ENCAPSED_AND_WHITESPACE' => [
  114. "<?php \$a = <<<'TEST'\nAAAA 1\n \$b\nTEST;\n",
  115. "<?php \$a = <<<'TEST'\r\nAAAA 1\r\n \$b\r\nTEST;\r\n",
  116. ],
  117. [
  118. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  119. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  120. ],
  121. ];
  122. }
  123. }