LineEndingFixerTest.php 4.0 KB

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