PsrAutoloadingFixerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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\Basic;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Graham Campbell <graham@alt-three.com>
  15. * @author Kuba Werłos <werlos@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer
  20. */
  21. final class PsrAutoloadingFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. * @param null|\SplFileInfo $file
  27. * @param null|string $dir
  28. *
  29. * @dataProvider provideFixCases
  30. * @dataProvider provideIgnoredCases
  31. */
  32. public function testFix($expected, $input = null, $file = null, $dir = null)
  33. {
  34. if (null === $file) {
  35. $file = $this->getTestFile(__FILE__);
  36. }
  37. if (null !== $dir) {
  38. $this->fixer->configure(['dir' => $dir]);
  39. }
  40. $this->doTest($expected, $input, $file);
  41. }
  42. public function provideFixCases()
  43. {
  44. $fileProphecy = $this->prophesize();
  45. $fileProphecy->willExtend(\SplFileInfo::class);
  46. $fileProphecy->getBasename('.php')->willReturn('Bar');
  47. $fileProphecy->getExtension()->willReturn('php');
  48. $fileProphecy->getRealPath()->willReturn(__DIR__.'/Psr/Foo/Bar.php');
  49. $file = $fileProphecy->reveal();
  50. yield [
  51. '<?php
  52. namespace Psr\foo;
  53. class Bar {}
  54. ',
  55. '<?php
  56. namespace Psr\foo;
  57. class bar {}
  58. ',
  59. $file,
  60. ];
  61. yield [
  62. '<?php
  63. class Psr_Foo_Bar {}
  64. ',
  65. '<?php
  66. class Psr_fOo_bAr {}
  67. ',
  68. $file,
  69. ];
  70. yield [
  71. '<?php
  72. namespace Psr\Foo;
  73. class Bar {}
  74. ',
  75. '<?php
  76. namespace Psr\foo;
  77. class bar {}
  78. ',
  79. $file,
  80. __DIR__,
  81. ];
  82. yield [ // ignore multiple classy in file
  83. '<?php
  84. namespace PhpCsFixer\Tests\Fixer\Basic;
  85. interface SomeInterfaceToBeUsedInTests {}
  86. class blah {}
  87. /* class foo */',
  88. ];
  89. yield [ // ignore multiple namespaces in file
  90. '<?php
  91. namespace PhpCsFixer\Tests\Fixer\Basic;
  92. interface SomeInterfaceToBeUsedInTests {}
  93. namespace AnotherNamespace;
  94. class blah {}
  95. /* class foo */',
  96. ];
  97. yield [ // fix class
  98. '<?php
  99. namespace PhpCsFixer\Tests\Fixer\Basic;
  100. class PsrAutoloadingFixerTest {}
  101. /* class foo */
  102. ',
  103. '<?php
  104. namespace PhpCsFixer\Tests\Fixer\Basic;
  105. class blah {}
  106. /* class foo */
  107. ',
  108. ];
  109. yield [ // abstract class
  110. '<?php
  111. namespace PhpCsFixer\Tests\Fixer\Basic;
  112. abstract class PsrAutoloadingFixerTest {}
  113. /* class foo */
  114. ',
  115. '<?php
  116. namespace PhpCsFixer\Tests\Fixer\Basic;
  117. abstract class blah {}
  118. /* class foo */
  119. ',
  120. ];
  121. yield [ // final class
  122. '<?php
  123. namespace PhpCsFixer\Tests\Fixer\Basic;
  124. final class PsrAutoloadingFixerTest {}
  125. /* class foo */
  126. ',
  127. '<?php
  128. namespace PhpCsFixer\Tests\Fixer\Basic;
  129. final class blah {}
  130. /* class foo */
  131. ',
  132. ];
  133. yield [ // class with comment
  134. '<?php
  135. namespace /* namespace here */ PhpCsFixer\Fixer\Psr;
  136. class /* hi there */ PsrAutoloadingFixerTest /* why hello */ {}
  137. /* class foo */
  138. ',
  139. '<?php
  140. namespace /* namespace here */ PhpCsFixer\Fixer\Psr;
  141. class /* hi there */ blah /* why hello */ {}
  142. /* class foo */
  143. ',
  144. ];
  145. yield [ // partial namespace
  146. '<?php
  147. namespace Foo\Bar\Baz\FIXER\Basic;
  148. class PsrAutoloadingFixer {}
  149. ',
  150. null,
  151. $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
  152. ];
  153. yield [ // partial namespace with comment
  154. '<?php
  155. namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
  156. class /* hi there */ PsrAutoloadingFixer {}
  157. ',
  158. null,
  159. $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
  160. ];
  161. yield [ // partial namespace
  162. '<?php
  163. namespace Foo\Bar\Baz;
  164. class PsrAutoloadingFixer {}
  165. ',
  166. null,
  167. $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
  168. ];
  169. yield [ // partial namespace with directory
  170. '<?php
  171. namespace Foo\Bar\Baz\Fixer\Basic;
  172. class PsrAutoloadingFixer {}
  173. ',
  174. '<?php
  175. namespace Foo\Bar\Baz\FIXER\Basic;
  176. class PsrAutoloadingFixer {}
  177. ',
  178. $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
  179. __DIR__.'/../../../src/',
  180. ];
  181. yield [ // partial namespace with comment and directory
  182. '<?php
  183. namespace /* hi there */ Foo\Bar\Baz\Fixer\Basic;
  184. class /* hi there */ PsrAutoloadingFixer {}
  185. ',
  186. '<?php
  187. namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
  188. class /* hi there */ PsrAutoloadingFixer {}
  189. ',
  190. $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
  191. __DIR__.'/../../../src/',
  192. ];
  193. yield [ // partial namespace with directory
  194. '<?php
  195. namespace Foo\Bar\Baz;
  196. class PsrAutoloadingFixer {}
  197. ',
  198. null,
  199. $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
  200. __DIR__.'/../../../src/Fixer/Basic',
  201. ];
  202. }
  203. public function provideIgnoredCases()
  204. {
  205. $cases = ['.php', 'Foo.class.php', '4Foo.php', '$#.php'];
  206. foreach (['__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'try', 'unset', 'use', 'var', 'while', 'xor'] as $keyword) {
  207. $cases[] = $keyword.'.php';
  208. }
  209. foreach (['__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__'] as $magicConstant) {
  210. $cases[] = $magicConstant.'.php';
  211. $cases[] = strtolower($magicConstant).'.php';
  212. }
  213. foreach ([
  214. 'T_CALLABLE' => 'callable',
  215. 'T_FINALLY' => 'finally',
  216. 'T_INSTEADOF' => 'insteadof',
  217. 'T_TRAIT' => 'trait',
  218. 'T_TRAIT_C' => '__TRAIT__',
  219. ] as $tokenType => $tokenValue) {
  220. if (\defined($tokenType)) {
  221. $cases[] = $tokenValue.'.php';
  222. $cases[] = strtolower($tokenValue).'.php';
  223. }
  224. }
  225. return array_map(function ($case) {
  226. return [
  227. '<?php
  228. namespace Aaa;
  229. class Bar {}',
  230. null,
  231. $this->getTestFile($case),
  232. ];
  233. }, $cases);
  234. }
  235. /**
  236. * @param string $expected
  237. * @param null|string $input
  238. *
  239. * @dataProvider provideFix70Cases
  240. * @requires PHP 7.0
  241. */
  242. public function testFix70($expected, $input = null)
  243. {
  244. $this->doTest($expected, $input, $this->getTestFile(__FILE__));
  245. }
  246. public function provideFix70Cases()
  247. {
  248. yield [ // class with anonymous class
  249. '<?php
  250. namespace PhpCsFixer\Tests\Fixer\Basic;
  251. class PsrAutoloadingFixerTest {
  252. public function foo() {
  253. return new class() implements FooInterface {};
  254. }
  255. }
  256. ',
  257. '<?php
  258. namespace PhpCsFixer\Tests\Fixer\Basic;
  259. class stdClass {
  260. public function foo() {
  261. return new class() implements FooInterface {};
  262. }
  263. }
  264. ',
  265. ];
  266. yield [ // ignore anonymous class
  267. '<?php
  268. namespace PhpCsFixer\Tests\Fixer\Basic;
  269. new class implements Countable {};
  270. ',
  271. ];
  272. yield [ // ignore anonymous class
  273. '<?php
  274. namespace PhpCsFixer\Tests\Fixer\Basic;
  275. new class extends stdClass {};
  276. ',
  277. ];
  278. yield [ // ignore multiple classy in file with anonymous class between them
  279. '<?php
  280. namespace PhpCsFixer\Tests\Fixer\Basic;
  281. class ClassOne {};
  282. new class extends stdClass {};
  283. class ClassTwo {};
  284. ',
  285. ];
  286. }
  287. }