Psr4FixerTest.php 5.8 KB

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