CastSpacesFixerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\CastNotation;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\CastNotation\CastSpacesFixer
  19. */
  20. final class CastSpacesFixerTest extends AbstractFixerTestCase
  21. {
  22. public function testInvalidConfigMissingKey(): void
  23. {
  24. $this->expectException(InvalidFixerConfigurationException::class);
  25. $this->expectExceptionMessageMatches('#^\[cast_spaces\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#');
  26. $this->fixer->configure(['a' => 1]);
  27. }
  28. public function testInvalidConfigValue(): void
  29. {
  30. $this->expectException(InvalidFixerConfigurationException::class);
  31. $this->expectExceptionMessageMatches('#^\[cast_spaces\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#');
  32. $this->fixer->configure(['space' => 'double']);
  33. }
  34. /**
  35. * @dataProvider provideFixCastsCases
  36. */
  37. public function testFixCastsWithDefaultConfiguration(string $expected, ?string $input = null): void
  38. {
  39. $this->doTest($expected, $input);
  40. }
  41. /**
  42. * @dataProvider provideFixCastsCases
  43. */
  44. public function testFixCastsSingleSpace(string $expected, ?string $input = null): void
  45. {
  46. $this->fixer->configure(['space' => 'single']);
  47. $this->doTest($expected, $input);
  48. }
  49. public static function provideFixCastsCases(): iterable
  50. {
  51. yield [
  52. '<?php echo "( int ) $foo";',
  53. ];
  54. yield [
  55. '<?php $bar = (int) $foo;',
  56. '<?php $bar = ( int)$foo;',
  57. ];
  58. yield [
  59. '<?php $bar = (int) $foo;',
  60. '<?php $bar = ( int)$foo;',
  61. ];
  62. yield [
  63. '<?php $bar = (int) $foo;',
  64. '<?php $bar = (int) $foo;',
  65. ];
  66. yield [
  67. '<?php $bar = (string) (int) $foo;',
  68. '<?php $bar = ( string )( int )$foo;',
  69. ];
  70. yield [
  71. '<?php $bar = (string) (int) $foo;',
  72. '<?php $bar = (string)(int)$foo;',
  73. ];
  74. yield [
  75. '<?php $bar = (string) (int) $foo;',
  76. '<?php $bar = ( string ) ( int )$foo;',
  77. ];
  78. yield [
  79. '<?php $bar = (string) $foo;',
  80. '<?php $bar = ( string ) $foo;',
  81. ];
  82. yield [
  83. '<?php $bar = (float) Foo::bar();',
  84. '<?php $bar = (float )Foo::bar();',
  85. ];
  86. yield [
  87. '<?php $bar = Foo::baz((float) Foo::bar());',
  88. '<?php $bar = Foo::baz((float )Foo::bar());',
  89. ];
  90. yield [
  91. '<?php $bar = $query["params"] = (array) $query["params"];',
  92. '<?php $bar = $query["params"] = (array)$query["params"];',
  93. ];
  94. yield [
  95. "<?php \$bar = (int)\n \$foo;",
  96. ];
  97. yield [
  98. "<?php \$bar = (int)\r\n \$foo;",
  99. ];
  100. }
  101. /**
  102. * @dataProvider provideFixCastsNoneSpaceCases
  103. */
  104. public function testFixCastsNoneSpace(string $expected, ?string $input = null): void
  105. {
  106. $this->fixer->configure(['space' => 'none']);
  107. $this->doTest($expected, $input);
  108. }
  109. public static function provideFixCastsNoneSpaceCases(): iterable
  110. {
  111. yield [
  112. '<?php echo "( int ) $foo";',
  113. ];
  114. yield [
  115. '<?php $bar = (int)$foo;',
  116. '<?php $bar = ( int)$foo;',
  117. ];
  118. yield [
  119. '<?php $bar = (int)$foo;',
  120. '<?php $bar = ( int)$foo;',
  121. ];
  122. yield [
  123. '<?php $bar = (int)$foo;',
  124. '<?php $bar = (int) $foo;',
  125. ];
  126. yield [
  127. '<?php $bar = (string)(int)$foo;',
  128. '<?php $bar = ( string )( int )$foo;',
  129. ];
  130. yield [
  131. '<?php $bar = (string)(int)$foo;',
  132. ];
  133. yield [
  134. '<?php $bar = (string)(int)$foo;',
  135. '<?php $bar = ( string ) ( int )$foo;',
  136. ];
  137. yield [
  138. '<?php $bar = (string)$foo;',
  139. '<?php $bar = ( string ) $foo;',
  140. ];
  141. yield [
  142. '<?php $bar = (float)Foo::bar();',
  143. '<?php $bar = (float )Foo::bar();',
  144. ];
  145. yield [
  146. '<?php $bar = Foo::baz((float)Foo::bar());',
  147. '<?php $bar = Foo::baz((float )Foo::bar());',
  148. ];
  149. yield [
  150. '<?php $bar = $query["params"] = (array)$query["params"];',
  151. ];
  152. yield [
  153. '<?php $bar = (int)$foo;',
  154. "<?php \$bar = (int)\n \$foo;",
  155. ];
  156. yield [
  157. '<?php $bar = (int)$foo;',
  158. "<?php \$bar = (int)\r\n \$foo;",
  159. ];
  160. }
  161. }