NonPrintableCharacterFixerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\Basic;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Ivan Boprzenkov <ivan.borzenkov@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
  24. */
  25. final class NonPrintableCharacterFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @param _AutogeneratedInputConfiguration $configuration
  29. *
  30. * @dataProvider provideFixCases
  31. */
  32. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  33. {
  34. $this->fixer->configure($configuration);
  35. $this->doTest($expected, $input);
  36. }
  37. /**
  38. * @return iterable<array{string, null|string, array{use_escape_sequences_in_strings: bool}}>
  39. */
  40. public static function provideFixCases(): iterable
  41. {
  42. yield [
  43. '<?php echo "Hello World !";',
  44. '<?php echo "'.pack('H*', 'e2808b').'Hello'.pack('H*', 'e28087').'World'.pack('H*', 'c2a0').'!";',
  45. ['use_escape_sequences_in_strings' => false],
  46. ];
  47. yield [
  48. '<?php echo "Hello World !";',
  49. '<?php echo "'.
  50. pack('H*', 'e2808b').
  51. pack('H*', 'e2808b').
  52. pack('H*', 'e2808b').
  53. pack('H*', 'e2808b').
  54. pack('H*', 'e2808b').
  55. pack('H*', 'e2808b').
  56. 'Hello World !";',
  57. ['use_escape_sequences_in_strings' => false],
  58. ];
  59. yield [
  60. '<?php
  61. // echo
  62. echo "Hello World !";',
  63. '<?php
  64. // ec'.pack('H*', 'e2808b').'ho
  65. echo "Hello'.pack('H*', 'e280af').'World'.pack('H*', 'c2a0').'!";',
  66. ['use_escape_sequences_in_strings' => false],
  67. ];
  68. yield [
  69. '<?php
  70. /**
  71. * @param string $p Param
  72. */
  73. function f(string $p)
  74. {
  75. echo $p;
  76. }',
  77. '<?php
  78. /**
  79. * @param '.pack('H*', 'e2808b').'string $p Param
  80. */
  81. function f(string $p)
  82. {
  83. echo $p;
  84. }',
  85. ['use_escape_sequences_in_strings' => false],
  86. ];
  87. yield [
  88. '<?php echo "$a[0] ${a}";',
  89. '<?php echo "$a'.pack('H*', 'e2808b').'[0]'.pack('H*', 'e2808b').' ${a'.pack('H*', 'e2808b').'}";',
  90. ['use_escape_sequences_in_strings' => false],
  91. ];
  92. yield [
  93. '<?php echo \'12345\';?>abc<?php ?>',
  94. '<?php echo \'123'.pack('H*', 'e2808b').'45\';?>a'.pack('H*', 'e2808b').'bc<?php ?>',
  95. ['use_escape_sequences_in_strings' => false],
  96. ];
  97. yield [
  98. '<?php echo "${foo'.pack('H*', 'c2a0').'bar} is great!";',
  99. null,
  100. ['use_escape_sequences_in_strings' => false],
  101. ];
  102. yield [
  103. '<?php echo $foo'.pack('H*', 'c2a0').'bar;',
  104. null,
  105. ['use_escape_sequences_in_strings' => false],
  106. ];
  107. yield [
  108. '<?php /* foo *'.pack('H*', 'e2808b').'/ bar */',
  109. null,
  110. ['use_escape_sequences_in_strings' => false],
  111. ];
  112. yield [
  113. '<?php /** foo *'.pack('H*', 'e2808b').'/ bar */',
  114. null,
  115. ['use_escape_sequences_in_strings' => false],
  116. ];
  117. yield [
  118. '<?php echo b"Hello World !";',
  119. '<?php echo b"'.pack('H*', 'e2808b').'Hello'.pack('H*', 'e28087').'World'.pack('H*', 'c2a0').'!";',
  120. ['use_escape_sequences_in_strings' => false],
  121. ];
  122. yield [
  123. '<?php
  124. /**
  125. * @param string $p Param
  126. */
  127. function f(string $p)
  128. {
  129. echo $p;
  130. }',
  131. '<?php
  132. /**
  133. * @param '.pack('H*', 'e2808b').'string $p Param
  134. */
  135. function f(string $p)
  136. {
  137. echo $p;
  138. }',
  139. ['use_escape_sequences_in_strings' => true],
  140. ];
  141. yield [
  142. '<?php echo \'FooBar\\\\\';',
  143. null,
  144. ['use_escape_sequences_in_strings' => true],
  145. ];
  146. yield [
  147. '<?php echo "Foo\u{200b}Bar";',
  148. '<?php echo "Foo'.pack('H*', 'e2808b').'Bar";',
  149. ['use_escape_sequences_in_strings' => true],
  150. ];
  151. yield [
  152. '<?php echo "Foo\u{200b}Bar";',
  153. '<?php echo \'Foo'.pack('H*', 'e2808b').'Bar\';',
  154. ['use_escape_sequences_in_strings' => true],
  155. ];
  156. yield [
  157. '<?php echo "Foo\u{200b} Bar \\\n \\\ \$variableToEscape";',
  158. '<?php echo \'Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape\';',
  159. ['use_escape_sequences_in_strings' => true],
  160. ];
  161. yield [
  162. '<?php echo <<<\'TXT\'
  163. FooBar\
  164. TXT;
  165. ',
  166. null,
  167. ['use_escape_sequences_in_strings' => true],
  168. ];
  169. yield [
  170. '<?php echo <<<TXT
  171. Foo\u{200b}Bar
  172. TXT;
  173. ',
  174. '<?php echo <<<TXT
  175. Foo'.pack('H*', 'e2808b').'Bar
  176. TXT;
  177. ',
  178. ['use_escape_sequences_in_strings' => true],
  179. ];
  180. yield [
  181. '<?php echo <<<TXT
  182. Foo\u{200b}Bar
  183. TXT;
  184. ',
  185. '<?php echo <<<\'TXT\'
  186. Foo'.pack('H*', 'e2808b').'Bar
  187. TXT;
  188. ',
  189. ['use_escape_sequences_in_strings' => true],
  190. ];
  191. yield [
  192. '<?php echo <<<TXT
  193. Foo\u{200b} Bar \\\n \\\ \$variableToEscape
  194. TXT;
  195. ',
  196. '<?php echo <<<\'TXT\'
  197. Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape
  198. TXT;
  199. ',
  200. ['use_escape_sequences_in_strings' => true],
  201. ];
  202. yield [
  203. '<?php echo \'。\';',
  204. null,
  205. ['use_escape_sequences_in_strings' => true],
  206. ];
  207. yield [
  208. <<<'EXPECTED'
  209. <?php echo "Double \" quote \u{200b} inside";
  210. EXPECTED,
  211. \sprintf(
  212. <<<'INPUT'
  213. <?php echo 'Double " quote %s inside';
  214. INPUT
  215. ,
  216. pack('H*', 'e2808b')
  217. ),
  218. ['use_escape_sequences_in_strings' => true],
  219. ];
  220. yield [
  221. <<<'EXPECTED'
  222. <?php echo "Single ' quote \u{200b} inside";
  223. EXPECTED,
  224. \sprintf(
  225. <<<'INPUT'
  226. <?php echo 'Single \' quote %s inside';
  227. INPUT
  228. ,
  229. pack('H*', 'e2808b')
  230. ),
  231. ['use_escape_sequences_in_strings' => true],
  232. ];
  233. yield [
  234. <<<'EXPECTED'
  235. <?php echo <<<STRING
  236. Quotes ' and " to be handled \u{200b} properly \\' and \\"
  237. STRING
  238. ;
  239. EXPECTED,
  240. \sprintf(
  241. <<<'INPUT'
  242. <?php echo <<<'STRING'
  243. Quotes ' and " to be handled %s properly \' and \"
  244. STRING
  245. ;
  246. INPUT
  247. ,
  248. pack('H*', 'e2808b')
  249. ),
  250. ['use_escape_sequences_in_strings' => true],
  251. ];
  252. yield [
  253. <<<'EXPECTED'
  254. <?php echo "\\\u{200b}\"";
  255. EXPECTED,
  256. \sprintf(
  257. <<<'INPUT'
  258. <?php echo '\\%s"';
  259. INPUT
  260. ,
  261. pack('H*', 'e2808b')
  262. ),
  263. ['use_escape_sequences_in_strings' => true],
  264. ];
  265. yield [
  266. <<<'EXPECTED'
  267. <?php echo "\\\u{200b}'";
  268. EXPECTED,
  269. \sprintf(
  270. <<<'INPUT'
  271. <?php echo '\\%s\'';
  272. INPUT
  273. ,
  274. pack('H*', 'e2808b')
  275. ),
  276. ['use_escape_sequences_in_strings' => true],
  277. ];
  278. yield [
  279. <<<'EXPECTED'
  280. <?php echo "Backslash 1 \\ \u{200b}";
  281. EXPECTED,
  282. \sprintf(
  283. <<<'INPUT'
  284. <?php echo 'Backslash 1 \ %s';
  285. INPUT
  286. ,
  287. pack('H*', 'e2808b')
  288. ),
  289. ['use_escape_sequences_in_strings' => true],
  290. ];
  291. yield [
  292. <<<'EXPECTED'
  293. <?php echo "Backslash 2 \\ \u{200b}";
  294. EXPECTED,
  295. \sprintf(
  296. <<<'INPUT'
  297. <?php echo 'Backslash 2 \\ %s';
  298. INPUT
  299. ,
  300. pack('H*', 'e2808b')
  301. ),
  302. ['use_escape_sequences_in_strings' => true],
  303. ];
  304. yield [
  305. <<<'EXPECTED'
  306. <?php echo "Backslash 3 \\\\ \u{200b}";
  307. EXPECTED,
  308. \sprintf(
  309. <<<'INPUT'
  310. <?php echo 'Backslash 3 \\\ %s';
  311. INPUT
  312. ,
  313. pack('H*', 'e2808b')
  314. ),
  315. ['use_escape_sequences_in_strings' => true],
  316. ];
  317. yield [
  318. <<<'EXPECTED'
  319. <?php echo "Backslash 4 \\\\ \u{200b}";
  320. EXPECTED,
  321. \sprintf(
  322. <<<'INPUT'
  323. <?php echo 'Backslash 4 \\\\ %s';
  324. INPUT
  325. ,
  326. pack('H*', 'e2808b')
  327. ),
  328. ['use_escape_sequences_in_strings' => true],
  329. ];
  330. yield [
  331. "<?php \"String in single quotes, having non-breaking space: \\u{a0}, linebreak: \n, and single quote inside: ' is a dangerous mix.\";",
  332. "<?php 'String in single quotes, having non-breaking space: ".pack('H*', 'c2a0').", linebreak: \n, and single quote inside: \\' is a dangerous mix.';",
  333. ['use_escape_sequences_in_strings' => true],
  334. ];
  335. }
  336. }