NoMixedEchoPrintFixerTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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\Alias;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Sullivan Senechal <soullivaneuh@gmail.com>
  15. * @author SpacePossum
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
  20. */
  21. final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. *
  27. * @dataProvider provideEchoToPrintFixCases
  28. */
  29. public function testFixEchoToPrint($expected, $input = null)
  30. {
  31. $this->fixer->configure(['use' => 'print']);
  32. $this->doTest($expected, $input);
  33. }
  34. public function provideEchoToPrintFixCases()
  35. {
  36. return [
  37. [
  38. '<?php
  39. print "test";
  40. ',
  41. ],
  42. [
  43. '<?php
  44. print ("test");
  45. ',
  46. ],
  47. [
  48. '<?php
  49. print("test");
  50. ',
  51. ],
  52. // `echo` can take multiple parameters (although such usage is rare) while `print` can take only one argument,
  53. // @see https://secure.php.net/manual/en/function.echo.php and @see https://secure.php.net/manual/en/function.print.php
  54. [
  55. '<?php
  56. echo "This ", "string ", "was ", "made ", "with multiple parameters.";
  57. ',
  58. ],
  59. [
  60. '<?php
  61. print "test";
  62. ',
  63. '<?php
  64. echo "test";
  65. ',
  66. ],
  67. [
  68. '<?php
  69. print ("test");
  70. ',
  71. '<?php
  72. echo ("test");
  73. ',
  74. ],
  75. [
  76. '<?php
  77. print("test");
  78. ',
  79. '<?php
  80. echo("test");
  81. ',
  82. ],
  83. [
  84. '<?php
  85. print foo(1, 2);
  86. ',
  87. '<?php
  88. echo foo(1, 2);
  89. ',
  90. ],
  91. [
  92. '<?php
  93. print ["foo", "bar", "baz"][$x];
  94. ',
  95. '<?php
  96. echo ["foo", "bar", "baz"][$x];
  97. ',
  98. ],
  99. [
  100. '<?php
  101. print $foo ? "foo" : "bar";
  102. ',
  103. '<?php
  104. echo $foo ? "foo" : "bar";
  105. ',
  106. ],
  107. [
  108. "<?php print 'foo' ?>...<?php echo 'bar', 'baz' ?>",
  109. "<?php echo 'foo' ?>...<?php echo 'bar', 'baz' ?>",
  110. ],
  111. [
  112. '<?php
  113. if ($foo) {
  114. print "foo";
  115. }
  116. print "bar";
  117. ',
  118. '<?php
  119. if ($foo) {
  120. echo "foo";
  121. }
  122. echo "bar";
  123. ',
  124. ],
  125. [
  126. "<div><?php print 'foo' ?></div>",
  127. "<div><?php echo 'foo' ?></div>",
  128. ],
  129. [
  130. '<?=$foo?>',
  131. ],
  132. ];
  133. }
  134. /**
  135. * @param string $expected
  136. * @param null|string $input
  137. *
  138. * @dataProvider providePrintToEchoFixCases
  139. */
  140. public function testFixPrintToEcho($expected, $input = null)
  141. {
  142. $this->fixer->configure(['use' => 'echo']);
  143. $this->doTest($expected, $input);
  144. }
  145. public function providePrintToEchoFixCases()
  146. {
  147. return [
  148. [
  149. '<?php
  150. echo "test";
  151. ',
  152. ],
  153. [
  154. '<?php
  155. echo ("test");
  156. ',
  157. ],
  158. [
  159. '<?php
  160. echo("test");
  161. ',
  162. ],
  163. // https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/1502#issuecomment-156436229
  164. [
  165. '<?php
  166. ($some_var) ? print "true" : print "false";
  167. ',
  168. ],
  169. // echo has no return value while print has a return value of 1 so it can be used in expressions.
  170. // http://www.w3schools.com/php/php_echo_print.asp
  171. [
  172. '<?php
  173. $ret = print "test";
  174. ',
  175. ],
  176. [
  177. '<?php
  178. @print foo();
  179. ',
  180. ],
  181. [
  182. '<?php
  183. function testFunction() {
  184. return print("test");
  185. }
  186. $a = testFunction();
  187. $b += print($a);
  188. $c=\'\';
  189. $c .= $b.print($a);
  190. $d = print($c) > 0 ? \'a\' : \'b\';
  191. switch(print(\'a\')) {}
  192. if (1 === print($a)) {}
  193. ',
  194. ],
  195. [
  196. '<?php
  197. some_function_call();
  198. echo "test";
  199. ',
  200. '<?php
  201. some_function_call();
  202. print "test";
  203. ',
  204. ],
  205. [
  206. '<?php
  207. echo "test";
  208. ',
  209. '<?php
  210. print "test";
  211. ',
  212. ],
  213. [
  214. '<?php
  215. echo ("test");
  216. ',
  217. '<?php
  218. print ("test");
  219. ',
  220. ],
  221. [
  222. '<?php
  223. echo("test");
  224. ',
  225. '<?php
  226. print("test");
  227. ',
  228. ],
  229. [
  230. '<?php
  231. echo foo(1, 2);
  232. ',
  233. '<?php
  234. print foo(1, 2);
  235. ',
  236. ],
  237. [
  238. '<?php
  239. echo $foo ? "foo" : "bar";
  240. ',
  241. '<?php
  242. print $foo ? "foo" : "bar";
  243. ',
  244. ],
  245. [
  246. '<?php
  247. if ($foo) {
  248. echo "foo";
  249. }
  250. echo "bar";
  251. ',
  252. '<?php
  253. if ($foo) {
  254. print "foo";
  255. }
  256. print "bar";
  257. ',
  258. ],
  259. [
  260. "<div><?php echo 'foo' ?></div>",
  261. "<div><?php print 'foo' ?></div>",
  262. ],
  263. ];
  264. }
  265. public function testDefaultConfig()
  266. {
  267. $this->fixer->configure([]);
  268. $this->assertAttributeSame(T_PRINT, 'candidateTokenType', $this->fixer);
  269. }
  270. /**
  271. * @dataProvider provideWrongConfigCases
  272. *
  273. * @param mixed $wrongConfig
  274. * @param string $expectedMessage
  275. */
  276. public function testWrongConfig($wrongConfig, $expectedMessage)
  277. {
  278. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  279. $this->expectExceptionMessageRegExp($expectedMessage);
  280. $this->fixer->configure($wrongConfig);
  281. }
  282. public function provideWrongConfigCases()
  283. {
  284. return [
  285. [
  286. ['a' => 'b'],
  287. '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#',
  288. ],
  289. [
  290. ['a' => 'b', 'b' => 'c'],
  291. '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#',
  292. ],
  293. [
  294. [1],
  295. '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#',
  296. ],
  297. [
  298. ['use' => '_invalid_'],
  299. '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#',
  300. ],
  301. ];
  302. }
  303. }