IndentationTypeFixerTest.php 6.7 KB

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