SimpleToComplexStringVariableFixerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Dave van der Brugge <dmvdbrugge@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\StringNotation\SimpleToComplexStringVariableFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\SimpleToComplexStringVariableFixer>
  22. */
  23. final class SimpleToComplexStringVariableFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'basic fix' => [
  38. <<<'EXPECTED'
  39. <?php
  40. $name = "World";
  41. echo "Hello {$name}!";
  42. EXPECTED,
  43. <<<'INPUT'
  44. <?php
  45. $name = "World";
  46. echo "Hello ${name}!";
  47. INPUT,
  48. ];
  49. yield 'heredoc' => [
  50. <<<'EXPECTED'
  51. <?php
  52. $name = 'World';
  53. echo <<<TEST
  54. Hello {$name}!
  55. TEST;
  56. EXPECTED,
  57. <<<'INPUT'
  58. <?php
  59. $name = 'World';
  60. echo <<<TEST
  61. Hello ${name}!
  62. TEST;
  63. INPUT,
  64. ];
  65. yield 'implicit' => [
  66. <<<'EXPECTED'
  67. <?php
  68. $name = 'World';
  69. echo "Hello $name!";
  70. EXPECTED,
  71. ];
  72. yield 'implicit again' => [
  73. <<<'EXPECTED'
  74. <?php
  75. $name = 'World';
  76. echo "Hello { $name }!";
  77. EXPECTED,
  78. ];
  79. yield 'escaped' => [
  80. <<<'EXPECTED'
  81. <?php
  82. $name = 'World';
  83. echo "Hello \${name}";
  84. EXPECTED,
  85. ];
  86. yield 'double dollar' => [
  87. <<<'EXPECTED'
  88. <?php
  89. $name = 'World';
  90. echo "Hello \${$name}";
  91. EXPECTED,
  92. <<<'INPUT'
  93. <?php
  94. $name = 'World';
  95. echo "Hello $${name}";
  96. INPUT,
  97. ];
  98. yield 'double dollar heredoc' => [
  99. <<<'EXPECTED'
  100. <?php
  101. $name = 'World';
  102. echo <<<TEST
  103. Hello \${$name}!
  104. TEST;
  105. EXPECTED,
  106. <<<'INPUT'
  107. <?php
  108. $name = 'World';
  109. echo <<<TEST
  110. Hello $${name}!
  111. TEST;
  112. INPUT,
  113. ];
  114. yield 'double dollar single quote' => [
  115. <<<'EXPECTED'
  116. <?php
  117. $name = 'World';
  118. echo 'Hello $${name}';
  119. EXPECTED,
  120. ];
  121. yield 'array elements' => [
  122. <<<'PHP'
  123. <?php
  124. "Hello {$array[0]}!";
  125. "Hello {$array['index']}!";
  126. PHP,
  127. <<<'PHP'
  128. <?php
  129. "Hello ${array[0]}!";
  130. "Hello ${array['index']}!";
  131. PHP,
  132. ];
  133. }
  134. }