Psr4FixerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\AbstractPsrAutoloadingFixer
  19. * @covers \PhpCsFixer\Fixer\Basic\Psr4Fixer
  20. */
  21. final class Psr4FixerTest extends AbstractFixerTestCase
  22. {
  23. public function testFixCase()
  24. {
  25. $fileProphecy = $this->prophesize();
  26. $fileProphecy->willExtend(\SplFileInfo::class);
  27. $fileProphecy->getBasename()->willReturn('Bar.php');
  28. $fileProphecy->getRealPath()->willReturn(__DIR__.'/Psr4/Foo/Bar.php');
  29. $file = $fileProphecy->reveal();
  30. $expected = <<<'EOF'
  31. <?php
  32. namespace Psr4\foo;
  33. class Bar {}
  34. EOF;
  35. $input = <<<'EOF'
  36. <?php
  37. namespace Psr4\foo;
  38. class bar {}
  39. EOF;
  40. $this->doTest($expected, $input, $file);
  41. $expected = <<<'EOF'
  42. <?php
  43. class Psr4_Foo_Bar {}
  44. EOF;
  45. $input = <<<'EOF'
  46. <?php
  47. class Psr4_fOo_bAr {}
  48. EOF;
  49. $this->doTest($expected, $input, $file);
  50. }
  51. public function testFixClassName()
  52. {
  53. $file = $this->getTestFile(__FILE__);
  54. $expected = <<<'EOF'
  55. <?php
  56. namespace PhpCsFixer\Tests\Fixer\Basic;
  57. class Psr4FixerTest {}
  58. /* class foo */
  59. EOF;
  60. $input = <<<'EOF'
  61. <?php
  62. namespace PhpCsFixer\Tests\Fixer\Basic;
  63. class blah {}
  64. /* class foo */
  65. EOF;
  66. $this->doTest($expected, $input, $file);
  67. }
  68. public function testFixAbstractClassName()
  69. {
  70. $file = $this->getTestFile(__FILE__);
  71. $expected = <<<'EOF'
  72. <?php
  73. namespace PhpCsFixer\Tests\Fixer\Basic;
  74. abstract class Psr4FixerTest {}
  75. /* class foo */
  76. EOF;
  77. $input = <<<'EOF'
  78. <?php
  79. namespace PhpCsFixer\Tests\Fixer\Basic;
  80. abstract class blah {}
  81. /* class foo */
  82. EOF;
  83. $this->doTest($expected, $input, $file);
  84. }
  85. public function testFixFinalClassName()
  86. {
  87. $file = $this->getTestFile(__FILE__);
  88. $expected = <<<'EOF'
  89. <?php
  90. namespace PhpCsFixer\Tests\Fixer\Basic;
  91. final class Psr4FixerTest {}
  92. /* class foo */
  93. EOF;
  94. $input = <<<'EOF'
  95. <?php
  96. namespace PhpCsFixer\Tests\Fixer\Basic;
  97. final class blah {}
  98. /* class foo */
  99. EOF;
  100. $this->doTest($expected, $input, $file);
  101. }
  102. public function testFixClassNameWithComment()
  103. {
  104. $file = $this->getTestFile(__FILE__);
  105. $expected = <<<'EOF'
  106. <?php
  107. namespace /* namespace here */ PhpCsFixer\Fixer\Psr4;
  108. class /* hi there */ Psr4FixerTest /* why hello */ {}
  109. /* class foo */
  110. EOF;
  111. $input = <<<'EOF'
  112. <?php
  113. namespace /* namespace here */ PhpCsFixer\Fixer\Psr4;
  114. class /* hi there */ blah /* why hello */ {}
  115. /* class foo */
  116. EOF;
  117. $this->doTest($expected, $input, $file);
  118. }
  119. public function testHandlePartialNamespaces()
  120. {
  121. $file = $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/Psr4Fixer.php');
  122. $expected = <<<'EOF'
  123. <?php
  124. namespace Foo\Bar\Baz\FIXER\Basic;
  125. class Psr4Fixer {}
  126. EOF;
  127. $this->doTest($expected, null, $file);
  128. $expected = <<<'EOF'
  129. <?php
  130. namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
  131. class /* hi there */ Psr4Fixer {}
  132. EOF;
  133. $this->doTest($expected, null, $file);
  134. $expected = <<<'EOF'
  135. <?php
  136. namespace Foo\Bar\Baz;
  137. class Psr4Fixer {}
  138. EOF;
  139. $this->doTest($expected, null, $file);
  140. }
  141. /**
  142. * @requires PHP 7.0
  143. */
  144. public function testClassWithAnonymousClass()
  145. {
  146. $file = $this->getTestFile(__FILE__);
  147. $expected = <<<'EOF'
  148. <?php
  149. namespace PhpCsFixer\Tests\Fixer\Basic;
  150. class Psr4FixerTest {
  151. public function foo() {
  152. return new class() implements FooInterface {};
  153. }
  154. }
  155. EOF;
  156. $input = <<<'EOF'
  157. <?php
  158. namespace PhpCsFixer\Tests\Fixer\Basic;
  159. class stdClass {
  160. public function foo() {
  161. return new class() implements FooInterface {};
  162. }
  163. }
  164. EOF;
  165. $this->doTest($expected, $input, $file);
  166. }
  167. /**
  168. * @requires PHP 7.0
  169. */
  170. public function testIgnoreAnonymousClass()
  171. {
  172. $file = $this->getTestFile(__FILE__);
  173. $expected = <<<'EOF'
  174. <?php
  175. namespace PhpCsFixer\Tests\Fixer\Basic;
  176. new class implements Countable {};
  177. EOF;
  178. $this->doTest($expected, null, $file);
  179. $expected = <<<'EOF'
  180. <?php
  181. namespace PhpCsFixer\Tests\Fixer\Basic;
  182. new class extends stdClass {};
  183. EOF;
  184. $this->doTest($expected, null, $file);
  185. }
  186. /**
  187. * @param string $filename
  188. *
  189. * @dataProvider provideIgnoredCases
  190. */
  191. public function testIgnoreWrongNames($filename)
  192. {
  193. $file = $this->getTestFile($filename);
  194. $expected = <<<'EOF'
  195. <?php
  196. namespace Aaa;
  197. class Bar {}
  198. EOF;
  199. $this->doTest($expected, null, $file);
  200. }
  201. public function provideIgnoredCases()
  202. {
  203. $ignoreCases = [
  204. ['.php'],
  205. ['Foo.class.php'],
  206. ['4Foo.php'],
  207. ['$#.php'],
  208. ];
  209. 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) {
  210. $ignoreCases[] = [$keyword.'.php'];
  211. }
  212. foreach (['__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__'] as $magicConstant) {
  213. $ignoreCases[] = [$magicConstant.'.php'];
  214. $ignoreCases[] = [strtolower($magicConstant).'.php'];
  215. }
  216. foreach ([
  217. 'T_CALLABLE' => 'callable',
  218. 'T_FINALLY' => 'finally',
  219. 'T_INSTEADOF' => 'insteadof',
  220. 'T_TRAIT' => 'trait',
  221. 'T_TRAIT_C' => '__TRAIT__',
  222. ] as $tokenType => $tokenValue) {
  223. if (\defined($tokenType)) {
  224. $ignoreCases[] = [$tokenValue.'.php'];
  225. $ignoreCases[] = [strtolower($tokenValue).'.php'];
  226. }
  227. }
  228. return $ignoreCases;
  229. }
  230. }