HeredocClosingMarkerFixerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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\HeredocClosingMarkerFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\HeredocClosingMarkerFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\StringNotation\HeredocClosingMarkerFixer
  22. */
  23. final class HeredocClosingMarkerFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param _AutogeneratedInputConfiguration $config
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $config = []): void
  31. {
  32. $this->fixer->configure($config);
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<array{0: string, 1?: null|string, 2?: array<string, mixed>}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'heredoc' => [
  41. <<<'PHP'
  42. <?php $a = <<<EOD
  43. xxx EOD xxx
  44. EOD;
  45. PHP,
  46. <<<'PHP'
  47. <?php $a = <<<TEST
  48. xxx EOD xxx
  49. TEST;
  50. PHP,
  51. ];
  52. yield 'nowdoc' => [
  53. <<<'PHP'
  54. <?php $a = <<<'EOD'
  55. xxx EOD xxx
  56. EOD;
  57. PHP,
  58. <<<'PHP'
  59. <?php $a = <<<'TEST'
  60. xxx EOD xxx
  61. TEST;
  62. PHP,
  63. ];
  64. yield 'heredoc /w custom preferred closing marker' => [
  65. <<<'PHP'
  66. <?php $a = <<<EOF
  67. xxx
  68. EOF;
  69. PHP,
  70. <<<'PHP'
  71. <?php $a = <<<TEST
  72. xxx
  73. TEST;
  74. PHP,
  75. ['closing_marker' => 'EOF'],
  76. ];
  77. yield 'heredoc /w custom explicit style' => [
  78. <<<'PHP'
  79. <?php $a = <<<"EOD"
  80. xxx
  81. EOD;
  82. $b = <<<"EOD"
  83. xxx2
  84. EOD;
  85. $b = <<<'EOD'
  86. xxx3
  87. EOD;
  88. PHP,
  89. <<<'PHP'
  90. <?php $a = <<<TEST
  91. xxx
  92. TEST;
  93. $b = <<<"TEST"
  94. xxx2
  95. TEST;
  96. $b = <<<'TEST'
  97. xxx3
  98. TEST;
  99. PHP,
  100. ['explicit_heredoc_style' => true],
  101. ];
  102. yield 'heredoc /w b' => [
  103. <<<'PHP'
  104. <?php $a = b<<<EOD
  105. xxx EOD xxx
  106. EOD;
  107. PHP,
  108. <<<'PHP'
  109. <?php $a = b<<<TEST
  110. xxx EOD xxx
  111. TEST;
  112. PHP,
  113. ];
  114. yield 'heredoc /w B' => [
  115. <<<'PHP'
  116. <?php $a = B<<<EOD
  117. xxx EOD xxx
  118. EOD;
  119. PHP,
  120. <<<'PHP'
  121. <?php $a = B<<<TEST
  122. xxx EOD xxx
  123. TEST;
  124. PHP,
  125. ];
  126. yield 'heredoc and reserved closing marker' => [
  127. <<<'PHP_'
  128. <?php $a = <<<PHP
  129. xxx
  130. PHP;
  131. PHP_,
  132. ];
  133. yield 'heredoc and reserved closing marker - different case' => [
  134. <<<'PHP_'
  135. <?php $a = <<<PHP
  136. xxx
  137. PHP;
  138. $a = <<<PHP
  139. PHP;
  140. PHP_,
  141. <<<'PHP'
  142. <?php $a = <<<php
  143. xxx
  144. php;
  145. $a = <<<Php
  146. Php;
  147. PHP,
  148. ];
  149. yield 'heredoc and reserved custom closing marker' => [
  150. <<<'PHP'
  151. <?php $a = <<<Žlutý
  152. xxx
  153. Žlutý;
  154. $aNormCase = <<<Žlutý
  155. xxx
  156. Žlutý;
  157. $aNormCase = <<<Žlutý
  158. xxx
  159. Žlutý;
  160. $b = <<<EOD
  161. xxx2
  162. EOD;
  163. $c = <<<EOD
  164. xxx3
  165. EOD;
  166. PHP,
  167. <<<'PHP_'
  168. <?php $a = <<<Žlutý
  169. xxx
  170. Žlutý;
  171. $aNormCase = <<<ŽluTý
  172. xxx
  173. ŽluTý;
  174. $aNormCase = <<<ŽLUTÝ
  175. xxx
  176. ŽLUTÝ;
  177. $b = <<<Žlutý2
  178. xxx2
  179. Žlutý2;
  180. $c = <<<PHP
  181. xxx3
  182. PHP;
  183. PHP_,
  184. ['reserved_closing_markers' => ['Žlutý']],
  185. ];
  186. yield 'no longer colliding reserved marker recovery' => [
  187. <<<'PHP'
  188. <?php
  189. $a = <<<CSS
  190. CSS;
  191. $a = <<<CSS
  192. CSS;
  193. $a = <<<CSS_
  194. CSS
  195. CSS_;
  196. $a = <<<CSS
  197. CSS_
  198. CSS;
  199. PHP,
  200. <<<'PHP'
  201. <?php
  202. $a = <<<CSS_
  203. CSS_;
  204. $a = <<<CSS__
  205. CSS__;
  206. $a = <<<CSS__
  207. CSS
  208. CSS__;
  209. $a = <<<CSS__
  210. CSS_
  211. CSS__;
  212. PHP,
  213. ];
  214. yield 'heredoc /w content starting with preferred closing marker' => [
  215. <<<'PHP'
  216. <?php $a = <<<EOD_
  217. EOD xxx
  218. EOD_;
  219. PHP,
  220. <<<'PHP'
  221. <?php $a = <<<TEST
  222. EOD xxx
  223. TEST;
  224. PHP,
  225. ];
  226. yield 'heredoc /w content starting with whitespace and preferred closing marker' => [
  227. <<<'PHP'
  228. <?php $a = <<<EOD_
  229. EOD xxx
  230. EOD_;
  231. PHP,
  232. <<<'PHP'
  233. <?php $a = <<<TEST
  234. EOD xxx
  235. TEST;
  236. PHP,
  237. ];
  238. yield 'heredoc /w content starting with preferred closing marker and single quote' => [
  239. <<<'PHP'
  240. <?php $a = <<<EOD_
  241. EOD'
  242. EOD_;
  243. PHP,
  244. <<<'PHP'
  245. <?php $a = <<<TEST
  246. EOD'
  247. TEST;
  248. PHP,
  249. ];
  250. yield 'heredoc /w content starting with preferred closing marker and semicolon' => [
  251. <<<'PHP'
  252. <?php $a = <<<EOD_
  253. EOD;
  254. EOD_;
  255. PHP,
  256. <<<'PHP'
  257. <?php $a = <<<TEST
  258. EOD;
  259. TEST;
  260. PHP,
  261. ];
  262. yield 'heredoc /w content ending with preferred closing marker' => [
  263. <<<'PHP'
  264. <?php $a = <<<EOD
  265. xxx EOD
  266. EOD;
  267. PHP,
  268. <<<'PHP'
  269. <?php $a = <<<TEST
  270. xxx EOD
  271. TEST;
  272. PHP,
  273. ];
  274. }
  275. }