Psr4FixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\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');
  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. * @param string $filename
  142. *
  143. * @dataProvider provideIgnoredCases
  144. */
  145. public function testIgnoreWrongNames($filename)
  146. {
  147. $file = $this->getTestFile($filename);
  148. $expected = <<<'EOF'
  149. <?php
  150. namespace Aaa;
  151. class Bar {}
  152. EOF;
  153. $this->doTest($expected, null, $file);
  154. }
  155. public function provideIgnoredCases()
  156. {
  157. $ignoreCases = array(
  158. array('.php'),
  159. array('Foo.class.php'),
  160. array('4Foo.php'),
  161. array('$#.php'),
  162. );
  163. foreach (array('__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) {
  164. $ignoreCases[] = array($keyword.'.php');
  165. }
  166. foreach (array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__') as $magicConstant) {
  167. $ignoreCases[] = array($magicConstant.'.php');
  168. $ignoreCases[] = array(strtolower($magicConstant).'.php');
  169. }
  170. foreach (array(
  171. 'T_CALLABLE' => 'callable',
  172. 'T_FINALLY' => 'finally',
  173. 'T_INSTEADOF' => 'insteadof',
  174. 'T_TRAIT' => 'trait',
  175. 'T_TRAIT_C' => '__TRAIT__',
  176. ) as $tokenType => $tokenValue) {
  177. if (defined($tokenType)) {
  178. $ignoreCases[] = array($tokenValue.'.php');
  179. $ignoreCases[] = array(strtolower($tokenValue).'.php');
  180. }
  181. }
  182. return $ignoreCases;
  183. }
  184. }