SpacesInsideParenthesesFixerTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Marc Aubé
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\SpacesInsideParenthesesFixer
  21. */
  22. final class SpacesInsideParenthesesFixerTest extends AbstractFixerTestCase
  23. {
  24. public function testInvalidConfigMissingKey(): void
  25. {
  26. $this->expectException(InvalidFixerConfigurationException::class);
  27. $this->expectExceptionMessageMatches('#^\[spaces_inside_parentheses\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#');
  28. $this->fixer->configure(['a' => 1]);
  29. }
  30. public function testInvalidConfigValue(): void
  31. {
  32. $this->expectException(InvalidFixerConfigurationException::class);
  33. $this->expectExceptionMessageMatches('#^\[spaces_inside_parentheses\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#');
  34. $this->fixer->configure(['space' => 'double']);
  35. }
  36. /**
  37. * @dataProvider provideDefaultFixCases
  38. */
  39. public function testDefaultFix(string $expected, ?string $input = null): void
  40. {
  41. $this->doTest($expected, $input);
  42. }
  43. /**
  44. * @dataProvider provideSpacesFixCases
  45. */
  46. public function testSpacesFix(string $expected, ?string $input = null): void
  47. {
  48. $this->fixer->configure(['space' => 'single']);
  49. $this->doTest($expected, $input);
  50. }
  51. /**
  52. * @return array<array{0: string, 1?: string}>
  53. */
  54. public static function provideDefaultFixCases(): iterable
  55. {
  56. // default leaves new lines alone
  57. yield [
  58. "<?php
  59. class Foo
  60. {
  61. private function bar()
  62. {
  63. if (foo(
  64. 'foo' ,
  65. 'bar' ,
  66. [1, 2, 3],
  67. 'baz' // a comment just to mix things up
  68. )) {
  69. return 1;
  70. };
  71. }
  72. }
  73. ",
  74. ];
  75. yield [
  76. '<?php foo();',
  77. '<?php foo( );',
  78. ];
  79. yield [
  80. '<?php
  81. if (true) {
  82. // if body
  83. }',
  84. '<?php
  85. if ( true ) {
  86. // if body
  87. }',
  88. ];
  89. yield [
  90. '<?php
  91. if (true) {
  92. // if body
  93. }',
  94. '<?php
  95. if ( true ) {
  96. // if body
  97. }',
  98. ];
  99. yield [
  100. '<?php
  101. function foo($bar, $baz)
  102. {
  103. // function body
  104. }',
  105. '<?php
  106. function foo( $bar, $baz )
  107. {
  108. // function body
  109. }',
  110. ];
  111. yield [
  112. '<?php
  113. $foo->bar($arg1, $arg2);',
  114. '<?php
  115. $foo->bar( $arg1, $arg2 );',
  116. ];
  117. yield [
  118. '<?php
  119. $var = array( 1, 2, 3 );
  120. ',
  121. ];
  122. yield [
  123. '<?php
  124. $var = [ 1, 2, 3 ];
  125. ',
  126. ];
  127. // list call with trailing comma - need to leave alone
  128. yield [
  129. '<?php list($path, $mode, ) = foo();',
  130. ];
  131. yield [
  132. '<?php list($path, $mode,) = foo();',
  133. ];
  134. yield [
  135. '<?php
  136. $a = $b->test( // do not remove space
  137. $e // between `(` and `)`
  138. // and this comment
  139. );',
  140. ];
  141. yield [
  142. '<?php
  143. function foo($bar, $baz)
  144. {
  145. // function body
  146. }',
  147. '<?php
  148. function foo( $bar, $baz )
  149. {
  150. // function body
  151. }',
  152. ];
  153. yield [
  154. '<?php
  155. function hello($value) {
  156. // code...
  157. }',
  158. '<?php
  159. function hello( $value ) {
  160. // code...
  161. }',
  162. ];
  163. yield [
  164. '<?php
  165. $code = function ($hello, $there) use ($ami, $tumi) {
  166. // code
  167. };
  168. ',
  169. '<?php
  170. $code = function ( $hello, $there ) use ( $ami, $tumi ) {
  171. // code
  172. };
  173. ',
  174. ];
  175. yield [
  176. '<?php
  177. for ($i = 0; $i < 42; $i++) {
  178. // code...
  179. }
  180. ',
  181. '<?php
  182. for ( $i = 0; $i < 42; $i++ ) {
  183. // code...
  184. }
  185. ',
  186. ];
  187. yield [
  188. '<?php
  189. explode($a, $b);
  190. ',
  191. '<?php
  192. explode( $a, $b );
  193. ',
  194. ];
  195. yield [
  196. '<?php
  197. if ($something) {
  198. // code
  199. }
  200. ',
  201. '<?php
  202. if ( $something ) {
  203. // code
  204. }
  205. ',
  206. ];
  207. yield [
  208. '<?php
  209. multiply((2 + 3) * 4);
  210. ',
  211. '<?php
  212. multiply( ( 2 + 3 ) * 4 );
  213. ',
  214. ];
  215. }
  216. /**
  217. * @return array<array{0: string, 1?: string}>
  218. */
  219. public static function provideSpacesFixCases(): iterable
  220. {
  221. // Leaves new lines alone
  222. yield [
  223. "<?php
  224. class Foo
  225. {
  226. private function bar()
  227. {
  228. if ( foo(
  229. 'foo' ,
  230. 'bar' ,
  231. [1, 2, 3],
  232. 'baz' // a comment just to mix things up
  233. ) ) {
  234. return 1;
  235. };
  236. }
  237. }",
  238. ];
  239. yield [
  240. '<?php foo();',
  241. '<?php foo( );',
  242. ];
  243. yield [
  244. '<?php
  245. if ( true ) {
  246. // if body
  247. }',
  248. '<?php
  249. if (true) {
  250. // if body
  251. }',
  252. ];
  253. yield [
  254. '<?php
  255. if ( true ) {
  256. // if body
  257. }',
  258. '<?php
  259. if ( true ) {
  260. // if body
  261. }',
  262. ];
  263. yield [
  264. '<?php
  265. function foo( $bar, $baz )
  266. {
  267. // function body
  268. }',
  269. '<?php
  270. function foo($bar, $baz)
  271. {
  272. // function body
  273. }',
  274. ];
  275. yield [
  276. '<?php
  277. $foo->bar( $arg1, $arg2 );',
  278. '<?php
  279. $foo->bar( $arg1, $arg2 );',
  280. ];
  281. yield [
  282. '<?php
  283. $var = array( 1, 2, 3 );
  284. ',
  285. '<?php
  286. $var = array(1, 2, 3);
  287. ',
  288. ];
  289. yield [
  290. '<?php
  291. $var = [ 1, 2, 3 ];
  292. ',
  293. ];
  294. yield [
  295. '<?php list( $path, $mode, ) = foo();',
  296. '<?php list($path, $mode,) = foo();',
  297. ];
  298. yield [
  299. '<?php
  300. $a = $b->test( // do not remove space
  301. $e // between `(` and `)`
  302. // and this comment
  303. );',
  304. ];
  305. yield [
  306. '<?php
  307. function foo( $bar, $baz )
  308. {
  309. // function body
  310. }',
  311. '<?php
  312. function foo($bar, $baz)
  313. {
  314. // function body
  315. }',
  316. ];
  317. yield [
  318. '<?php
  319. function hello( $value ) {
  320. // code...
  321. }',
  322. '<?php
  323. function hello($value) {
  324. // code...
  325. }',
  326. ];
  327. yield [
  328. '<?php
  329. $code = function ( $hello, $there ) use ( $ami, $tumi ) {
  330. // code
  331. };
  332. ',
  333. '<?php
  334. $code = function ($hello, $there) use ($ami, $tumi) {
  335. // code
  336. };
  337. ',
  338. ];
  339. yield [
  340. '<?php
  341. for ( $i = 0; $i < 42; $i++ ) {
  342. // code...
  343. }
  344. ',
  345. '<?php
  346. for ($i = 0; $i < 42; $i++) {
  347. // code...
  348. }
  349. ',
  350. ];
  351. yield [
  352. '<?php
  353. explode( $a, $b );
  354. ',
  355. '<?php
  356. explode($a, $b);
  357. ',
  358. ];
  359. yield [
  360. '<?php
  361. if ( $something ) {
  362. // code
  363. }
  364. ',
  365. '<?php
  366. if ( $something ) {
  367. // code
  368. }
  369. ',
  370. ];
  371. yield [
  372. '<?php
  373. multiply( ( 2 + 3 ) * 4 );
  374. ',
  375. '<?php
  376. multiply((2 + 3) * 4);
  377. ',
  378. ];
  379. }
  380. /**
  381. * @dataProvider provideDefaultFix80Cases
  382. *
  383. * @requires PHP 8.0
  384. */
  385. public function testDefaultFix80(string $expected, string $input): void
  386. {
  387. $this->doTest($expected, $input);
  388. }
  389. /**
  390. * @return iterable<string, array{0: string, 1?: string}>
  391. */
  392. public static function provideDefaultFix80Cases(): iterable
  393. {
  394. yield 'mixed argument' => [
  395. '<?php function foo(mixed $a){}',
  396. '<?php function foo( mixed $a ){}',
  397. ];
  398. }
  399. /**
  400. * @dataProvider provideSpacesFix80Cases
  401. *
  402. * @requires PHP 8.0
  403. */
  404. public function testSpacesFix80(string $expected, string $input): void
  405. {
  406. $this->fixer->configure(['space' => 'single']);
  407. $this->doTest($expected, $input);
  408. }
  409. /**
  410. * @return iterable<string, array{0: string, 1?: string}>
  411. */
  412. public static function provideSpacesFix80Cases(): iterable
  413. {
  414. yield 'mixed argument' => [
  415. '<?php function foo( mixed $a ){}',
  416. '<?php function foo(mixed $a){}',
  417. ];
  418. }
  419. }