WhitespaceAfterCommaInArrayFixerTest.php 5.5 KB

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