LineEndingFixerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Whitespace;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. * @author SpacePossum
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\LineEndingFixer
  21. */
  22. final class LineEndingFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param string $expected
  26. * @param null|string $input
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix($expected, $input = null)
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. public function provideFixCases()
  35. {
  36. $cases = $this->provideCommonCases();
  37. $cases[] = [
  38. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  39. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\n", // both cases
  40. ];
  41. $cases[] = [
  42. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  43. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \n |\r\nTEST;\r\n", // both cases
  44. ];
  45. // !T_INLINE_HTML
  46. $cases[] = [
  47. "<?php ?>\r\n<?php ?>\r\n",
  48. ];
  49. // !T_CONSTANT_ENCAPSED_STRING
  50. $cases[] = [
  51. "<?php \$a=\"a\r\n\";",
  52. ];
  53. return $cases;
  54. }
  55. /**
  56. * @param string $expected
  57. * @param null|string $input
  58. *
  59. * @dataProvider provideMessyWhitespacesCases
  60. */
  61. public function testMessyWhitespaces($expected, $input = null)
  62. {
  63. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  64. $this->doTest($expected, $input);
  65. }
  66. public function provideMessyWhitespacesCases()
  67. {
  68. $cases = array_map(static function (array $case) {
  69. return array_reverse($case);
  70. }, $this->provideCommonCases());
  71. $cases[] = [
  72. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  73. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\r\n", // both types
  74. ];
  75. $cases[] = [
  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 \r\n |\nTEST;\n", // both types
  78. ];
  79. return $cases;
  80. }
  81. private function provideCommonCases()
  82. {
  83. return [
  84. // T_OPEN_TAG
  85. [
  86. "<?php\n \$a = 1;",
  87. "<?php\r\n \$a = 1;",
  88. ],
  89. // T_WHITESPACE
  90. [
  91. "<?php \n \$a\n= 1;\n",
  92. "<?php \r\n \$a\r\n= 1;\r\n",
  93. ],
  94. // T_COMMENT
  95. [
  96. "<?php /*\n*/",
  97. "<?php /*\r\n*/",
  98. ],
  99. // T_DOC_COMMENT
  100. [
  101. "<?php /**\n*/",
  102. "<?php /**\r\n*/",
  103. ],
  104. // T_START_HEREDOC
  105. [
  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. [
  115. "<?php \$a = <<<'TEST'\nAAAA 1\n \$b\nTEST;\n",
  116. "<?php \$a = <<<'TEST'\r\nAAAA 1\r\n \$b\r\nTEST;\r\n",
  117. ],
  118. [
  119. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\nAAAAA \n |\nTEST;\n",
  120. "<?php \$b = \" \$a \r\n 123\"; \$a = <<<TEST\r\nAAAAA \r\n |\r\nTEST;\r\n",
  121. ],
  122. ];
  123. }
  124. }