MultilineWhitespaceBeforeSemicolonsFixerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Semicolon;
  12. use PhpCsFixer\Fixer\Semicolon\MultilineWhitespaceBeforeSemicolonsFixer;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author John Kelly <wablam@gmail.com>
  17. * @author Graham Campbell <graham@alt-three.com>
  18. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  19. * @author Egidijus Girčys <e.gircys@gmail.com>
  20. *
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Fixer\Semicolon\MultilineWhitespaceBeforeSemicolonsFixer
  24. */
  25. final class MultilineWhitespaceBeforeSemicolonsFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @param string $expected
  29. * @param null|string $input
  30. *
  31. * @dataProvider provideMultiLineWhitespaceFixCases
  32. */
  33. public function testFixMultiLineWhitespace($expected, $input = null)
  34. {
  35. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NO_MULTI_LINE]);
  36. $this->doTest($expected, $input);
  37. }
  38. public function provideMultiLineWhitespaceFixCases()
  39. {
  40. return [
  41. [
  42. '<?php
  43. $foo->bar() // test
  44. ;',
  45. '<?php
  46. $foo->bar() // test
  47. ;',
  48. ],
  49. [
  50. "<?php echo(1) // test\n;",
  51. ],
  52. [
  53. '<?php
  54. $foo->bar() # test
  55. ;',
  56. '<?php
  57. $foo->bar() # test
  58. ;',
  59. ],
  60. [
  61. "<?php\n;",
  62. ],
  63. [
  64. '<?php
  65. $this
  66. ->setName(\'readme1\')
  67. ->setDescription(\'Generates the README\');
  68. ',
  69. '<?php
  70. $this
  71. ->setName(\'readme1\')
  72. ->setDescription(\'Generates the README\')
  73. ;
  74. ',
  75. ],
  76. [
  77. '<?php
  78. $this
  79. ->setName(\'readme2\')
  80. ->setDescription(\'Generates the README\');
  81. ',
  82. '<?php
  83. $this
  84. ->setName(\'readme2\')
  85. ->setDescription(\'Generates the README\')
  86. ;
  87. ',
  88. ],
  89. [
  90. '<?php echo "$this->foo(\'with param containing ;\') ;" ;',
  91. ],
  92. [
  93. '<?php $this->foo();',
  94. ],
  95. [
  96. '<?php $this->foo() ;',
  97. ],
  98. [
  99. '<?php $this->foo(\'with param containing ;\') ;',
  100. ],
  101. [
  102. '<?php $this->foo(\'with param containing ) ; \') ;',
  103. ],
  104. [
  105. '<?php $this->foo("with param containing ) ; ") ; ?>',
  106. ],
  107. [
  108. '<?php $this->foo("with semicolon in string) ; "); ?>',
  109. ],
  110. [
  111. '<?php
  112. $this
  113. ->example();',
  114. '<?php
  115. $this
  116. ->example()
  117. ;',
  118. ],
  119. ];
  120. }
  121. /**
  122. * @param string $expected
  123. * @param null|string $input
  124. *
  125. * @dataProvider provideMessyWhitespacesMultiLineWhitespaceFixCases
  126. */
  127. public function testMessyWhitespacesMultiLineWhitespace($expected, $input = null)
  128. {
  129. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  130. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NO_MULTI_LINE]);
  131. $this->doTest($expected, $input);
  132. }
  133. public function provideMessyWhitespacesMultiLineWhitespaceFixCases()
  134. {
  135. return [
  136. [
  137. "<?php echo(1) // test\r\n;",
  138. ],
  139. ];
  140. }
  141. /**
  142. * @param string $expected
  143. * @param null|string $input
  144. *
  145. * @dataProvider provideSemicolonForChainedCallsFixCases
  146. */
  147. public function testSemicolonForChainedCallsFix($expected, $input = null)
  148. {
  149. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS]);
  150. $this->doTest($expected, $input);
  151. }
  152. public function provideSemicolonForChainedCallsFixCases()
  153. {
  154. return [
  155. [
  156. '<?php
  157. $this
  158. ->method1()
  159. ->method2()
  160. ;
  161. ?>',
  162. '<?php
  163. $this
  164. ->method1()
  165. ->method2();
  166. ?>',
  167. ], [
  168. '<?php
  169. $this
  170. ->method1()
  171. ->method2() // comment
  172. ;
  173. ',
  174. '<?php
  175. $this
  176. ->method1()
  177. ->method2(); // comment
  178. ',
  179. ], [
  180. '<?php
  181. $service->method1()
  182. ->method2()
  183. ;
  184. $service->method3();
  185. $this
  186. ->method1()
  187. ->method2()
  188. ;',
  189. '<?php
  190. $service->method1()
  191. ->method2()
  192. ;
  193. $service->method3();
  194. $this
  195. ->method1()
  196. ->method2();',
  197. ], [
  198. '<?php
  199. $service
  200. ->method2()
  201. ;
  202. ?>',
  203. '<?php
  204. $service
  205. ->method2();
  206. ?>',
  207. ], [
  208. '<?php
  209. $service->method1()
  210. ->method2()
  211. ->method3()
  212. ->method4()
  213. ;
  214. ?>',
  215. '<?php
  216. $service->method1()
  217. ->method2()
  218. ->method3()
  219. ->method4();
  220. ?>',
  221. ], [
  222. '<?php
  223. $this->service->method1()
  224. ->method2([1, 2])
  225. ->method3(
  226. "2",
  227. 2,
  228. [1, 2]
  229. )
  230. ->method4()
  231. ;
  232. ?>',
  233. '<?php
  234. $this->service->method1()
  235. ->method2([1, 2])
  236. ->method3(
  237. "2",
  238. 2,
  239. [1, 2]
  240. )
  241. ->method4();
  242. ?>',
  243. ], [
  244. '<?php
  245. $service
  246. ->method1()
  247. ->method2()
  248. ->method3()
  249. ->method4()
  250. ;
  251. ?>',
  252. '<?php
  253. $service
  254. ->method1()
  255. ->method2()
  256. ->method3()
  257. ->method4();
  258. ?>',
  259. ], [
  260. '<?php
  261. $f = "g";
  262. $service
  263. ->method1("a", true)
  264. ->method2(true, false)
  265. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  266. ->method4(1, "a", $f)
  267. ;
  268. ?>',
  269. '<?php
  270. $f = "g";
  271. $service
  272. ->method1("a", true)
  273. ->method2(true, false)
  274. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  275. ->method4(1, "a", $f);
  276. ?>',
  277. ], [
  278. '<?php
  279. $f = "g";
  280. $service
  281. ->method1("a", true) // this is a comment
  282. /* ->method2(true, false) */
  283. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  284. ->method4(1, "a", $f) /* this is a comment */
  285. ;
  286. ?>',
  287. '<?php
  288. $f = "g";
  289. $service
  290. ->method1("a", true) // this is a comment
  291. /* ->method2(true, false) */
  292. ->method3([1, 2, 3], ["a" => "b", "c" => 1, "d" => true])
  293. ->method4(1, "a", $f); /* this is a comment */
  294. ?>',
  295. ], [
  296. '<?php
  297. $service->method1();
  298. $service->method2()->method3();
  299. ?>',
  300. ], [
  301. '<?php
  302. $service->method1() ;
  303. $service->method2()->method3() ;
  304. ?>',
  305. ], [
  306. '<?php
  307. $service
  308. ->method2(function ($a) {
  309. $a->otherCall()
  310. ->a()
  311. ->b()
  312. ;
  313. })
  314. ;
  315. ?>',
  316. '<?php
  317. $service
  318. ->method2(function ($a) {
  319. $a->otherCall()
  320. ->a()
  321. ->b()
  322. ;
  323. });
  324. ?>',
  325. ], [
  326. '<?php
  327. $data = $service
  328. ->method2(function ($a) {
  329. $a->otherCall()
  330. ->a()
  331. ->b(array_merge([
  332. 1 => 1,
  333. 2 => 2,
  334. ], $this->getOtherArray()
  335. ))
  336. ;
  337. })
  338. ;
  339. ?>',
  340. '<?php
  341. $data = $service
  342. ->method2(function ($a) {
  343. $a->otherCall()
  344. ->a()
  345. ->b(array_merge([
  346. 1 => 1,
  347. 2 => 2,
  348. ], $this->getOtherArray()
  349. ));
  350. });
  351. ?>',
  352. ], [
  353. '<?php
  354. $service
  355. ->method1(null, null, [
  356. null => null,
  357. 1 => $data->getId() > 0,
  358. ])
  359. ->method2(4, Type::class)
  360. ;
  361. ',
  362. '<?php
  363. $service
  364. ->method1(null, null, [
  365. null => null,
  366. 1 => $data->getId() > 0,
  367. ])
  368. ->method2(4, Type::class);
  369. ',
  370. ],
  371. [
  372. '<?php
  373. $this
  374. ->method1()
  375. ->method2()
  376. ;
  377. ?>',
  378. '<?php
  379. $this
  380. ->method1()
  381. ->method2();
  382. ?>',
  383. ],
  384. ];
  385. }
  386. /**
  387. * @param string $expected
  388. * @param null|string $input
  389. *
  390. * @dataProvider provideMessyWhitespacesSemicolonForChainedCallsFixCases
  391. */
  392. public function testMessyWhitespacesSemicolonForChainedCalls($expected, $input = null)
  393. {
  394. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  395. $this->fixer->configure(['strategy' => MultilineWhitespaceBeforeSemicolonsFixer::STRATEGY_NEW_LINE_FOR_CHAINED_CALLS]);
  396. $this->doTest($expected, $input);
  397. }
  398. public function provideMessyWhitespacesSemicolonForChainedCallsFixCases()
  399. {
  400. return [
  401. [
  402. "<?php\r\n\r\n \$this\r\n\t->method1()\r\n\t\t->method2()\r\n ;",
  403. "<?php\r\n\r\n \$this\r\n\t->method1()\r\n\t\t->method2();",
  404. ],
  405. [
  406. "<?php\r\n\r\n\t\$this->method1()\r\n\t\t->method2()\r\n\t\t->method(3)\r\n\t;",
  407. "<?php\r\n\r\n\t\$this->method1()\r\n\t\t->method2()\r\n\t\t->method(3);",
  408. ], [
  409. "<?php\r\n\r\n\t\$data = \$service\r\n\t ->method2(function (\$a) {\r\n\t\t\t\$a->otherCall()\r\n\t\t\t\t->a()\r\n\t\t\t\t->b(array_merge([\r\n\t\t\t\t\t\t1 => 1,\r\n\t\t\t\t\t\t2 => 2,\r\n\t\t\t\t\t], \$this->getOtherArray()\r\n\t\t\t\t))\r\n\t\t\t;\r\n\t\t})\r\n\t;\r\n?>",
  410. "<?php\r\n\r\n\t\$data = \$service\r\n\t ->method2(function (\$a) {\r\n\t\t\t\$a->otherCall()\r\n\t\t\t\t->a()\r\n\t\t\t\t->b(array_merge([\r\n\t\t\t\t\t\t1 => 1,\r\n\t\t\t\t\t\t2 => 2,\r\n\t\t\t\t\t], \$this->getOtherArray()\r\n\t\t\t\t));\r\n\t\t});\r\n?>",
  411. ],
  412. ];
  413. }
  414. }