ConcatSpaceFixerTest.php 5.7 KB

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