ConcatSpaceFixerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\Operator;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Operator\ConcatSpaceFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\ConcatSpaceFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\ConcatSpaceFixer
  25. */
  26. final class ConcatSpaceFixerTest extends AbstractFixerTestCase
  27. {
  28. public function testInvalidConfigMissingKey(): void
  29. {
  30. $this->expectException(InvalidFixerConfigurationException::class);
  31. $this->expectExceptionMessageMatches('#^\[concat_space\] Invalid configuration: The option "a" does not exist\. Defined options are: "spacing"\.$#');
  32. $this->fixer->configure(['a' => 1]);
  33. }
  34. public function testInvalidConfigValue(): void
  35. {
  36. $this->expectException(InvalidFixerConfigurationException::class);
  37. $this->expectExceptionMessageMatches('#^\[concat_space\] Invalid configuration: The option "spacing" with value "tabs" is invalid\. Accepted values are: "one", "none"\.$#');
  38. $this->fixer->configure(['spacing' => 'tabs']); // @phpstan-ignore-line
  39. }
  40. /**
  41. * @dataProvider provideFixWithoutSpaceCases
  42. */
  43. public function testFixWithoutSpace(string $expected, ?string $input = null): void
  44. {
  45. $this->fixer->configure(['spacing' => 'none']);
  46. $this->doTest($expected, $input);
  47. }
  48. /**
  49. * @return iterable<array{0: string, 1?: string}>
  50. */
  51. public static function provideFixWithoutSpaceCases(): iterable
  52. {
  53. yield [
  54. '<?php $foo = "a".\'b\'."c"."d".$e.($f + 1);',
  55. '<?php $foo = "a" . \'b\' ."c". "d" . $e.($f + 1);',
  56. ];
  57. yield [
  58. '<?php $foo = 1 ."foo";',
  59. '<?php $foo = 1 . "foo";',
  60. ];
  61. yield [
  62. '<?php $foo = "foo". 1;',
  63. '<?php $foo = "foo" . 1;',
  64. ];
  65. yield [
  66. '<?php $foo = "a".
  67. "b";',
  68. '<?php $foo = "a" .
  69. "b";',
  70. ];
  71. yield [
  72. '<?php $a = "foobar"
  73. ."baz";',
  74. ];
  75. yield [
  76. '<?php $a = "foobar"
  77. //test
  78. ."baz";',
  79. ];
  80. yield [
  81. '<?php $a = "foobar"
  82. /* test */
  83. ."baz";',
  84. ];
  85. yield [
  86. '<?php $a = "foobar" //
  87. ."baz";',
  88. ];
  89. yield [
  90. '<?php $a = "foobar" //
  91. ."baz"//
  92. ."cex"/**/
  93. ."dev"/** */
  94. ."baz" //
  95. ."cex" /**/
  96. ."ewer23" '.'
  97. ."dev" /** */
  98. ;',
  99. ];
  100. yield [
  101. '<?php $a = "foobar" //
  102. ."baz" /**/
  103. ."something";',
  104. ];
  105. yield [
  106. '<?php $a = "foobar"
  107. ."baz". //
  108. "something";',
  109. ];
  110. yield [
  111. '<?php $a = "foobar"
  112. ."baz". /** */
  113. "something";',
  114. ];
  115. yield [
  116. "<?php
  117. \$longString = '*'
  118. .'*****'
  119. .'*****'
  120. .'*****'
  121. // Comment about next line
  122. .'*****'
  123. // Other comment
  124. .'*****';
  125. ",
  126. "<?php
  127. \$longString = '*'
  128. . '*****'
  129. . '*****'
  130. . '*****'
  131. // Comment about next line
  132. . '*****'
  133. // Other comment
  134. . '*****';
  135. ",
  136. ];
  137. }
  138. /**
  139. * @dataProvider provideFixWithSpaceCases
  140. */
  141. public function testFixWithSpace(string $expected, ?string $input = null): void
  142. {
  143. $this->fixer->configure(['spacing' => 'one']);
  144. $this->doTest($expected, $input);
  145. }
  146. /**
  147. * @return iterable<array{string, string}>
  148. */
  149. public static function provideFixWithSpaceCases(): iterable
  150. {
  151. yield [
  152. '<?php
  153. $a = //
  154. $c . /**/
  155. $d #
  156. . $e /** */
  157. . $f . //
  158. $z;
  159. ',
  160. '<?php
  161. $a = //
  162. $c . /**/
  163. $d #
  164. . $e /** */
  165. . $f . //
  166. $z;
  167. ',
  168. ];
  169. yield [
  170. '<?php $foo = "a" . \'b\' . "c" . "d" . $e . ($f + 1);',
  171. '<?php $foo = "a" . \'b\' ."c". "d" . $e.($f + 1);',
  172. ];
  173. yield [
  174. '<?php $foo = "a" .
  175. "b";',
  176. '<?php $foo = "a".
  177. "b";',
  178. ];
  179. yield [
  180. '<?php $a = "foobar"
  181. . "baz";',
  182. '<?php $a = "foobar"
  183. ."baz";',
  184. ];
  185. yield [
  186. '<?php echo $a . $b;
  187. echo $d . $e . //
  188. $f;
  189. echo $a . $b?>
  190. <?php
  191. echo $c;
  192. ',
  193. '<?php echo $a.$b;
  194. echo $d . $e . //
  195. $f;
  196. echo $a . $b?>
  197. <?php
  198. echo $c;
  199. ',
  200. ];
  201. }
  202. }