LineEndingFixerTest.php 4.1 KB

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