ClassReferenceNameCasingFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Casing;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer
  18. */
  19. final class ClassReferenceNameCasingFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @dataProvider provideFixCases
  23. */
  24. public function testFix(string $expected, string $input = null): void
  25. {
  26. $this->doTest($expected, $input);
  27. }
  28. public static function provideFixCases(): iterable
  29. {
  30. yield [
  31. '<?php
  32. $a = new Exception;
  33. $b = new \Exception;
  34. $c = new Exception();
  35. $d = new \Exception();
  36. $e = "a".Exception::class;
  37. $f = "a".\Exception::class;
  38. $g .= "exception";
  39. echo \Exception::class;
  40. print(Exception::class);
  41. // $a = new exception();
  42. /** $a = new exception(); */
  43. ',
  44. '<?php
  45. $a = new exception;
  46. $b = new \exception;
  47. $c = new exception();
  48. $d = new \exception();
  49. $e = "a".exception::class;
  50. $f = "a".\exception::class;
  51. $g .= "exception";
  52. echo \exception::class;
  53. print(exception::class);
  54. // $a = new exception();
  55. /** $a = new exception(); */
  56. ',
  57. ];
  58. yield [
  59. '<?php namespace Foo {
  60. $a = new exception;
  61. $b = new \Exception;
  62. }',
  63. '<?php namespace Foo {
  64. $a = new exception;
  65. $b = new \exception;
  66. }',
  67. ];
  68. yield [
  69. '<?php namespace Foo;
  70. $a = new exception;
  71. $b = new \Exception;
  72. ',
  73. '<?php namespace Foo;
  74. $a = new exception;
  75. $b = new \EXCEPTION;
  76. ',
  77. ];
  78. yield [
  79. '<?php
  80. $a = exception();
  81. $b = new A\exception;
  82. $c = new A\B\C\exception;
  83. $a1 = \exception();
  84. $b1 = new \A\exception;
  85. $c1 = new \A\B\C\exception;
  86. ',
  87. ];
  88. yield [
  89. '<?php class Foo extends Exception {};',
  90. '<?php class Foo extends exception {};',
  91. ];
  92. yield [
  93. '<?php class exception {}; new foO();',
  94. ];
  95. yield [
  96. '<?php interface exception {};',
  97. ];
  98. yield [
  99. '<?php trait exception {};',
  100. ];
  101. yield [
  102. '<?php function exception() {};',
  103. ];
  104. yield [
  105. '<?php const exception = "abc";',
  106. ];
  107. yield [
  108. '<?php $a = Foo::exception;',
  109. ];
  110. yield [
  111. '<?php $a = $foo->exception;',
  112. ];
  113. yield [
  114. '<?php use Foo as exception;',
  115. ];
  116. yield [
  117. '<?php class Foo { use exception; }',
  118. ];
  119. yield [
  120. '<?php $foo = ["const" => exception];',
  121. ];
  122. yield [
  123. '<?php
  124. namespace {
  125. $a = new Exception;
  126. $b = new \Exception;
  127. }
  128. namespace Bar {
  129. $a = new exception;
  130. $b = new \Exception;
  131. }
  132. namespace Foo {
  133. $a = new exception;
  134. $b = new \Exception;
  135. $c = new foO();
  136. }',
  137. '<?php
  138. namespace {
  139. $a = new exception;
  140. $b = new \exception;
  141. }
  142. namespace Bar {
  143. $a = new exception;
  144. $b = new \exception;
  145. }
  146. namespace Foo {
  147. $a = new exception;
  148. $b = new \exception;
  149. $c = new foO();
  150. }',
  151. ];
  152. yield [
  153. '<?php use Exception as baR;',
  154. '<?php use exception as baR;',
  155. ];
  156. yield [
  157. '<?php try { foo(); } catch(\LogicException $e) {}',
  158. '<?php try { foo(); } catch(\logicexception $e) {}',
  159. ];
  160. yield [
  161. '<?php try { foo(); } catch(LogicException $e) {}',
  162. '<?php try { foo(); } catch(logicexception $e) {}',
  163. ];
  164. yield [
  165. '<?php
  166. Closure::bind(fn () => null, null, new class {});
  167. \Closure::bind(fn () => null, null, new class {});
  168. Foo\Bar::bind(fn () => null, null, new class {});
  169. \A\B\\Bar::bind(fn () => null, null, new class {});
  170. ',
  171. '<?php
  172. CLOSURE::bind(fn () => null, null, new class {});
  173. \CLOSURE::bind(fn () => null, null, new class {});
  174. Foo\Bar::bind(fn () => null, null, new class {});
  175. \A\B\\Bar::bind(fn () => null, null, new class {});
  176. ',
  177. ];
  178. yield [
  179. "<?php
  180. declare(strict_types=1);
  181. use Sonata\\Exporter\\Writer\\EXCEPTION;
  182. \$services->set('sonata.exporter.writer.xml', EXCEPTION::class);
  183. ",
  184. ];
  185. yield [
  186. '<?php
  187. const error = 1;
  188. foo(error);
  189. $b2 = $a[error];
  190. $b21 = [1,error];
  191. $b22 = [error,2];
  192. $b23 = [1,error,2];
  193. $b24 = [1,error,2,];
  194. $b3 = [error];
  195. $b4 = $a->{error};
  196. ',
  197. ];
  198. yield [
  199. '<?php
  200. foo(\error);
  201. $b2 = $a[\error];
  202. $b21 = [1,\error];
  203. $b22 = [\error,2];
  204. $b23 = [1,\error,2];
  205. $b24 = [1,\error,2,];
  206. $b3 = [\error];
  207. $b4 = $a->{\error};
  208. ',
  209. ];
  210. yield ['<?php echo error ?><?php echo error;'];
  211. }
  212. /**
  213. * @dataProvider provideFix81Cases
  214. *
  215. * @requires PHP 8.1
  216. */
  217. public function testFix81(string $expected, ?string $input = null): void
  218. {
  219. $this->doTest($expected, $input);
  220. }
  221. public static function provideFix81Cases(): iterable
  222. {
  223. yield [
  224. '<?php enum exception {}',
  225. ];
  226. yield [
  227. '<?php enum Foo {
  228. case exception;
  229. }',
  230. ];
  231. yield 'multiple type catch with variable' => [
  232. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
  233. '<?php try { foo(); } catch(\INVALIDARGUMENTEXCEPTION|\logicexception $e) {}',
  234. ];
  235. yield 'multiple type catch without variable 3' => [
  236. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
  237. '<?php try { foo(); } catch(\INVALIDARGUMENTEXCEPTION|\logicexception) {}',
  238. ];
  239. }
  240. }