Psr0FixerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * @internal
  15. *
  16. * @covers \PhpCsFixer\AbstractPsrAutoloadingFixer
  17. * @covers \PhpCsFixer\Fixer\Basic\Psr0Fixer
  18. */
  19. final class Psr0FixerTest extends AbstractFixerTestCase
  20. {
  21. public function testFixCase()
  22. {
  23. $this->fixer->configure(['dir' => __DIR__]);
  24. $fileProphecy = $this->prophesize();
  25. $fileProphecy->willExtend(\SplFileInfo::class);
  26. $fileProphecy->getBasename()->willReturn('Bar.php');
  27. $fileProphecy->getRealPath()->willReturn(__DIR__.'/Psr0/Foo/Bar.php');
  28. $file = $fileProphecy->reveal();
  29. $expected = <<<'EOF'
  30. <?php
  31. namespace Psr0\Foo;
  32. class Bar {}
  33. EOF;
  34. $input = <<<'EOF'
  35. <?php
  36. namespace Psr0\foo;
  37. class bar {}
  38. EOF;
  39. $this->doTest($expected, $input, $file);
  40. $expected = <<<'EOF'
  41. <?php
  42. class Psr0_Foo_Bar {}
  43. EOF;
  44. $input = <<<'EOF'
  45. <?php
  46. class Psr0_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\Contrib;
  56. class Psr0FixerTest {}
  57. /* class foo */
  58. EOF;
  59. $input = <<<'EOF'
  60. <?php
  61. namespace PhpCsFixer\Tests\Fixer\Contrib;
  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\Contrib;
  73. abstract class Psr0FixerTest {}
  74. /* class foo */
  75. EOF;
  76. $input = <<<'EOF'
  77. <?php
  78. namespace PhpCsFixer\Tests\Fixer\Contrib;
  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\Contrib;
  90. final class Psr0FixerTest {}
  91. /* class foo */
  92. EOF;
  93. $input = <<<'EOF'
  94. <?php
  95. namespace PhpCsFixer\Tests\Fixer\Contrib;
  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\PSR0;
  107. class /* hi there */ Psr0FixerTest /* why hello */ {}
  108. /* class foo */
  109. EOF;
  110. $input = <<<'EOF'
  111. <?php
  112. namespace /* namespace here */ PhpCsFixer\Fixer\PSR0;
  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. $this->fixer->configure(['dir' => __DIR__.'/../../../src/']);
  121. $file = $this->getTestFile(__DIR__.'/../../../src/Fixer/Basic/Psr0Fixer.php');
  122. $expected = <<<'EOF'
  123. <?php
  124. namespace Foo\Bar\Baz\Fixer\Basic;
  125. class Psr0Fixer {}
  126. EOF;
  127. $input = <<<'EOF'
  128. <?php
  129. namespace Foo\Bar\Baz\FIXER\Basic;
  130. class Psr0Fixer {}
  131. EOF;
  132. $this->doTest($expected, $input, $file);
  133. $expected = <<<'EOF'
  134. <?php
  135. namespace /* hi there */ Foo\Bar\Baz\Fixer\Basic;
  136. class /* hi there */ Psr0Fixer {}
  137. EOF;
  138. $input = <<<'EOF'
  139. <?php
  140. namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
  141. class /* hi there */ Psr0Fixer {}
  142. EOF;
  143. $this->doTest($expected, $input, $file);
  144. $this->fixer->configure(['dir' => __DIR__.'/../../../src/Fixer/Basic']);
  145. $expected = <<<'EOF'
  146. <?php
  147. namespace Foo\Bar\Baz;
  148. class Psr0Fixer {}
  149. EOF;
  150. $this->doTest($expected, null, $file);
  151. }
  152. /**
  153. * @requires PHP 7.0
  154. */
  155. public function testIgnoreAnonymousClass()
  156. {
  157. $file = $this->getTestFile(__FILE__);
  158. $expected = <<<'EOF'
  159. <?php
  160. namespace PhpCsFixer\Tests\Fixer\Basic;
  161. new class implements Countable {};
  162. EOF;
  163. $this->doTest($expected, null, $file);
  164. $expected = <<<'EOF'
  165. <?php
  166. namespace PhpCsFixer\Tests\Fixer\Basic;
  167. new class extends stdClass {};
  168. EOF;
  169. $this->doTest($expected, null, $file);
  170. }
  171. /**
  172. * @param string $filename
  173. *
  174. * @dataProvider provideIgnoredCases
  175. */
  176. public function testIgnoreWrongNames($filename)
  177. {
  178. $file = $this->getTestFile($filename);
  179. $expected = <<<'EOF'
  180. <?php
  181. namespace Aaa;
  182. class Bar {}
  183. EOF;
  184. $this->doTest($expected, null, $file);
  185. }
  186. public function provideIgnoredCases()
  187. {
  188. $ignoreCases = [
  189. ['.php'],
  190. ['Foo.class.php'],
  191. ['4Foo.php'],
  192. ['$#.php'],
  193. ];
  194. 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) {
  195. $ignoreCases[] = [$keyword.'.php'];
  196. }
  197. foreach (['__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__'] as $magicConstant) {
  198. $ignoreCases[] = [$magicConstant.'.php'];
  199. $ignoreCases[] = [strtolower($magicConstant).'.php'];
  200. }
  201. foreach ([
  202. 'T_CALLABLE' => 'callable',
  203. 'T_FINALLY' => 'finally',
  204. 'T_INSTEADOF' => 'insteadof',
  205. 'T_TRAIT' => 'trait',
  206. 'T_TRAIT_C' => '__TRAIT__',
  207. ] as $tokenType => $tokenValue) {
  208. if (defined($tokenType)) {
  209. $ignoreCases[] = [$tokenValue.'.php'];
  210. $ignoreCases[] = [strtolower($tokenValue).'.php'];
  211. }
  212. }
  213. return $ignoreCases;
  214. }
  215. }