NoWhitespaceBeforeCommaInArrayFixerTest.php 5.4 KB

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