IncludeFixerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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\ControlStructure;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Саша Стаменковић <umpirsky@gmail.com>
  15. * @author SpacePossum
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\IncludeFixer
  20. */
  21. final class IncludeFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix($expected, $input = null)
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideFixCases()
  34. {
  35. $template = '<?php %s';
  36. $tests = [
  37. [
  38. '<?php include # A
  39. # B
  40. # C
  41. "a"# D
  42. # E
  43. # F
  44. ;# G
  45. # H',
  46. '<?php include# A
  47. (# B
  48. # C
  49. "a"# D
  50. # E
  51. )# F
  52. ;# G
  53. # H',
  54. ],
  55. [
  56. '<?php include $a;',
  57. '<?php include ( $a ) ;',
  58. ],
  59. ];
  60. foreach (['require', 'require_once', 'include', 'include_once'] as $statement) {
  61. $tests[] = [
  62. sprintf($template.' "foo.php"?>', $statement),
  63. sprintf($template.' ("foo.php") ?>', $statement),
  64. ];
  65. $tests[] = [
  66. sprintf($template.' /**/"foo.php"// test
  67. ?>', $statement),
  68. sprintf($template.'/**/ ("foo.php") // test
  69. ?>', $statement),
  70. ];
  71. $tests[] = [
  72. sprintf($template.' $a;', $statement),
  73. sprintf($template.'$a;', $statement),
  74. ];
  75. $tests[] = [
  76. sprintf($template.' $a;', $statement),
  77. sprintf($template.' $a;', $statement),
  78. ];
  79. $tests[] = [
  80. sprintf($template.' $a; ', $statement),
  81. sprintf($template.' $a ; ', $statement),
  82. ];
  83. $tests[] = [
  84. sprintf($template." /**/'foo.php';", $statement),
  85. sprintf($template."/**/'foo.php';", $statement),
  86. ];
  87. $tests[] = [
  88. sprintf($template." 'foo.php';", $statement),
  89. sprintf($template."'foo.php';", $statement),
  90. ];
  91. $tests[] = [
  92. sprintf($template." 'foo.php';", $statement),
  93. sprintf($template." 'foo.php';", $statement),
  94. ];
  95. $tests[] = [
  96. sprintf($template." 'foo.php';", $statement),
  97. sprintf($template."('foo.php');", $statement),
  98. ];
  99. $tests[] = [
  100. sprintf($template." 'foo.php';", $statement),
  101. sprintf($template."( 'foo.php');", $statement),
  102. ];
  103. $tests[] = [
  104. sprintf($template." 'foo.php';", $statement),
  105. sprintf($template." ( 'foo.php' );", $statement),
  106. ];
  107. $tests[] = [
  108. sprintf($template." '\".__DIR__.\"/../bootstrap.php';", $statement),
  109. ];
  110. $tests[] = [
  111. sprintf('<?php // %s foo', $statement),
  112. ];
  113. $tests[] = [
  114. sprintf('<?php /* %s foo */', $statement),
  115. ];
  116. $tests[] = [
  117. sprintf('<?php /** %s foo */', $statement),
  118. ];
  119. $tests[] = [
  120. sprintf($template.'($a ? $b : $c) . $d;', $statement),
  121. ];
  122. $tests[] = [
  123. sprintf($template.' ($a ? $b : $c) . $d;', $statement),
  124. ];
  125. $tests[] = [
  126. sprintf('<?php exit("POST must %s \"file\"");', $statement),
  127. ];
  128. $tests[] = [
  129. sprintf('<?php ClassCollectionLoader::load(%s($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);', $statement),
  130. ];
  131. $tests[] = [
  132. sprintf('<?php $foo = (false === %s($zfLibraryPath."/Zend/Loader/StandardAutoloader.php"));', $statement),
  133. ];
  134. $tests[] = [
  135. sprintf($template.' "Buzz/foo-Bar.php";', $statement),
  136. sprintf($template.' ( "Buzz/foo-Bar.php" );', $statement),
  137. ];
  138. $tests[] = [
  139. sprintf($template.' "$buzz/foo-Bar.php";', $statement),
  140. sprintf($template.' ( "$buzz/foo-Bar.php" );', $statement),
  141. ];
  142. $tests[] = [
  143. sprintf($template.' "{$buzz}/foo-Bar.php";', $statement),
  144. sprintf($template.' ( "{$buzz}/foo-Bar.php" );', $statement),
  145. ];
  146. $tests[] = [
  147. sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  148. sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  149. ];
  150. $tests[] = [
  151. sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  152. sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  153. ];
  154. $tests[] = [
  155. sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  156. sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  157. ];
  158. $tests[] = [
  159. sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  160. sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  161. ];
  162. $tests[] = [
  163. sprintf("<?php %s __DIR__.('foo.php');", $statement),
  164. sprintf("<?php %s (__DIR__.('foo.php'));", $statement),
  165. ];
  166. $tests[] = [
  167. sprintf("<?php %s __DIR__ . ('foo.php');", $statement),
  168. sprintf("<?php %s (__DIR__ . ('foo.php'));", $statement),
  169. ];
  170. $tests[] = [
  171. sprintf("<?php %s dirname(__FILE__).'foo.php';", $statement),
  172. sprintf("<?php %s (dirname(__FILE__).'foo.php');", $statement),
  173. ];
  174. $tests[] = [
  175. sprintf('<?php %s "foo/".CONSTANT."/bar.php";', $statement),
  176. sprintf('<?php %s("foo/".CONSTANT."/bar.php");', $statement),
  177. ];
  178. $tests[] = [
  179. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; %s "foo/".CONSTANT."/bar.php";', $statement, $statement),
  180. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); %s("foo/".CONSTANT."/bar.php");', $statement, $statement),
  181. ];
  182. $tests[] = [
  183. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; $foo = "bar";', $statement),
  184. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); $foo = "bar";', $statement),
  185. ];
  186. $tests[] = [
  187. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; foo();', $statement),
  188. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); foo();', $statement),
  189. ];
  190. $tests[] = [
  191. sprintf('<?php %s "foo/" . CONSTANT . "/bar.php";', $statement),
  192. sprintf('<?php %s("foo/" . CONSTANT . "/bar.php");', $statement),
  193. ];
  194. $tests[] = [
  195. sprintf('<?php %s SOME_CONST . "file.php"; %s Foo::Bar($baz);', $statement, $statement),
  196. sprintf('<?php %s( SOME_CONST . "file.php" ); %s Foo::Bar($baz);', $statement, $statement),
  197. ];
  198. $tests[] = [
  199. sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
  200. sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
  201. ];
  202. }
  203. return $tests;
  204. }
  205. }