MultilineStringToHeredocFixerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\StringNotation\MultilineStringToHeredocFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\MultilineStringToHeredocFixer>
  20. */
  21. final class MultilineStringToHeredocFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<array{0: string, 1?: null|string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'empty string' => [
  36. '<?php $a = \'\';',
  37. ];
  38. yield 'single line string' => [
  39. '<?php $a = \'a b\';',
  40. ];
  41. yield 'single line string with "\n"' => [
  42. '<?php $a = \'a\nb\';',
  43. ];
  44. yield 'simple single quoted' => [
  45. <<<'EOF'
  46. <?php
  47. $a = <<<'EOD'
  48. line1
  49. line2
  50. EOD;
  51. EOF,
  52. <<<'EOD'
  53. <?php
  54. $a = 'line1
  55. line2';
  56. EOD,
  57. ];
  58. yield 'simple double quoted' => [
  59. <<<'EOF'
  60. <?php
  61. $a = <<<EOD
  62. line1
  63. line2
  64. EOD;
  65. EOF,
  66. <<<'EOD'
  67. <?php
  68. $a = "line1
  69. line2";
  70. EOD,
  71. ];
  72. yield 'colliding closing marker - one' => [
  73. <<<'EOF'
  74. <?php
  75. $a = <<<'EOD_'
  76. line1
  77. EOD
  78. line2
  79. EOD_;
  80. EOF,
  81. <<<'EOF'
  82. <?php
  83. $a = 'line1
  84. EOD
  85. line2';
  86. EOF,
  87. ];
  88. yield 'colliding closing marker - two' => [
  89. <<<'EOF'
  90. <?php
  91. $a = <<<'EOD__'
  92. line1
  93. EOD
  94. EOD_
  95. line2
  96. EOD__;
  97. EOF,
  98. <<<'EOF'
  99. <?php
  100. $a = 'line1
  101. EOD
  102. EOD_
  103. line2';
  104. EOF,
  105. ];
  106. yield 'single quoted unescape' => [
  107. <<<'EOF'
  108. <?php
  109. $a = <<<'EOD'
  110. line1
  111. \
  112. \n
  113. '
  114. \\'
  115. \"
  116. \
  117. EOD;
  118. EOF,
  119. <<<'EOD'
  120. <?php
  121. $a = 'line1
  122. \\
  123. \n
  124. \'
  125. \\\\\'
  126. \"
  127. \
  128. ';
  129. EOD,
  130. ];
  131. yield 'double quoted unescape' => [
  132. <<<'EOF'
  133. <?php
  134. $a = <<<EOD
  135. line1
  136. \\
  137. \n
  138. "
  139. \\\\"
  140. \'
  141. \
  142. "{$rawPath}"
  143. EOD;
  144. EOF,
  145. <<<'EOD'
  146. <?php
  147. $a = "line1
  148. \\
  149. \n
  150. \"
  151. \\\\\"
  152. \'
  153. \
  154. \"{$rawPath}\"
  155. ";
  156. EOD,
  157. ];
  158. yield 'single quoted /w variable' => [
  159. <<<'EOF'
  160. <?php
  161. $a = <<<'EOD'
  162. line1$var
  163. line2
  164. EOD;
  165. EOF,
  166. <<<'EOD'
  167. <?php
  168. $a = 'line1$var
  169. line2';
  170. EOD,
  171. ];
  172. yield 'double quoted /w simple variable' => [
  173. <<<'EOF'
  174. <?php
  175. $a = <<<EOD
  176. line1$var
  177. line2
  178. EOD;
  179. EOF,
  180. <<<'EOD'
  181. <?php
  182. $a = "line1$var
  183. line2";
  184. EOD,
  185. ];
  186. yield 'double quoted /w simple curly variable' => [
  187. <<<'EOF'
  188. <?php
  189. $a = <<<EOD
  190. line1{$var}
  191. line2
  192. EOD;
  193. EOF,
  194. <<<'EOD'
  195. <?php
  196. $a = "line1{$var}
  197. line2";
  198. EOD,
  199. ];
  200. yield 'double quoted /w complex curly variable' => [
  201. <<<'EOF'
  202. <?php
  203. $a = <<<EOD
  204. {$arr['foo'][3]}
  205. { $obj->values[3]->name }
  206. {${getName()}}
  207. EOD;
  208. EOF,
  209. <<<'EOD'
  210. <?php
  211. $a = "{$arr['foo'][3]}
  212. { $obj->values[3]->name }
  213. {${getName()}}";
  214. EOD,
  215. ];
  216. yield 'test stateful fixing loop' => [
  217. <<<'EOF'
  218. <?php
  219. <<<EOD
  220. $a
  221. {$b['x']}
  222. EOD;
  223. <<<'EOD'
  224. c
  225. d
  226. EOD;
  227. <<<EOD
  228. $a
  229. $b
  230. EOD;
  231. <<<EOD
  232. $c
  233. $d
  234. EOD;
  235. 'a';
  236. <<<'EOD'
  237. b
  238. c
  239. EOD;
  240. <<<'EOD'
  241. EOD;
  242. <<<EOD
  243. $a $b
  244. EOD;
  245. <<<'EOD'
  246. c d
  247. EOD;
  248. <<<EOD
  249. $a $b
  250. EOD;
  251. <<<EOD
  252. $a
  253. $b
  254. EOD;
  255. <<<'EOD'
  256. $c
  257. $d
  258. EOD;
  259. EOF,
  260. <<<'EOF'
  261. <?php
  262. "$a
  263. {$b['x']}";
  264. 'c
  265. d';
  266. "$a
  267. $b";
  268. "$c
  269. $d";
  270. 'a';
  271. 'b
  272. c';
  273. <<<'EOD'
  274. EOD;
  275. <<<EOD
  276. $a $b
  277. EOD;
  278. <<<'EOD'
  279. c d
  280. EOD;
  281. <<<EOD
  282. $a $b
  283. EOD;
  284. <<<EOD
  285. $a
  286. $b
  287. EOD;
  288. <<<'EOD'
  289. $c
  290. $d
  291. EOD;
  292. EOF,
  293. ];
  294. yield 'simple strings prefixed with b/B' => [
  295. <<<'EOF'
  296. <?php
  297. $a = <<<'EOD'
  298. line1
  299. line2
  300. EOD;
  301. $b = <<<EOD
  302. line1
  303. line2
  304. EOD;
  305. EOF,
  306. <<<'EOD'
  307. <?php
  308. $a = b'line1
  309. line2';
  310. $b = B"line1
  311. line2";
  312. EOD,
  313. ];
  314. yield 'double quoted /w simple variable prefixed with b/B' => [
  315. <<<'EOF'
  316. <?php
  317. $a = <<<EOD
  318. line1$var
  319. line2
  320. EOD;
  321. $b = <<<EOD
  322. line1$var
  323. line2
  324. EOD;
  325. EOF,
  326. <<<'EOD'
  327. <?php
  328. $a = b"line1$var
  329. line2";
  330. $b = B"line1$var
  331. line2";
  332. EOD,
  333. ];
  334. }
  335. }