NoTrailingCommaInSinglelineArrayFixerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. final class NoTrailingCommaInSinglelineArrayFixerTest 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 static function provideFixCases(): iterable
  31. {
  32. yield ['<?php $x = array();'];
  33. yield ['<?php $x = array("foo");'];
  34. yield [
  35. '<?php $x = array("foo");',
  36. '<?php $x = array("foo", );',
  37. ];
  38. yield ["<?php \$x = array(\n'foo', \n);"];
  39. yield ["<?php \$x = array('foo', \n);"];
  40. yield ["<?php \$x = array(array('foo'), \n);", "<?php \$x = array(array('foo',), \n);"];
  41. yield ["<?php \$x = array(array('foo',\n), \n);"];
  42. yield [
  43. '<?php
  44. $test = array("foo", <<<TWIG
  45. foo
  46. TWIG
  47. , $twig, );',
  48. ];
  49. yield [
  50. '<?php
  51. $test = array(
  52. "foo", <<<TWIG
  53. foo
  54. TWIG
  55. , $twig, );',
  56. ];
  57. yield [
  58. '<?php
  59. $test = array("foo", <<<\'TWIG\'
  60. foo
  61. TWIG
  62. , $twig, );',
  63. ];
  64. yield [
  65. '<?php
  66. $test = array(
  67. "foo", <<<\'TWIG\'
  68. foo
  69. TWIG
  70. , $twig, );',
  71. ];
  72. // Short syntax
  73. yield ['<?php $x = array([]);'];
  74. yield ['<?php $x = [[]];'];
  75. yield ['<?php $x = ["foo"];', '<?php $x = ["foo",];'];
  76. yield ['<?php $x = bar(["foo"]);', '<?php $x = bar(["foo",]);'];
  77. yield ["<?php \$x = bar([['foo'],\n]);"];
  78. yield ["<?php \$x = ['foo', \n];"];
  79. yield ['<?php $x = array([]);', '<?php $x = array([],);'];
  80. yield ['<?php $x = [[]];', '<?php $x = [[],];'];
  81. yield ['<?php $x = [$y[""]];', '<?php $x = [$y[""],];'];
  82. yield [
  83. '<?php
  84. $test = ["foo", <<<TWIG
  85. foo
  86. TWIG
  87. , $twig, ];',
  88. ];
  89. yield [
  90. '<?php
  91. $test = [
  92. "foo", <<<TWIG
  93. foo
  94. TWIG
  95. , $twig, ];',
  96. ];
  97. yield [
  98. '<?php
  99. $test = ["foo", <<<\'TWIG\'
  100. foo
  101. TWIG
  102. , $twig, ];',
  103. ];
  104. yield [
  105. '<?php
  106. $test = [
  107. "foo", <<<\'TWIG\'
  108. foo
  109. TWIG
  110. , $twig, ];',
  111. ];
  112. yield [
  113. '<?php $x = array(...$foo);',
  114. '<?php $x = array(...$foo, );',
  115. ];
  116. yield [
  117. '<?php $x = [...$foo];',
  118. '<?php $x = [...$foo, ];',
  119. ];
  120. }
  121. }