IndentationTypeFixerTest.php 6.8 KB

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