IncludeFixerTest.php 8.6 KB

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