VisibilityFixerTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests\Fixer\PSR2;
  11. use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
  12. class VisibilityFixerTest extends AbstractFixerTestBase
  13. {
  14. public function testFixProperties()
  15. {
  16. $expected = <<<'EOF'
  17. <?php
  18. class Foo {
  19. public $var;
  20. protected $var_foo;
  21. private $FooBar;
  22. public static $var1;
  23. protected static $var_foo2;
  24. private static $FooBar1;
  25. public static $var2;
  26. protected static $var_foo3;
  27. private static $FooBar2;
  28. private static $FooBar3;
  29. public $old = 'foo';
  30. }
  31. EOF;
  32. $input = <<<'EOF'
  33. <?php
  34. class Foo {
  35. public $var;
  36. protected $var_foo;
  37. private $FooBar;
  38. static public $var1;
  39. static protected $var_foo2;
  40. static private $FooBar1;
  41. public static $var2;
  42. protected static $var_foo3;
  43. private static $FooBar2;
  44. private static
  45. $FooBar3;
  46. var $old = 'foo';
  47. }
  48. EOF;
  49. $this->makeTest($expected, $input);
  50. }
  51. public function testFixPropertiesAfterMethod()
  52. {
  53. $expected = <<<'EOF'
  54. <?php
  55. class Foo {
  56. public function aaa() {}
  57. public $bbb;
  58. }
  59. EOF;
  60. $this->makeTest($expected);
  61. }
  62. public function testFixMethods()
  63. {
  64. $expected = <<<'EOF'
  65. <?php
  66. abstract class Foo {
  67. public function& foo1() {}
  68. public function &foo2() {}
  69. protected function foo3() {}
  70. abstract protected function foo4();
  71. private function foo5() {}
  72. final public function foo6() {}
  73. abstract public function foo7();
  74. final public function foo8() {}
  75. abstract public function foo9();
  76. public static function fooA() {}
  77. public static function fooD() {}
  78. final public static function fooE() {}
  79. abstract public function fooF();
  80. public function fooG ($foo) {}
  81. public function fooH() {
  82. static $foo;
  83. $bar = function($baz) {};
  84. }
  85. }
  86. EOF;
  87. $input = <<<'EOF'
  88. <?php
  89. abstract class Foo {
  90. public function& foo1() {}
  91. function &foo2() {}
  92. protected function foo3() {}
  93. protected
  94. abstract function foo4();
  95. private function foo5() {}
  96. final public function foo6() {}
  97. abstract public function foo7();
  98. public final function foo8() {}
  99. public abstract function foo9();
  100. public static function fooA() {}
  101. public static
  102. function fooD() {}
  103. final static function fooE() {}
  104. abstract function fooF();
  105. function fooG ($foo) {}
  106. function fooH() {
  107. static $foo;
  108. $bar = function($baz) {};
  109. }
  110. }
  111. EOF;
  112. $this->makeTest($expected, $input);
  113. }
  114. public function testLeaveFunctionsAlone()
  115. {
  116. $expected = <<<'EOF'
  117. <?php
  118. function foo() {
  119. static $foo;
  120. }
  121. EOF;
  122. $this->makeTest($expected);
  123. }
  124. public function testLeaveFunctionsAloneWithVariablesMatchingOopWords()
  125. {
  126. $expected = <<<'EOF'
  127. <?php
  128. function foo() {
  129. static $class;
  130. $interface = 'foo';
  131. $trait = 'bar';
  132. }
  133. EOF;
  134. $this->makeTest($expected);
  135. }
  136. public function testLeaveFunctionsAloneInsideConditionals()
  137. {
  138. $expected = <<<'EOF'
  139. <?php
  140. if (!function_exists('foo')) {
  141. function foo($arg)
  142. {
  143. return $arg;
  144. }
  145. }
  146. EOF;
  147. $this->makeTest($expected);
  148. }
  149. public function testLeaveFunctionsAloneInsideConditionalsWithOopWordInComment()
  150. {
  151. $expected = <<<'EOF'
  152. <?php
  153. /* class <= this is just a stop-word */
  154. if (!function_exists('foo')) {
  155. function foo($arg)
  156. {
  157. return $arg;
  158. }
  159. }
  160. EOF;
  161. $this->makeTest($expected);
  162. }
  163. public function testLeaveFunctionsAloneWithOopWordInComment()
  164. {
  165. $expected = <<<'EOF'
  166. <?php
  167. /* class */
  168. function foo($arg)
  169. {
  170. return $arg;
  171. }
  172. EOF;
  173. $this->makeTest($expected);
  174. }
  175. public function testLeaveFunctionsAloneOutsideClassesWithOopWordInInlineHtml()
  176. {
  177. $expected = <<<'EOF'
  178. <?php
  179. if (!function_exists('foo')) {
  180. function foo($arg)
  181. {
  182. ?>
  183. <div class="test"></div>
  184. <?php
  185. return $arg;
  186. }
  187. }
  188. EOF;
  189. $this->makeTest($expected);
  190. }
  191. public function testLeaveFunctionsAloneOutsideClassesWithOopWordInStringValue()
  192. {
  193. $expected = <<<'EOF'
  194. <?php
  195. if (!function_exists('foo')) {
  196. function foo($arg)
  197. {
  198. return 'she has class right?';
  199. }
  200. }
  201. EOF;
  202. $this->makeTest($expected);
  203. }
  204. public function testLeaveFunctionsAloneOutsideClassesWithOopWordInFunctionName()
  205. {
  206. $expected = <<<'EOF'
  207. <?php
  208. comment_class();
  209. if (!function_exists('foo')) {
  210. function foo($arg)
  211. {
  212. return $arg;
  213. }
  214. }
  215. EOF;
  216. $this->makeTest($expected);
  217. }
  218. /**
  219. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  220. */
  221. public function testLeaveFunctionsAloneAfterClass()
  222. {
  223. $expected = <<<'EOF'
  224. <?php
  225. class Foo
  226. {
  227. public $foo;
  228. }
  229. if (!function_exists('bar')) {
  230. function bar()
  231. {
  232. return 'bar';
  233. }
  234. }
  235. EOF;
  236. $this->makeTest($expected);
  237. }
  238. /**
  239. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  240. */
  241. public function testCurlyOpenSyntax()
  242. {
  243. $expected = <<<'EOF'
  244. <?php
  245. class Foo
  246. {
  247. private $bar;
  248. public function foo()
  249. {
  250. $foo = "foo";
  251. $fooA = "ab{$foo}cd";
  252. $bar = "bar"; // test if variable after T_CURLY_OPEN is intact
  253. }
  254. }
  255. EOF;
  256. $this->makeTest($expected);
  257. }
  258. /**
  259. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  260. */
  261. public function testDollarOpenCurlyBracesSyntax()
  262. {
  263. $expected = <<<'EOF'
  264. <?php
  265. class Foo {
  266. public function bar()
  267. {
  268. $foo = "foo${width}foo";
  269. $bar = "bar"; // test if variable after T_DOLLAR_OPEN_CURLY_BRACES is intact
  270. }
  271. }
  272. EOF;
  273. $this->makeTest($expected);
  274. }
  275. /**
  276. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  277. */
  278. public function testLeaveJavascriptOutsidePhpAlone()
  279. {
  280. $expected = <<<'EOF'
  281. <?php
  282. function foo()
  283. {
  284. return "foo";
  285. }
  286. ?>
  287. <script type="text/javascript">
  288. function foo(bar) {
  289. alert(bar);
  290. }
  291. </script>
  292. EOF;
  293. $this->makeTest($expected);
  294. }
  295. /**
  296. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  297. */
  298. public function testLeaveJavascriptInStringAlone()
  299. {
  300. $expected = <<<'EOF'
  301. <?php
  302. function registerJS()
  303. {
  304. echo '<script type="text/javascript">
  305. function foo(bar) {
  306. alert(bar);
  307. }
  308. </script>';
  309. }
  310. EOF;
  311. $this->makeTest($expected);
  312. }
  313. /**
  314. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  315. */
  316. public function testLeaveJavascriptInVariableAlone()
  317. {
  318. $expected = <<<'EOF'
  319. <?php
  320. class Foo
  321. {
  322. public function bar()
  323. {
  324. $script = <<<JAVASCRIPT
  325. <script type="text/javascript">
  326. function foo(bar) {
  327. alert(bar);
  328. }
  329. </script>
  330. JAVASCRIPT;
  331. return $script;
  332. }
  333. }
  334. EOF;
  335. $this->makeTest($expected);
  336. }
  337. /**
  338. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  339. */
  340. public function testFixCommaSeparatedProperty()
  341. {
  342. $expected = <<<'EOF'
  343. <?php
  344. class Foo
  345. {
  346. public $foo1;
  347. private $foo2;
  348. protected $bar1, $bar2;
  349. public $baz1 = null, $baz2, $baz3 = false;
  350. public $foo, $bar;
  351. }
  352. EOF;
  353. $input = <<<'EOF'
  354. <?php
  355. class Foo
  356. {
  357. var $foo1;
  358. private $foo2;
  359. protected $bar1, $bar2;
  360. public $baz1 = null, $baz2, $baz3 = false;
  361. var $foo, $bar;
  362. }
  363. EOF;
  364. $this->makeTest($expected, $input);
  365. }
  366. public function testFixesVarDeclarationsWithArrayValue()
  367. {
  368. $expected = <<<'EOF'
  369. <?php
  370. class Foo
  371. {
  372. public $foo1 = 1;
  373. public $foo2a = array('foo');
  374. public $foo2b = ['foo'];
  375. public $foo3a = array('foo', 'bar');
  376. public $foo3b = ['foo', 'bar'];
  377. public $foo4a = '1a', $foo5a = array(1, 2, 3), $foo6a = 10;
  378. public $foo4b = '1b', $foo5b = array(1, 2, 3), $foo6b = 10;
  379. }
  380. EOF;
  381. $input = <<<'EOF'
  382. <?php
  383. class Foo
  384. {
  385. var $foo1 = 1;
  386. var $foo2a = array('foo');
  387. var $foo2b = ['foo'];
  388. var $foo3a = array('foo', 'bar');
  389. var $foo3b = ['foo', 'bar'];
  390. var $foo4a = '1a', $foo5a = array(1, 2, 3), $foo6a = 10;
  391. var $foo4b = '1b', $foo5b = array(1, 2, 3), $foo6b = 10;
  392. }
  393. EOF;
  394. $this->makeTest($expected, $input);
  395. }
  396. }