LowercaseStaticReferenceFixerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\Casing;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Kuba Werłos <werlos@gmail.com>
  15. *
  16. * @covers \PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer
  17. *
  18. * @internal
  19. */
  20. final class LowercaseStaticReferenceFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. return [
  35. [
  36. '<?php class Foo extends Bar { public function baz() { self::qux(); } }',
  37. '<?php class Foo extends Bar { public function baz() { SELF::qux(); } }',
  38. ],
  39. [
  40. '<?php class Foo extends Bar { public function baz() { static::qux(); } }',
  41. '<?php class Foo extends Bar { public function baz() { STATIC::qux(); } }',
  42. ],
  43. [
  44. '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
  45. '<?php class Foo extends Bar { public function baz() { PARENT::baz(); } }',
  46. ],
  47. [
  48. '<?php class Foo extends Bar { public function baz() { parent::baz(); } }',
  49. '<?php class Foo extends Bar { public function baz() { Parent::baz(); } }',
  50. ],
  51. [
  52. '<?php class Foo extends Bar { public function baz() { return new self(); } }',
  53. '<?php class Foo extends Bar { public function baz() { return new Self(); } }',
  54. ],
  55. [
  56. '<?php class Foo extends Bar { public function baz() { return SelfFoo::FOO; } }',
  57. ],
  58. [
  59. '<?php class Foo extends Bar { public function baz() { return FooSelf::FOO; } }',
  60. ],
  61. [
  62. '<?php class Foo extends Bar { private STATIC $baz; }',
  63. ],
  64. [
  65. '<?php class Foo extends Bar { STATIC private $baz; }',
  66. ],
  67. [
  68. '<?php class Foo extends Bar { public function paRent() {} }',
  69. ],
  70. [
  71. '<?php $foo->Self();',
  72. ],
  73. [
  74. '<?php Foo::Self();',
  75. ],
  76. [
  77. '<?php if ($foo instanceof self) { return true; }',
  78. '<?php if ($foo instanceof Self) { return true; }',
  79. ],
  80. [
  81. '<?php if ($foo instanceof static) { return true; }',
  82. '<?php if ($foo instanceof Static) { return true; }',
  83. ],
  84. [
  85. '<?php if ($foo instanceof parent) { return true; }',
  86. '<?php if ($foo instanceof Parent) { return true; }',
  87. ],
  88. [
  89. '<?php if ($foo instanceof Self\Bar) { return true; }',
  90. ],
  91. [
  92. '<?php if ($foo instanceof MySelf) { return true; }',
  93. ],
  94. [
  95. '<?php class Foo extends Bar { public function baz(self $x) {} }',
  96. '<?php class Foo extends Bar { public function baz(Self $x) {} }',
  97. ],
  98. [
  99. '<?php class Foo extends Bar { public function baz(parent $x) {} }',
  100. '<?php class Foo extends Bar { public function baz(Parent $x) {} }',
  101. ],
  102. [
  103. '<?php class Foo extends Bar { public function baz(MySelf $x) {} }',
  104. ],
  105. [
  106. '<?php class Foo extends Bar { public function baz(Self\Qux $x) {} }',
  107. ],
  108. [
  109. '<?php $a = STATIC function() {};',
  110. ],
  111. [
  112. '<?php class A { public function B() { STATIC $a; echo $a; }}',
  113. ],
  114. [
  115. '<?php class A { public function B() { $collection = $static ? new static($b) : new self(); } }',
  116. '<?php class A { public function B() { $collection = $static ? new STATIC($b) : new self(); } }',
  117. ],
  118. [
  119. '<?php class A { STATIC public function B() {} }',
  120. ],
  121. [
  122. '<?php
  123. $a = function () {
  124. STATIC $B = false;
  125. if ($B) {
  126. echo 1;
  127. }
  128. $B = true;
  129. };
  130. ',
  131. ],
  132. [
  133. '<?php class A { const PARENT = 42; }',
  134. ],
  135. [
  136. '<?php namespace Foo\Parent;',
  137. ],
  138. [
  139. '<?php namespace Parent\Foo;',
  140. ],
  141. ];
  142. }
  143. /**
  144. * @param string $expected
  145. * @param null|string $input
  146. *
  147. * @requires PHP 7.0
  148. * @dataProvider provideFix70Cases
  149. */
  150. public function testFix70($expected, $input = null)
  151. {
  152. $this->doTest($expected, $input);
  153. }
  154. public function provideFix70Cases()
  155. {
  156. return [
  157. [
  158. '<?php class Foo extends Bar { public function baz() : self {} }',
  159. '<?php class Foo extends Bar { public function baz() : Self {} }',
  160. ],
  161. [
  162. '<?php class Foo extends Bar { public function baz() : parent {} }',
  163. '<?php class Foo extends Bar { public function baz() : Parent {} }',
  164. ],
  165. [
  166. '<?php class Foo extends Bar { public function baz() : MySelf {} }',
  167. ],
  168. [
  169. '<?php class Foo extends Bar { public function baz() : Self\Qux {} }',
  170. ],
  171. ];
  172. }
  173. /**
  174. * @param string $expected
  175. * @param null|string $input
  176. *
  177. * @requires PHP 7.1
  178. * @dataProvider provideFix71Cases
  179. */
  180. public function testFix71($expected, $input = null)
  181. {
  182. $this->doTest($expected, $input);
  183. }
  184. public function provideFix71Cases()
  185. {
  186. return [
  187. [
  188. '<?php class Foo extends Bar { public function baz(?self $x) {} }',
  189. '<?php class Foo extends Bar { public function baz(?Self $x) {} }',
  190. ],
  191. [
  192. '<?php class Foo extends Bar { public function baz(?parent $x) {} }',
  193. '<?php class Foo extends Bar { public function baz(?Parent $x) {} }',
  194. ],
  195. [
  196. '<?php class Foo extends Bar { public function baz() : ?self {} }',
  197. '<?php class Foo extends Bar { public function baz() : ?Self {} }',
  198. ],
  199. [
  200. '<?php class Foo extends Bar { public function baz() : ?parent {} }',
  201. '<?php class Foo extends Bar { public function baz() : ?Parent {} }',
  202. ],
  203. [
  204. '<?php class Foo extends Bar { public function baz() : ?MySelf {} }',
  205. ],
  206. [
  207. '<?php class Foo extends Bar { public function baz() : ?Self\Qux {} }',
  208. ],
  209. ];
  210. }
  211. }