WhitespaceAfterCommaInArrayFixerTest.php 5.5 KB

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