StringLineEndingFixerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\StringNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Ilija Tovilo <ilija.tovilo@me.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\StringNotation\StringLineEndingFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\StringLineEndingFixer>
  23. */
  24. final class StringLineEndingFixerTest 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{string, string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. $heredocTemplate = "<?php\n\$a=\n<<<EOT\n%s\n\nEOT;\n";
  39. $nowdocTemplate = "<?php\n\$a=\n<<<'EOT'\n%s\n\nEOT;\n";
  40. $input = '/**
  41. * @SWG\Get(
  42. * path="/api/v0/cards",
  43. * operationId="listCards",
  44. * tags={"Банковские карты"},
  45. * summary="Возвращает список банковских карт."
  46. * )
  47. */';
  48. yield [
  49. "<?php \$a = 'my\nmulti\nline\nstring';\r\n",
  50. "<?php \$a = 'my\r\nmulti\nline\r\nstring';\r\n",
  51. ];
  52. yield [
  53. "<?php \$a = \"my\nmulti\nline\nstring\";\r\n",
  54. "<?php \$a = \"my\r\nmulti\nline\r\nstring\";\r\n",
  55. ];
  56. yield [
  57. "<?php \$a = \"my\nmulti\nline\nstring\nwith\n\$b\ninterpolation\";\r\n",
  58. "<?php \$a = \"my\r\nmulti\nline\r\nstring\nwith\r\n\$b\ninterpolation\";\r\n",
  59. ];
  60. yield [
  61. \sprintf($heredocTemplate, $input),
  62. \sprintf($heredocTemplate, str_replace("\n", "\r", $input)),
  63. ];
  64. yield [
  65. \sprintf($heredocTemplate, $input),
  66. \sprintf($heredocTemplate, str_replace("\n", "\r\n", $input)),
  67. ];
  68. yield [
  69. \sprintf($nowdocTemplate, $input),
  70. \sprintf($nowdocTemplate, str_replace("\n", "\r", $input)),
  71. ];
  72. yield [
  73. \sprintf($nowdocTemplate, $input),
  74. \sprintf($nowdocTemplate, str_replace("\n", "\r\n", $input)),
  75. ];
  76. yield [
  77. \sprintf(str_replace('<<<', 'b<<<', $nowdocTemplate), $input),
  78. \sprintf(str_replace('<<<', 'b<<<', $nowdocTemplate), str_replace("\n", "\r\n", $input)),
  79. ];
  80. yield [
  81. \sprintf(str_replace('<<<', 'B<<<', $nowdocTemplate), $input),
  82. \sprintf(str_replace('<<<', 'B<<<', $nowdocTemplate), str_replace("\n", "\r\n", $input)),
  83. ];
  84. yield [
  85. \sprintf(str_replace('<<<', 'b<<<', $heredocTemplate), $input),
  86. \sprintf(str_replace('<<<', 'b<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
  87. ];
  88. yield [
  89. \sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), $input),
  90. \sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
  91. ];
  92. yield 'not T_CLOSE_TAG, do T_INLINE_HTML' => [
  93. "<?php foo(); ?>\r\nA\n\n",
  94. "<?php foo(); ?>\r\nA\r\n\r\n",
  95. ];
  96. yield [
  97. "<?php \$a = b'my\nmulti\nline\nstring';\r\n",
  98. "<?php \$a = b'my\r\nmulti\nline\r\nstring';\r\n",
  99. ];
  100. }
  101. public function testWithWhitespacesConfig(): void
  102. {
  103. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  104. $this->doTest(
  105. "<?php \$a = 'my\r\nmulti\r\nline\r\nstring';",
  106. "<?php \$a = 'my\nmulti\nline\nstring';"
  107. );
  108. }
  109. }