MethodChainingIndentationFixerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\Whitespace;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @author Vladimir Boliev <voff.web@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer
  20. */
  21. final class MethodChainingIndentationFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. *
  26. * @param string $expected
  27. * @param null|string $input
  28. */
  29. public function testFix($expected, $input = null)
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideFixCases()
  34. {
  35. return [
  36. [
  37. '<?php
  38. $user->setEmail(\'voff.web@gmail.com\')
  39. ->setPassword(\'233434\')
  40. ->setEmailConfirmed(false)
  41. ->setEmailConfirmationCode(\'123456\')
  42. ->setHashsalt(\'1234\')
  43. ->setTncAccepted(true);
  44. ',
  45. '<?php
  46. $user->setEmail(\'voff.web@gmail.com\')
  47. ->setPassword(\'233434\')
  48. ->setEmailConfirmed(false)
  49. ->setEmailConfirmationCode(\'123456\')
  50. ->setHashsalt(\'1234\')
  51. ->setTncAccepted(true);
  52. ',
  53. ],
  54. [
  55. '<?php
  56. $foo
  57. ->bar1() // comment
  58. ->bar2() /*
  59. comment
  60. */
  61. ->bar3()
  62. // comment
  63. ->bar4()
  64. ->bar5()
  65. /** buahaha */
  66. ->bar6()
  67. /** buahaha */
  68. ->bar7();',
  69. '<?php
  70. $foo
  71. ->bar1() // comment
  72. ->bar2() /*
  73. comment
  74. */
  75. ->bar3()
  76. // comment
  77. ->bar4()
  78. ->bar5()
  79. /** buahaha */ ->bar6()
  80. /** buahaha */->bar7();',
  81. ],
  82. [
  83. '<?php
  84. $foo
  85. ->bar1()
  86. ->bar2();',
  87. '<?php
  88. $foo
  89. ->bar1()
  90. ->bar2();',
  91. ],
  92. [
  93. '<?php $foo
  94. ->bar();',
  95. '<?php $foo
  96. ->bar();',
  97. ],
  98. [
  99. '<?php $foo->bar()->baz()
  100. ->qux();',
  101. '<?php $foo->bar()->baz()
  102. ->qux();',
  103. ],
  104. [
  105. '<?php
  106. someCodeHereAndMultipleBreaks();
  107. $foo
  108. ->bar1()
  109. ->bar2();',
  110. ],
  111. [
  112. '<?php
  113. if (null !== $files) {
  114. return $files;
  115. }
  116. $finder = Finder::create()
  117. ->files()
  118. ;',
  119. ],
  120. [
  121. '<?php
  122. $finder = Finder::create()
  123. ->files()
  124. ;',
  125. ],
  126. [
  127. '<?php
  128. $replacements = $replacements
  129. ->setAllowedTypes([\'array\'])
  130. ->setNormalizer(function (Options $options, $value) use ($toTypes, $default) {
  131. return $normalizedValue;
  132. })
  133. ->setDefault($default)
  134. ->setWhitespacesConfig(
  135. new WhitespacesFixerConfig($config[\'indent\'], $config[\'lineEnding\'])
  136. )
  137. ;',
  138. ],
  139. [
  140. '<?php
  141. return foo()
  142. ->bar (
  143. new foo()
  144. )
  145. ->bar();
  146. ',
  147. ],
  148. [
  149. '<?php
  150. return new Foo("param", [
  151. (new Bar("param1", "param2"))
  152. ->Foo([
  153. (new Bar())->foo(),
  154. ])
  155. ]);
  156. ',
  157. ],
  158. [
  159. '<?php
  160. (new Foo(
  161. \'argument on line 1\',
  162. \'argument on line 2\'
  163. ))
  164. ->foo()
  165. ->bar()
  166. ;',
  167. '<?php
  168. (new Foo(
  169. \'argument on line 1\',
  170. \'argument on line 2\'
  171. ))
  172. ->foo()
  173. ->bar()
  174. ;',
  175. ],
  176. ];
  177. }
  178. /**
  179. * @param string $expected
  180. * @param null|string $input
  181. *
  182. * @dataProvider provideWindowsWhitespacesCases
  183. */
  184. public function testWindowsWhitespaces($expected, $input = null)
  185. {
  186. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  187. $this->doTest($expected, $input);
  188. }
  189. public function provideWindowsWhitespacesCases()
  190. {
  191. return [
  192. [
  193. "<?php\r\n\$user->setEmail('voff.web@gmail.com')\r\n\t->setPassword('233434')\r\n\t->setEmailConfirmed(false)\r\n\t->setEmailConfirmationCode('123456')\r\n\t->setHashsalt('1234')\r\n\t->setTncAccepted(true);",
  194. "<?php\r\n\$user->setEmail('voff.web@gmail.com')\r\n\r\n ->setPassword('233434')\r\n\t\t\t->setEmailConfirmed(false)\r\n\t\t ->setEmailConfirmationCode('123456')\r\n->setHashsalt('1234')\r\n\t\t->setTncAccepted(true);",
  195. ],
  196. ];
  197. }
  198. }