ConcatSpaceFixerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return [
  47. [
  48. '<?php $foo = "a".\'b\'."c"."d".$e.($f + 1);',
  49. '<?php $foo = "a" . \'b\' ."c". "d" . $e.($f + 1);',
  50. ],
  51. [
  52. '<?php $foo = 1 ."foo";',
  53. '<?php $foo = 1 . "foo";',
  54. ],
  55. [
  56. '<?php $foo = "foo". 1;',
  57. '<?php $foo = "foo" . 1;',
  58. ],
  59. [
  60. '<?php $foo = "a".
  61. "b";',
  62. '<?php $foo = "a" .
  63. "b";',
  64. ],
  65. [
  66. '<?php $a = "foobar"
  67. ."baz";',
  68. ],
  69. [
  70. '<?php $a = "foobar"
  71. //test
  72. ."baz";',
  73. ],
  74. [
  75. '<?php $a = "foobar"
  76. /* test */
  77. ."baz";',
  78. ],
  79. [
  80. '<?php $a = "foobar" //
  81. ."baz";',
  82. ],
  83. [
  84. '<?php $a = "foobar" //
  85. ."baz"//
  86. ."cex"/**/
  87. ."dev"/** */
  88. ."baz" //
  89. ."cex" /**/
  90. ."ewer23" '.'
  91. ."dev" /** */
  92. ;',
  93. ],
  94. [
  95. '<?php $a = "foobar" //
  96. ."baz" /**/
  97. ."something";',
  98. ],
  99. [
  100. '<?php $a = "foobar"
  101. ."baz". //
  102. "something";',
  103. ],
  104. [
  105. '<?php $a = "foobar"
  106. ."baz". /** */
  107. "something";',
  108. ],
  109. [
  110. "<?php
  111. \$longString = '*'
  112. .'*****'
  113. .'*****'
  114. .'*****'
  115. // Comment about next line
  116. .'*****'
  117. // Other comment
  118. .'*****';
  119. ",
  120. "<?php
  121. \$longString = '*'
  122. . '*****'
  123. . '*****'
  124. . '*****'
  125. // Comment about next line
  126. . '*****'
  127. // Other comment
  128. . '*****';
  129. ",
  130. ],
  131. ];
  132. }
  133. /**
  134. * @dataProvider provideFixWithSpaceCases
  135. */
  136. public function testFixWithSpace(string $expected, ?string $input = null): void
  137. {
  138. $this->fixer->configure(['spacing' => 'one']);
  139. $this->doTest($expected, $input);
  140. }
  141. public static function provideFixWithSpaceCases(): iterable
  142. {
  143. return [
  144. [
  145. '<?php
  146. $a = //
  147. $c . /**/
  148. $d #
  149. . $e /** */
  150. . $f . //
  151. $z;
  152. ',
  153. '<?php
  154. $a = //
  155. $c . /**/
  156. $d #
  157. . $e /** */
  158. . $f . //
  159. $z;
  160. ',
  161. ],
  162. [
  163. '<?php $foo = "a" . \'b\' . "c" . "d" . $e . ($f + 1);',
  164. '<?php $foo = "a" . \'b\' ."c". "d" . $e.($f + 1);',
  165. ],
  166. [
  167. '<?php $foo = "a" .
  168. "b";',
  169. '<?php $foo = "a".
  170. "b";',
  171. ],
  172. [
  173. '<?php $a = "foobar"
  174. . "baz";',
  175. '<?php $a = "foobar"
  176. ."baz";',
  177. ],
  178. [
  179. '<?php echo $a . $b;
  180. echo $d . $e . //
  181. $f;
  182. echo $a . $b?>
  183. <?php
  184. echo $c;
  185. ',
  186. '<?php echo $a.$b;
  187. echo $d . $e . //
  188. $f;
  189. echo $a . $b?>
  190. <?php
  191. echo $c;
  192. ',
  193. ],
  194. ];
  195. }
  196. }