PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Symfony/CS/Tests/Fixer/Symfony/NoEmptyStatementFixerTest.php

http://github.com/fabpot/PHP-CS-Fixer
PHP | 615 lines | 555 code | 18 blank | 42 comment | 5 complexity | 7f3c8e828cfd7dbd0cf88cf0f2f9e583 MD5 | raw file
  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 Symfony\CS\Tests\Fixer\Symfony;
  12. use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
  13. /**
  14. * @author SpacePossum
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. */
  19. final class NoEmptyStatementFixerTest extends AbstractFixerTestBase
  20. {
  21. /**
  22. * @dataProvider provideNoEmptyStatementsCases
  23. */
  24. public function testNoEmptyStatements($expected, $input = null)
  25. {
  26. $this->makeTest($expected, $input);
  27. }
  28. public function provideNoEmptyStatementsCases()
  29. {
  30. return array(
  31. array(
  32. '<?php
  33. abstract class TestClass0 extends Test IMPLEMENTS TestInterface, TestInterface2
  34. {
  35. }
  36. ',
  37. '<?php
  38. abstract class TestClass0 extends Test IMPLEMENTS TestInterface, TestInterface2
  39. {
  40. };
  41. ',
  42. ),
  43. array(
  44. '<?php
  45. abstract class TestClass1 EXTENDS Test implements TestInterface
  46. {
  47. }
  48. ',
  49. '<?php
  50. abstract class TestClass1 EXTENDS Test implements TestInterface
  51. {
  52. };
  53. ',
  54. ),
  55. array(
  56. '<?php
  57. CLASS TestClass2 extends Test
  58. {
  59. }
  60. ',
  61. '<?php
  62. CLASS TestClass2 extends Test
  63. {
  64. };
  65. ',
  66. ),
  67. array(
  68. '<?php
  69. class TestClass3 implements TestInterface1
  70. {
  71. }
  72. ',
  73. '<?php
  74. class TestClass3 implements TestInterface1
  75. {
  76. };
  77. ',
  78. ),
  79. array(
  80. '<?php
  81. class TestClass4
  82. {
  83. }
  84. ',
  85. '<?php
  86. class TestClass4
  87. {
  88. };
  89. ',
  90. ),
  91. array(
  92. '<?php
  93. interface TestInterface1
  94. {
  95. }
  96. ',
  97. '<?php
  98. interface TestInterface1
  99. {
  100. };
  101. ',
  102. ),
  103. array(
  104. '<?php
  105. interface TestExtendingInterface extends TestInterface2, TestInterface3 {
  106. }
  107. ',
  108. '<?php
  109. interface TestExtendingInterface extends TestInterface2, TestInterface3 {
  110. };
  111. ',
  112. ),
  113. array(
  114. '<?php
  115. namespace Two {
  116. $a = 1; {
  117. }
  118. }
  119. ',
  120. '<?php
  121. namespace Two {;;
  122. $a = 1; {
  123. };
  124. }
  125. ',
  126. ),
  127. array(
  128. '<?php
  129. {
  130. '.'
  131. }
  132. echo 1;
  133. ',
  134. '<?php
  135. {
  136. ;
  137. };
  138. echo 1;
  139. ',
  140. ),
  141. array(
  142. '<?php
  143. while($time < $a)
  144. ;
  145. echo "done waiting.";
  146. $b = \Test;
  147. ',
  148. ),
  149. array(
  150. '<?php
  151. if($a>1){
  152. }
  153. ',
  154. '<?php
  155. if($a>1){
  156. };
  157. ',
  158. ),
  159. array(
  160. '<?php
  161. if($a>1) {
  162. } else {
  163. }
  164. ',
  165. '<?php
  166. if($a>1) {
  167. } else {
  168. };
  169. ',
  170. ),
  171. array(
  172. '<?php
  173. try{
  174. }catch (\Exception $e) {
  175. }
  176. ',
  177. '<?php
  178. try{
  179. }catch (\Exception $e) {
  180. };
  181. ',
  182. ),
  183. array(
  184. '<?php ',
  185. '<?php ;',
  186. ),
  187. array(
  188. '<?php
  189. function foo()
  190. {
  191. // a
  192. }
  193. ',
  194. '<?php
  195. function foo()
  196. {
  197. ; // a
  198. }
  199. ',
  200. ),
  201. array(
  202. '<?php function foo(){}',
  203. '<?php function foo(){;;}',
  204. ),
  205. array(
  206. '<?php class Test{}',
  207. '<?php class Test{};',
  208. ),
  209. array(
  210. '<?php
  211. for(;;) {
  212. }
  213. ',
  214. '<?php
  215. for(;;) {
  216. };
  217. ',
  218. ),
  219. array(
  220. '<?php
  221. foreach($a as $b) {
  222. }
  223. foreach($a as $b => $c) {
  224. }
  225. ',
  226. '<?php
  227. foreach($a as $b) {
  228. };
  229. foreach($a as $b => $c) {
  230. };
  231. ',
  232. ),
  233. array(
  234. '<?php
  235. while($a > 1){
  236. }
  237. do {
  238. } while($a>1);
  239. ',
  240. '<?php
  241. while($a > 1){
  242. };
  243. do {
  244. } while($a>1);
  245. ',
  246. ),
  247. array(
  248. '<?php
  249. switch($a) {
  250. default : {echo 1;}
  251. }
  252. ',
  253. '<?php
  254. switch($a) {
  255. default : {echo 1;}
  256. };
  257. ',
  258. ),
  259. array(
  260. '<?php
  261. function test($a, $b) {
  262. }
  263. ',
  264. '<?php
  265. function test($a, $b) {
  266. };
  267. ',
  268. ),
  269. array(
  270. '<?php
  271. function foo($n)
  272. {
  273. '.'
  274. $a = function(){};
  275. $b = function() use ($a) {};
  276. ++${"a"};
  277. switch(fooBar()) {
  278. case 5;{
  279. }
  280. }
  281. return $n->{$o};
  282. }
  283. ',
  284. '<?php
  285. function foo($n)
  286. {
  287. ;
  288. $a = function(){};
  289. $b = function() use ($a) {};
  290. ++${"a"};
  291. switch(fooBar()) {
  292. case 5;{
  293. }
  294. };
  295. return $n->{$o};
  296. };
  297. ',
  298. ),
  299. array(
  300. '<?php
  301. declare(ticks=1) {
  302. // entire script here
  303. }
  304. declare(ticks=1);
  305. ',
  306. '<?php
  307. declare(ticks=1) {
  308. // entire script here
  309. };
  310. declare(ticks=1);
  311. ',
  312. ),
  313. array(
  314. '<?php
  315. namespace A\B\C;
  316. use D;
  317. ',
  318. '<?php
  319. namespace A\B\C;;;;
  320. use D;;;;
  321. ',
  322. ),
  323. array(
  324. '<?php
  325. namespace A\B\C;
  326. use D;
  327. ',
  328. '<?php
  329. namespace A\B\C;
  330. use D;;;;
  331. ',
  332. ),
  333. array(
  334. '<?php
  335. namespace A\B\C;use D;
  336. ',
  337. '<?php
  338. namespace A\B\C;;use D;
  339. ',
  340. ),
  341. array(
  342. '<?php
  343. namespace A\B\C {
  344. }
  345. ',
  346. '<?php
  347. namespace A\B\C {
  348. };
  349. ',
  350. ),
  351. array(
  352. '<?php
  353. namespace A{
  354. }
  355. ',
  356. '<?php
  357. namespace A{
  358. };
  359. ',
  360. ),
  361. array(
  362. '<?php
  363. namespace{
  364. }
  365. ',
  366. '<?php
  367. namespace{
  368. };
  369. ',
  370. ),
  371. );
  372. }
  373. /**
  374. * @dataProvider provide54Cases
  375. * @requires PHP 5.4
  376. */
  377. public function testFix54($expected, $input = null)
  378. {
  379. $this->makeTest($expected, $input);
  380. }
  381. public function provide54Cases()
  382. {
  383. return array(
  384. array(
  385. '<?php
  386. trait TestTrait
  387. {
  388. }
  389. ',
  390. '<?php
  391. trait TestTrait
  392. {
  393. };
  394. ',
  395. ),
  396. );
  397. }
  398. /**
  399. * @dataProvider provide55Cases
  400. * @requires PHP 5.5
  401. */
  402. public function testFix55($expected, $input = null)
  403. {
  404. $this->makeTest($expected, $input);
  405. }
  406. public function provide55Cases()
  407. {
  408. return array(
  409. array(
  410. '<?php
  411. try {
  412. throw new \Exception("a");
  413. } catch (\Exception $e){
  414. //
  415. } finally {
  416. } '.'
  417. ',
  418. '<?php
  419. try {
  420. throw new \Exception("a");
  421. } catch (\Exception $e){
  422. //
  423. } finally {
  424. } ;
  425. ',
  426. ),
  427. );
  428. }
  429. /**
  430. * @dataProvider providePHP7Cases
  431. * @requires PHP 7.0
  432. */
  433. public function testFixPHP7($expected, $input = null)
  434. {
  435. $this->makeTest($expected, $input);
  436. }
  437. public function providePHP7Cases()
  438. {
  439. return array(
  440. array(
  441. '<?php
  442. use function Functional\map;
  443. $a = new class {
  444. public function log($msg)
  445. {
  446. }
  447. };
  448. ',
  449. ),
  450. array(
  451. '<?php
  452. use function Functional\map;
  453. $a = new class extends A {
  454. };
  455. ',
  456. ),
  457. array(
  458. '<?php
  459. use function Functional\map;
  460. $a = new class implements B {
  461. };
  462. ',
  463. ),
  464. array(
  465. '<?php
  466. use function Functional\map;
  467. $a = new class extends A implements B {
  468. };
  469. ',
  470. ),
  471. array(
  472. '<?php
  473. $a = new class extends \A implements B\C {
  474. };
  475. ',
  476. ),
  477. array(
  478. '<?php {{}}',
  479. '<?php {{}};',
  480. ),
  481. );
  482. }
  483. /**
  484. * @dataProvider provideCasesWithShortOpenTag
  485. */
  486. public function testCasesWithShortOpenTag($expected, $input = null)
  487. {
  488. $this->makeTest($expected, $input);
  489. }
  490. public function provideCasesWithShortOpenTag()
  491. {
  492. $cases = array();
  493. /*
  494. * short_open_tag setting is ignored by HHVM
  495. * @see https://github.com/facebook/hhvm/issues/4758
  496. */
  497. if (ini_get('short_open_tag') || defined('HHVM_VERSION')) {
  498. $cases[] =
  499. array(
  500. '<? ',
  501. '<? ;',
  502. );
  503. }
  504. // HHVM parses '<?=' as T_ECHO instead of T_OPEN_TAG_WITH_ECHO
  505. // test the fixer doesn't break anything
  506. if (ini_get('short_open_tag') && !defined('HHVM_VERSION')) {
  507. $cases[] =
  508. array(
  509. '<?= ',
  510. '<?= ;',
  511. );
  512. }
  513. if (count($cases) < 1) {
  514. $this->markTestSkipped('No short tag tests possible.');
  515. }
  516. return $cases;
  517. }
  518. /**
  519. * @dataProvider provideFixMultipleSemicolonsCases
  520. */
  521. public function testFixMultipleSemicolons($expected, $input = null)
  522. {
  523. $this->makeTest($expected, $input);
  524. }
  525. public function provideFixMultipleSemicolonsCases()
  526. {
  527. return array(
  528. array(
  529. '<?php $foo = 2 ; //
  530. '.'
  531. ',
  532. '<?php $foo = 2 ; //
  533. ;
  534. ',
  535. ),
  536. array(
  537. '<?php $foo = 3; /**/ ',
  538. '<?php $foo = 3; /**/; ;',
  539. ),
  540. array(
  541. '<?php $foo = 1;',
  542. '<?php $foo = 1;;;',
  543. ),
  544. array(
  545. '<?php $foo = 4; ',
  546. '<?php $foo = 4;; ;;',
  547. ),
  548. array(
  549. '<?php $foo = 5;
  550. ',
  551. '<?php $foo = 5;;
  552. ;
  553. ;',
  554. ),
  555. array(
  556. '<?php $foo = 6; ',
  557. '<?php $foo = 6;; ',
  558. ),
  559. array(
  560. '<?php for ($i = 7; ; ++$i) {}',
  561. ),
  562. array(
  563. '<?php
  564. switch($a){
  565. case 8;
  566. echo 9;
  567. }
  568. ',
  569. '<?php
  570. switch($a){
  571. case 8;;
  572. echo 9;
  573. }
  574. ',
  575. ),
  576. );
  577. }
  578. }