SingleQuoteFixerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /**
  15. * @author Gregor Harlan <gharlan@web.de>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer
  24. */
  25. final class SingleQuoteFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @param _AutogeneratedInputConfiguration $configuration
  29. *
  30. * @dataProvider provideFixCases
  31. */
  32. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  33. {
  34. $this->fixer->configure($configuration);
  35. $this->doTest($expected, $input);
  36. }
  37. public static function provideFixCases(): iterable
  38. {
  39. yield [
  40. '<?php $a = \'\';',
  41. '<?php $a = "";',
  42. ];
  43. yield [
  44. '<?php $a = \'foo bar\';',
  45. '<?php $a = "foo bar";',
  46. ];
  47. yield [
  48. '<?php $a = b\'\';',
  49. '<?php $a = b"";',
  50. ];
  51. yield [
  52. '<?php $a = B\'\';',
  53. '<?php $a = B"";',
  54. ];
  55. yield [
  56. '<?php $a = b\'foo bar\';',
  57. '<?php $a = b"foo bar";',
  58. ];
  59. yield [
  60. '<?php $a = B\'foo bar\';',
  61. '<?php $a = B"foo bar";',
  62. ];
  63. yield [
  64. '<?php $a = \'foo
  65. bar\';',
  66. '<?php $a = "foo
  67. bar";',
  68. ];
  69. yield [
  70. '<?php $a = \'foo\'.\'bar\'."$baz";',
  71. '<?php $a = \'foo\'."bar"."$baz";',
  72. ];
  73. yield [
  74. '<?php $a = \'foo "bar"\';',
  75. '<?php $a = "foo \"bar\"";',
  76. ];
  77. yield [
  78. <<<'EOF'
  79. <?php $a = '\\foo\\bar\\\\';
  80. EOF,
  81. <<<'EOF'
  82. <?php $a = "\\foo\\bar\\\\";
  83. EOF,
  84. ];
  85. yield [
  86. '<?php $a = \'foo $bar7\';',
  87. '<?php $a = "foo \$bar7";',
  88. ];
  89. yield [
  90. '<?php $a = \'foo $(bar7)\';',
  91. '<?php $a = "foo \$(bar7)";',
  92. ];
  93. yield [
  94. '<?php $a = \'foo \\\($bar8)\';',
  95. '<?php $a = "foo \\\(\$bar8)";',
  96. ];
  97. yield ['<?php $a = "foo \" \$$bar";'];
  98. yield ['<?php $a = b"foo \" \$$bar";'];
  99. yield ['<?php $a = B"foo \" \$$bar";'];
  100. yield ['<?php $a = "foo \'bar\'";'];
  101. yield ['<?php $a = b"foo \'bar\'";'];
  102. yield ['<?php $a = B"foo \'bar\'";'];
  103. yield ['<?php $a = "foo $bar";'];
  104. yield ['<?php $a = b"foo $bar";'];
  105. yield ['<?php $a = B"foo $bar";'];
  106. yield ['<?php $a = "foo ${bar}";'];
  107. yield ['<?php $a = b"foo ${bar}";'];
  108. yield ['<?php $a = B"foo ${bar}";'];
  109. yield ['<?php $a = "foo\n bar";'];
  110. yield ['<?php $a = b"foo\n bar";'];
  111. yield ['<?php $a = B"foo\n bar";'];
  112. yield [
  113. <<<'EOF'
  114. <?php $a = "\\\n";
  115. EOF,
  116. ];
  117. yield [
  118. '<?php $a = \'foo \\\'bar\\\'\';',
  119. '<?php $a = "foo \'bar\'";',
  120. ['strings_containing_single_quote_chars' => true],
  121. ];
  122. yield [
  123. <<<'EOT'
  124. <?php
  125. // none
  126. $a = 'start \' end';
  127. // one escaped backslash
  128. $b = 'start \\\' end';
  129. // two escaped backslash
  130. $c = 'start \\\\\' end';
  131. EOT,
  132. <<<'EOT'
  133. <?php
  134. // none
  135. $a = "start ' end";
  136. // one escaped backslash
  137. $b = "start \\' end";
  138. // two escaped backslash
  139. $c = "start \\\\' end";
  140. EOT,
  141. ['strings_containing_single_quote_chars' => true],
  142. ];
  143. yield [
  144. <<<'EOT'
  145. <?php
  146. // one unescaped backslash
  147. $a = "start \' end";
  148. // one escaped + one unescaped backslash
  149. $b = "start \\\' end";
  150. EOT,
  151. null,
  152. ['strings_containing_single_quote_chars' => true],
  153. ];
  154. }
  155. }