NoTrailingCommaInSinglelineArrayFixerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Sebastiaan Stok <s.stok@rollerscapes.net>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer>
  22. */
  23. final class NoTrailingCommaInSinglelineArrayFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield ['<?php $x = array();'];
  38. yield ['<?php $x = array("foo");'];
  39. yield [
  40. '<?php $x = array("foo");',
  41. '<?php $x = array("foo", );',
  42. ];
  43. yield ["<?php \$x = array(\n'foo', \n);"];
  44. yield ["<?php \$x = array('foo', \n);"];
  45. yield ["<?php \$x = array(array('foo'), \n);", "<?php \$x = array(array('foo',), \n);"];
  46. yield ["<?php \$x = array(array('foo',\n), \n);"];
  47. yield [
  48. '<?php
  49. $test = array("foo", <<<TWIG
  50. foo
  51. TWIG
  52. , $twig, );',
  53. ];
  54. yield [
  55. '<?php
  56. $test = array(
  57. "foo", <<<TWIG
  58. foo
  59. TWIG
  60. , $twig, );',
  61. ];
  62. yield [
  63. '<?php
  64. $test = array("foo", <<<\'TWIG\'
  65. foo
  66. TWIG
  67. , $twig, );',
  68. ];
  69. yield [
  70. '<?php
  71. $test = array(
  72. "foo", <<<\'TWIG\'
  73. foo
  74. TWIG
  75. , $twig, );',
  76. ];
  77. // Short syntax
  78. yield ['<?php $x = array([]);'];
  79. yield ['<?php $x = [[]];'];
  80. yield ['<?php $x = ["foo"];', '<?php $x = ["foo",];'];
  81. yield ['<?php $x = bar(["foo"]);', '<?php $x = bar(["foo",]);'];
  82. yield ["<?php \$x = bar([['foo'],\n]);"];
  83. yield ["<?php \$x = ['foo', \n];"];
  84. yield ['<?php $x = array([]);', '<?php $x = array([],);'];
  85. yield ['<?php $x = [[]];', '<?php $x = [[],];'];
  86. yield ['<?php $x = [$y[""]];', '<?php $x = [$y[""],];'];
  87. yield [
  88. '<?php
  89. $test = ["foo", <<<TWIG
  90. foo
  91. TWIG
  92. , $twig, ];',
  93. ];
  94. yield [
  95. '<?php
  96. $test = [
  97. "foo", <<<TWIG
  98. foo
  99. TWIG
  100. , $twig, ];',
  101. ];
  102. yield [
  103. '<?php
  104. $test = ["foo", <<<\'TWIG\'
  105. foo
  106. TWIG
  107. , $twig, ];',
  108. ];
  109. yield [
  110. '<?php
  111. $test = [
  112. "foo", <<<\'TWIG\'
  113. foo
  114. TWIG
  115. , $twig, ];',
  116. ];
  117. yield [
  118. '<?php $x = array(...$foo);',
  119. '<?php $x = array(...$foo, );',
  120. ];
  121. yield [
  122. '<?php $x = [...$foo];',
  123. '<?php $x = [...$foo, ];',
  124. ];
  125. }
  126. }