ArraySyntaxFixerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  17. * @author Gregor Harlan <gharlan@web.de>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer
  22. */
  23. final class ArraySyntaxFixerTest extends AbstractFixerTestCase
  24. {
  25. public function testInvalidConfiguration(): void
  26. {
  27. $this->expectException(InvalidFixerConfigurationException::class);
  28. $this->expectExceptionMessageMatches('#^\[array_syntax\] Invalid configuration: The option "a" does not exist\. Defined options are: "syntax"\.$#');
  29. $this->fixer->configure(['a' => 1]);
  30. }
  31. public function testFixWithDefaultConfiguration(): void
  32. {
  33. $this->fixer->configure([]);
  34. $this->doTest(
  35. '<?php $a = []; $b = [];',
  36. '<?php $a = []; $b = array();'
  37. );
  38. }
  39. /**
  40. * @dataProvider provideFixLongSyntaxCases
  41. */
  42. public function testFixLongSyntax(string $expected, ?string $input = null): void
  43. {
  44. $this->fixer->configure(['syntax' => 'long']);
  45. $this->doTest($expected, $input);
  46. }
  47. public static function provideFixLongSyntaxCases(): iterable
  48. {
  49. yield ['<?php $x = array();', '<?php $x = [];'];
  50. yield ['<?php $x = array(); $y = array();', '<?php $x = []; $y = [];'];
  51. yield ['<?php $x = array( );', '<?php $x = [ ];'];
  52. yield ['<?php $x = array(\'foo\');', '<?php $x = [\'foo\'];'];
  53. yield ['<?php $x = array( \'foo\' );', '<?php $x = [ \'foo\' ];'];
  54. yield ['<?php $x = array(($y ? true : false));', '<?php $x = [($y ? true : false)];'];
  55. yield ['<?php $x = array(($y ? array(true) : array(false)));', '<?php $x = [($y ? [true] : [false])];'];
  56. yield ['<?php $x = array(($y ? array(true) : array( false )));', '<?php $x = [($y ? [true] : [ false ])];'];
  57. yield ['<?php $x = array(($y ? array("t" => true) : array("f" => false)));', '<?php $x = [($y ? ["t" => true] : ["f" => false])];'];
  58. yield ['<?php print_r(array(($y ? true : false)));', '<?php print_r([($y ? true : false)]);'];
  59. yield ['<?php $x = array(array(array()));', '<?php $x = [[[]]];'];
  60. yield ['<?php $x = array(array(array())); $y = array(array(array()));', '<?php $x = [[[]]]; $y = [[[]]];'];
  61. yield ['<?php function(array $foo = array()) {};', '<?php function(array $foo = []) {};'];
  62. yield ['<?php $x = array(1, 2)[0];', '<?php $x = [1, 2][0];'];
  63. yield ['<?php $x[] = 1;'];
  64. yield ['<?php $x[ ] = 1;'];
  65. yield ['<?php $x[2] = 1;'];
  66. yield ['<?php $x["a"] = 1;'];
  67. yield ['<?php $x = func()[$x];'];
  68. yield ['<?php $x = "foo"[$x];'];
  69. yield ['<?php $text = "foo ${aaa[123]} bar $bbb[0] baz";'];
  70. yield ['<?php foreach ($array as [$x, $y]) {}'];
  71. yield ['<?php foreach ($array as $key => [$x, $y]) {}'];
  72. }
  73. /**
  74. * @dataProvider provideFixShortSyntaxCases
  75. */
  76. public function testFixShortSyntax(string $expected, ?string $input = null): void
  77. {
  78. $this->fixer->configure(['syntax' => 'short']);
  79. $this->doTest($expected, $input);
  80. }
  81. public static function provideFixShortSyntaxCases(): iterable
  82. {
  83. yield ['<?php $x = [];', '<?php $x = array();'];
  84. yield ['<?php $x = []; $y = [];', '<?php $x = array(); $y = array();'];
  85. yield ['<?php $x = [ ];', '<?php $x = array( );'];
  86. yield ['<?php $x = [\'foo\'];', '<?php $x = array(\'foo\');'];
  87. yield ['<?php $x = [ \'foo\' ];', '<?php $x = array( \'foo\' );'];
  88. yield ['<?php $x = [($y ? true : false)];', '<?php $x = array(($y ? true : false));'];
  89. yield ['<?php $x = [($y ? [true] : [false])];', '<?php $x = array(($y ? array(true) : array(false)));'];
  90. yield ['<?php $x = [($y ? [true] : [ false ])];', '<?php $x = array(($y ? array(true) : array( false )));'];
  91. yield ['<?php $x = [($y ? ["t" => true] : ["f" => false])];', '<?php $x = array(($y ? array("t" => true) : array("f" => false)));'];
  92. yield ['<?php print_r([($y ? true : false)]);', '<?php print_r(array(($y ? true : false)));'];
  93. yield ['<?php $x = [[[]]];', '<?php $x = array(array(array()));'];
  94. yield ['<?php $x = [[[]]]; $y = [[[]]];', '<?php $x = array(array(array())); $y = array(array(array()));'];
  95. yield ['<?php function(array $foo = []) {};', '<?php function(array $foo = array()) {};'];
  96. yield ['<?php function(array $foo) {};'];
  97. yield ['<?php function(array $foo = []) {};', '<?php function(array $foo = array()) {};'];
  98. yield ['<?php $a = [ ];', '<?php $a = array ( );'];
  99. }
  100. }