CastSpacesFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\CastNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @internal
  15. *
  16. * @covers \PhpCsFixer\Fixer\CastNotation\CastSpacesFixer
  17. */
  18. final class CastSpacesFixerTest extends AbstractFixerTestCase
  19. {
  20. public function testInvalidConfigMissingKey()
  21. {
  22. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  23. $this->expectExceptionMessageRegExp('#^\[cast_spaces\] Invalid configuration: The option "a" does not exist\. Defined options are: "space"\.$#');
  24. $this->fixer->configure(['a' => 1]);
  25. }
  26. public function testInvalidConfigValue()
  27. {
  28. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  29. $this->expectExceptionMessageRegExp('#^\[cast_spaces\] Invalid configuration: The option "space" with value "double" is invalid\. Accepted values are: "none", "single"\.$#');
  30. $this->fixer->configure(['space' => 'double']);
  31. }
  32. /**
  33. * @param string $expected
  34. * @param null|string $input
  35. *
  36. * @dataProvider provideFixCastsCases
  37. */
  38. public function testFixCastsWithDefaultConfiguration($expected, $input = null)
  39. {
  40. $this->doTest($expected, $input);
  41. }
  42. /**
  43. * @param string $expected
  44. * @param null|string $input
  45. *
  46. * @dataProvider provideFixCastsCases
  47. */
  48. public function testFixCastsSingleSpace($expected, $input = null)
  49. {
  50. $this->fixer->configure(['space' => 'single']);
  51. $this->doTest($expected, $input);
  52. }
  53. public function provideFixCastsCases()
  54. {
  55. return [
  56. [
  57. '<?php echo "( 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 = (int) $foo;',
  69. '<?php $bar = (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) (int) $foo;',
  81. '<?php $bar = ( string ) ( int )$foo;',
  82. ],
  83. [
  84. '<?php $bar = (string) $foo;',
  85. '<?php $bar = ( string ) $foo;',
  86. ],
  87. [
  88. '<?php $bar = (float) Foo::bar();',
  89. '<?php $bar = (float )Foo::bar();',
  90. ],
  91. [
  92. '<?php $bar = Foo::baz((float) Foo::bar());',
  93. '<?php $bar = Foo::baz((float )Foo::bar());',
  94. ],
  95. [
  96. '<?php $bar = $query["params"] = (array) $query["params"];',
  97. '<?php $bar = $query["params"] = (array)$query["params"];',
  98. ],
  99. [
  100. "<?php \$bar = (int)\n \$foo;",
  101. ],
  102. [
  103. "<?php \$bar = (int)\r\n \$foo;",
  104. ],
  105. ];
  106. }
  107. /**
  108. * @param string $expected
  109. * @param null|string $input
  110. *
  111. * @dataProvider provideNoneSpaceFixCases
  112. */
  113. public function testFixCastsNoneSpace($expected, $input = null)
  114. {
  115. $this->fixer->configure(['space' => 'none']);
  116. $this->doTest($expected, $input);
  117. }
  118. public function provideNoneSpaceFixCases()
  119. {
  120. return [
  121. [
  122. '<?php echo "( int ) $foo";',
  123. ],
  124. [
  125. '<?php $bar = (int)$foo;',
  126. '<?php $bar = ( int)$foo;',
  127. ],
  128. [
  129. '<?php $bar = (int)$foo;',
  130. '<?php $bar = ( int)$foo;',
  131. ],
  132. [
  133. '<?php $bar = (int)$foo;',
  134. '<?php $bar = (int) $foo;',
  135. ],
  136. [
  137. '<?php $bar = (string)(int)$foo;',
  138. '<?php $bar = ( string )( int )$foo;',
  139. ],
  140. [
  141. '<?php $bar = (string)(int)$foo;',
  142. ],
  143. [
  144. '<?php $bar = (string)(int)$foo;',
  145. '<?php $bar = ( string ) ( int )$foo;',
  146. ],
  147. [
  148. '<?php $bar = (string)$foo;',
  149. '<?php $bar = ( string ) $foo;',
  150. ],
  151. [
  152. '<?php $bar = (float)Foo::bar();',
  153. '<?php $bar = (float )Foo::bar();',
  154. ],
  155. [
  156. '<?php $bar = Foo::baz((float)Foo::bar());',
  157. '<?php $bar = Foo::baz((float )Foo::bar());',
  158. ],
  159. [
  160. '<?php $bar = $query["params"] = (array)$query["params"];',
  161. ],
  162. [
  163. '<?php $bar = (int)$foo;',
  164. "<?php \$bar = (int)\n \$foo;",
  165. ],
  166. [
  167. '<?php $bar = (int)$foo;',
  168. "<?php \$bar = (int)\r\n \$foo;",
  169. ],
  170. ];
  171. }
  172. }