IndentationTypeFixerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Whitespace\IndentationTypeFixer
  20. */
  21. final class IndentationTypeFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix($expected, $input = null)
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideFixCases()
  34. {
  35. $cases = [];
  36. $cases[] = [
  37. '<?php
  38. echo ALPHA;',
  39. "<?php
  40. \t\techo ALPHA;",
  41. ];
  42. $cases[] = [
  43. '<?php
  44. echo BRAVO;',
  45. "<?php
  46. \t\techo BRAVO;",
  47. ];
  48. $cases[] = [
  49. '<?php
  50. echo CHARLIE;',
  51. "<?php
  52. \t\techo CHARLIE;",
  53. ];
  54. $cases[] = [
  55. '<?php
  56. echo DELTA;',
  57. "<?php
  58. \t\techo DELTA;",
  59. ];
  60. $cases[] = [
  61. "<?php
  62. echo 'ECHO';",
  63. "<?php
  64. \t\techo 'ECHO';",
  65. ];
  66. $cases[] = [
  67. '<?php
  68. echo FOXTROT;',
  69. "<?php
  70. \t \techo FOXTROT;",
  71. ];
  72. $cases[] = [
  73. '<?php
  74. echo GOLF;',
  75. "<?php
  76. \t \techo GOLF;",
  77. ];
  78. $cases[] = [
  79. '<?php
  80. echo HOTEL;',
  81. "<?php
  82. \t \techo HOTEL;",
  83. ];
  84. $cases[] = [
  85. '<?php
  86. echo INDIA;',
  87. "<?php
  88. \t echo INDIA;",
  89. ];
  90. $cases[] = [
  91. '<?php
  92. echo JULIET;',
  93. "<?php
  94. \t \techo JULIET;",
  95. ];
  96. $cases[] = [
  97. '<?php
  98. echo KILO;',
  99. "<?php
  100. \t \techo KILO;",
  101. ];
  102. $cases[] = [
  103. '<?php
  104. echo MIKE;',
  105. "<?php
  106. \t \techo MIKE;",
  107. ];
  108. $cases[] = [
  109. '<?php
  110. echo NOVEMBER;',
  111. "<?php
  112. \techo NOVEMBER;",
  113. ];
  114. $cases[] = [
  115. '<?php
  116. echo OSCAR;',
  117. "<?php
  118. \t \t echo OSCAR;",
  119. ];
  120. $cases[] = [
  121. '<?php
  122. echo PAPA;',
  123. "<?php
  124. \t \t echo PAPA;",
  125. ];
  126. $cases[] = [
  127. '<?php
  128. echo QUEBEC;',
  129. "<?php
  130. \t \t echo QUEBEC;",
  131. ];
  132. $cases[] = [
  133. '<?php $x = "a: \t";',
  134. ];
  135. $cases[] = [
  136. "<?php
  137. \$x = \"
  138. \tLike
  139. \ta
  140. \tdog\";",
  141. ];
  142. $cases[] = [
  143. '<?php
  144. /**
  145. * Test that tabs in docblocks are converted to spaces.
  146. *
  147. * @test
  148. *
  149. * @return
  150. */',
  151. "<?php
  152. \t/**
  153. \t * Test that tabs in docblocks are converted to spaces.
  154. \t *
  155. \t * @test
  156. \t *
  157. \t * @return
  158. \t */",
  159. ];
  160. $cases[] = [
  161. '<?php
  162. /**
  163. * Test that tabs in docblocks are converted to spaces.
  164. */',
  165. "<?php
  166. \t\t/**
  167. \t\t * Test that tabs in docblocks are converted to spaces.
  168. \t\t */",
  169. ];
  170. $cases[] = [
  171. '<?php
  172. /*
  173. | Test that tabs in comments are converted to spaces '."\t".'.
  174. */',
  175. "<?php
  176. \t/*
  177. \t | Test that tabs in comments are converted to spaces \t.
  178. \t */",
  179. ];
  180. $cases[] = [
  181. "<?php
  182. /**
  183. * This variable
  184. * should not be '\t', really!
  185. */",
  186. "<?php
  187. \t/**
  188. \t * This variable
  189. \t * should not be '\t', really!
  190. \t */",
  191. ];
  192. $cases[] = [
  193. "<?php\necho 1;\n?>\r\n\t\$a = ellow;",
  194. ];
  195. return $cases;
  196. }
  197. /**
  198. * @param string $expected
  199. * @param null|string $input
  200. *
  201. * @dataProvider provideMessyWhitespacesCases
  202. */
  203. public function testMessyWhitespaces($expected, $input = null)
  204. {
  205. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  206. $this->doTest($expected, $input);
  207. }
  208. public function provideMessyWhitespacesCases()
  209. {
  210. $cases = [];
  211. $cases[] = [
  212. "<?php
  213. \t\techo KILO;",
  214. '<?php
  215. echo KILO;',
  216. ];
  217. $cases[] = [
  218. "<?php
  219. \t\t echo QUEBEC;",
  220. '<?php
  221. echo QUEBEC;',
  222. ];
  223. $cases[] = [
  224. "<?php
  225. \t/**
  226. \t * This variable
  227. \t * should not be '\t', really!
  228. \t */",
  229. "<?php
  230. /**
  231. * This variable
  232. * should not be '\t', really!
  233. */",
  234. ];
  235. $cases['mix indentation'] = [
  236. "<?php
  237. \t\t/*
  238. \t\t * multiple indentation
  239. \t\t * shall be handled properly
  240. \t\t */",
  241. "<?php
  242. \t\t/*
  243. \t\t * multiple indentation
  244. \t * shall be handled properly
  245. \t */",
  246. ];
  247. $cases[] = [
  248. "<?php
  249. function myFunction() {
  250. \t\$foo = 1;
  251. \t//abc
  252. \t\$myFunction = 2;
  253. \t\$middleVar = 1;
  254. }",
  255. '<?php
  256. function myFunction() {
  257. $foo = 1;
  258. //abc
  259. $myFunction = 2;
  260. $middleVar = 1;
  261. }',
  262. ];
  263. return $cases;
  264. }
  265. /**
  266. * @param string $expected
  267. * @param null|string $input
  268. *
  269. * @dataProvider provideMessyWhitespacesReversedCases
  270. */
  271. public function testMessyWhitespacesReversed($expected, $input = null)
  272. {
  273. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n"));
  274. $this->doTest($input, $expected);
  275. }
  276. public function provideMessyWhitespacesReversedCases()
  277. {
  278. return array_filter(
  279. $this->provideMessyWhitespacesCases(),
  280. static function ($key) {
  281. return !\is_string($key) || false === strpos($key, 'mix indentation');
  282. },
  283. ARRAY_FILTER_USE_KEY
  284. );
  285. }
  286. /**
  287. * @param string $expected
  288. * @param null|string $input
  289. *
  290. * @dataProvider provideDoubleSpaceIndentCases
  291. */
  292. public function testDoubleSpaceIndent($expected, $input = null)
  293. {
  294. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' '));
  295. $this->doTest($expected, $input);
  296. }
  297. public function provideDoubleSpaceIndentCases()
  298. {
  299. return [
  300. ['<?php
  301. if (true) {
  302. if (true) {
  303. (new stdClass())->foo(
  304. "text",
  305. "text2"
  306. );
  307. }
  308. }'],
  309. [
  310. "<?php
  311. if (true) {
  312. if (true) {
  313. (new stdClass())->foo(
  314. 'text',
  315. 'text2'
  316. );
  317. }
  318. }",
  319. "<?php
  320. if (true) {
  321. if (true) {
  322. \t(new stdClass())->foo(
  323. \t 'text',
  324. \t 'text2'
  325. \t);
  326. }
  327. }",
  328. ],
  329. [
  330. '<?php
  331. /*
  332. * Foo
  333. */
  334. ',
  335. "<?php
  336. \t/*
  337. \t * Foo
  338. \t */
  339. ", ],
  340. ];
  341. }
  342. }