ControlStructureContinuationPositionFixerTest.php 7.4 KB

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