ControlStructureContinuationPositionFixerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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\ControlStructure;
  13. use PhpCsFixer\Preg;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer
  24. */
  25. final class ControlStructureContinuationPositionFixerTest 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 'else (same line, default)' => [
  40. '<?php
  41. if ($foo) {
  42. foo();
  43. } else {
  44. bar();
  45. }',
  46. '<?php
  47. if ($foo) {
  48. foo();
  49. }
  50. else {
  51. bar();
  52. }',
  53. ];
  54. yield 'elseif (same line, default)' => [
  55. '<?php
  56. if ($foo) {
  57. foo();
  58. } elseif ($bar) {
  59. bar();
  60. }',
  61. '<?php
  62. if ($foo) {
  63. foo();
  64. }
  65. elseif ($bar) {
  66. bar();
  67. }',
  68. ];
  69. yield 'else if (same line, default)' => [
  70. '<?php
  71. if ($foo) {
  72. foo();
  73. } else if ($bar) {
  74. bar();
  75. }',
  76. '<?php
  77. if ($foo) {
  78. foo();
  79. }
  80. else if ($bar) {
  81. bar();
  82. }',
  83. ];
  84. yield 'do while (same line, default)' => [
  85. '<?php
  86. do {
  87. foo();
  88. } while ($foo);',
  89. '<?php
  90. do {
  91. foo();
  92. }
  93. while ($foo);',
  94. ];
  95. yield 'try catch finally (same line, default)' => [
  96. '<?php
  97. try {
  98. foo();
  99. } catch (Throwable $e) {
  100. bar();
  101. } finally {
  102. baz();
  103. }',
  104. '<?php
  105. try {
  106. foo();
  107. }
  108. catch (Throwable $e) {
  109. bar();
  110. }
  111. finally {
  112. baz();
  113. }',
  114. ];
  115. yield 'else (next line)' => [
  116. '<?php
  117. if ($foo) {
  118. foo();
  119. }
  120. else {
  121. bar();
  122. }',
  123. '<?php
  124. if ($foo) {
  125. foo();
  126. } else {
  127. bar();
  128. }',
  129. ['position' => 'next_line'],
  130. ];
  131. yield 'elseif (next line)' => [
  132. '<?php
  133. if ($foo) {
  134. foo();
  135. }
  136. elseif ($bar) {
  137. bar();
  138. }',
  139. '<?php
  140. if ($foo) {
  141. foo();
  142. } elseif ($bar) {
  143. bar();
  144. }',
  145. ['position' => 'next_line'],
  146. ];
  147. yield 'else if (next line)' => [
  148. '<?php
  149. if ($foo) {
  150. foo();
  151. }
  152. else if ($bar) {
  153. bar();
  154. }',
  155. '<?php
  156. if ($foo) {
  157. foo();
  158. } else if ($bar) {
  159. bar();
  160. }',
  161. ['position' => 'next_line'],
  162. ];
  163. yield 'do while (next line)' => [
  164. '<?php
  165. do {
  166. foo();
  167. }
  168. while ($foo);',
  169. '<?php
  170. do {
  171. foo();
  172. } while ($foo);',
  173. ['position' => 'next_line'],
  174. ];
  175. yield 'try catch finally (next line)' => [
  176. '<?php
  177. try {
  178. foo();
  179. }
  180. catch (Throwable $e) {
  181. bar();
  182. }
  183. finally {
  184. baz();
  185. }',
  186. '<?php
  187. try {
  188. foo();
  189. } catch (Throwable $e) {
  190. bar();
  191. } finally {
  192. baz();
  193. }',
  194. ['position' => 'next_line'],
  195. ];
  196. yield 'else with comment after closing brace' => [
  197. '<?php
  198. if ($foo) {
  199. foo();
  200. } // comment
  201. else {
  202. bar();
  203. }',
  204. ];
  205. yield 'elseif with comment after closing brace' => [
  206. '<?php
  207. if ($foo) {
  208. foo();
  209. } // comment
  210. elseif ($bar) {
  211. bar();
  212. }',
  213. ];
  214. yield 'else if with comment after closing brace' => [
  215. '<?php
  216. if ($foo) {
  217. foo();
  218. } // comment
  219. else if ($bar) {
  220. bar();
  221. }',
  222. ];
  223. yield 'do while with comment after closing brace' => [
  224. '<?php
  225. do {
  226. foo();
  227. } // comment
  228. while (false);',
  229. ];
  230. yield 'try catch finally with comment after closing brace' => [
  231. '<?php
  232. try {
  233. foo();
  234. } // comment
  235. catch (Throwable $e) {
  236. bar();
  237. } // comment
  238. finally {
  239. baz();
  240. }',
  241. ];
  242. yield 'while not after do' => [
  243. '<?php
  244. if ($foo) {
  245. foo();
  246. }
  247. while ($bar) {
  248. bar();
  249. }',
  250. ];
  251. }
  252. /**
  253. * @param _AutogeneratedInputConfiguration $configuration
  254. *
  255. * @dataProvider provideWithWhitespacesConfigCases
  256. */
  257. public function testWithWhitespacesConfig(string $expected, ?string $input = null, ?array $configuration = null): void
  258. {
  259. if (null !== $configuration) {
  260. $this->fixer->configure($configuration);
  261. }
  262. $this->fixer->setWhitespacesConfig(
  263. new WhitespacesFixerConfig(' ', "\r\n")
  264. );
  265. $this->doTest($expected, $input);
  266. }
  267. public static function provideWithWhitespacesConfigCases(): iterable
  268. {
  269. foreach (self::provideFixCases() as $label => $case) {
  270. yield $label => [
  271. Preg::replace('/\n/', "\r\n", $case[0]),
  272. isset($case[1]) ? Preg::replace('/\n/', "\r\n", $case[1]) : null,
  273. $case[2] ?? null,
  274. ];
  275. }
  276. }
  277. }