NoWhitespaceBeforeCommaInArrayFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. final class NoWhitespaceBeforeCommaInArrayFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public function provideFixCases(): array
  31. {
  32. return [
  33. //old style array
  34. [
  35. '<?php $x = array(1, "2",3);',
  36. '<?php $x = array(1 , "2",3);',
  37. ],
  38. //old style array with comments
  39. [
  40. '<?php $x = array /* comment */ (1, "2", 3);',
  41. '<?php $x = array /* comment */ (1 , "2", 3);',
  42. ],
  43. //old style array with comments
  44. [
  45. '<?php $x = array(1#
  46. ,#
  47. "2", 3);',
  48. '<?php $x = array(1#
  49. ,#
  50. "2" , 3);',
  51. ],
  52. //short array
  53. [
  54. '<?php $x = [1, "2", 3,$y];',
  55. '<?php $x = [1 , "2", 3 ,$y];',
  56. ],
  57. // don't change function calls
  58. [
  59. '<?php $x = [ 1, "2",getValue(1,2 ,3 ),$y];',
  60. '<?php $x = [ 1 , "2",getValue(1,2 ,3 ) ,$y];',
  61. ],
  62. // don't change function declarations
  63. [
  64. '<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
  65. '<?php $x = [1 , "2", function( $x ,$y) { return $x + $y; }, $y];',
  66. ],
  67. // don't change function declarations but change array inside
  68. [
  69. '<?php $x = [ 1, "2","c" => function( $x ,$y) { return [$x, $y]; }, $y];',
  70. '<?php $x = [ 1 , "2","c" => function( $x ,$y) { return [$x , $y]; }, $y];',
  71. ],
  72. // associative array (old)
  73. [
  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. [
  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. [
  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. [
  89. '<?php $x = [ "a" =>$a,
  90. "b"=>
  91. "b",
  92. 3 => $this->foo(),
  93. "d" => 30 ];',
  94. '<?php $x = [ "a" =>$a ,
  95. "b"=>
  96. "b",
  97. 3 => $this->foo() ,
  98. "d" => 30 ];',
  99. ],
  100. // multi line array
  101. [
  102. '<?php $a = [
  103. "foo",
  104. "bar",
  105. ];',
  106. '<?php $a = [
  107. "foo" ,
  108. "bar"
  109. ,
  110. ];',
  111. ],
  112. // nested multiline
  113. [
  114. '<?php $a = array(array(
  115. array(T_OPEN_TAG),
  116. array(T_VARIABLE, "$x"),
  117. ), 1);',
  118. ],
  119. [
  120. '<?php $a = array( // comment
  121. 123,
  122. );',
  123. ],
  124. [
  125. "<?php \$x = array(<<<'EOF'
  126. <?php \$a = '\\foo\\bar\\\\';
  127. EOF
  128. , <<<'EOF'
  129. <?php \$a = \"\\foo\\bar\\\\\";
  130. EOF
  131. );",
  132. ],
  133. ];
  134. }
  135. /**
  136. * @dataProvider provideFix73Cases
  137. * @requires PHP 7.3
  138. */
  139. public function testFix73(string $expected, ?string $input = null, array $config = []): void
  140. {
  141. $this->fixer->configure($config);
  142. $this->doTest($expected, $input);
  143. }
  144. public function provideFix73Cases(): array
  145. {
  146. return [
  147. [
  148. "<?php \$x = array(<<<'EOF'
  149. <?php \$a = '\\foo\\bar\\\\';
  150. EOF, <<<'EOF'
  151. <?php \$a = \"\\foo\\bar\\\\\";
  152. EOF
  153. );",
  154. "<?php \$x = array(<<<'EOF'
  155. <?php \$a = '\\foo\\bar\\\\';
  156. EOF
  157. , <<<'EOF'
  158. <?php \$a = \"\\foo\\bar\\\\\";
  159. EOF
  160. );",
  161. ['after_heredoc' => true],
  162. ],
  163. ];
  164. }
  165. /**
  166. * @dataProvider provideFixPhp74Cases
  167. * @requires PHP 7.4
  168. */
  169. public function testFixPhp74(string $expected, ?string $input = null): void
  170. {
  171. $this->doTest($expected, $input);
  172. }
  173. public function provideFixPhp74Cases(): array
  174. {
  175. return [
  176. [
  177. '<?php $x = array(...$foo, ...$bar);',
  178. '<?php $x = array(...$foo , ...$bar);',
  179. ],
  180. [
  181. '<?php $x = [...$foo, ...$bar];',
  182. '<?php $x = [...$foo , ...$bar];',
  183. ],
  184. ];
  185. }
  186. }