WhitespaceAfterCommaInArrayFixerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. 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. //short array
  44. [
  45. '<?php $x = [ 1 , "2", 3 , $y];',
  46. '<?php $x = [ 1 , "2",3 ,$y];',
  47. ],
  48. // don't change function calls
  49. [
  50. '<?php $x = [1, "2", getValue(1,2 ,3 ) , $y];',
  51. '<?php $x = [1, "2",getValue(1,2 ,3 ) ,$y];',
  52. ],
  53. // don't change function declarations
  54. [
  55. '<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
  56. '<?php $x = [1, "2",function( $x ,$y) { return $x + $y; },$y];',
  57. ],
  58. // don't change function declarations but change array inside
  59. [
  60. '<?php $x = [1, "2", "c" => function( $x ,$y) { return [$x , $y]; }, $y ];',
  61. '<?php $x = [1, "2","c" => function( $x ,$y) { return [$x ,$y]; },$y ];',
  62. ],
  63. // associative array (old)
  64. [
  65. '<?php $x = array("a" => $a , "b" => "b", 3=>$this->foo(), "d" => 30 );',
  66. '<?php $x = array("a" => $a , "b" => "b",3=>$this->foo(), "d" => 30 );',
  67. ],
  68. // associative array (short)
  69. [
  70. '<?php $x = [ "a" => $a , "b"=>"b", 3 => $this->foo(), "d" =>30];',
  71. '<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo(), "d" =>30];',
  72. ],
  73. // nested arrays
  74. [
  75. '<?php $x = ["a" => $a, "b" => "b", 3=> [5, 6, 7] , "d" => array(1, 2, 3 , 4)];',
  76. '<?php $x = ["a" => $a, "b" => "b",3=> [5,6, 7] , "d" => array(1, 2,3 ,4)];',
  77. ],
  78. // multi line array
  79. [
  80. '<?php $x = ["a" =>$a,
  81. "b"=> "b",
  82. 3 => $this->foo(),
  83. "d" => 30];',
  84. ],
  85. // multi line array
  86. [
  87. '<?php $a = [
  88. "foo" ,
  89. "bar",
  90. ];',
  91. ],
  92. // nested multiline
  93. [
  94. '<?php $a = array(array(
  95. array(T_OPEN_TAG),
  96. array(T_VARIABLE, "$x"),
  97. ), 1, );',
  98. '<?php $a = array(array(
  99. array(T_OPEN_TAG),
  100. array(T_VARIABLE,"$x"),
  101. ),1,);',
  102. ],
  103. [
  104. '<?php $a = array( // comment
  105. 123,
  106. );',
  107. ],
  108. ];
  109. }
  110. /**
  111. * @dataProvider provideFixPhp74Cases
  112. * @requires PHP 7.4
  113. */
  114. public function testFixPhp74(string $expected, ?string $input = null): void
  115. {
  116. $this->doTest($expected, $input);
  117. }
  118. public function provideFixPhp74Cases(): array
  119. {
  120. return [
  121. [
  122. '<?php $x = array(...$foo, ...$bar);',
  123. '<?php $x = array(...$foo,...$bar);',
  124. ],
  125. [
  126. '<?php $x = [...$foo, ...$bar];',
  127. '<?php $x = [...$foo,...$bar];',
  128. ],
  129. ];
  130. }
  131. }