Psr0FixerTest.php 5.8 KB

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