IncludeFixerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\ControlStructure;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Саша Стаменковић <umpirsky@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\IncludeFixer
  20. */
  21. final class IncludeFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public function provideFixCases(): array
  31. {
  32. $template = '<?php %s';
  33. $tests = [
  34. [
  35. '<?php include # A
  36. # B
  37. # C
  38. "a"# D
  39. # E
  40. # F
  41. ;# G
  42. # H',
  43. '<?php include# A
  44. (# B
  45. # C
  46. "a"# D
  47. # E
  48. )# F
  49. ;# G
  50. # H',
  51. ],
  52. [
  53. '<?php include $a;',
  54. '<?php include ( $a ) ;',
  55. ],
  56. [
  57. '<?php
  58. require_once "test1.php";
  59. include_once "test2.php";
  60. require "test3.php";
  61. include "test4.php";',
  62. '<?php
  63. require_once("test1.php");
  64. include_once("test2.php");
  65. require("test3.php");
  66. include("test4.php");',
  67. ],
  68. [
  69. '<?php
  70. require_once #1
  71. #2
  72. #3
  73. "test1.php"#4
  74. #5
  75. #6
  76. ;',
  77. '<?php
  78. require_once #1
  79. (#2
  80. #3
  81. "test1.php"#4
  82. )#5
  83. #6
  84. ;',
  85. ],
  86. [
  87. '<?php foo(require "foo.php");',
  88. '<?php foo(require("foo.php"));',
  89. ],
  90. [
  91. '<?php $foo[include "foo.php"];',
  92. '<?php $foo[include("foo.php")];',
  93. ],
  94. ];
  95. foreach (['require', 'require_once', 'include', 'include_once'] as $statement) {
  96. $tests[] = [
  97. sprintf($template.' "foo.php"?>', $statement),
  98. sprintf($template.' ("foo.php") ?>', $statement),
  99. ];
  100. $tests[] = [
  101. sprintf($template.' /**/"foo.php"// test
  102. ?>', $statement),
  103. sprintf($template.'/**/ ("foo.php") // test
  104. ?>', $statement),
  105. ];
  106. $tests[] = [
  107. sprintf($template.' $a;', $statement),
  108. sprintf($template.'$a;', $statement),
  109. ];
  110. $tests[] = [
  111. sprintf($template.' $a;', $statement),
  112. sprintf($template.' $a;', $statement),
  113. ];
  114. $tests[] = [
  115. sprintf($template.' $a; ', $statement),
  116. sprintf($template.' $a ; ', $statement),
  117. ];
  118. $tests[] = [
  119. sprintf($template." /**/'foo.php';", $statement),
  120. sprintf($template."/**/'foo.php';", $statement),
  121. ];
  122. $tests[] = [
  123. sprintf($template." 'foo.php';", $statement),
  124. sprintf($template."'foo.php';", $statement),
  125. ];
  126. $tests[] = [
  127. sprintf($template." 'foo.php';", $statement),
  128. sprintf($template." 'foo.php';", $statement),
  129. ];
  130. $tests[] = [
  131. sprintf($template." 'foo.php';", $statement),
  132. sprintf($template."('foo.php');", $statement),
  133. ];
  134. $tests[] = [
  135. sprintf($template." 'foo.php';", $statement),
  136. sprintf($template."( 'foo.php');", $statement),
  137. ];
  138. $tests[] = [
  139. sprintf($template." 'foo.php';", $statement),
  140. sprintf($template." ( 'foo.php' );", $statement),
  141. ];
  142. $tests[] = [
  143. sprintf($template." '\".__DIR__.\"/../bootstrap.php';", $statement),
  144. ];
  145. $tests[] = [
  146. sprintf('<?php // %s foo', $statement),
  147. ];
  148. $tests[] = [
  149. sprintf('<?php /* %s foo */', $statement),
  150. ];
  151. $tests[] = [
  152. sprintf('<?php /** %s foo */', $statement),
  153. ];
  154. $tests[] = [
  155. sprintf($template.'($a ? $b : $c) . $d;', $statement),
  156. ];
  157. $tests[] = [
  158. sprintf($template.' ($a ? $b : $c) . $d;', $statement),
  159. ];
  160. $tests[] = [
  161. sprintf('<?php exit("POST must %s \"file\"");', $statement),
  162. ];
  163. $tests[] = [
  164. sprintf('<?php ClassCollectionLoader::load(%s($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);', $statement),
  165. ];
  166. $tests[] = [
  167. sprintf('<?php $foo = (false === %s($zfLibraryPath."/Zend/Loader/StandardAutoloader.php"));', $statement),
  168. ];
  169. $tests[] = [
  170. sprintf($template.' "Buzz/foo-Bar.php";', $statement),
  171. sprintf($template.' ( "Buzz/foo-Bar.php" );', $statement),
  172. ];
  173. $tests[] = [
  174. sprintf($template.' "$buzz/foo-Bar.php";', $statement),
  175. sprintf($template.' ( "$buzz/foo-Bar.php" );', $statement),
  176. ];
  177. $tests[] = [
  178. sprintf($template.' "{$buzz}/foo-Bar.php";', $statement),
  179. sprintf($template.' ( "{$buzz}/foo-Bar.php" );', $statement),
  180. ];
  181. $tests[] = [
  182. sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  183. sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  184. ];
  185. $tests[] = [
  186. sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  187. sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  188. ];
  189. $tests[] = [
  190. sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  191. sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  192. ];
  193. $tests[] = [
  194. sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  195. sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  196. ];
  197. $tests[] = [
  198. sprintf("<?php %s __DIR__.('foo.php');", $statement),
  199. sprintf("<?php %s (__DIR__.('foo.php'));", $statement),
  200. ];
  201. $tests[] = [
  202. sprintf("<?php %s __DIR__ . ('foo.php');", $statement),
  203. sprintf("<?php %s (__DIR__ . ('foo.php'));", $statement),
  204. ];
  205. $tests[] = [
  206. sprintf("<?php %s dirname(__FILE__).'foo.php';", $statement),
  207. sprintf("<?php %s (dirname(__FILE__).'foo.php');", $statement),
  208. ];
  209. $tests[] = [
  210. sprintf('<?php %s "foo/".CONSTANT."/bar.php";', $statement),
  211. sprintf('<?php %s("foo/".CONSTANT."/bar.php");', $statement),
  212. ];
  213. $tests[] = [
  214. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; %s "foo/".CONSTANT."/bar.php";', $statement, $statement),
  215. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); %s("foo/".CONSTANT."/bar.php");', $statement, $statement),
  216. ];
  217. $tests[] = [
  218. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; $foo = "bar";', $statement),
  219. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); $foo = "bar";', $statement),
  220. ];
  221. $tests[] = [
  222. sprintf('<?php %s "foo/".CONSTANT."/bar.php"; foo();', $statement),
  223. sprintf('<?php %s("foo/".CONSTANT."/bar.php"); foo();', $statement),
  224. ];
  225. $tests[] = [
  226. sprintf('<?php %s "foo/" . CONSTANT . "/bar.php";', $statement),
  227. sprintf('<?php %s("foo/" . CONSTANT . "/bar.php");', $statement),
  228. ];
  229. $tests[] = [
  230. sprintf('<?php %s SOME_CONST . "file.php"; %s Foo::Bar($baz);', $statement, $statement),
  231. sprintf('<?php %s( SOME_CONST . "file.php" ); %s Foo::Bar($baz);', $statement, $statement),
  232. ];
  233. $tests[] = [
  234. sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
  235. sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
  236. ];
  237. }
  238. return $tests;
  239. }
  240. }