ConcatSpaceFixerTest.php 5.5 KB

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