IncludeFixerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\IncludeFixer>
  22. */
  23. final class IncludeFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  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. yield [
  56. '<?php include $a;',
  57. '<?php include ( $a ) ;',
  58. ];
  59. yield [
  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. yield [
  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. yield [
  90. '<?php foo(require "foo.php");',
  91. '<?php foo(require("foo.php"));',
  92. ];
  93. yield [
  94. '<?php $foo[include "foo.php"];',
  95. '<?php $foo[include("foo.php")];',
  96. ];
  97. $template = '<?php %s';
  98. foreach (['require', 'require_once', 'include', 'include_once'] as $statement) {
  99. yield [
  100. \sprintf($template.' "foo.php"?>', $statement),
  101. \sprintf($template.' ("foo.php") ?>', $statement),
  102. ];
  103. yield [
  104. \sprintf($template.' /**/"foo.php"// test
  105. ?>', $statement),
  106. \sprintf($template.'/**/ ("foo.php") // test
  107. ?>', $statement),
  108. ];
  109. yield [
  110. \sprintf($template.' $a;', $statement),
  111. \sprintf($template.'$a;', $statement),
  112. ];
  113. yield [
  114. \sprintf($template.' $a;', $statement),
  115. \sprintf($template.' $a;', $statement),
  116. ];
  117. yield [
  118. \sprintf($template.' $a; ', $statement),
  119. \sprintf($template.' $a ; ', $statement),
  120. ];
  121. yield [
  122. \sprintf($template." /**/'foo.php';", $statement),
  123. \sprintf($template."/**/'foo.php';", $statement),
  124. ];
  125. yield [
  126. \sprintf($template." 'foo.php';", $statement),
  127. \sprintf($template."'foo.php';", $statement),
  128. ];
  129. yield [
  130. \sprintf($template." 'foo.php';", $statement),
  131. \sprintf($template." 'foo.php';", $statement),
  132. ];
  133. yield [
  134. \sprintf($template." 'foo.php';", $statement),
  135. \sprintf($template."('foo.php');", $statement),
  136. ];
  137. yield [
  138. \sprintf($template." 'foo.php';", $statement),
  139. \sprintf($template."( 'foo.php');", $statement),
  140. ];
  141. yield [
  142. \sprintf($template." 'foo.php';", $statement),
  143. \sprintf($template." ( 'foo.php' );", $statement),
  144. ];
  145. yield [
  146. \sprintf($template." '\".__DIR__.\"/../bootstrap.php';", $statement),
  147. ];
  148. yield [
  149. \sprintf('<?php // %s foo', $statement),
  150. ];
  151. yield [
  152. \sprintf('<?php /* %s foo */', $statement),
  153. ];
  154. yield [
  155. \sprintf('<?php /** %s foo */', $statement),
  156. ];
  157. yield [
  158. \sprintf($template.'($a ? $b : $c) . $d;', $statement),
  159. ];
  160. yield [
  161. \sprintf($template.' ($a ? $b : $c) . $d;', $statement),
  162. ];
  163. yield [
  164. \sprintf('<?php exit("POST must %s \"file\"");', $statement),
  165. ];
  166. yield [
  167. \sprintf('<?php ClassCollectionLoader::load(%s($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);', $statement),
  168. ];
  169. yield [
  170. \sprintf('<?php $foo = (false === %s($zfLibraryPath."/Zend/Loader/StandardAutoloader.php"));', $statement),
  171. ];
  172. yield [
  173. \sprintf($template.' "Buzz/foo-Bar.php";', $statement),
  174. \sprintf($template.' ( "Buzz/foo-Bar.php" );', $statement),
  175. ];
  176. yield [
  177. \sprintf($template.' "$buzz/foo-Bar.php";', $statement),
  178. \sprintf($template.' ( "$buzz/foo-Bar.php" );', $statement),
  179. ];
  180. yield [
  181. \sprintf($template.' "{$buzz}/foo-Bar.php";', $statement),
  182. \sprintf($template.' ( "{$buzz}/foo-Bar.php" );', $statement),
  183. ];
  184. yield [
  185. \sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  186. \sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  187. ];
  188. yield [
  189. \sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
  190. \sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
  191. ];
  192. yield [
  193. \sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  194. \sprintf("<?php return %s __DIR__.'foo.php';", $statement),
  195. ];
  196. yield [
  197. \sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  198. \sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
  199. ];
  200. yield [
  201. \sprintf("<?php %s __DIR__.('foo.php');", $statement),
  202. \sprintf("<?php %s (__DIR__.('foo.php'));", $statement),
  203. ];
  204. yield [
  205. \sprintf("<?php %s __DIR__ . ('foo.php');", $statement),
  206. \sprintf("<?php %s (__DIR__ . ('foo.php'));", $statement),
  207. ];
  208. yield [
  209. \sprintf("<?php %s dirname(__FILE__).'foo.php';", $statement),
  210. \sprintf("<?php %s (dirname(__FILE__).'foo.php');", $statement),
  211. ];
  212. yield [
  213. \sprintf('<?php %s "foo/".CONSTANT."/bar.php";', $statement),
  214. \sprintf('<?php %s("foo/".CONSTANT."/bar.php");', $statement),
  215. ];
  216. yield [
  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. yield [
  221. \sprintf('<?php %s "foo/".CONSTANT."/bar.php"; $foo = "bar";', $statement),
  222. \sprintf('<?php %s("foo/".CONSTANT."/bar.php"); $foo = "bar";', $statement),
  223. ];
  224. yield [
  225. \sprintf('<?php %s "foo/".CONSTANT."/bar.php"; foo();', $statement),
  226. \sprintf('<?php %s("foo/".CONSTANT."/bar.php"); foo();', $statement),
  227. ];
  228. yield [
  229. \sprintf('<?php %s "foo/" . CONSTANT . "/bar.php";', $statement),
  230. \sprintf('<?php %s("foo/" . CONSTANT . "/bar.php");', $statement),
  231. ];
  232. yield [
  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. yield [
  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. yield $statement.': binary string lower case' => [
  241. \sprintf($template." b'foo.php';", $statement),
  242. \sprintf($template."(b'foo.php');", $statement),
  243. ];
  244. yield $statement.': binary string upper case' => [
  245. \sprintf($template." B'foo.php';", $statement),
  246. \sprintf($template."(B'foo.php');", $statement),
  247. ];
  248. }
  249. }
  250. }