StringLineEndingFixerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. final class StringLineEndingFixerTest 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. $heredocTemplate = "<?php\n\$a=\n<<<EOT\n%s\n\nEOT;\n";
  34. $nowdocTemplate = "<?php\n\$a=\n<<<'EOT'\n%s\n\nEOT;\n";
  35. $input = '/**
  36. * @SWG\Get(
  37. * path="/api/v0/cards",
  38. * operationId="listCards",
  39. * tags={"Банковские карты"},
  40. * summary="Возвращает список банковских карт."
  41. * )
  42. */';
  43. yield [
  44. "<?php \$a = 'my\nmulti\nline\nstring';\r\n",
  45. "<?php \$a = 'my\r\nmulti\nline\r\nstring';\r\n",
  46. ];
  47. yield [
  48. "<?php \$a = \"my\nmulti\nline\nstring\";\r\n",
  49. "<?php \$a = \"my\r\nmulti\nline\r\nstring\";\r\n",
  50. ];
  51. yield [
  52. "<?php \$a = \"my\nmulti\nline\nstring\nwith\n\$b\ninterpolation\";\r\n",
  53. "<?php \$a = \"my\r\nmulti\nline\r\nstring\nwith\r\n\$b\ninterpolation\";\r\n",
  54. ];
  55. yield [
  56. sprintf($heredocTemplate, $input),
  57. sprintf($heredocTemplate, str_replace("\n", "\r", $input)),
  58. ];
  59. yield [
  60. sprintf($heredocTemplate, $input),
  61. sprintf($heredocTemplate, str_replace("\n", "\r\n", $input)),
  62. ];
  63. yield [
  64. sprintf($nowdocTemplate, $input),
  65. sprintf($nowdocTemplate, str_replace("\n", "\r", $input)),
  66. ];
  67. yield [
  68. sprintf($nowdocTemplate, $input),
  69. sprintf($nowdocTemplate, str_replace("\n", "\r\n", $input)),
  70. ];
  71. yield [
  72. sprintf(str_replace('<<<', 'b<<<', $nowdocTemplate), $input),
  73. sprintf(str_replace('<<<', 'b<<<', $nowdocTemplate), str_replace("\n", "\r\n", $input)),
  74. ];
  75. yield [
  76. sprintf(str_replace('<<<', 'B<<<', $nowdocTemplate), $input),
  77. sprintf(str_replace('<<<', 'B<<<', $nowdocTemplate), str_replace("\n", "\r\n", $input)),
  78. ];
  79. yield [
  80. sprintf(str_replace('<<<', 'b<<<', $heredocTemplate), $input),
  81. sprintf(str_replace('<<<', 'b<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
  82. ];
  83. yield [
  84. sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), $input),
  85. sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
  86. ];
  87. yield 'not T_CLOSE_TAG, do T_INLINE_HTML' => [
  88. "<?php foo(); ?>\r\nA\n\n",
  89. "<?php foo(); ?>\r\nA\r\n\r\n",
  90. ];
  91. yield [
  92. "<?php \$a = b'my\nmulti\nline\nstring';\r\n",
  93. "<?php \$a = b'my\r\nmulti\nline\r\nstring';\r\n",
  94. ];
  95. }
  96. public function testWithDifferentLineEndingConfiguration(): void
  97. {
  98. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  99. $this->doTest(
  100. "<?php \$a = 'my\r\nmulti\r\nline\r\nstring';",
  101. "<?php \$a = 'my\nmulti\nline\nstring';"
  102. );
  103. }
  104. }