IncludeFixerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. '<?php
  61. require_once "test1.php";
  62. include_once "test2.php";
  63. require "test3.php";
  64. include "test4.php";',
  65. '<?php
  66. require_once("test1.php");
  67. include_once("test2.php");
  68. require("test3.php");
  69. include("test4.php");',
  70. ],
  71. [
  72. '<?php
  73. require_once #1
  74. #2
  75. #3
  76. "test1.php"#4
  77. #5
  78. #6
  79. ;',
  80. '<?php
  81. require_once #1
  82. (#2
  83. #3
  84. "test1.php"#4
  85. )#5
  86. #6
  87. ;',
  88. ],
  89. [
  90. '<?php foo(require "foo.php");',
  91. '<?php foo(require("foo.php"));',
  92. ],
  93. [
  94. '<?php $foo[include "foo.php"];',
  95. '<?php $foo[include("foo.php")];',
  96. ],
  97. ];
  98. foreach (['require', 'require_once', 'include', 'include_once'] as $statement) {
  99. $tests[] = [
  100. sprintf($template.' "foo.php"?>', $statement),
  101. sprintf($template.' ("foo.php") ?>', $statement),
  102. ];
  103. $tests[] = [
  104. sprintf($template.' /**/"foo.php"// test
  105. ?>', $statement),
  106. sprintf($template.'/**/ ("foo.php") // test
  107. ?>', $statement),
  108. ];
  109. $tests[] = [
  110. sprintf($template.' $a;', $statement),
  111. sprintf($template.'$a;', $statement),
  112. ];
  113. $tests[] = [
  114. sprintf($template.' $a;', $statement),
  115. sprintf($template.' $a;', $statement),
  116. ];
  117. $tests[] = [
  118. sprintf($template.' $a; ', $statement),
  119. sprintf($template.' $a ; ', $statement),
  120. ];
  121. $tests[] = [
  122. sprintf($template." /**/'foo.php';", $statement),
  123. sprintf($template."/**/'foo.php';", $statement),
  124. ];
  125. $tests[] = [
  126. sprintf($template." 'foo.php';", $statement),
  127. sprintf($template."'foo.php';", $statement),
  128. ];
  129. $tests[] = [
  130. sprintf($template." 'foo.php';", $statement),
  131. sprintf($template." 'foo.php';", $statement),
  132. ];
  133. $tests[] = [
  134. sprintf($template." 'foo.php';", $statement),
  135. sprintf($template."('foo.php');", $statement),
  136. ];
  137. $tests[] = [
  138. sprintf($template." 'foo.php';", $statement),
  139. sprintf($template."( 'foo.php');", $statement),
  140. ];
  141. $tests[] = [
  142. sprintf($template." 'foo.php';", $statement),
  143. sprintf($template." ( 'foo.php' );", $statement),
  144. ];
  145. $tests[] = [
  146. sprintf($template." '\".__DIR__.\"/../bootstrap.php';", $statement),
  147. ];
  148. $tests[] = [
  149. sprintf('<?php // %s foo', $statement),
  150. ];
  151. $tests[] = [
  152. sprintf('<?php /* %s foo */', $statement),
  153. ];
  154. $tests[] = [
  155. sprintf('<?php /** %s foo */', $statement),
  156. ];
  157. $tests[] = [
  158. sprintf($template.'($a ? $b : $c) . $d;', $statement),
  159. ];
  160. $tests[] = [
  161. sprintf($template.' ($a ? $b : $c) . $d;', $statement),
  162. ];
  163. $tests[] = [
  164. sprintf('<?php exit("POST must %s \"file\"");', $statement),
  165. ];
  166. $tests[] = [
  167. sprintf('<?php ClassCollectionLoader::load(%s($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);', $statement),
  168. ];
  169. $tests[] = [
  170. sprintf('<?php $foo = (false === %s($zfLibraryPath."/Zend/Loader/StandardAutoloader.php"));', $statement),
  171. ];
  172. $tests[] = [
  173. sprintf($template.' "Buzz/foo-Bar.php";', $statement),
  174. sprintf($template.' ( "Buzz/foo-Bar.php" );', $statement),
  175. ];
  176. $tests[] = [
  177. sprintf($template.' "$buzz/foo-Bar.php";', $statement),
  178. sprintf($template.' ( "$buzz/foo-Bar.php" );', $statement),
  179. ];
  180. $tests[] = [
  181. sprintf($template.' "{$buzz}/foo-Bar.php";', $statement),
  182. sprintf($template.' ( "{$buzz}/foo-Bar.php" );', $statement),
  183. ];
  184. $tests[] = [
  185. sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  186. sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  187. ];
  188. $tests[] = [
  189. sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  190. sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  191. ];
  192. $tests[] = [
  193. sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  194. sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  195. ];
  196. $tests[] = [
  197. sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  198. sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  199. ];
  200. $tests[] = [
  201. sprintf("<?php %s __DIR__.('foo.php');", $statement),
  202. sprintf("<?php %s (__DIR__.('foo.php'));", $statement),
  203. ];
  204. $tests[] = [
  205. sprintf("<?php %s __DIR__ . ('foo.php');", $statement),
  206. sprintf("<?php %s (__DIR__ . ('foo.php'));", $statement),
  207. ];
  208. $tests[] = [
  209. sprintf("<?php %s dirname(__FILE__).'foo.php';", $statement),
  210. sprintf("<?php %s (dirname(__FILE__).'foo.php');", $statement),
  211. ];
  212. $tests[] = [
  213. sprintf('<?php %s "foo/".CONSTANT."/bar.php";', $statement),
  214. sprintf('<?php %s("foo/".CONSTANT."/bar.php");', $statement),
  215. ];
  216. $tests[] = [
  217. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; %s "foo/".CONSTANT."/bar.php";', $statement, $statement),
  218. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); %s("foo/".CONSTANT."/bar.php");', $statement, $statement),
  219. ];
  220. $tests[] = [
  221. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; $foo = "bar";', $statement),
  222. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); $foo = "bar";', $statement),
  223. ];
  224. $tests[] = [
  225. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; foo();', $statement),
  226. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); foo();', $statement),
  227. ];
  228. $tests[] = [
  229. sprintf('<?php %s "foo/" . CONSTANT . "/bar.php";', $statement),
  230. sprintf('<?php %s("foo/" . CONSTANT . "/bar.php");', $statement),
  231. ];
  232. $tests[] = [
  233. sprintf('<?php %s SOME_CONST . "file.php"; %s Foo::Bar($baz);', $statement, $statement),
  234. sprintf('<?php %s( SOME_CONST . "file.php" ); %s Foo::Bar($baz);', $statement, $statement),
  235. ];
  236. $tests[] = [
  237. sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
  238. sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
  239. ];
  240. }
  241. return $tests;
  242. }
  243. }