HeredocIndentationFixerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Gregor Harlan
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\HeredocIndentationFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\HeredocIndentationFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\HeredocIndentationFixer
  25. */
  26. final class HeredocIndentationFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @param _AutogeneratedInputConfiguration $config
  30. *
  31. * @dataProvider provideFixCases
  32. */
  33. public function testFix(string $expected, ?string $input = null, array $config = []): void
  34. {
  35. $this->fixer->configure($config);
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixCases(): iterable
  39. {
  40. yield [
  41. <<<'EXPECTED'
  42. <?php
  43. foo(<<<EOD
  44. EOD
  45. );
  46. EXPECTED,
  47. <<<'INPUT'
  48. <?php
  49. foo(<<<EOD
  50. EOD
  51. );
  52. INPUT,
  53. ];
  54. yield [
  55. <<<'EXPECTED'
  56. <?php
  57. foo(<<<EOD
  58. EOD
  59. );
  60. EXPECTED,
  61. <<<'INPUT'
  62. <?php
  63. foo(<<<EOD
  64. EOD
  65. );
  66. INPUT,
  67. ];
  68. yield [
  69. <<<'EXPECTED'
  70. <?php
  71. foo(<<<EOD
  72. abc
  73. def
  74. EOD
  75. );
  76. EXPECTED,
  77. <<<'INPUT'
  78. <?php
  79. foo(<<<EOD
  80. abc
  81. def
  82. EOD
  83. );
  84. INPUT,
  85. ];
  86. yield [
  87. <<<'EXPECTED'
  88. <?php
  89. foo(<<<'EOD'
  90. abc
  91. def
  92. EOD
  93. );
  94. EXPECTED,
  95. <<<'INPUT'
  96. <?php
  97. foo(<<<'EOD'
  98. abc
  99. def
  100. EOD
  101. );
  102. INPUT,
  103. ];
  104. yield [
  105. <<<'EXPECTED'
  106. <?php
  107. foo(<<<'EOD'
  108. abc
  109. def
  110. EOD
  111. );
  112. EXPECTED,
  113. <<<'INPUT'
  114. <?php
  115. foo(<<<'EOD'
  116. abc
  117. def
  118. EOD
  119. );
  120. INPUT,
  121. ];
  122. yield [
  123. <<<'EXPECTED'
  124. <?php
  125. foo(<<<EOD
  126. $abc
  127. $def
  128. {$ghi}
  129. EOD
  130. );
  131. EXPECTED,
  132. <<<'INPUT'
  133. <?php
  134. foo(<<<EOD
  135. $abc
  136. $def
  137. {$ghi}
  138. EOD
  139. );
  140. INPUT,
  141. ];
  142. yield [
  143. <<<'EXPECTED'
  144. <?php
  145. $a = <<<'EOD'
  146. <?php
  147. $b = <<<FOO
  148. abc
  149. FOO;
  150. EOD;
  151. EXPECTED,
  152. <<<'INPUT'
  153. <?php
  154. $a = <<<'EOD'
  155. <?php
  156. $b = <<<FOO
  157. abc
  158. FOO;
  159. EOD;
  160. INPUT,
  161. ];
  162. yield [
  163. /* EXPECTED */ '
  164. <?php
  165. foo(<<<EOD
  166. '.'
  167. abc
  168. '.'
  169. def
  170. '.'
  171. EOD
  172. );',
  173. /* INPUT */ '
  174. <?php
  175. foo(<<<EOD
  176. '.'
  177. abc
  178. '.'
  179. def
  180. '.'
  181. EOD
  182. );',
  183. ];
  184. yield [
  185. /* EXPECTED */ '
  186. <?php
  187. foo(<<<EOD
  188. abc
  189. def
  190. EOD
  191. );',
  192. /* INPUT */ '
  193. <?php
  194. foo(<<<EOD
  195. '.'
  196. abc
  197. '.'
  198. def
  199. '.'
  200. EOD
  201. );',
  202. ];
  203. yield [
  204. <<<'EXPECTED'
  205. <?php foo(<<<EOD
  206. EOD
  207. );
  208. EXPECTED,
  209. <<<'INPUT'
  210. <?php foo(<<<EOD
  211. EOD
  212. );
  213. INPUT,
  214. ];
  215. yield [
  216. <<<'EXPECTED'
  217. <?php
  218. foo(<<<EOD
  219. abc
  220. def
  221. EOD
  222. );
  223. EXPECTED,
  224. <<<'INPUT'
  225. <?php
  226. foo(<<<EOD
  227. abc
  228. def
  229. EOD
  230. );
  231. INPUT,
  232. ['indentation' => 'same_as_start'],
  233. ];
  234. yield [
  235. <<<'EXPECTED'
  236. <?php
  237. foo(<<<EOD
  238. abc
  239. def
  240. EOD
  241. );
  242. EXPECTED,
  243. <<<'INPUT'
  244. <?php
  245. foo(<<<EOD
  246. abc
  247. def
  248. EOD
  249. );
  250. INPUT,
  251. ['indentation' => 'same_as_start'],
  252. ];
  253. }
  254. public function testWithWhitespacesConfig(): void
  255. {
  256. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t"));
  257. $expected = <<<EXPECTED
  258. <?php
  259. \t\$a = <<<'EOD'
  260. \t\tabc
  261. \t\t def
  262. \t\t\tghi
  263. \t\tEOD;
  264. EXPECTED;
  265. $input = <<<INPUT
  266. <?php
  267. \t\$a = <<<'EOD'
  268. abc
  269. def
  270. \tghi
  271. EOD;
  272. INPUT;
  273. $this->doTest($expected, $input);
  274. }
  275. }