CastSpacesFixerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. return [
  52. [
  53. '<?php echo "( int ) $foo";',
  54. ],
  55. [
  56. '<?php $bar = (int) $foo;',
  57. '<?php $bar = ( int)$foo;',
  58. ],
  59. [
  60. '<?php $bar = (int) $foo;',
  61. '<?php $bar = ( int)$foo;',
  62. ],
  63. [
  64. '<?php $bar = (int) $foo;',
  65. '<?php $bar = (int) $foo;',
  66. ],
  67. [
  68. '<?php $bar = (string) (int) $foo;',
  69. '<?php $bar = ( string )( int )$foo;',
  70. ],
  71. [
  72. '<?php $bar = (string) (int) $foo;',
  73. '<?php $bar = (string)(int)$foo;',
  74. ],
  75. [
  76. '<?php $bar = (string) (int) $foo;',
  77. '<?php $bar = ( string ) ( int )$foo;',
  78. ],
  79. [
  80. '<?php $bar = (string) $foo;',
  81. '<?php $bar = ( string ) $foo;',
  82. ],
  83. [
  84. '<?php $bar = (float) Foo::bar();',
  85. '<?php $bar = (float )Foo::bar();',
  86. ],
  87. [
  88. '<?php $bar = Foo::baz((float) Foo::bar());',
  89. '<?php $bar = Foo::baz((float )Foo::bar());',
  90. ],
  91. [
  92. '<?php $bar = $query["params"] = (array) $query["params"];',
  93. '<?php $bar = $query["params"] = (array)$query["params"];',
  94. ],
  95. [
  96. "<?php \$bar = (int)\n \$foo;",
  97. ],
  98. [
  99. "<?php \$bar = (int)\r\n \$foo;",
  100. ],
  101. ];
  102. }
  103. /**
  104. * @dataProvider provideFixCastsNoneSpaceCases
  105. */
  106. public function testFixCastsNoneSpace(string $expected, ?string $input = null): void
  107. {
  108. $this->fixer->configure(['space' => 'none']);
  109. $this->doTest($expected, $input);
  110. }
  111. public static function provideFixCastsNoneSpaceCases(): iterable
  112. {
  113. return [
  114. [
  115. '<?php echo "( int ) $foo";',
  116. ],
  117. [
  118. '<?php $bar = (int)$foo;',
  119. '<?php $bar = ( int)$foo;',
  120. ],
  121. [
  122. '<?php $bar = (int)$foo;',
  123. '<?php $bar = ( int)$foo;',
  124. ],
  125. [
  126. '<?php $bar = (int)$foo;',
  127. '<?php $bar = (int) $foo;',
  128. ],
  129. [
  130. '<?php $bar = (string)(int)$foo;',
  131. '<?php $bar = ( string )( int )$foo;',
  132. ],
  133. [
  134. '<?php $bar = (string)(int)$foo;',
  135. ],
  136. [
  137. '<?php $bar = (string)(int)$foo;',
  138. '<?php $bar = ( string ) ( int )$foo;',
  139. ],
  140. [
  141. '<?php $bar = (string)$foo;',
  142. '<?php $bar = ( string ) $foo;',
  143. ],
  144. [
  145. '<?php $bar = (float)Foo::bar();',
  146. '<?php $bar = (float )Foo::bar();',
  147. ],
  148. [
  149. '<?php $bar = Foo::baz((float)Foo::bar());',
  150. '<?php $bar = Foo::baz((float )Foo::bar());',
  151. ],
  152. [
  153. '<?php $bar = $query["params"] = (array)$query["params"];',
  154. ],
  155. [
  156. '<?php $bar = (int)$foo;',
  157. "<?php \$bar = (int)\n \$foo;",
  158. ],
  159. [
  160. '<?php $bar = (int)$foo;',
  161. "<?php \$bar = (int)\r\n \$foo;",
  162. ],
  163. ];
  164. }
  165. }