IndentationTypeFixerTest.php 6.4 KB

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