WhitespaceAfterCommaInArrayFixerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\ArrayNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Adam Marczuk <adam@marczuk.info>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer
  20. */
  21. final class WhitespaceAfterCommaInArrayFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. *
  26. * @param null|array<string, bool> $configuration
  27. */
  28. public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
  29. {
  30. if (null !== $configuration) {
  31. $this->fixer->configure($configuration);
  32. }
  33. $this->doTest($expected, $input);
  34. }
  35. public static function provideFixCases(): iterable
  36. {
  37. // old style array
  38. yield [
  39. '<?php $x = array( 1 , "2", 3);',
  40. '<?php $x = array( 1 ,"2",3);',
  41. ];
  42. // old style array with comments
  43. yield [
  44. '<?php $x = array /* comment */ ( 1 , "2", 3);',
  45. '<?php $x = array /* comment */ ( 1 , "2",3);',
  46. ];
  47. // short array
  48. yield [
  49. '<?php $x = [ 1 , "2", 3 , $y];',
  50. '<?php $x = [ 1 , "2",3 ,$y];',
  51. ];
  52. // don't change function calls
  53. yield [
  54. '<?php $x = [1, "2", getValue(1,2 ,3 ) , $y];',
  55. '<?php $x = [1, "2",getValue(1,2 ,3 ) ,$y];',
  56. ];
  57. // don't change function declarations
  58. yield [
  59. '<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
  60. '<?php $x = [1, "2",function( $x ,$y) { return $x + $y; },$y];',
  61. ];
  62. // don't change function declarations but change array inside
  63. yield [
  64. '<?php $x = [1, "2", "c" => function( $x ,$y) { return [$x , $y]; }, $y ];',
  65. '<?php $x = [1, "2","c" => function( $x ,$y) { return [$x ,$y]; },$y ];',
  66. ];
  67. // don't change anonymous class implements list but change array inside
  68. yield [
  69. '<?php $x = [1, "2", "c" => new class implements Foo ,Bar { const FOO = ["x", "y"]; }, $y ];',
  70. '<?php $x = [1, "2","c" => new class implements Foo ,Bar { const FOO = ["x","y"]; },$y ];',
  71. ];
  72. // associative array (old)
  73. yield [
  74. '<?php $x = array("a" => $a , "b" => "b", 3=>$this->foo(), "d" => 30 );',
  75. '<?php $x = array("a" => $a , "b" => "b",3=>$this->foo(), "d" => 30 );',
  76. ];
  77. // associative array (short)
  78. yield [
  79. '<?php $x = [ "a" => $a , "b"=>"b", 3 => $this->foo(), "d" =>30];',
  80. '<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo(), "d" =>30];',
  81. ];
  82. // nested arrays
  83. yield [
  84. '<?php $x = ["a" => $a, "b" => "b", 3=> [5, 6, 7] , "d" => array(1, 2, 3 , 4)];',
  85. '<?php $x = ["a" => $a, "b" => "b",3=> [5,6, 7] , "d" => array(1, 2,3 ,4)];',
  86. ];
  87. // multi line array
  88. yield [
  89. '<?php $x = ["a" =>$a,
  90. "b"=> "b",
  91. 3 => $this->foo(),
  92. "d" => 30];',
  93. ];
  94. // multi line array
  95. yield [
  96. '<?php $a = [
  97. "foo" ,
  98. "bar",
  99. ];',
  100. ];
  101. // nested multiline
  102. yield [
  103. '<?php $a = array(array(
  104. array(T_OPEN_TAG),
  105. array(T_VARIABLE, "$x"),
  106. ), 1, );',
  107. '<?php $a = array(array(
  108. array(T_OPEN_TAG),
  109. array(T_VARIABLE,"$x"),
  110. ),1,);',
  111. ];
  112. yield [
  113. '<?php $a = array( // comment
  114. 123,
  115. );',
  116. ];
  117. yield [
  118. '<?php $x = array(...$foo, ...$bar);',
  119. '<?php $x = array(...$foo,...$bar);',
  120. ];
  121. yield [
  122. '<?php $x = [...$foo, ...$bar];',
  123. '<?php $x = [...$foo,...$bar];',
  124. ];
  125. yield [
  126. '<?php [0, 1, 2, 3, 4, 5, 6];',
  127. '<?php [0,1, 2, 3, 4, 5, 6];',
  128. ['ensure_single_space' => true],
  129. ];
  130. yield [
  131. '<?php [0, 1, 2, 3, 4, 5];',
  132. "<?php [0,\t1,\t\t\t2,\t 3, \t4, \t 5];",
  133. ['ensure_single_space' => true],
  134. ];
  135. yield [
  136. '<?php [
  137. 0, # less than one
  138. 1, // one
  139. 42, /* more than one */
  140. 1000500100900, /** much more than one */
  141. ];',
  142. null,
  143. ['ensure_single_space' => true],
  144. ];
  145. yield [
  146. '<?php [0, /* comment */ 1, /** PHPDoc */ 2];',
  147. '<?php [0, /* comment */ 1, /** PHPDoc */ 2];',
  148. ['ensure_single_space' => true],
  149. ];
  150. }
  151. }