WhitespaceAfterCommaInArrayFixerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\ArrayNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Adam Marczuk <adam@marczuk.info>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer
  19. */
  20. final class WhitespaceAfterCommaInArrayFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. return [
  35. //old style array
  36. [
  37. '<?php $x = array( 1 , "2", 3);',
  38. '<?php $x = array( 1 ,"2",3);',
  39. ],
  40. //old style array with comments
  41. [
  42. '<?php $x = array /* comment */ ( 1 , "2", 3);',
  43. '<?php $x = array /* comment */ ( 1 , "2",3);',
  44. ],
  45. //short array
  46. [
  47. '<?php $x = [ 1 , "2", 3 , $y];',
  48. '<?php $x = [ 1 , "2",3 ,$y];',
  49. ],
  50. // don't change function calls
  51. [
  52. '<?php $x = [1, "2", getValue(1,2 ,3 ) , $y];',
  53. '<?php $x = [1, "2",getValue(1,2 ,3 ) ,$y];',
  54. ],
  55. // don't change function declarations
  56. [
  57. '<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
  58. '<?php $x = [1, "2",function( $x ,$y) { return $x + $y; },$y];',
  59. ],
  60. // don't change function declarations but change array inside
  61. [
  62. '<?php $x = [1, "2", "c" => function( $x ,$y) { return [$x , $y]; }, $y ];',
  63. '<?php $x = [1, "2","c" => function( $x ,$y) { return [$x ,$y]; },$y ];',
  64. ],
  65. // associative array (old)
  66. [
  67. '<?php $x = array("a" => $a , "b" => "b", 3=>$this->foo(), "d" => 30 );',
  68. '<?php $x = array("a" => $a , "b" => "b",3=>$this->foo(), "d" => 30 );',
  69. ],
  70. // associative array (short)
  71. [
  72. '<?php $x = [ "a" => $a , "b"=>"b", 3 => $this->foo(), "d" =>30];',
  73. '<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo(), "d" =>30];',
  74. ],
  75. // nested arrays
  76. [
  77. '<?php $x = ["a" => $a, "b" => "b", 3=> [5, 6, 7] , "d" => array(1, 2, 3 , 4)];',
  78. '<?php $x = ["a" => $a, "b" => "b",3=> [5,6, 7] , "d" => array(1, 2,3 ,4)];',
  79. ],
  80. // multi line array
  81. [
  82. '<?php $x = ["a" =>$a,
  83. "b"=> "b",
  84. 3 => $this->foo(),
  85. "d" => 30];',
  86. ],
  87. // multi line array
  88. [
  89. '<?php $a = [
  90. "foo" ,
  91. "bar",
  92. ];',
  93. ],
  94. // nested multiline
  95. [
  96. '<?php $a = array(array(
  97. array(T_OPEN_TAG),
  98. array(T_VARIABLE, "$x"),
  99. ), 1, );',
  100. '<?php $a = array(array(
  101. array(T_OPEN_TAG),
  102. array(T_VARIABLE,"$x"),
  103. ),1,);',
  104. ],
  105. [
  106. '<?php $a = array( // comment
  107. 123,
  108. );',
  109. ],
  110. ];
  111. }
  112. /**
  113. * @param string $expected
  114. * @param null|string $input
  115. *
  116. * @dataProvider provideFixPhp74Cases
  117. * @requires PHP 7.4
  118. */
  119. public function testFixPhp74($expected, $input = null)
  120. {
  121. $this->doTest($expected, $input);
  122. }
  123. public function provideFixPhp74Cases()
  124. {
  125. return [
  126. [
  127. '<?php $x = array(...$foo, ...$bar);',
  128. '<?php $x = array(...$foo,...$bar);',
  129. ],
  130. [
  131. '<?php $x = [...$foo, ...$bar];',
  132. '<?php $x = [...$foo,...$bar];',
  133. ],
  134. ];
  135. }
  136. }