123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\Basic;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Ivan Boprzenkov <ivan.borzenkov@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
- */
- final class NonPrintableCharacterFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $configuration = []): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, null|string, array{use_escape_sequences_in_strings: bool}}>
- */
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php echo "Hello World !";',
- '<?php echo "'.pack('H*', 'e2808b').'Hello'.pack('H*', 'e28087').'World'.pack('H*', 'c2a0').'!";',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php echo "Hello World !";',
- '<?php echo "'.
- pack('H*', 'e2808b').
- pack('H*', 'e2808b').
- pack('H*', 'e2808b').
- pack('H*', 'e2808b').
- pack('H*', 'e2808b').
- pack('H*', 'e2808b').
- 'Hello World !";',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php
- // echo
- echo "Hello World !";',
- '<?php
- // ec'.pack('H*', 'e2808b').'ho
- echo "Hello'.pack('H*', 'e280af').'World'.pack('H*', 'c2a0').'!";',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php
- /**
- * @param string $p Param
- */
- function f(string $p)
- {
- echo $p;
- }',
- '<?php
- /**
- * @param '.pack('H*', 'e2808b').'string $p Param
- */
- function f(string $p)
- {
- echo $p;
- }',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php echo "$a[0] ${a}";',
- '<?php echo "$a'.pack('H*', 'e2808b').'[0]'.pack('H*', 'e2808b').' ${a'.pack('H*', 'e2808b').'}";',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php echo \'12345\';?>abc<?php ?>',
- '<?php echo \'123'.pack('H*', 'e2808b').'45\';?>a'.pack('H*', 'e2808b').'bc<?php ?>',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php echo "${foo'.pack('H*', 'c2a0').'bar} is great!";',
- null,
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php echo $foo'.pack('H*', 'c2a0').'bar;',
- null,
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php /* foo *'.pack('H*', 'e2808b').'/ bar */',
- null,
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php /** foo *'.pack('H*', 'e2808b').'/ bar */',
- null,
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php echo b"Hello World !";',
- '<?php echo b"'.pack('H*', 'e2808b').'Hello'.pack('H*', 'e28087').'World'.pack('H*', 'c2a0').'!";',
- ['use_escape_sequences_in_strings' => false],
- ];
- yield [
- '<?php
- /**
- * @param string $p Param
- */
- function f(string $p)
- {
- echo $p;
- }',
- '<?php
- /**
- * @param '.pack('H*', 'e2808b').'string $p Param
- */
- function f(string $p)
- {
- echo $p;
- }',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo \'FooBar\\\\\';',
- null,
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo "Foo\u{200b}Bar";',
- '<?php echo "Foo'.pack('H*', 'e2808b').'Bar";',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo "Foo\u{200b}Bar";',
- '<?php echo \'Foo'.pack('H*', 'e2808b').'Bar\';',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo "Foo\u{200b} Bar \\\n \\\ \$variableToEscape";',
- '<?php echo \'Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape\';',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo <<<\'TXT\'
- FooBar\
- TXT;
- ',
- null,
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo <<<TXT
- Foo\u{200b}Bar
- TXT;
- ',
- '<?php echo <<<TXT
- Foo'.pack('H*', 'e2808b').'Bar
- TXT;
- ',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo <<<TXT
- Foo\u{200b}Bar
- TXT;
- ',
- '<?php echo <<<\'TXT\'
- Foo'.pack('H*', 'e2808b').'Bar
- TXT;
- ',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo <<<TXT
- Foo\u{200b} Bar \\\n \\\ \$variableToEscape
- TXT;
- ',
- '<?php echo <<<\'TXT\'
- Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape
- TXT;
- ',
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- '<?php echo \'。\';',
- null,
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "Double \" quote \u{200b} inside";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo 'Double " quote %s inside';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "Single ' quote \u{200b} inside";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo 'Single \' quote %s inside';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo <<<STRING
- Quotes ' and " to be handled \u{200b} properly \\' and \\"
- STRING
- ;
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo <<<'STRING'
- Quotes ' and " to be handled %s properly \' and \"
- STRING
- ;
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "\\\u{200b}\"";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo '\\%s"';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "\\\u{200b}'";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo '\\%s\'';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "Backslash 1 \\ \u{200b}";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo 'Backslash 1 \ %s';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "Backslash 2 \\ \u{200b}";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo 'Backslash 2 \\ %s';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "Backslash 3 \\\\ \u{200b}";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo 'Backslash 3 \\\ %s';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- <<<'EXPECTED'
- <?php echo "Backslash 4 \\\\ \u{200b}";
- EXPECTED,
- \sprintf(
- <<<'INPUT'
- <?php echo 'Backslash 4 \\\\ %s';
- INPUT
- ,
- pack('H*', 'e2808b')
- ),
- ['use_escape_sequences_in_strings' => true],
- ];
- yield [
- "<?php \"String in single quotes, having non-breaking space: \\u{a0}, linebreak: \n, and single quote inside: ' is a dangerous mix.\";",
- "<?php 'String in single quotes, having non-breaking space: ".pack('H*', 'c2a0').", linebreak: \n, and single quote inside: \\' is a dangerous mix.';",
- ['use_escape_sequences_in_strings' => true],
- ];
- }
- }
|