CastSpacesFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\CastSpacesFixer>
  21. *
  22. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\CastNotation\CastSpacesFixer
  23. */
  24. final class CastSpacesFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @param _AutogeneratedInputConfiguration $config
  28. *
  29. * @dataProvider provideInvalidConfigurationCases
  30. */
  31. public function testInvalidConfiguration(array $config, string $expectedMessage): void
  32. {
  33. $this->expectException(InvalidFixerConfigurationException::class);
  34. $this->expectExceptionMessageMatches($expectedMessage);
  35. $this->fixer->configure($config);
  36. }
  37. /**
  38. * @return iterable<array{array<string, int|string>, string}>
  39. */
  40. public static function provideInvalidConfigurationCases(): iterable
  41. {
  42. yield 'missing key' => [
  43. ['a' => 1],
  44. '#^\[cast_spaces\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#',
  45. ];
  46. yield 'invalid value' => [
  47. ['space' => 'double'],
  48. '#^\[cast_spaces\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#',
  49. ];
  50. }
  51. /**
  52. * @param _AutogeneratedInputConfiguration $configuration
  53. *
  54. * @dataProvider provideFixCases
  55. */
  56. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  57. {
  58. $this->fixer->configure($configuration);
  59. $this->doTest($expected, $input);
  60. }
  61. /**
  62. * @return iterable<array{string, 1?: null|string, 2?: array{space: string}}>
  63. */
  64. public static function provideFixCases(): iterable
  65. {
  66. yield [
  67. '<?php echo "( int ) $foo";',
  68. ];
  69. yield [
  70. '<?php $bar = (int) $foo;',
  71. '<?php $bar = ( int)$foo;',
  72. ];
  73. yield [
  74. '<?php $bar = (int) $foo;',
  75. '<?php $bar = ( int)$foo;',
  76. ];
  77. yield [
  78. '<?php $bar = (int) $foo;',
  79. '<?php $bar = (int) $foo;',
  80. ];
  81. yield [
  82. '<?php $bar = (string) (int) $foo;',
  83. '<?php $bar = ( string )( int )$foo;',
  84. ];
  85. yield [
  86. '<?php $bar = (string) (int) $foo;',
  87. '<?php $bar = (string)(int)$foo;',
  88. ];
  89. yield [
  90. '<?php $bar = (string) (int) $foo;',
  91. '<?php $bar = ( string ) ( int )$foo;',
  92. ];
  93. yield [
  94. '<?php $bar = (string) $foo;',
  95. '<?php $bar = ( string ) $foo;',
  96. ];
  97. yield [
  98. '<?php $bar = (float) Foo::bar();',
  99. '<?php $bar = (float )Foo::bar();',
  100. ];
  101. yield [
  102. '<?php $bar = Foo::baz((float) Foo::bar());',
  103. '<?php $bar = Foo::baz((float )Foo::bar());',
  104. ];
  105. yield [
  106. '<?php $bar = $query["params"] = (array) $query["params"];',
  107. '<?php $bar = $query["params"] = (array)$query["params"];',
  108. ];
  109. yield [
  110. "<?php \$bar = (int)\n \$foo;",
  111. ];
  112. yield [
  113. "<?php \$bar = (int)\r\n \$foo;",
  114. ];
  115. yield [
  116. '<?php echo "( int ) $foo";',
  117. null,
  118. ['space' => 'none'],
  119. ];
  120. yield [
  121. '<?php $bar = (int)$foo;',
  122. '<?php $bar = ( int)$foo;',
  123. ['space' => 'none'],
  124. ];
  125. yield [
  126. '<?php $bar = (int)$foo;',
  127. '<?php $bar = ( int)$foo;',
  128. ['space' => 'none'],
  129. ];
  130. yield [
  131. '<?php $bar = (int)$foo;',
  132. '<?php $bar = (int) $foo;',
  133. ['space' => 'none'],
  134. ];
  135. yield [
  136. '<?php $bar = (string)(int)$foo;',
  137. '<?php $bar = ( string )( int )$foo;',
  138. ['space' => 'none'],
  139. ];
  140. yield [
  141. '<?php $bar = (string)(int)$foo;',
  142. null,
  143. ['space' => 'none'],
  144. ];
  145. yield [
  146. '<?php $bar = (string)(int)$foo;',
  147. '<?php $bar = ( string ) ( int )$foo;',
  148. ['space' => 'none'],
  149. ];
  150. yield [
  151. '<?php $bar = (string)$foo;',
  152. '<?php $bar = ( string ) $foo;',
  153. ['space' => 'none'],
  154. ];
  155. yield [
  156. '<?php $bar = (float)Foo::bar();',
  157. '<?php $bar = (float )Foo::bar();',
  158. ['space' => 'none'],
  159. ];
  160. yield [
  161. '<?php $bar = Foo::baz((float)Foo::bar());',
  162. '<?php $bar = Foo::baz((float )Foo::bar());',
  163. ['space' => 'none'],
  164. ];
  165. yield [
  166. '<?php $bar = $query["params"] = (array)$query["params"];',
  167. null,
  168. ['space' => 'none'],
  169. ];
  170. yield [
  171. '<?php $bar = (int)$foo;',
  172. "<?php \$bar = (int)\n \$foo;",
  173. ['space' => 'none'],
  174. ];
  175. yield [
  176. '<?php $bar = (int)$foo;',
  177. "<?php \$bar = (int)\r\n \$foo;",
  178. ['space' => 'none'],
  179. ];
  180. }
  181. }