repo stringlengths 7 63 | file_url stringlengths 81 284 | file_path stringlengths 5 200 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 15:02:33 2026-01-05 05:24:06 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocNoUselessInheritdocFixerTest.php | tests/Fixer/Phpdoc/PhpdocNoUselessInheritdocFixerTest.php | <?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\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoUselessInheritdocFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocNoUselessInheritdocFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocNoUselessInheritdocFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string, 2?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
"<?php\n/** */class min1{}",
"<?php\n/** @inheritdoc */class min1{}",
];
yield [
"<?php\n/** */class min1{}",
"<?php\n/** @inheritDoc */class min1{}",
];
yield [
"<?php\nclass min2{/** */}",
"<?php\nclass min2{/** @inheritdoc */}",
];
yield [
"<?php\nclass min2{/** */}",
"<?php\nclass min2{/** @inheritDoc */}",
];
yield [
'<?php
class A
{
/** */
public function A(){}
/**
* '.'
*/
public function B(){}
/**
* Descr.
*
* @param int $c
* '.'
*/
public function C($c){}
}
',
'<?php
class A
{
/** @inheritdoc */
public function A(){}
/**
* @inheritdoc
*/
public function B(){}
/**
* Descr.
*
* @param int $c
* @inheritdoc
*/
public function C($c){}
}
',
];
yield [
'<?php
class B
{
/** */
public function B(){}
}
',
'<?php
class B
{
/** {@INHERITDOC} */
public function B(){}
}
',
];
yield [
'<?php
/** D C */
class C
{
}
',
'<?php
/** D { @INHERITDOC } C */
class C
{
}
',
];
yield [
'<?php
/** E */
class E
{
}
',
'<?php
/** {{@Inheritdoc}} E */
class E
{
}
',
];
yield [
'<?php
/** F */
class F
{
}
',
'<?php
/** F @inheritdoc */
class F
{
}
',
];
yield [
'<?php
/** */
class G1{}
/** */
class G2{}
',
'<?php
/** @inheritdoc */
class G1{}
/** @inheritdoc */
class G2{}
',
];
yield [
'<?php
class H
{
/* @inheritdoc comment, not PHPDoc */
public function H(){}
}
',
];
yield [
'<?php
class J extends Z
{
/** @inheritdoc */
public function H(){}
}
',
];
yield [
'<?php
interface K extends Z
{
/** @inheritdoc */
public function H();
}
',
];
yield [
'<?php
/** */
interface K
{
/** */
public function H();
}
',
'<?php
/** @{inheritdoc} */
interface K
{
/** {@Inheritdoc} */
public function H();
}
',
];
yield [
'<?php
trait T
{
/** @inheritdoc */
public function T()
{
}
}',
];
yield [
'<?php
class B
{
/** */
public function falseImportFromTrait()
{
}
}
/** */
class A
{
use T;
/** @inheritdoc */
public function importFromTrait()
{
}
}
',
'<?php
class B
{
/** @inheritdoc */
public function falseImportFromTrait()
{
}
}
/** @inheritdoc */
class A
{
use T;
/** @inheritdoc */
public function importFromTrait()
{
}
}
',
];
yield [
'<?php
/** delete 1 */
class A
{
/** delete 2 */
public function B()
{
$a = new class implements I {
/** @inheritdoc keep */
public function A()
{
$b = new class extends D {
/** @inheritdoc keep */
public function C()
{
$d = new class() {
/** delete 3 */
public function D()
{
}
};
}
};
}
};
}
/** delete 4 */
public function B1()
{
$a1 = new class(){ };
}
/** delete 5 */
public function B2()
{
//$a1 = new class(){ use D; };
}
}
',
'<?php
/** @inheritdoc delete 1 */
class A
{
/** @inheritdoc delete 2 */
public function B()
{
$a = new class implements I {
/** @inheritdoc keep */
public function A()
{
$b = new class extends D {
/** @inheritdoc keep */
public function C()
{
$d = new class() {
/** @inheritdoc delete 3 */
public function D()
{
}
};
}
};
}
};
}
/** @inheritdoc delete 4 */
public function B1()
{
$a1 = new class(){ };
}
/** @inheritdoc delete 5 */
public function B2()
{
//$a1 = new class(){ use D; };
}
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTagCasingFixerTest.php | tests/Fixer/Phpdoc/PhpdocTagCasingFixerTest.php | <?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\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTagCasingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTagCasingFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocTagCasingFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTagCasingFixerTest 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<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php /** @inheritDoc */',
'<?php /** @inheritdoc */',
];
yield [
'<?php /** @inheritDoc */',
'<?php /** @inheritdoc */',
['tags' => ['inheritDoc']],
];
yield [
'<?php /** @inheritdoc */',
'<?php /** @inheritDoc */',
['tags' => ['inheritdoc']],
];
yield [
'<?php /** {@inheritDoc} */',
'<?php /** {@inheritdoc} */',
];
yield [
'<?php /** {@inheritDoc} */',
'<?php /** {@inheritdoc} */',
['tags' => ['inheritDoc']],
];
yield [
'<?php /** {@inheritdoc} */',
'<?php /** {@inheritDoc} */',
['tags' => ['inheritdoc']],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocSingleLineVarSpacingFixerTest.php | tests/Fixer/Phpdoc/PhpdocSingleLineVarSpacingFixerTest.php | <?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\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocSingleLineVarSpacingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocSingleLineVarSpacingFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocSingleLineVarSpacingFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
class A {
/** @var MyCass6 $a */
public $test6 = 6;
/** @var MyCass6 */
public $testB = 7;
}
',
'<?php
class A {
/**@var MyCass6 $a */
public $test6 = 6;
/**@var MyCass6*/
public $testB = 7;
}
',
];
yield [
'<?php
/** @var MyCass1 $test1 description and more. */
$test0 = 1;
/** @var MyCass2 description and such. */
$test1 = 2;
/** @var MyCass3 description. */
$test2 = 3;
class A {
/** @var MyCass4 aa */
public $test4 = 4;
/** @var MyCass5 */
public $test5 = 5;
/** @var MyCass6 */
public $test6 = 6;
}
',
'<?php
/** @var MyCass1 $test1 description and more.*/
$test0 = 1;
/** @var MyCass2 description and such. */
$test1 = 2;
/** @var MyCass3 description. */
$test2 = 3;
class A {
/** @var MyCass4 aa */
public $test4 = 4;
/** @var MyCass5 */
public $test5 = 5;
/** @var MyCass6*/
public $test6 = 6;
}
',
];
yield [
'<?php
class A
{
/**
* @param array $options {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/
public function __construct(array $options = array())
{
}
/**
* @var bool $required Whether this element is required
* @var string $label The display name for this element
*/
public function test($required, $label)
{
}
/** @var MyCass3
*/
private $test0 = 0;
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocNoAccessFixerTest.php | tests/Fixer/Phpdoc/PhpdocNoAccessFixerTest.php | <?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\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoAccessFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocNoAccessFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocNoAccessFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'access' => [
<<<'PHP'
<?php
/**
*/
PHP,
<<<'PHP'
<?php
/**
* @access public
*/
PHP,
];
yield 'many' => [
<<<'PHP'
<?php
/**
* Hello!
* @notaccess bar
*/
PHP,
<<<'PHP'
<?php
/**
* Hello!
* @access private
* @notaccess bar
* @access foo
*/
PHP,
];
yield 'do nothing' => [
<<<'PHP'
<?php
/**
* @var access
*/
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocIndentFixerTest.php | tests/Fixer/Phpdoc/PhpdocIndentFixerTest.php | <?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\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocIndentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocIndentFixer>
*
* @author Ceeram <ceeram@cakephp.org>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocIndentFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php /** @var Foo $foo */ ?>'];
yield ['<?php /** foo */'];
yield [
'<?php
/**
* Do not indent
*/
/**
* Do not indent this
*/
class DocBlocks
{
/**
*Test that attribute docblocks are indented
*/
protected $indent = false;
/**
* Test that method docblocks are indented.
*/
public function test() {}
}',
'<?php
/**
* Do not indent
*/
/**
* Do not indent this
*/
class DocBlocks
{
/**
*Test that attribute docblocks are indented
*/
protected $indent = false;
/**
* Test that method docblocks are indented.
*/
public function test() {}
}',
];
yield [
'<?php
class DocBlocks
{
/**
* Test constants
*/
const INDENT = 1;
/**
* Test with var keyword
*/
var $oldStyle = false;
/**
* Test static
*/
public static function test1() {}
/**
* Test static first.
*/
static public function test2() {}
/**
* Test final first.
*/
final public function test3() {}
/**
* Test no keywords
*/
function test4() {}
}',
'<?php
class DocBlocks
{
/**
* Test constants
*/
const INDENT = 1;
/**
* Test with var keyword
*/
var $oldStyle = false;
/**
* Test static
*/
public static function test1() {}
/**
* Test static first.
*/
static public function test2() {}
/**
* Test final first.
*/
final public function test3() {}
/**
* Test no keywords
*/
function test4() {}
}',
];
yield [
'<?php
/**
* Final class should also not be indented
*/
final class DocBlocks
{
/**
* Test with var keyword
*/
var $oldStyle = false;
}',
'<?php
/**
* Final class should also not be indented
*/
final class DocBlocks
{
/**
* Test with var keyword
*/
var $oldStyle = false;
}',
];
yield [
'<?php
if (1) {
class Foo {
/**
* Foo
*/
function foo() {}
/**
* Bar
*/
function bar() {}
}
}',
'<?php
if (1) {
class Foo {
/**
* Foo
*/
function foo() {}
/**
* Bar
*/
function bar() {}
}
}',
];
yield [
'<?php
/**
* Variable
*/
$variable = true;
/**
* Partial docblock fix
*/
$partialFix = true;
/**
* Other partial docblock fix
*/
$otherPartial = true;
/** Single line */
$single = true;
/**
* Function
*/
function something()
{
/**
* Inside functions
*/
return;
}
/**
* function call
*/
something();
/**
* Control structure
* @var \Sqlite3 $sqlite
*/
foreach($connections as $sqlite) {
$sqlite->open();
}',
'<?php
/**
* Variable
*/
$variable = true;
/**
* Partial docblock fix
*/
$partialFix = true;
/**
* Other partial docblock fix
*/
$otherPartial = true;
/** Single line */
$single = true;
/**
* Function
*/
function something()
{
/**
* Inside functions
*/
return;
}
/**
* function call
*/
something();
/**
* Control structure
* @var \Sqlite3 $sqlite
*/
foreach($connections as $sqlite) {
$sqlite->open();
}',
];
yield [
'<?php
$user = $event->getForm()->getData(); /** @var User $user */
echo "Success";',
];
yield [
'<?php
$user = $event->getForm()->getData();/** @var User $user */
echo "Success";',
];
yield [
"<?php
class DocBlocks
{
\t/**
\t *Test that attribute docblocks are indented
\t */
\tprotected \$indent = false;
\t/**
\t * Test that method docblocks are indented.
\t */
\tpublic function test() {}
}",
"<?php
class DocBlocks
{
/**
*Test that attribute docblocks are indented
*/
\tprotected \$indent = false;
/**
* Test that method docblocks are indented.
*/
\tpublic function test() {}
}",
];
yield [
'<?php
/**
* Used to write a value to a session key.
*
* ...
*/
function write($name) {}
',
"<?php
\t/**
* Used to write a value to a session key.
*
* ...
*/
function write(\$name) {}
",
];
yield [
'<?php
class Foo
{
public function bar()
{
/**
* baz
*/
}
}',
];
yield [
'<?php
/**
* docs
*/
// comment
$foo = $bar;
',
];
yield [
'<?php
function foo()
{
$foo->bar(/** oops */$baz);
$foo->bar($a,/** oops */$baz);
}',
];
yield [
'<?php
/**
* Foo
Bar
*/
class Foo
{
}',
];
yield [
'<?php
class Application
{
}/**
*/
class Dispatcher
{
}
',
];
yield [
'<?php
$foo->bar() /** comment */
->baz();
',
'<?php
$foo->bar()/** comment */
->baz();
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTypesFixerTest.php | tests/Fixer/Phpdoc/PhpdocTypesFixerTest.php | <?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\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractPhpdocTypesFixer
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTypesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'windows line breaks' => [
"<?php /**\r\n * @param string|string[] \$bar\r\n *\r\n * @return int[]\r\n */\r\n",
"<?php /**\r\n * @param STRING|String[] \$bar\r\n *\r\n * @return inT[]\r\n */\r\n",
];
yield 'conversion' => [
'<?php
/**
* @param boolean|array|Foo $bar
*
* @return int|float
*/
',
'<?php
/**
* @param Boolean|Array|Foo $bar
*
* @return inT|Float
*/
',
];
yield 'nullable' => [
'<?php
/** @return ?int */',
'<?php
/** @return ?inT */',
];
yield 'array stuff' => [
'<?php
/**
* @param string|string[] $bar
*
* @return int[]
*/
',
'<?php
/**
* @param STRING|String[] $bar
*
* @return inT[]
*/
',
];
yield 'nested array stuff' => [
'<?php
/**
* @return int[][][]
*/
',
'<?php
/**
* @return INT[][][]
*/
',
];
yield 'mixed and void' => [
'<?php
/**
* @param mixed $foo
*
* @return void
*/
',
'<?php
/**
* @param Mixed $foo
*
* @return Void
*/
',
];
yield 'iterable' => [
'<?php
/**
* @param iterable $foo
*
* @return Itter
*/
',
'<?php
/**
* @param Iterable $foo
*
* @return Itter
*/
',
];
yield 'method and property' => [
'<?php
/**
* @method self foo()
* @property int $foo
* @property-read boolean $bar
* @property-write mixed $baz
*/
',
'<?php
/**
* @method Self foo()
* @property Int $foo
* @property-read Boolean $bar
* @property-write MIXED $baz
*/
',
];
yield 'throws' => [
'<?php
/**
* @throws static
*/
',
'<?php
/**
* @throws STATIC
*/
',
];
yield 'inline doc' => [
'<?php
/**
* Does stuff with stuffs.
*
* @param array $stuffs {
* @var bool $foo
* @var int $bar
* }
*/
',
'<?php
/**
* Does stuff with stuffs.
*
* @param array $stuffs {
* @var Bool $foo
* @var INT $bar
* }
*/
',
];
yield 'with config' => [
'<?php
/**
* @param self|array|Foo $bar
*
* @return int|float|boolean|Double
*/
',
'<?php
/**
* @param SELF|Array|Foo $bar
*
* @return inT|Float|boolean|Double
*/
',
['groups' => ['simple', 'meta']],
];
yield 'generics' => [
'<?php
/**
* @param array<int, object> $a
* @param array<iterable> $b
* @param array<parent|$this|self> $c
* @param iterable<Foo\Int\Bar|Foo\Int|Int\Bar> $thisShouldNotBeChanged
* @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
*
* @return array<int, array<string, array<int, DoNotChangeThisAsThisIsAClass>>>
*/',
'<?php
/**
* @param ARRAY<INT, OBJECT> $a
* @param ARRAY<ITERABLE> $b
* @param array<Parent|$This|Self> $c
* @param iterable<Foo\Int\Bar|Foo\Int|Int\Bar> $thisShouldNotBeChanged
* @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
*
* @return ARRAY<INT, ARRAY<STRING, ARRAY<INT, DoNotChangeThisAsThisIsAClass>>>
*/',
['groups' => ['simple', 'meta']],
];
yield 'callable' => [
'<?php /**
* @param callable() $a
* @param callable(): void $b
* @param callable(bool, int, string): float $c
*/',
'<?php /**
* @param CALLABLE() $a
* @param Callable(): VOID $b
* @param CALLABLE(BOOL, INT, STRING): FLOAT $c
*/',
];
yield 'array shape with key name being also type name' => [
'<?php /**
* @return array{FOO: bool, NULL: null|int, BAR: string|BAZ}
*/',
'<?php /**
* @return ARRAY{FOO: BOOL, NULL: NULL|INT, BAR: STRING|BAZ}
*/',
];
yield 'union with \'NULL\'' => [
'<?php /**
* @return \'NULL\'|null|false
*/',
'<?php /**
* @return \'NULL\'|NULL|false
*/',
];
yield 'union with "NULL"' => [
'<?php /**
* @return null|"NULL"|false
*/',
'<?php /**
* @return NULL|"NULL"|false
*/',
];
yield 'method with reserved identifier' => [
'<?php /**
* @method bool BOOL(): void
*/',
'<?php /**
* @method BOOL BOOL(): void
*/',
];
yield 'no space between type and variable' => [
'<?php /** @param null|string$foo */',
'<?php /** @param NULL|STRING$foo */',
];
yield '"Callback" class in phpdoc must not be lowered' => [
'<?php
/**
* @param Callback $foo
*
* @return Callback
*/
',
];
yield 'param with extra chevrons' => [
'<?php /** @param array <3> $value */',
'<?php /** @param ARRAY <3> $value */',
];
yield 'param with extra parentheses' => [
'<?php /** @param \Closure (int) $value */',
'<?php /** @param \Closure (INT) $value */',
];
yield 'param with union type and extra parentheses' => [
'<?php /** @param \Closure (float|int) $value */',
'<?php /** @param \Closure (FLOAT|INT) $value */',
];
yield 'return with union type and extra parentheses' => [
'<?php /** @return float|int (number) count of something */',
'<?php /** @return FLOAT|INT (number) count of something */',
];
yield 'multiline array' => [
<<<'PHP'
<?php /**
* @param array<
* int,
* string
* > $x
*
* @param array<
* bool
* > $y
*/
PHP,
<<<'PHP'
<?php /**
* @param array<
* INT,
* STRING
* > $x
*
* @param array<
* BOOL
* > $y
*/
PHP,
];
yield 'multiline array shape' => [
<<<'PHP'
<?php /**
* @param array{
* foo: bool,
* bar: int,
* withoutTrailingComma: string
* } $x
*
* @param array{
* withTrailingComma: bool,
* } $y
*/
PHP,
<<<'PHP'
<?php /**
* @param array{
* foo: BOOL,
* bar: INT,
* withoutTrailingComma: STRING
* } $x
*
* @param array{
* withTrailingComma: BOOL,
* } $y
*/
PHP,
];
}
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('/^\[phpdoc_types\] Invalid configuration: The option "groups" .*\.$/');
$this->fixer->configure(['groups' => ['__TEST__']]); // @phpstan-ignore-line
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ReturnNotation/ReturnAssignmentFixerTest.php | tests/Fixer/ReturnNotation/ReturnAssignmentFixerTest.php | <?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\ReturnNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ReturnAssignmentFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
function A($a0,$a1,$a2,$d)
{
if ($a0) {
return 1;
// fix me
}
if ($a1) {
return 2;
// fix me
}
$nested0 = function() {
global $a;
++$a;
$d = 2;
$nested1 = function () use ($d) {
if ($d) {
return 3;
// fix me
}
$nested2 = function (&$d) {
if ($d) {
$f = 1;
return $f; // fix me not
}
$d = function () {
return 4;
// fix me
};
if ($d+1) {
$f = 1;
return $f; // fix me not
}
};
return $nested2();
};
return $a; // fix me not
};
if ($a2) {
return 5;
// fix me
}
}
function B($b0, $b1, $b2)
{
if ($b0) {
return 10;
// fix me
}
if ($b1) {
return 20;
// fix me
}
if ($b2) {
return 30;
// fix me
}
}
',
'<?php
function A($a0,$a1,$a2,$d)
{
if ($a0) {
$b = 1;
return $b; // fix me
}
if ($a1) {
$c = 2;
return $c; // fix me
}
$nested0 = function() {
global $a;
++$a;
$d = 2;
$nested1 = function () use ($d) {
if ($d) {
$f = 3;
return $f; // fix me
}
$nested2 = function (&$d) {
if ($d) {
$f = 1;
return $f; // fix me not
}
$d = function () {
$a = 4;
return $a; // fix me
};
if ($d+1) {
$f = 1;
return $f; // fix me not
}
};
return $nested2();
};
return $a; // fix me not
};
if ($a2) {
$d = 5;
return $d; // fix me
}
}
function B($b0, $b1, $b2)
{
if ($b0) {
$b = 10;
return $b; // fix me
}
if ($b1) {
$c = 20;
return $c; // fix me
}
if ($b2) {
$d = 30;
return $d; // fix me
}
}
',
];
yield [
'<?php
function A()
{
return 15;
}
',
'<?php
function A()
{
$a = 15;
return $a;
}
',
];
yield [
'<?php
function A()
{
/*0*/return /*1*//*2*/15;/*3*//*4*/ /*5*/ /*6*//*7*//*8*/
}
',
'<?php
function A()
{
/*0*/$a/*1*/=/*2*/15;/*3*//*4*/ /*5*/ return/*6*/$a/*7*/;/*8*/
}
',
];
yield 'comments with leading space' => [
'<?php
function A()
{ #1
#2
return #3
#4
#5
#6
15 #7
; #8
#9
#10
#11
#12
#13
#14
#15
}
',
'<?php
function A()
{ #1
#2
$a #3
#4
= #5
#6
15 #7
; #8
#9
return #10
#11
$a #12
#13
; #14
#15
}
',
];
yield [
'<?php
abstract class B
{
abstract protected function Z();public function A()
{
return 16;
}
}
',
'<?php
abstract class B
{
abstract protected function Z();public function A()
{
$a = 16; return $a;
}
}
',
];
yield [
'<?php
function b() {
if ($c) {
return 0;
}
return testFunction(654+1);
}
',
'<?php
function b() {
if ($c) {
$b = 0;
return $b;
}
$a = testFunction(654+1);
return $a;
}
',
];
yield 'minimal notation' => [
'<?php $e=function(){return 1;};$f=function(){return 1;};$g=function(){return 1;};',
'<?php $e=function(){$a=1;return$a;};$f=function(){$a=1;return$a;};$g=function(){$a=1;return$a;};',
];
yield [
'<?php
function A()
{#1
#2 '.'
return #3
#4
#5
#6
15#7
;#8
#9
#10
#11
#12
#13
#14
#15
}
',
'<?php
function A()
{#1
#2 '.'
$a#3
#4
=#5
#6
15#7
;#8
#9
return#10
#11
$a#12
#13
;#14
#15
}
',
];
yield [
'<?php
function A($b)
{
// Comment
return a("2", 4, $b);
}
',
'<?php
function A($b)
{
// Comment
$value = a("2", 4, $b);
return $value;
}
',
];
yield [
'<?php function a($b,$c) {if($c>1){echo 1;} return (1 + 2 + $b); }',
'<?php function a($b,$c) {if($c>1){echo 1;} $a= (1 + 2 + $b);return $a; }',
];
yield [
'<?php function a($b,$c) {return (3 * 4 + $b); }',
'<?php function a($b,$c) {$zz= (3 * 4 + $b);return $zz; }',
];
yield [
'<?php
function a() {
return 4563;
?> <?php
}
',
'<?php
function a() {
$a = 4563;
return $a ?> <?php
}
',
];
yield [
'<?php
function a()
{
return $c + 1; /*
var names are case-insensitive */ }
',
'<?php
function a()
{
$A = $c + 1; /*
var names are case-insensitive */ return $a ;}
',
];
yield [
'<?php
function A()
{
return $f[1]->a();
}
',
'<?php
function A()
{
$a = $f[1]->a();
return $a;
}
',
];
yield [
'<?php
function a($foos) {
return array_map(function ($foo) {
return (string) $foo;
}, $foos);
}',
'<?php
function a($foos) {
$bars = array_map(function ($foo) {
return (string) $foo;
}, $foos);
return $bars;
}',
];
yield [
'<?php
function a($foos) {
return ($foos = [\'bar\']);
}',
'<?php
function a($foos) {
$bars = ($foos = [\'bar\']);
return $bars;
}',
];
yield [
'<?php
function a($foos) {
return (function ($foos) {
return $foos;
})($foos);
}',
'<?php
function a($foos) {
$bars = (function ($foos) {
return $foos;
})($foos);
return $bars;
}',
];
yield 'anonymous classes' => [
'<?php
function A()
{
return new class {};
}
function B()
{
return new class() {};
}
function C()
{
return new class(1,2) { public function Z(Foo $d){} };
}
function D()
{
return new class extends Y {};
}
function E()
{
return new class extends Y implements A\O,P {};
}
',
'<?php
function A()
{
$a = new class {};
return $a;
}
function B()
{
$b = new class() {};
return $b;
}
function C()
{
$c = new class(1,2) { public function Z(Foo $d){} };
return $c;
}
function D()
{
$c = new class extends Y {};
return $c;
}
function E()
{
$c = new class extends Y implements A\O,P {};
return $c;
}
',
];
yield 'lambda' => [
'<?php
function A()
{
return function () {};
}
function B()
{
return function ($a, $b) use ($z) {};
}
function C()
{
return static function ($a, $b) use ($z) {};
}
function D()
{
return function &() use(&$b) {
return $b; // do not fix
};
// fix
}
function E()
{
return function &() {
$z = new A(); return $z; // do not fix
};
// fix
}
function A99()
{
$v = static function ($a, $b) use ($z) {};
return 15;
}
',
'<?php
function A()
{
$a = function () {};
return $a;
}
function B()
{
$b = function ($a, $b) use ($z) {};
return $b;
}
function C()
{
$c = static function ($a, $b) use ($z) {};
return $c;
}
function D()
{
$a = function &() use(&$b) {
return $b; // do not fix
};
return $a; // fix
}
function E()
{
$a = function &() {
$z = new A(); return $z; // do not fix
};
return $a; // fix
}
function A99()
{
$v = static function ($a, $b) use ($z) {};
$a = 15;
return $a;
}
',
];
yield 'arrow functions' => [
'<?php
function Foo() {
return fn($x) => $x + $y;
}
',
'<?php
function Foo() {
$fn1 = fn($x) => $x + $y;
return $fn1;
}
',
];
yield 'try catch' => [
'<?php
function foo()
{
if (isSomeCondition()) {
return getSomeResult();
}
try {
$result = getResult();
return $result;
} catch (\Throwable $exception) {
baz($result ?? null);
}
}
',
'<?php
function foo()
{
if (isSomeCondition()) {
$result = getSomeResult();
return $result;
}
try {
$result = getResult();
return $result;
} catch (\Throwable $exception) {
baz($result ?? null);
}
}
',
];
yield 'multiple try/catch blocks separated with conditional return' => [
'<?php
function foo()
{
try {
return getResult();
} catch (\Throwable $exception) {
error_log($exception->getMessage());
}
if (isSomeCondition()) {
return getSomeResult();
}
try {
$result = $a + $b;
return $result;
} catch (\Throwable $th) {
var_dump($result ?? null);
}
}
',
'<?php
function foo()
{
try {
$result = getResult();
return $result;
} catch (\Throwable $exception) {
error_log($exception->getMessage());
}
if (isSomeCondition()) {
$result = getSomeResult();
return $result;
}
try {
$result = $a + $b;
return $result;
} catch (\Throwable $th) {
var_dump($result ?? null);
}
}
',
];
yield 'try/catch/finally' => [
'<?php
function foo()
{
if (isSomeCondition()) {
return getSomeResult();
}
try {
$result = getResult();
return $result;
} catch (\Throwable $exception) {
error_log($exception->getMessage());
} finally {
baz($result);
}
}
',
'<?php
function foo()
{
if (isSomeCondition()) {
$result = getSomeResult();
return $result;
}
try {
$result = getResult();
return $result;
} catch (\Throwable $exception) {
error_log($exception->getMessage());
} finally {
baz($result);
}
}
',
];
yield 'multiple try/catch separated with conditional return, with finally block' => [
'<?php
function foo()
{
try {
return getResult();
} catch (\Throwable $exception) {
error_log($exception->getMessage());
}
if (isSomeCondition()) {
return getSomeResult();
}
try {
$result = $a + $b;
return $result;
} catch (\Throwable $th) {
throw $th;
} finally {
echo "result:", $result, \PHP_EOL;
}
}
',
'<?php
function foo()
{
try {
$result = getResult();
return $result;
} catch (\Throwable $exception) {
error_log($exception->getMessage());
}
if (isSomeCondition()) {
$result = getSomeResult();
return $result;
}
try {
$result = $a + $b;
return $result;
} catch (\Throwable $th) {
throw $th;
} finally {
echo "result:", $result, \PHP_EOL;
}
}
',
];
yield 'invalid reference stays invalid' => [
'<?php
function bar() {
$foo = &foo();
return $foo;
}',
];
yield 'static' => [
'<?php
function a() {
static $a;
$a = time();
return $a;
}
',
];
yield 'global' => [
'<?php
function a() {
global $a;
$a = time();
return $a;
}
',
];
yield 'passed by reference' => [
'<?php
function foo(&$var)
{
$var = 1;
return $var;
}
',
];
yield 'not in function scope' => [
'<?php
$a = 1; // var might be global here
return $a;
',
];
yield 'open-close with ;' => [
'<?php
function a()
{
$a = 1;
?>
<?php
;
return $a;
}
',
];
yield 'open-close single line' => [
'<?php
function a()
{
$a = 1 ?><?php return $a;
}',
];
yield 'open-close' => [
'<?php
function a()
{
$a = 1
?>
<?php
return $a;
}
',
];
yield 'open-close before function' => [
'<?php
$zz = 1 ?><?php
function a($zz)
{
;
return $zz;
}
',
];
yield 'return complex statement' => [
'<?php
function a($c)
{
$a = 1;
return $a + $c;
}
',
];
yield 'array assign' => [
'<?php
function a($c)
{
$_SERVER["abc"] = 3;
return $_SERVER;
}
',
];
yield 'if assign' => [
'<?php
function foo ($bar)
{
$a = 123;
if ($bar)
$a = 12345;
return $a;
}
',
];
yield 'else assign' => [
'<?php
function foo ($bar)
{
$a = 123;
if ($bar)
;
else
$a = 12345;
return $a;
}
',
];
yield 'elseif assign' => [
'<?php
function foo ($bar)
{
$a = 123;
if ($bar)
;
elseif($b)
$a = 12345;
return $a;
}
',
];
yield 'echo $a = N / comment $a = N;' => [
'<?php
function a($c)
{
$a = 1;
echo $a."=1";
return $a;
}
function b($c)
{
$a = 1;
echo $a."=1;";
return $a;
}
function c($c)
{
$a = 1;
echo $a;
// $a =1;
return $a;
}
',
];
yield 'if ($a = N)' => [
'<?php
function a($c)
{
if ($a = 1)
return $a;
}
',
];
yield 'changed after declaration' => [
'<?php
function a($c)
{
$a = 1;
$a += 1;
return $a;
}
function b($c)
{
$a = 1;
$a -= 1;
return $a;
}
',
];
yield 'complex statement' => [
'<?php
function a($c)
{
$d = $c && $a = 1;
return $a;
}
',
];
yield 'PHP close tag within function' => [
'<?php
function a($zz)
{
$zz = 1 ?><?php
;
return $zz;
}
',
];
yield 'import global using "require"' => [
'<?php
function a()
{
require __DIR__."/test3.php";
$b = 1;
return $b;
}
',
];
yield 'import global using "require_once"' => [
'<?php
function a()
{
require_once __DIR__."/test3.php";
$b = 1;
return $b;
}
',
];
yield 'import global using "include"' => [
'<?php
function a()
{
include __DIR__."/test3.php";
$b = 1;
return $b;
}
',
];
yield 'import global using "include_once"' => [
'<?php
function a()
{
include_once __DIR__."/test3.php";
$b = 1;
return $b;
}
',
];
yield 'eval' => [
'<?php
$b = function ($z) {
$c = eval($z);
return $c;
};
$c = function ($x) {
$x = eval($x);
$x = 1;
return $x;
};
',
];
yield '${X}' => [
'<?php
function A($g)
{
$h = ${$g};
return $h;
}
',
];
yield '$$' => [
'<?php
function B($c)
{
$b = $$c;
return $b;
}
',
];
yield [
'<?php
class XYZ
{
public function test1()
{
$GLOBALS[\'a\'] = 2;
return $GLOBALS;
}
public function test2()
{
$_server = 2;
return $_server;
}
public function __destruct()
{
$GLOBALS[\'a\'] = 2;
return $GLOBALS[\'a\']; // destruct cannot return but still lints
}
};
$a = new XYZ();
$a = 1;
var_dump($a); // $a = 2 here _╯°□°╯︵┻━┻
',
];
yield 'variable returned by reference in function' => [
'<?php
function &foo() {
$var = 1;
return $var;
}',
];
yield 'variable returned by reference in method' => [
'<?php
class Foo {
public function &bar() {
$var = 1;
return $var;
}
}',
];
yield 'variable returned by reference in lambda' => [
'<?php $a = function &() {$z = new A(); return $z;};',
];
yield [
'<?php
function F() {
$a = 1;
while(bar()) {
++$a;
}; // keep this
return $a;
}
',
];
yield 'try/catch/finally - do not fix' => [
'<?php
function add($a, $b): mixed
{
try {
$result = $a + $b;
return $result;
} catch (\Throwable $th) {
throw $th;
} finally {
echo \'result:\', $result, \PHP_EOL;
}
}
',
];
yield 'try with multiple catch blocks' => [
'<?php
function foo() {
try {
$bar = bar();
return $bar;
} catch (\LogicException $e) {
echo "catch ... ";
} catch (\RuntimeException $e) {
echo $bar;
}
}',
];
yield 'try/catch/finally with some comments' => [
'<?php
function add($a, $b): mixed
{
try {
$result = $a + $b;
return $result;
} /* foo */ catch /** bar */ (\LogicException $th) {
throw $th;
}
// Or maybe this....
catch (\RuntimeException $th) {
throw $th;
}
# Print the result anyway
finally {
echo \'result:\', $result, \PHP_EOL;
}
}
',
];
yield [
'<?php
function foo() {
return bar();
}
',
'<?php
function foo() {
$a = bar();
$b = $a;
return $b;
}
',
];
yield [
'<?php
function foo(&$c) {
$a = $c;
$b = $a;
return $b;
}
',
];
$expected = "<?php\n";
$input = "<?php\n";
for ($i = 0; $i < 10; ++$i) {
$expected .= \sprintf("\nfunction foo%d() {\n\treturn bar();\n}", $i);
$input .= \sprintf("\nfunction foo%d() {\n\t\$a = bar();\n\t\$b = \$a;\n\nreturn \$b;\n}", $i);
}
yield [$expected, $input];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'try with non-capturing catch block' => [
'<?php
function add($a, $b): mixed
{
try {
$result = $a + $b;
return $result;
}
catch (\Throwable) {
noop();
}
finally {
echo \'result:\', $result, \PHP_EOL;
}
}
',
];
yield 'match' => [
'<?php
function Foo($food) {
return match ($food) {
"apple" => "This food is an apple",
"cake" => "This food is a cake",
};
}
',
'<?php
function Foo($food) {
$return_value = match ($food) {
"apple" => "This food is an apple",
"cake" => "This food is a cake",
};
return $return_value;
}
',
];
yield 'attribute before anonymous `class`' => [
'<?php
function A()
{
return new #[Foo] class {};
}
',
'<?php
function A()
{
$a = new #[Foo] class {};
return $a;
}
',
];
}
/**
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ReturnNotation/NoUselessReturnFixerTest.php | tests/Fixer/ReturnNotation/NoUselessReturnFixerTest.php | <?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\ReturnNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ReturnNotation\NoUselessReturnFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ReturnNotation\NoUselessReturnFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUselessReturnFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
function bar($baz)
{
if ($baz)
return $this->baz();
else
return;
}',
];
yield [
'<?php
function bar($baz)
{
if ($baz)
return $this->baz();
elseif($a)
return;
}',
];
yield [
'<?php
function bar($baz)
{
if ($baz)
return $this->baz();
else if($a)
return;
}',
];
yield [
'<?php
function bar($baz)
{
if ($baz)
return;
}',
];
yield [
'<?php
function b($b) {
if ($b) {
return;
}
/**/
}',
'<?php
function b($b) {
if ($b) {
return;
}
return /**/;
}',
];
yield [
'<?php
class Test2
{
private static function a($a)
{
if ($a) {
return;
}
$c1 = function() use ($a) {
if ($a)
return;
if ($a > 1) return;
echo $a;
'.'
};
$c1();
'.'
'.'
}
private function test()
{
$d = function(){
echo 123;
'.'
};
$d();
}
}',
'<?php
class Test2
{
private static function a($a)
{
if ($a) {
return;
}
$c1 = function() use ($a) {
if ($a)
return;
if ($a > 1) return;
echo $a;
return;
};
$c1();
return
;
}
private function test()
{
$d = function(){
echo 123;
return;
};
$d();
}
}',
];
yield [
'<?php
function aT($a) {
if ($a) {
return;
}
'.'
}',
'<?php
function aT($a) {
if ($a) {
return;
}
return ;
}',
];
yield [
'<?php return;',
];
yield [
'<?php
function c($c) {
if ($c) {
return;
}
//'.'
}',
'<?php
function c($c) {
if ($c) {
return;
}
return;//
}',
];
yield [
'<?php
class Test {
private static function d($d) {
if ($d) {
return;
}
}
}',
'<?php
class Test {
private static function d($d) {
if ($d) {
return;
}
return;}
}',
];
yield [
'<?php
interface FooInterface
{
public function fnc();
}',
];
yield [
'<?php
abstract class AbstractFoo
{
abstract public function fnc();
abstract public function fnc1();
static private function fn2(){}
public function fnc3() {
echo 1 . self::fn2();//{}
}
}',
];
yield [
'<?php
function foo () { }',
];
yield [
'<?php
$a = function() {
/**/
'.'
/* a */ //
'.'
};
',
'<?php
$a = function() {
return ; /**/
return ;
/* a */ return; //
return;
};
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ReturnNotation/SimplifiedNullReturnFixerTest.php | tests/Fixer/ReturnNotation/SimplifiedNullReturnFixerTest.php | <?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\ReturnNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ReturnNotation\SimplifiedNullReturnFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ReturnNotation\SimplifiedNullReturnFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SimplifiedNullReturnFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFixCases(): iterable
{
// check correct statements aren't changed
yield ['<?php return ;'];
yield ['<?php return \'null\';'];
yield ['<?php return false;'];
yield ['<?php return (false );'];
yield ['<?php return null === foo();'];
yield ['<?php return array() == null ;'];
// check we modified those that can be changed
yield ['<?php return;', '<?php return null;'];
yield ['<?php return;', '<?php return (null);'];
yield ['<?php return;', '<?php return ( null );'];
yield ['<?php return;', '<?php return ( (( null)));'];
yield ['<?php return /* hello */;', '<?php return /* hello */ null ;'];
yield ['<?php return;', '<?php return NULL;'];
yield ['<?php return;', "<?php return\n(\nnull\n)\n;"];
yield ['<?php function foo(): ? /* C */ int { return null; }'];
yield ['<?php function foo(): ?int { if (false) { return null; } }'];
yield ['<?php function foo(): int { return null; }'];
yield ['<?php function foo(): A\B\C { return null; }'];
yield [
'<?php function foo(): ?int { return null; } return;',
'<?php function foo(): ?int { return null; } return null;',
];
yield [
'<?php function foo() { return; } function bar(): ?A\B\C\D { return null; } function baz() { return; }',
'<?php function foo() { return null; } function bar(): ?A\B\C\D { return null; } function baz() { return null; }',
];
yield [
'<?php function foo(): ?int { $bar = function() { return; }; return null; }',
'<?php function foo(): ?int { $bar = function() { return null; }; return null; }',
];
yield [
'<?php function foo(): void { return; }',
];
yield ['<?php return ?>', '<?php return null ?>'];
yield ['<?php return [] ?>'];
yield [
'<?php
return // hello
?>
',
'<?php
return null // hello
?>
',
];
yield [
'<?php
return
// hello
?>
',
'<?php
return null
// hello
?>
',
];
yield [
'<?php
return // hello
;
',
'<?php
return null // hello
;
',
];
yield [
'<?php
return
// hello
;
',
'<?php
return null
// hello
;
',
];
}
/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: non-empty-string, 1?: non-empty-string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
function test(): null|int
{
if (true) { return null; }
return 42;
}',
];
yield [
'<?php
function test(): null|array
{
if (true) { return null; }
return [];
}',
];
yield [
'<?php
function test(): array|null
{
if (true) { return null; }
return [];
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Strict/DeclareStrictTypesFixerTest.php | tests/Fixer/Strict/DeclareStrictTypesFixerTest.php | <?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\Strict;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DeclareStrictTypesFixerTest 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{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
declare(ticks=1);
//
declare(strict_types=1);
namespace A\B\C;
class A {
}',
];
yield [
'<?php declare/* A b C*/(strict_types=1);',
];
yield [
'<?php declare(strict_types=1, ticks=1);',
];
yield [
'<?php declare(ticks=1, strict_types=1);',
];
yield [
'<?php declare(ticks=1, strict_types=1);',
'<?php declare(ticks=1, strict_types=0);',
];
yield 'monolithic file with closing tag' => [
'<?php /**/ /**/ deClarE (strict_types=1) ?>',
'<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>',
];
yield 'monolithic file with closing tag and extra new line' => [
'<?php /**/ /**/ deClarE (strict_types=1) ?>'."\n",
'<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>'."\n",
];
yield 'monolithic file with closing tag and extra content' => [
'<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>Test',
];
yield [
'<?php DECLARE ( strict_types=1 ) ;',
];
yield [
'<?php
/**/
declare(strict_types=1);',
];
yield [
'<?php declare(strict_types=1);
phpinfo();',
'<?php
phpinfo();',
];
yield [
'<?php declare(strict_types=1);
/**
* Foo
*/
phpinfo();',
'<?php
/**
* Foo
*/
phpinfo();',
];
yield [
'<?php declare(strict_types=1);
// comment after empty line',
'<?php
// comment after empty line',
];
yield [
'<?php declare(strict_types=1);
// comment without empty line before',
'<?php
// comment without empty line before',
];
yield [
'<?php declare(strict_types=1);
phpinfo();',
'<?php phpinfo();',
];
yield [
'<?php declare(strict_types=1);
$a = 456;
',
'<?php
$a = 456;
',
];
yield [
'<?php declare(strict_types=1);
/**/',
'<?php /**/',
];
yield [
'<?php declare(strict_types=1);',
'<?php declare(strict_types=0);',
];
yield [
'<?php declare(strict_types=1);',
'<?php declare(strict_types=0);',
['preserve_existing_declaration' => false],
];
yield [
'<?php declare(strict_types=0);',
null,
['preserve_existing_declaration' => true],
];
yield [' <?php echo 123;']; // first statement must be an open tag
yield ['<?= 123;']; // first token open with echo is not fixed
yield 'empty file /wo open tag' => [
'',
];
yield 'empty file /w open tag' => [
'<?php declare(strict_types=1);',
'<?php',
];
yield 'non-empty file /wo open tag' => [
'x',
];
yield 'non-empty file /w open tag' => [
'x<?php',
];
yield 'file with shebang /w open tag' => [
<<<'EOD'
#!x
<?php declare(strict_types=1);
EOD,
<<<'EOD'
#!x
<?php
EOD,
];
yield 'file with shebang /wo open tag' => [
<<<'EOD'
#!x
y
EOD,
];
yield 'file with shebang not followed by open tag' => [
<<<'EOD'
#!x
#!not_a_shebang
<?php
EOD,
];
}
/**
* @dataProvider provideWithWhitespacesConfigCases
*/
public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
{
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideWithWhitespacesConfigCases(): iterable
{
yield [
"<?php declare(strict_types=1);\r\nphpinfo();",
"<?php\r\n\tphpinfo();",
];
yield [
"<?php declare(strict_types=1);\r\nphpinfo();",
"<?php\nphpinfo();",
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Strict/StrictComparisonFixerTest.php | tests/Fixer/Strict/StrictComparisonFixerTest.php | <?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\Strict;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Strict\StrictComparisonFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Strict\StrictComparisonFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class StrictComparisonFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php $a === $b;', '<?php $a == $b;'];
yield ['<?php $a !== $b;', '<?php $a != $b;'];
yield ['<?php $a !== $b;', '<?php $a <> $b;'];
yield ['<?php echo "$a === $b";'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Strict/StrictParamFixerTest.php | tests/Fixer/Strict/StrictParamFixerTest.php | <?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\Strict;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Strict\StrictParamFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Strict\StrictParamFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class StrictParamFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
in_array(1, $a, true);
in_array(1, $a, false);
in_array(1, $a, $useStrict);',
];
yield [
'<?php class Foo
{
public function in_array($needle, $haystack) {}
}',
];
yield [
'<?php
in_array(1, $a, true);',
'<?php
in_array(1, $a);',
];
yield [
'<?php
in_array(1, foo(), true);',
'<?php
in_array(1, foo());',
];
yield [
'<?php
in_array(1, array(1, 2, 3), true);',
'<?php
in_array(1, array(1, 2, 3));',
];
yield [
'<?php
in_array(1, [1, 2, 3], true);',
'<?php
in_array(1, [1, 2, 3]);',
];
yield [
'<?php
in_array(in_array(1, [1, in_array(1, [1, 2, 3], true) ? 21 : 22, 3], true) ? 111 : 222, [1, in_array(1, [1, 2, 3], true) ? 21 : 22, 3], true);',
'<?php
in_array(in_array(1, [1, in_array(1, [1, 2, 3]) ? 21 : 22, 3]) ? 111 : 222, [1, in_array(1, [1, 2, 3]) ? 21 : 22, 3]);',
];
yield [
'<?php
in_Array(1, $a, true);',
'<?php
in_Array(1, $a);',
];
yield [
'<?php
base64_decode($foo, true);
base64_decode($foo, false);
base64_decode($foo, $useStrict);',
];
yield [
'<?php
base64_decode($foo, true);',
'<?php
base64_decode($foo);',
];
yield [
'<?php
array_search($foo, $bar, true);
array_search($foo, $bar, false);
array_search($foo, $bar, $useStrict);',
];
yield [
'<?php
array_search($foo, $bar, true);',
'<?php
array_search($foo, $bar);',
];
yield [
'<?php
array_keys($foo);
array_keys($foo, $bar, true);
array_keys($foo, $bar, false);
array_keys($foo, $bar, $useStrict);',
];
yield [
'<?php
array_keys($foo, $bar, true);',
'<?php
array_keys($foo, $bar);',
];
yield [
'<?php
mb_detect_encoding($foo, $bar, true);
mb_detect_encoding($foo, $bar, false);
mb_detect_encoding($foo, $bar, $useStrict);',
];
yield [
'<?php
mb_detect_encoding($foo, mb_detect_order(), true);',
'<?php
mb_detect_encoding($foo);',
];
yield [
'<?php
use function in_array;
class Foo
{
public function __construct($foo, $bar) {}
}',
];
yield [
'<?php
namespace Foo {
array_keys($foo, $bar, true);
}
namespace Bar {
use function Foo\LoremIpsum;
array_keys($foo, $bar, true);
}',
'<?php
namespace Foo {
array_keys($foo, $bar);
}
namespace Bar {
use function Foo\LoremIpsum;
array_keys($foo, $bar);
}',
];
yield [
'<?php
use function \base64_decode;
foo($bar);',
];
yield [
'<?php
use function Baz\base64_decode;
foo($bar);',
];
yield [
'<?php
in_array(1, foo(), true /* 1 *//* 2 *//* 3 */);',
'<?php
in_array(1, foo() /* 1 *//* 2 *//* 3 */);',
];
yield [
'<?php in_array($b, $c, true, );',
'<?php in_array($b, $c, );',
];
yield [
'<?php in_array($b, $c/* 0 *//* 1 */, true,/* 2 *//* 3 */);',
'<?php in_array($b, $c/* 0 *//* 1 */,/* 2 *//* 3 */);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ClassUsage/DateTimeImmutableFixerTest.php | tests/Fixer/ClassUsage/DateTimeImmutableFixerTest.php | <?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\ClassUsage;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ClassUsage\DateTimeImmutableFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassUsage\DateTimeImmutableFixer>
*
* @author Kuba Werłos <werlos@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DateTimeImmutableFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php new DateTimeImmutable();',
'<?php new DateTime();',
];
yield [
'<?php new DateTimeImmutable();',
'<?php new DATETIME();',
];
yield [
'<?php new \DateTimeImmutable();',
'<?php new \DateTime();',
];
yield [
'<?php new Foo\DateTime();',
];
yield [
'<?php namespace Foo; new DateTime();',
];
yield [
'<?php namespace Foo; new \DateTimeImmutable();',
'<?php namespace Foo; new \DateTime();',
];
yield [
'<?php namespace Foo; use DateTime; new \DateTimeImmutable();',
'<?php namespace Foo; use DateTime; new DateTime();',
];
yield [
'<?php namespace Foo; use DateTime; new Bar\DateTime();',
];
yield [
'<?php namespace Foo; use DateTime\Bar; new DateTime();',
];
yield [
'<?php namespace Foo; use Bar\DateTime; new DateTime();',
];
yield [
'<?php namespace Foo; use DateTime\Bar; use DateTime; use Baz\DateTime as BazDateTime; new \DateTimeImmutable();',
'<?php namespace Foo; use DateTime\Bar; use DateTime; use Baz\DateTime as BazDateTime; new DateTime();',
];
yield [
'<?php $foo = DateTime::ISO8601;',
];
yield [
'<?php $foo = \datetime::ISO8601 + 24;',
];
yield [
"<?php DateTimeImmutable::createFromFormat('j-M-Y', '15-Feb-2009');",
"<?php DateTime::createFromFormat('j-M-Y', '15-Feb-2009');",
];
yield [
'<?php \DateTimeImmutable::getLastErrors();',
'<?php \DateTime::getLastErrors();',
];
yield [
'<?php Foo\DateTime::createFromFormat();',
];
yield [
'<?php $foo->DateTime();',
];
yield [
'<?php Foo::DateTime();',
];
yield [
'<?php Foo\DateTime();',
];
yield [
'<?php date_create_immutable("now");',
'<?php date_create("now");',
];
yield [
'<?php date_create_immutable();',
'<?php Date_Create();',
];
yield [
'<?php \date_create_immutable();',
'<?php \date_create();',
];
yield [
'<?php namespace Foo; date_create_immutable();',
'<?php namespace Foo; date_create();',
];
yield [
'<?php namespace Foo; \date_create_immutable();',
'<?php namespace Foo; \date_create();',
];
yield [
"<?php date_create_immutable_from_format('j-M-Y', '15-Feb-2009');",
"<?php date_create_from_format('j-M-Y', '15-Feb-2009');",
];
yield [
'<?php Foo\date_create();',
];
yield [
'<?php $foo->date_create();',
];
yield [
'<?php Foo::date_create();',
];
yield [
'<?php new date_create();',
];
yield [
'<?php new \date_create();',
];
yield [
'<?php new Foo\date_create();',
];
yield [
'<?php class Foo { public function datetime() {} }',
];
yield [
'<?php class Foo { public function date_create() {} }',
];
yield [
'<?php namespace Foo; use DateTime; class Bar { public function datetime() {} }',
];
yield [
'<?php
namespace Foo;
use DateTime\Bar;
use DateTime;
use Baz\DateTime as BazDateTime;
new \DateTimeImmutable();
new \DateTimeImmutable();
new \DateTimeImmutable();
new \DateTimeImmutable();
',
'<?php
namespace Foo;
use DateTime\Bar;
use DateTime;
use Baz\DateTime as BazDateTime;
new DateTime();
new DateTime();
new DateTime();
new DateTime();
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected): void
{
$this->doTest($expected);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield ['<?php $foo?->DateTime();'];
yield ['<?php $foo?->date_create();'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitTestCaseStaticMethodCallsFixerTest.php | tests/Fixer/PhpUnit/PhpUnitTestCaseStaticMethodCallsFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\Utils;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitTestCaseStaticMethodCallsFixerTest extends AbstractFixerTestCase
{
public function testFixerContainsAllPhpunitStaticMethodsInItsList(): void
{
$assertionRefClass = new \ReflectionClass(TestCase::class);
$updatedStaticMethodsList = $assertionRefClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED);
$methods = (new \ReflectionClass(PhpUnitTestCaseStaticMethodCallsFixer::class))->getConstant('METHODS');
$missingMethods = [];
foreach ($updatedStaticMethodsList as $method) {
if ($method->isStatic() && !isset($methods[$method->name])) {
$missingMethods[] = $method->name;
}
}
self::assertSame([], $missingMethods, \sprintf(
'The following static methods from "%s" are missing from "%s::METHODS": %s',
TestCase::class,
PhpUnitTestCaseStaticMethodCallsFixer::class,
// `Utils::naturalLanguageJoin` does not accept empty array, so let's use it only if there's an actual difference.
[] === $missingMethods ? '' : Utils::naturalLanguageJoin($missingMethods),
));
}
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'wrong type for methods key' => [
['methods' => [123 => 1]],
'[php_unit_test_case_static_method_calls] Invalid configuration: The option "methods" with value array is expected to be of type "string[]", but one of the elements is of type "int".',
];
yield 'wrong type for methods value' => [
['methods' => ['assertSame' => 123]],
'[php_unit_test_case_static_method_calls] Invalid configuration: The option "methods" with value array is expected to be of type "string[]", but one of the elements is of type "int".',
];
}
public function testWrongConfigTypeForMethodsAndTargetVersion(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage('[php_unit_test_case_static_method_calls] Configuration cannot contain method "once" and target "11.0", it is dynamic in that PHPUnit version.');
$this->fixer->configure([
'methods' => ['once' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_SELF],
'target' => PhpUnitTargetVersion::VERSION_11_0,
]);
}
/**
* @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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
static::assertSame(1, 2);
static::markTestIncomplete('foo');
static::fail('foo');
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
$this->assertSame(1, 2);
$this->markTestIncomplete('foo');
$this->fail('foo');
}
}
EOF,
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testMocks()
{
$mock = $this->createMock(MyInterface::class);
$mock
->expects(static::once())
->method('run')
->with(
static::identicalTo(1),
static::stringContains('foo')
)
->will(static::onConsecutiveCalls(
static::returnSelf(),
static::throwException(new \Exception())
))
;
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testMocks()
{
$mock = $this->createMock(MyInterface::class);
$mock
->expects($this->once())
->method('run')
->with(
$this->identicalTo(1),
$this->stringContains('foo')
)
->will($this->onConsecutiveCalls(
$this->returnSelf(),
$this->throwException(new \Exception())
))
;
}
}
EOF,
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testWeirdIndentation()
{
static
// @TODO
::
assertSame
(1, 2);
// $this->markTestIncomplete('foo');
/*
$this->fail('foo');
*/
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testWeirdIndentation()
{
$this
// @TODO
->
assertSame
(1, 2);
// $this->markTestIncomplete('foo');
/*
$this->fail('foo');
*/
}
}
EOF,
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
$this->assertSame(1, 2);
$this->markTestIncomplete('foo');
$this->fail('foo');
$lambda = function () {
$this->assertSame(1, 23);
self::assertSame(1, 23);
static::assertSame(1, 23);
};
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
$this->assertSame(1, 2);
self::markTestIncomplete('foo');
static::fail('foo');
$lambda = function () {
$this->assertSame(1, 23);
self::assertSame(1, 23);
static::assertSame(1, 23);
};
}
}
EOF,
['call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS],
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
self::assertSame(1, 2);
self::markTestIncomplete('foo');
self::fail('foo');
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
$this->assertSame(1, 2);
self::markTestIncomplete('foo');
static::fail('foo');
}
}
EOF,
['call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_SELF],
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
$this->assertSame(1, 2);
$this->assertSame(1, 2);
static::setUpBeforeClass();
static::setUpBeforeClass();
$otherTest->setUpBeforeClass();
OtherTest::setUpBeforeClass();
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
static::assertSame(1, 2);
$this->assertSame(1, 2);
static::setUpBeforeClass();
$this->setUpBeforeClass();
$otherTest->setUpBeforeClass();
OtherTest::setUpBeforeClass();
}
}
EOF,
[
'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
'methods' => ['setUpBeforeClass' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_STATIC],
],
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public static function foo()
{
$this->assertSame(1, 2);
self::assertSame(1, 2);
static::assertSame(1, 2);
$lambda = function () {
$this->assertSame(1, 2);
self::assertSame(1, 2);
static::assertSame(1, 2);
};
}
public function bar()
{
$lambda = static function () {
$this->assertSame(1, 2);
self::assertSame(1, 2);
static::assertSame(1, 2);
};
$myProphecy->setCount(0)->will(function () {
$this->getCount()->willReturn(0);
});
}
static public function baz()
{
$this->assertSame(1, 2);
self::assertSame(1, 2);
static::assertSame(1, 2);
$lambda = function () {
$this->assertSame(1, 2);
self::assertSame(1, 2);
static::assertSame(1, 2);
};
}
static final protected function xyz()
{
static::assertSame(1, 2);
}
}
EOF,
null,
[
'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
],
];
yield [
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function foo()
{
$this->assertSame(1, 2);
$this->assertSame(1, 2);
$this->assertSame(1, 2);
}
public function bar()
{
$lambdaOne = static function () {
$this->assertSame(1, 21);
self::assertSame(1, 21);
static::assertSame(1, 21);
};
$lambdaTwo = function () {
$this->assertSame(1, 21);
self::assertSame(1, 21);
static::assertSame(1, 21);
};
}
public function baz2()
{
$this->assertSame(1, 22);
$this->assertSame(1, 22);
$this->assertSame(1, 22);
$this->assertSame(1, 23);
}
}
EOF,
<<<'EOF'
<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function foo()
{
$this->assertSame(1, 2);
self::assertSame(1, 2);
static::assertSame(1, 2);
}
public function bar()
{
$lambdaOne = static function () {
$this->assertSame(1, 21);
self::assertSame(1, 21);
static::assertSame(1, 21);
};
$lambdaTwo = function () {
$this->assertSame(1, 21);
self::assertSame(1, 21);
static::assertSame(1, 21);
};
}
public function baz2()
{
$this->assertSame(1, 22);
self::assertSame(1, 22);
static::assertSame(1, 22);
STATIC::assertSame(1, 23);
}
}
EOF,
[
'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
],
];
yield 'do not change class property and method signature' => [
<<<'EOF'
<?php
class FooTest extends TestCase
{
public function foo()
{
$this->assertSame = 42;
}
public function assertSame($foo, $bar){}
}
EOF,
];
yield 'do not change when only case is different' => [
<<<'EOF'
<?php
class FooTest extends TestCase
{
public function foo()
{
STATIC::assertSame(1, 1);
}
}
EOF,
];
yield 'do not crash on abstract static function' => [
<<<'EOF'
<?php
abstract class FooTest extends TestCase
{
abstract public static function dataProvider();
}
EOF,
null,
[
'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
],
];
yield 'handle $this with double colon following' => [
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
static::assertTrue(true);
}
}',
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
$this::assertTrue(true);
}
}',
];
yield 'anonymous class' => [
'<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
static::assertSame(1, 2);
$foo = new class() {
public function assertSame($a, $b)
{
$this->assertSame(1, 2);
}
};
}
}',
'<?php
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testBaseCase()
{
$this->assertSame(1, 2);
$foo = new class() {
public function assertSame($a, $b)
{
$this->assertSame(1, 2);
}
};
}
}',
];
yield 'anonymous class extending TestCase' => [
<<<'PHP'
<?php
$myTest = new class () extends \PHPUnit_Framework_TestCase
{
public function testSomething()
{
static::assertTrue(true);
static::assertTrue(true);
}
};
PHP,
<<<'PHP'
<?php
$myTest = new class () extends \PHPUnit_Framework_TestCase
{
public function testSomething()
{
self::assertTrue(true);
static::assertTrue(true);
}
};
PHP,
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
$a = $this::assertTrue(...);
}
}
',
];
}
/**
* @requires PHPUnit ^10.0
*/
public function testPHPUnit10(): void
{
self::assertPHPUnit(PhpUnitTargetVersion::VERSION_10_0);
}
/**
* @requires PHPUnit ^11.0
*/
public function testPHPUnit11(): void
{
self::assertPHPUnit(PhpUnitTargetVersion::VERSION_11_0);
}
/**
* @requires PHPUnit ^12.0
*/
public function testPHPUnit12(): void
{
self::assertPHPUnit(PhpUnitTargetVersion::VERSION_NEWEST);
}
/**
* @requires PHPUnit ^13.0
*/
public function testPHPUnit13(): void
{
self::fail('Hello, please implement me, and add new case for PHPUnit 13.');
}
/**
* @param PhpUnitTargetVersion::VERSION_10_0|PhpUnitTargetVersion::VERSION_11_0|PhpUnitTargetVersion::VERSION_NEWEST $version
*/
private static function assertPHPUnit(string $version): void
{
$fixer = new PhpUnitTestCaseStaticMethodCallsFixer();
$fixer->configure(['target' => $version]);
$configuredMethods = \Closure::bind(static fn (PhpUnitTestCaseStaticMethodCallsFixer $fixer): array => $fixer->configuration['methods'], null, PhpUnitTestCaseStaticMethodCallsFixer::class)($fixer);
$methodsForVersion = [];
foreach ($configuredMethods as $methodName => $calType) {
self::assertSame(PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS, $calType);
$methodsForVersion[] = $methodName;
}
sort($methodsForVersion);
$fixerReflection = new \ReflectionClass(PhpUnitTestCaseStaticMethodCallsFixer::class);
$testCaseReflection = new \ReflectionClass(TestCase::class);
$expectedMethodsForVersion = [];
/** @var string $method */
foreach (array_keys($fixerReflection->getConstant('METHODS')) as $method) {
if (!$testCaseReflection->hasMethod($method)) {
continue;
}
if ($testCaseReflection->getMethod($method)->isStatic()) {
continue;
}
$expectedMethodsForVersion[] = $method;
}
sort($expectedMethodsForVersion);
self::assertSame($expectedMethodsForVersion, $methodsForVersion);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitNamespacedFixerTest.php | tests/Fixer/PhpUnit/PhpUnitNamespacedFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitNamespacedFixerTest 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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'class_mapping' => [
'<?php new PHPUnit\Framework\Error\Error();',
'<?php new PHPUnit_Framework_Error();',
];
yield 'class_mapping_bogus_fqcn' => [
'<?php new \PHPUnit\Framework\MockObject\Stub\ReturnStub();',
'<?php new \PHPUnit_Framework_MockObject_Stub_Return();',
];
yield 'class_mapping_bogus_fqcn_lowercase' => [
'<?php new \PHPUnit\Framework\MockObject\Stub\ReturnStub();',
'<?php new \phpunit_framework_mockobject_stub_return();',
];
yield 'class_mapping_bogus_fqcn_uppercase' => [
'<?php new \PHPUnit\Framework\MockObject\Stub\ReturnStub();',
'<?php new \PHPUNIT_FRAMEWORK_MOCKOBJECT_STUB_RETURN();',
];
yield [
'<?php
final class MyTest extends \PHPUnit\Framework\TestCase
{
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
}',
];
yield [
'<?php
final class TextDiffTest extends PHPUnit\Framework\TestCase
{
}',
'<?php
final class TextDiffTest extends PHPUnit_Framework_TestCase
{
}',
];
yield [
'<?php
use \PHPUnit\Framework\TestCase;
final class TextDiffTest extends TestCase
{
}',
'<?php
use \PHPUnit_Framework_TestCase;
final class TextDiffTest extends PHPUnit_Framework_TestCase
{
}',
];
yield [
'<?php
use \PHPUnit\FRAMEWORK\TestCase as TestAlias;
final class TextDiffTest extends TestAlias
{
}',
'<?php
use \PHPUnit_FRAMEWORK_TestCase as TestAlias;
final class TextDiffTest extends TestAlias
{
}',
];
yield [
'<?php
namespace Foo;
use PHPUnit\Framework\TestCase;
final class TextDiffTest extends TestCase
{
}',
'<?php
namespace Foo;
use PHPUnit_Framework_TestCase;
final class TextDiffTest extends PHPUnit_Framework_TestCase
{
}',
];
yield [
'<?php
namespace Foo;
use PHPUnit\Framework\TestCase as TestAlias;
final class TextDiffTest extends TestAlias
{
}',
'<?php
namespace Foo;
use PHPUnit_Framework_TestCase as TestAlias;
final class TextDiffTest extends TestAlias
{
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit\Framework\TestCase
{
public function aaa()
{
$a = new PHPUnit_Framework_Assert();
$b = new PHPUnit_Framework_BaseTestListener();
$c = new PHPUnit_Framework_TestListener();
$d1 = new PHPUnit_Aaa();
$d2 = new PHPUnit_Aaa_Bbb();
$d3 = new PHPUnit_Aaa_Bbb_Ccc();
$d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
$d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function aaa()
{
$a = new PHPUnit_Framework_Assert();
$b = new PHPUnit_Framework_BaseTestListener();
$c = new PHPUnit_Framework_TestListener();
$d1 = new PHPUnit_Aaa();
$d2 = new PHPUnit_Aaa_Bbb();
$d3 = new PHPUnit_Aaa_Bbb_Ccc();
$d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
$d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
}
}',
['target' => PhpUnitTargetVersion::VERSION_4_8],
];
yield [
'<?php
final class MyTest extends \PHPUnit\Framework\TestCase
{
public function aaa()
{
$a = new PHPUnit\Framework\Assert();
$b = new PHPUnit\Framework\BaseTestListener();
$c = new PHPUnit\Framework\TestListener();
$d1 = new PHPUnit_Aaa();
$d2 = new PHPUnit_Aaa_Bbb();
$d3 = new PHPUnit_Aaa_Bbb_Ccc();
$d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
$d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function aaa()
{
$a = new PHPUnit_Framework_Assert();
$b = new PHPUnit_Framework_BaseTestListener();
$c = new PHPUnit_Framework_TestListener();
$d1 = new PHPUnit_Aaa();
$d2 = new PHPUnit_Aaa_Bbb();
$d3 = new PHPUnit_Aaa_Bbb_Ccc();
$d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
$d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
}
}',
['target' => PhpUnitTargetVersion::VERSION_5_7],
];
yield [
'<?php
final class MyTest extends \PHPUnit\Framework\TestCase
{
public function aaa()
{
$a = new PHPUnit\Framework\Assert();
$b = new PHPUnit\Framework\BaseTestListener();
$c = new PHPUnit\Framework\TestListener();
$d1 = new PHPUnit\Aaa();
$d2 = new PHPUnit\Aaa\Bbb();
$d3 = new PHPUnit\Aaa\Bbb\Ccc();
$d4 = new PHPUnit\Aaa\Bbb\Ccc\Ddd();
$d5 = new PHPUnit\Aaa\Bbb\Ccc\Ddd\Eee();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function aaa()
{
$a = new PHPUnit_Framework_Assert();
$b = new PHPUnit_Framework_BaseTestListener();
$c = new PHPUnit_Framework_TestListener();
$d1 = new PHPUnit_Aaa();
$d2 = new PHPUnit_Aaa_Bbb();
$d3 = new PHPUnit_Aaa_Bbb_Ccc();
$d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
$d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
}
}',
['target' => PhpUnitTargetVersion::VERSION_6_0],
];
yield [
'<?php
echo \PHPUnit\Runner\Version::id();
echo \PHPUnit\Runner\Version::id();
',
'<?php
echo \PHPUnit_Runner_Version::id();
echo \PHPUnit_Runner_Version::id();
',
];
yield [
'<?php
final class MyTest extends TestCase
{
const PHPUNIT_FOO = "foo";
}',
];
yield [
'<?php
final class MyTest extends TestCase
{
const FOO = Bar::PHPUNIT_FOO;
}',
];
yield [
'<?php
define(\'PHPUNIT_COMPOSER_INSTALL\', dirname(__DIR__).\'/vendor/autoload.php\');
require PHPUNIT_COMPOSER_INSTALL;',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php
final class MyTest extends TestCase
{
final public const PHPUNIT_FOO_A = "foo";
final public const PHPUNIT_FOO_B = Bar::PHPUNIT_FOO;
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitConstructFixerTest.php | tests/Fixer/PhpUnit/PhpUnitConstructFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitConstructFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
{
if (null !== $configuration) {
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
} else {
$this->doTest($expected, $input);
foreach (['assertSame', 'assertEquals', 'assertNotEquals', 'assertNotSame'] as $method) {
$this->fixer->configure(['assertions' => [$method]]);
$this->doTest(
$expected,
null !== $input && str_contains($input, $method) ? $input : null,
);
}
}
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
$cases = [
['$sth->assertSame(true, $foo);'],
['$this->assertSame($b, null);'],
[
'$this->assertNull(/*bar*/ $a);',
'$this->assertSame(null /*foo*/, /*bar*/ $a);',
],
[
'$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException());',
],
[
'$this->assertSame(null /*comment*/ === $eventException ? $exception : $eventException, $event->getException());',
],
[
'
$this->assertTrue(
$a,
"foo" . $bar
);',
'
$this->assertSame(
true,
$a,
"foo" . $bar
);',
],
[
'
$this->assertTrue(#
#
$a,#
"foo" . $bar#
);',
'
$this->assertSame(#
true,#
$a,#
"foo" . $bar#
);',
],
[
'$this->assertSame("a", $a); $this->assertTrue($b);',
'$this->assertSame("a", $a); $this->assertSame(true, $b);',
],
[
'$this->assertSame(true || $a, $b); $this->assertTrue($c);',
'$this->assertSame(true || $a, $b); $this->assertSame(true, $c);',
],
[
'$this->assertFalse($foo);',
'$this->assertEquals(FALSE, $foo);',
],
[
'$this->assertTrue($foo);',
'$this->assertEquals(TruE, $foo);',
],
[
'$this->assertNull($foo);',
'$this->assertEquals(NULL, $foo);',
],
];
array_walk(
$cases,
static function (array &$case): void {
$case[0] = self::generateTest($case[0]);
if (isset($case[1])) {
$case[1] = self::generateTest($case[1]);
}
},
);
yield from array_merge(
$cases,
[
'not in a class' => ['<?php $this->assertEquals(NULL, $foo);'],
'not phpunit class' => ['<?php class Foo { public function testFoo(){ $this->assertEquals(NULL, $foo); }}'],
'multiple candidates in multiple classes' => [
'<?php
class FooTest1 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertNull($foo); }}
class FooTest2 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertNull($foo); }}
class FooTest3 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertNull($foo); }}
',
'<?php
class FooTest1 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertEquals(NULL, $foo); }}
class FooTest2 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertEquals(NULL, $foo); }}
class FooTest3 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertEquals(NULL, $foo); }}
',
],
],
self::generateCases('$this->assert%s%s($a); //%s %s', '$this->assert%s(%s, $a); //%s %s'),
self::generateCases('$this->assert%s%s($a, "%s", "%s");', '$this->assert%s(%s, $a, "%s", "%s");'),
self::generateCases('static::assert%s%s($a); //%s %s', 'static::assert%s(%s, $a); //%s %s'),
self::generateCases('STATIC::assert%s%s($a); //%s %s', 'STATIC::assert%s(%s, $a); //%s %s'),
self::generateCases('self::assert%s%s($a); //%s %s', 'self::assert%s(%s, $a); //%s %s'),
);
yield [
self::generateTest('$this->assertTrue($a, );'),
self::generateTest('$this->assertSame(true, $a, );'),
];
yield [
self::generateTest('$this->assertTrue($a, $message , );'),
self::generateTest('$this->assertSame(true, $a, $message , );'),
];
yield [
self::generateTest('$this->assertSame(null, $a);'),
null,
['assertions' => []],
];
}
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('/^\[php_unit_construct\] Invalid configuration: The option "assertions" .*\.$/');
$this->fixer->configure(['assertions' => ['__TEST__']]); // @phpstan-ignore-line
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
self::generateTest('$this->assertEquals(...);'),
];
}
/**
* @return list<array{string, string}>
*/
private static function generateCases(string $expectedTemplate, string $inputTemplate): array
{
$functionTypes = ['Same' => true, 'NotSame' => false, 'Equals' => true, 'NotEquals' => false];
$cases = [];
foreach (['true', 'false', 'null'] as $type) {
foreach ($functionTypes as $method => $positive) {
$cases[] = [
self::generateTest(\sprintf($expectedTemplate, $positive ? '' : 'Not', ucfirst($type), $method, $type)),
self::generateTest(\sprintf($inputTemplate, $method, $type, $method, $type)),
];
}
}
return $cases;
}
private static function generateTest(string $content): string
{
return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitDataProviderMethodOrderFixerTest.php | tests/Fixer/PhpUnit/PhpUnitDataProviderMethodOrderFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderMethodOrderFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderMethodOrderFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderMethodOrderFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitDataProviderMethodOrderFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'simple - placement after' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
}
PHP,
];
yield 'simple - placement before' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
['placement' => 'before'],
];
yield 'empty test class' => [
<<<'PHP'
<?php
class FooTest extends TestCase {}
PHP,
];
yield 'data provider named with different casing' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function PROVIDEFOOCASES(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function PROVIDEFOOCASES(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
}
PHP,
];
yield 'with test method annotated' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @test
* @dataProvider provideFooCases
*/
public function foo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/**
* @test
* @dataProvider provideFooCases
*/
public function foo(): void {}
}
PHP,
];
yield 'data provider not found' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/** @dataProvider notExistingFunction */
public function testFoo(): void {}
}
PHP,
];
yield 'data provider used multiple times - placement after' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
public function testFoo(): void {}
/** @dataProvider provideFooCases */
public function testFoo2(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
/** @dataProvider provideFooCases */
public function testFoo2(): void {}
}
PHP,
];
yield 'data provider used multiple times - placement after - do not move provider if already after first test where used' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testBar(): void {}
}
PHP,
];
yield 'data provider used multiple times - placement before' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
/** @dataProvider provideFooCases */
public function testFoo2(): void {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
public function testFoo(): void {}
/** @dataProvider provideFooCases */
public function testFoo2(): void {}
public function provideFooCases(): iterable {}
}
PHP,
['placement' => 'before'],
];
yield 'multiple data providers for one test method' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
* @dataProvider provideFooCases3
* @dataProvider provideFooCases2
*/
public function testFoo(): void {}
public function provideFooCases(): iterable {}
public function provideFooCases3(): iterable {}
public function provideFooCases2(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/**
* @dataProvider provideFooCases
* @dataProvider provideFooCases3
* @dataProvider provideFooCases2
*/
public function testFoo(): void {}
public function provideFooCases2(): iterable {}
public function provideFooCases3(): iterable {}
}
PHP,
];
yield 'data provider used multiple times II - placement after' => [
<<<'PHP'
<?php
class FooTest {
/** @dataProvider provideACases */
public function testA1(): void {}
/** @dataProvider provideBCases */
public function testB(): void {}
public static function provideBCases(): iterable {}
/** @dataProvider provideACases */
public function testA2(): void {}
public static function provideACases(): iterable {}
}
PHP,
];
yield 'data provider used multiple times II - placement before' => [
<<<'PHP'
<?php
class FooTest {
public static function provideACases(): iterable {}
/** @dataProvider provideACases */
public function testA2(): void {}
public static function provideBCases(): iterable {}
/** @dataProvider provideBCases */
public function testB(): void {}
/** @dataProvider provideACases */
public function testA1(): void {}
}
PHP,
null,
['placement' => 'before'],
];
yield 'with other methods - placement after' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
public function testA(): void {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function provideFooCases(): iterable {}
public function testB(): void {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function testA(): void {}
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function testB(): void {}
}
PHP,
];
yield 'with other methods - placement before' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
public function testA(): void {}
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function testB(): void {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function testA(): void {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function provideFooCases(): iterable {}
public function testB(): void {}
}
PHP,
['placement' => 'before'],
];
yield 'with other methods II' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
public function testA(): void {}
public function testB(): void {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function provideFooCases(): iterable {}
public function testC(): void {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
public function testA(): void {}
public function provideFooCases(): iterable {}
public function testB(): void {}
/** @dataProvider provideFooCases */
public function testFoo(): void {}
public function testC(): void {}
}
PHP,
];
yield 'data provider defined by both annotation and attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
/** @dataProvider provideFooCases */
#[DataProvider('provideFooCases')]
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
/** @dataProvider provideFooCases */
#[DataProvider('provideFooCases')]
public function testFoo(): void {}
}
PHP,
];
yield 'multiple data providers defined by both annotation and attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
/** @dataProvider provider1 */
#[DataProvider('provider1')]
public function testFoo1(): void {}
public function provider1(): iterable {}
/**
* @dataProvider provider2a
* @dataProvider provider2b
* @dataProvider provider2c
*/
#[DataProvider('provider2a')]
#[DataProvider('provider2b')]
#[DataProvider('provider2c')]
public function testFoo2(): void {}
public function provider2a(): iterable {}
public function provider2b(): iterable {}
public function provider2c(): iterable {}
/** @dataProvider provider3 */
#[DataProvider('provider3')]
public function testFoo3(): void {}
public function provider3(): iterable {}
}
PHP,
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
public function provider2b(): iterable {}
/** @dataProvider provider1 */
#[DataProvider('provider1')]
public function testFoo1(): void {}
public function provider3(): iterable {}
public function provider2c(): iterable {}
/**
* @dataProvider provider2a
* @dataProvider provider2b
* @dataProvider provider2c
*/
#[DataProvider('provider2a')]
#[DataProvider('provider2b')]
#[DataProvider('provider2c')]
public function testFoo2(): void {}
public function provider1(): iterable {}
public function provider2a(): iterable {}
/** @dataProvider provider3 */
#[DataProvider('provider3')]
public function testFoo3(): void {}
}
PHP,
];
}
/**
* @requires PHP ^8.0
*
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield 'with an attribute between PHPDoc and data provider/test method - placement after' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomTestAttribute]
public function testFoo(): void {}
#[CustomProviderAttribute]
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
#[CustomProviderAttribute]
public function provideFooCases(): iterable {}
/**
* @dataProvider provideFooCases
*/
#[CustomTestAttribute]
public function testFoo(): void {}
}
PHP,
];
yield 'with an attribute between PHPDoc and data provider/test method - placement before' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
#[CustomProviderAttribute]
public function provideFooCases(): iterable {}
/**
* @dataProvider provideFooCases
*/
#[CustomTestAttribute]
public function testFoo(): void {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomTestAttribute]
public function testFoo(): void {}
#[CustomProviderAttribute]
public function provideFooCases(): iterable {}
}
PHP,
['placement' => 'before'],
];
yield 'data provider defined by an attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
#[DataProvider('provideFooCases')]
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
public function provideFooCases(): iterable {}
#[DataProvider('provideFooCases')]
public function testFoo(): void {}
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitSizeClassFixerTest.php | tests/Fixer/PhpUnit/PhpUnitSizeClassFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\AbstractPhpUnitFixer
* @covers \PhpCsFixer\Fixer\DocBlockAnnotationTrait
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitSizeClassFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitSizeClassFixer>
*
* @author Jefersson Nathan <malukenho.dev@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitSizeClassFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitSizeClassFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'It does not change normal classes' => [
'<?php
class Hello
{
}
',
];
yield 'It marks a test class as @small by default' => [
'<?php
/**
* @small
*/
class Test extends TestCase
{
}
',
'<?php
class Test extends TestCase
{
}
',
];
yield 'It marks a test class as specified in the configuration' => [
'<?php
/**
* @large
*/
class Test extends TestCase
{
}
',
'<?php
class Test extends TestCase
{
}
',
['group' => 'large'],
];
yield 'It adds an @small tag to a class that already has a doc block' => [
'<?php
/**
* @coversNothing
* @small
*/
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*/
class Test extends TestCase
{
}
',
];
yield 'It does not change a class that is already @small' => [
'<?php
/**
* @small
*/
class Test extends TestCase
{
}
',
];
yield 'It does not change a class that is already @small and has other annotations' => [
'<?php
/**
* @author malukenho
* @coversNothing
* @large
* @group large
*/
class Test extends TestCase
{
}
',
];
yield 'It works on other indentation levels' => [
'<?php
if (class_exists("Foo\Bar")) {
/**
* @small
*/
class Test Extends TestCase
{
}
}
',
'<?php
if (class_exists("Foo\Bar")) {
class Test Extends TestCase
{
}
}
',
];
yield 'It works on other indentation levels when the class has other annotations' => [
'<?php
if (class_exists("Foo\Bar")) {
/**
* @author malukenho again
*
*
* @covers \Other\Class
* @small
*/
class Test Extends TestCase
{
}
}
',
'<?php
if (class_exists("Foo\Bar")) {
/**
* @author malukenho again
*
*
* @covers \Other\Class
*/
class Test Extends TestCase
{
}
}
',
];
yield 'It always adds @small to the bottom of the doc block' => [
'<?php
/**
* @coversNothing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* @small
*/
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
class Test extends TestCase
{
}
',
];
yield 'It does not change a class with a single line @{size} doc block' => [
'<?php
/** @medium */
class Test extends TestCase
{
}
',
];
yield 'It adds an @small tag to a class that already has a one linedoc block' => [
'<?php
/**
* @coversNothing
* @small
*/
class Test extends TestCase
{
}
',
'<?php
/** @coversNothing */
class Test extends TestCase
{
}
',
];
yield 'By default it will not mark an abstract class as @small' => [
'<?php
abstract class Test
{
}
',
];
yield 'It works correctly with multiple classes in one file, even when one of them is not allowed' => [
'<?php
/**
* @small
*/
class Test extends TestCase
{
}
abstract class Test2 extends TestCase
{
}
class FooBar
{
}
/**
* @small
*/
class Test3 extends TestCase
{
}
',
'<?php
class Test extends TestCase
{
}
abstract class Test2 extends TestCase
{
}
class FooBar
{
}
class Test3 extends TestCase
{
}
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'it adds a docblock above when there is an attribute' => [
'<?php
/**
* @small
*/
#[SimpleTest]
class Test extends TestCase
{
}
',
'<?php
#[SimpleTest]
class Test extends TestCase
{
}
',
];
yield 'it adds the internal tag along other tags when there is an attribute' => [
'<?php
/**
* @coversNothing
* @small
*/
#[SimpleTest]
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*/
#[SimpleTest]
class Test extends TestCase
{
}
',
];
yield 'it adds a docblock above when there are attributes' => [
'<?php
/**
* @small
*/
#[SimpleTest]
#[AnotherAttribute]
#[Annotated]
class Test extends TestCase
{
}
',
'<?php
#[SimpleTest]
#[AnotherAttribute]
#[Annotated]
class Test extends TestCase
{
}
',
];
yield 'it adds the internal tag along other tags when there are attributes' => [
'<?php
/**
* @coversNothing
* @small
*/
#[SimpleTest]
#[AnotherAttribute]
#[Annotated]
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*/
#[SimpleTest]
#[AnotherAttribute]
#[Annotated]
class Test extends TestCase
{
}
',
];
yield 'already with attribute Small' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\Small]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute Medium' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\Medium;
#[Medium]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute Large' => [
<<<'PHP'
<?php
namespace Tests;
use PHPUnit\Framework\Attributes as PHPUnitAttributes;
#[PHPUnitAttributes\Large]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitSetUpTearDownVisibilityFixerTest.php | tests/Fixer/PhpUnit/PhpUnitSetUpTearDownVisibilityFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitSetUpTearDownVisibilityFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitSetUpTearDownVisibilityFixer>
*
* @author Gert de Pagter
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitSetUpTearDownVisibilityFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'setUp and tearDown are made protected if they are public' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
protected function setUp() {}
protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public function setUp() {}
public function tearDown() {}
}
',
];
yield 'Other functions are ignored' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public function hello() {}
protected function setUp() {}
protected function tearDown() {}
public function testWork() {}
protected function testProtectedFunction() {}
private function privateHelperFunction() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public function hello() {}
public function setUp() {}
public function tearDown() {}
public function testWork() {}
protected function testProtectedFunction() {}
private function privateHelperFunction() {}
}
',
];
yield 'It works when setUp and tearDown are final' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
protected final function setUp() {}
final protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public final function setUp() {}
final public function tearDown() {}
}
',
];
yield 'It works when setUp and tearDown do not have visibility defined' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
protected function setUp() {}
protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
function setUp() {}
function tearDown() {}
}
',
];
yield 'It works when setUp and tearDown do not have visibility defined and are final' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
final protected function setUp() {}
final protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
final function setUp() {}
final function tearDown() {}
}
',
];
yield 'Functions outside a test class do not get changed' => [
'<?php
class Fixer extends OtherClass
{
public function hello() {}
public function setUp() {}
public function tearDown() {}
public function testWork() {}
protected function testProtectedFunction() {}
private function privateHelperFunction() {}
}
',
];
yield 'It works even when setup and teardown have improper casing' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
protected function sEtUp() {}
protected function TeArDoWn() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public function sEtUp() {}
public function TeArDoWn() {}
}
',
];
yield 'It works even with messy comments' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
protected /** foo */ function setUp() {}
/** foo */protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public /** foo */ function setUp() {}
/** foo */public function tearDown() {}
}
',
];
yield 'It works even with messy comments and no defined visibility' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
/** foo */protected function setUp() {}
/** bar */protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
/** foo */function setUp() {}
/** bar */function tearDown() {}
}
',
];
yield 'Nothing changes if setUp or tearDown are private' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
private function setUp() {}
private function tearDown() {}
}
',
];
yield 'It works when there are multiple classes in one file' => [
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
protected function setUp() {}
protected function tearDown() {}
}
class OtherTest extends \PhpUnit\FrameWork\TestCase
{
protected function setUp() {}
protected function tearDown() {}
}
',
'<?php
class FixerTest extends \PhpUnit\FrameWork\TestCase
{
public function setUp() {}
public function tearDown() {}
}
class OtherTest extends \PhpUnit\FrameWork\TestCase
{
public function setUp() {}
public function tearDown() {}
}
',
];
yield 'It does not touch anonymous class' => [
'<?php
class FooTest extends \PhpUnit\FrameWork\TestCase
{
protected function setUp(): void {
$mock = new class {
public function setUp() {}
};
}
protected function testSomethingElse() {
$mock = new class implements SetupableInterface {
public function setUp() {}
};
}
}
',
'<?php
class FooTest extends \PhpUnit\FrameWork\TestCase
{
public function setUp(): void {
$mock = new class {
public function setUp() {}
};
}
protected function testSomethingElse() {
$mock = new class implements SetupableInterface {
public function setUp() {}
};
}
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitAttributesFixerTest.php | tests/Fixer/PhpUnit/PhpUnitAttributesFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitAttributesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitAttributesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitAttributesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitAttributesFixerTest extends AbstractFixerTestCase
{
/**
* @requires PHP 8.0
*
* @dataProvider provideFixCases
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'do not fix with wrong values' => [<<<'PHP'
<?php
/**
* @requires
* @uses
*/
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @backupGlobals
* @backupStaticAttributes
* @covers
* @dataProvider
* @depends
* @group
* @preserveGlobalState
* @testDox
* @testWith
* @ticket
*/
public function testFoo() { self::assertTrue(true); }
}
PHP];
yield 'do not fix with wrong casing' => [<<<'PHP'
<?php
/**
* @COVERS \Foo
*/
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @dataPROVIDER provideFooCases
* @requires php 8.3
*/
public function testFoo() { self::assertTrue(true); }
}
PHP];
yield 'do not fix when not supported by attributes' => [<<<'PHP'
<?php
/**
* @covers FooClass::FooMethod
* @uses ClassName::methodName
*/
class FooTest extends \PHPUnit\Framework\TestCase {
public function testFoo() { self::assertTrue(true); }
}
PHP];
yield 'do not fix when there is already attribute of related class' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^8.2
*/
#[\FirstAttributeToMakeSureMultipleAttributesWorks]
#[\PHPUnit\Framework\Attributes\RequiresPhp('^7.1')]
#[\ThirdAttributeToMakeSureMultipleAttributesWorks]
public function testFoo() {}
}
PHP,
null,
['keep_annotations' => true],
];
yield 'do not duplicate attribute when used without leading slash' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^6.1
*/
#[PHPUnit\Framework\Attributes\RequiresPhp('^6.1')]
public function testFoo() {}
}
PHP,
null,
['keep_annotations' => true],
];
yield 'do not duplicate attribute when used with import' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\RequiresPhp;
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^6.2
*/
#[RequiresPhp('^6.2')]
public function testFoo() {}
}
PHP,
null,
['keep_annotations' => true],
];
yield 'do not duplicate attribute when used with partial import' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes;
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^6.2
*/
#[Attributes\RequiresPhp('^6.2')]
public function testFoo() {}
}
PHP,
null,
['keep_annotations' => true],
];
yield 'do not duplicate attribute when used with alias' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\RequiresPhp as PHPUnitRequiresPhp;
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^6.0
*/
#[PHPUnitRequiresPhp('^6.0')]
public function testFoo() {}
}
PHP,
null,
['keep_annotations' => true],
];
yield 'do not duplicate attribute when used with partial alias' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes as PHPUnitAttributes;
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^6.0
*/
#[PHPUnitAttributes\RequiresPhp('^6.0')]
public function testFoo() {}
}
PHP,
null,
['keep_annotations' => true],
];
yield 'fix multiple annotations' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @copyright ACME Corporation
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideFooCases')]
#[\PHPUnit\Framework\Attributes\RequiresPhp('^8.2')]
#[\PHPUnit\Framework\Attributes\RequiresOperatingSystem('Linux|Darwin')]
public function testFoo($x) { self::assertTrue($x); }
public static function provideFooCases() { yield [true]; yield [false]; }
}
PHP,
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @copyright ACME Corporation
* @dataProvider provideFooCases
* @requires PHP ^8.2
* @requires OS Linux|Darwin
*/
public function testFoo($x) { self::assertTrue($x); }
public static function provideFooCases() { yield [true]; yield [false]; }
}
PHP,
];
yield 'fix with multiple spaces' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
*/
#[\PHPUnit\Framework\Attributes\RequiresPhp('^7.4|^8.1')]
public function testFoo() { self::assertTrue(true); }
}
PHP,
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP ^7.4|^8.1
*/
public function testFoo() { self::assertTrue(true); }
}
PHP,
];
yield 'fix with > in operator' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
*/
#[\PHPUnit\Framework\Attributes\RequiresPhp('>= 8.1')]
public function testFoo() { self::assertTrue(true); }
}
PHP,
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @requires PHP >= 8.1
*/
public function testFoo() { self::assertTrue(true); }
}
PHP,
];
yield 'fix with trailing spaces' => self::createCase(
['class'],
'#[CoversClass(Foo::class)]',
'@covers Foo ',
);
$byte224 = \chr(224);
yield 'fix with non-alphanumeric characters' => [
<<<PHP
<?php
class FooTest extends \\PHPUnit\\Framework\\TestCase {
/**
*/
#[\\PHPUnit\\Framework\\Attributes\\TestDox('a\\'b"c')]
public function testFoo() { self::assertTrue(true); }
/**
*/
#[\\PHPUnit\\Framework\\Attributes\\TestDox('龍')]
public function testBar() { self::assertTrue(true); }
/**
*/
#[\\PHPUnit\\Framework\\Attributes\\TestDox('byte224: {$byte224}')]
public function testBaz() { self::assertTrue(true); }
}
PHP,
<<<PHP
<?php
class FooTest extends \\PHPUnit\\Framework\\TestCase {
/**
* @testDox a'b"c
*/
public function testFoo() { self::assertTrue(true); }
/**
* @testDox 龍
*/
public function testBar() { self::assertTrue(true); }
/**
* @testDox byte224: {$byte224}
*/
public function testBaz() { self::assertTrue(true); }
}
PHP,
];
yield 'handle After' => self::createCase(
['method'],
'#[After]',
'@after',
);
yield 'handle AfterClass' => self::createCase(
['method'],
'#[AfterClass]',
'@afterClass',
);
yield 'handle BackupGlobals enabled' => self::createCase(
['class', 'method'],
'#[BackupGlobals(true)]',
'@backupGlobals enabled',
);
yield 'handle BackupGlobals disabled' => self::createCase(
['class', 'method'],
'#[BackupGlobals(false)]',
'@backupGlobals disabled',
);
yield 'handle BackupGlobals no' => self::createCase(
['class', 'method'],
'#[BackupGlobals(false)]',
'@backupGlobals no',
);
yield 'handle BackupStaticProperties enabled' => self::createCase(
['class', 'method'],
'#[BackupStaticProperties(true)]',
'@backupStaticAttributes enabled',
);
yield 'handle BackupStaticProperties disabled' => self::createCase(
['class', 'method'],
'#[BackupStaticProperties(false)]',
'@backupStaticAttributes disabled',
);
yield 'handle Before' => self::createCase(
['method'],
'#[Before]',
'@before',
);
yield 'handle BeforeClass' => self::createCase(
['method'],
'#[BeforeClass]',
'@beforeClass',
);
yield 'handle CoversClass' => self::createCase(
['class'],
'#[CoversClass(\VendorName\ClassName::class)]',
'@covers \VendorName\ClassName',
);
yield 'handle CoversFunction' => self::createCase(
['class'],
"#[CoversFunction('functionName')]",
'@covers ::functionName',
);
yield 'handle CoversNothing' => self::createCase(
['class', 'method'],
'#[CoversNothing]',
'@coversNothing',
);
yield 'handle DataProvider' => self::createCase(
['method'],
"#[DataProvider('provideFooCases')]",
'@dataProvider provideFooCases',
);
yield 'handle DataProviderExternal' => self::createCase(
['method'],
"#[DataProviderExternal(BarTest::class, 'provideFooCases')]",
'@dataProvider BarTest::provideFooCases',
);
yield 'handle Depends' => self::createCase(
['method'],
"#[Depends('methodName')]",
'@depends methodName',
);
yield 'handle DependsExternal' => self::createCase(
['method'],
"#[DependsExternal(ClassName::class, 'methodName')]",
'@depends ClassName::methodName',
);
yield 'handle DependsExternalUsingDeepClone' => self::createCase(
['method'],
"#[DependsExternalUsingDeepClone(ClassName::class, 'methodName')]",
'@depends clone ClassName::methodName',
);
yield 'handle DependsExternalUsingShallowClone' => self::createCase(
['method'],
"#[DependsExternalUsingShallowClone(ClassName::class, 'methodName')]",
'@depends shallowClone ClassName::methodName',
);
yield 'handle DependsOnClass' => self::createCase(
['method'],
'#[DependsOnClass(ClassName::class)]',
'@depends ClassName::class',
);
yield 'handle DependsOnClassUsingDeepClone' => self::createCase(
['method'],
'#[DependsOnClassUsingDeepClone(ClassName::class)]',
'@depends clone ClassName::class',
);
yield 'handle DependsOnClassUsingShallowClone' => self::createCase(
['method'],
'#[DependsOnClassUsingShallowClone(ClassName::class)]',
'@depends shallowClone ClassName::class',
);
yield 'handle DependsUsingDeepClone' => self::createCase(
['method'],
"#[DependsUsingDeepClone('methodName')]",
'@depends clone methodName',
);
yield 'handle DependsUsingShallowClone' => self::createCase(
['method'],
"#[DependsUsingShallowClone('methodName')]",
'@depends shallowClone methodName',
);
yield 'handle DoesNotPerformAssertions' => self::createCase(
['class', 'method'],
'#[DoesNotPerformAssertions]',
'@doesNotPerformAssertions',
);
yield 'handle Group' => self::createCase(
['class', 'method'],
"#[Group('groupName')]",
'@group groupName',
);
yield 'handle Large' => self::createCase(
['class'],
'#[Large]',
'@large',
);
yield 'handle Medium' => self::createCase(
['class'],
'#[Medium]',
'@medium',
);
yield 'handle PostCondition' => self::createCase(
['method'],
'#[PostCondition]',
'@postCondition',
);
yield 'handle PreCondition' => self::createCase(
['method'],
'#[PreCondition]',
'@preCondition',
);
yield 'handle PreserveGlobalState enabled' => self::createCase(
['class', 'method'],
'#[PreserveGlobalState(true)]',
'@preserveGlobalState enabled',
);
yield 'handle PreserveGlobalState disabled' => self::createCase(
['class', 'method'],
'#[PreserveGlobalState(false)]',
'@preserveGlobalState disabled',
);
yield 'handle RequiresFunction' => self::createCase(
['class', 'method'],
"#[RequiresFunction('imap_open')]",
'@requires function imap_open',
);
yield 'handle RequiresMethod' => self::createCase(
['class', 'method'],
"#[RequiresMethod(ReflectionMethod::class, 'setAccessible')]",
'@requires function ReflectionMethod::setAccessible',
);
yield 'handle RequiresOperatingSystem' => self::createCase(
['class', 'method'],
"#[RequiresOperatingSystem('Linux')]",
'@requires OS Linux',
);
yield 'handle RequiresOperatingSystemFamily' => self::createCase(
['class', 'method'],
"#[RequiresOperatingSystemFamily('Windows')]",
'@requires OSFAMILY Windows',
);
yield 'handle RequiresPhp with only version' => self::createCase(
['class', 'method'],
"#[RequiresPhp('>= 8.1.20')]",
'@requires PHP 8.1.20',
);
yield 'handle RequiresPhp with caret' => self::createCase(
['class', 'method'],
"#[RequiresPhp('^8.1.21')]",
'@requires PHP ^8.1.21',
);
yield 'handle RequiresPhp with less than' => self::createCase(
['class', 'method'],
"#[RequiresPhp('<8.1.22')]",
'@requires PHP <8.1.22',
);
yield 'handle RequiresPhp with less than and space' => self::createCase(
['class', 'method'],
"#[RequiresPhp('< 8.1.23')]",
'@requires PHP < 8.1.23',
);
yield 'handle RequiresPhp with alternative versions' => self::createCase(
['class', 'method'],
"#[RequiresPhp('6|8')]",
'@requires PHP 6|8',
);
yield 'handle RequiresPhp with alpha versions' => self::createCase(
['class', 'method'],
"#[RequiresPhp('>= 5.4.0-alpha1')]",
'@requires PHP 5.4.0-alpha1',
);
yield 'handle RequiresPhpExtension' => self::createCase(
['class', 'method'],
"#[RequiresPhpExtension('mysqli', '>= 8.3.0')]",
'@requires extension mysqli >= 8.3.0',
);
yield 'handle RequiresPhpExtension with only version' => self::createCase(
['class', 'method'],
"#[RequiresPhpExtension('mysqli', '>= 8.3.0')]",
'@requires extension mysqli 8.3.0',
);
yield 'handle RequiresPhpunit' => self::createCase(
['class', 'method'],
"#[RequiresPhpunit('^10.1.0')]",
'@requires PHPUnit ^10.1.0',
);
yield 'handle RequiresPhpunit with only version' => self::createCase(
['class', 'method'],
"#[RequiresPhpunit('>= 11.1.1')]",
'@requires PHPUnit 11.1.1',
);
yield 'handle RequiresSetting' => self::createCase(
['class', 'method'],
"#[RequiresSetting('date.timezone', 'Europe/London')]",
'@requires setting date.timezone Europe/London',
);
yield 'handle RunInSeparateProcess' => self::createCase(
['method'],
'#[RunInSeparateProcess]',
'@runInSeparateProcess',
);
yield 'handle RunTestsInSeparateProcesses' => self::createCase(
['class'],
'#[RunTestsInSeparateProcesses]',
'@runTestsInSeparateProcesses',
);
yield 'handle Small' => self::createCase(
['class'],
'#[Small]',
'@small',
);
yield 'handle Test' => self::createCase(
['method'],
'#[Test]',
'@test',
);
yield 'handle TestDox' => self::createCase(
['class', 'method'],
"#[TestDox('Hello world!')]",
'@testDox Hello world!',
);
yield 'handle Ticket' => self::createCase(
['class', 'method'],
"#[Ticket('ABC-123')]",
'@ticket ABC-123',
);
yield 'handle UsesClass' => self::createCase(
['class'],
'#[UsesClass(ClassName::class)]',
'@uses ClassName',
);
yield 'handle UsesFunction' => self::createCase(
['class'],
"#[UsesFunction('functionName')]",
'@uses ::functionName',
);
yield 'handle TestWith' => [
<<<'PHP'
<?php
/**
* @testWith [true, false]
*/
class FooTest extends \PHPUnit\Framework\TestCase {
/**
*/
#[\PHPUnit\Framework\Attributes\TestWithJson('[1, 2]')]
public function testFoo($x) {}
/**
*/
#[\PHPUnit\Framework\Attributes\TestWithJson('[3, 4, 5]')]
#[\PHPUnit\Framework\Attributes\TestWithJson('[6, 7, 8]')]
#[\PHPUnit\Framework\Attributes\TestWithJson('["a", "b"]')]
#[\PHPUnit\Framework\Attributes\TestWithJson('["c\'d"]')]
public function testBar($x) {}
}
PHP,
<<<'PHP'
<?php
/**
* @testWith [true, false]
*/
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @testWith [1, 2]
*/
public function testFoo($x) {}
/**
* @testWith [3, 4, 5]
* [6, 7, 8]
* ["a", "b"]
* ["c'd"]
*/
public function testBar($x) {}
}
PHP,
];
yield 'handle multiple annotations of the same name' => [
<<<'PHP'
<?php
/**
*/
#[\PHPUnit\Framework\Attributes\Group('foo')]
#[\PHPUnit\Framework\Attributes\Group('bar')]
class TheTest extends \PHPUnit\Framework\TestCase {}
PHP,
<<<'PHP'
<?php
/**
* @group foo
* @group bar
*/
class TheTest extends \PHPUnit\Framework\TestCase {}
PHP,
];
yield 'keep annotations' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @copyright ACME Corporation
* @dataProvider provideFooCases
* @requires PHP ^8.2
* @requires OS Linux|Darwin
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideFooCases')]
#[\PHPUnit\Framework\Attributes\RequiresPhp('^8.2')]
#[\PHPUnit\Framework\Attributes\RequiresOperatingSystem('Linux|Darwin')]
public function testFoo($x) { self::assertTrue($x); }
public static function provideFooCases() { yield [true]; yield [false]; }
}
PHP,
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* @copyright ACME Corporation
* @dataProvider provideFooCases
* @requires PHP ^8.2
* @requires OS Linux|Darwin
*/
public function testFoo($x) { self::assertTrue($x); }
public static function provideFooCases() { yield [true]; yield [false]; }
}
PHP,
['keep_annotations' => true],
];
yield 'data provider with trailing parentheses' => [
<<<'PHP'
<?php
class TheTest extends \PHPUnit\Framework\TestCase {
/**
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideFooCases')]
#[\PHPUnit\Framework\Attributes\DataProviderExternal(AnotherTest::class, 'provideBarCases')]
public function testFoo($x) { self::assertTrue($x); }
public static function provideFooCases() { yield [true]; }
}
PHP,
<<<'PHP'
<?php
class TheTest extends \PHPUnit\Framework\TestCase {
/**
* @dataProvider provideFooCases()
* @dataProvider AnotherTest::provideBarCases()
*/
public function testFoo($x) { self::assertTrue($x); }
public static function provideFooCases() { yield [true]; }
}
PHP,
];
yield 'remove annotation if both annotation and attribute are present (keep_annotations = false)' => [
<<<'PHP'
<?php
/**
*/
#[\PHPUnit\Framework\Attributes\CoversNothing]
class TheTest extends \PHPUnit\Framework\TestCase {}
PHP,
<<<'PHP'
<?php
/**
* @coversNothing
*/
#[\PHPUnit\Framework\Attributes\CoversNothing]
class TheTest extends \PHPUnit\Framework\TestCase {}
PHP,
['keep_annotations' => false],
];
yield 'do remove annotation if both annotation and attribute are present (keep_annotations = true)' => [
<<<'PHP'
<?php
/**
* @coversNothing
*/
#[\PHPUnit\Framework\Attributes\CoversNothing]
class TheTest extends \PHPUnit\Framework\TestCase {}
PHP,
null,
['keep_annotations' => true],
];
}
/**
* @param non-empty-list<'class'|'method'> $scopes
*
* @return array{string, string}
*/
private static function createCase(array $scopes, string $expectedAttribute, string $inputAnnotation): array
{
$expectedAttribute = str_replace('#[', '#[\PHPUnit\Framework\Attributes\\', $expectedAttribute);
return [
\sprintf(
<<<'PHP'
<?php
%s
class FooTest extends \PHPUnit\Framework\TestCase {
%s
public function testFoo($x) {}
%s
public function testBar($x) {}
}
PHP,
\in_array('class', $scopes, true)
? \sprintf("/**\n */\n%s", $expectedAttribute)
: \sprintf("/**\n * %s\n */", $inputAnnotation),
\in_array('method', $scopes, true)
? \sprintf("/**\n */\n %s", $expectedAttribute)
: \sprintf("/**\n * %s\n */", $inputAnnotation),
\in_array('method', $scopes, true)
? \sprintf("\n %s", $expectedAttribute)
: \sprintf('/** %s */', $inputAnnotation),
),
\sprintf(
<<<'PHP'
<?php
/**
* %s
*/
class FooTest extends \PHPUnit\Framework\TestCase {
/**
* %s
*/
public function testFoo($x) {}
/** %s */
public function testBar($x) {}
}
PHP,
$inputAnnotation,
$inputAnnotation,
$inputAnnotation,
),
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitDataProviderNameFixerTest.php | tests/Fixer/PhpUnit/PhpUnitDataProviderNameFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitDataProviderNameFixerTest 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<string, array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'data provider named with different casing' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public function provideFooCases() {}
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public function PROVIDEFOOCASES() {}
}',
];
yield 'fixing simple scenario with test class prefixed' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public function provideFooCases() {}
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider fooDataProvider
*/
public function testFoo() {}
public function fooDataProvider() {}
}',
];
yield 'fixing simple scenario with test class annotated' => [
'<?php
class FooTest extends TestCase {
/**
* @test
* @dataProvider provideFooCases
*/
public function foo() {}
public function provideFooCases() {}
}',
'<?php
class FooTest extends TestCase {
/**
* @test
* @dataProvider fooDataProvider
*/
public function foo() {}
public function fooDataProvider() {}
}',
];
yield 'data provider not found' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider notExistingFunction
*/
public function testFoo() {}
}',
];
yield 'data provider used multiple times' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider reusedDataProvider
*/
public function testFoo() {}
/**
* @dataProvider reusedDataProvider
*/
public function testBar() {}
public function reusedDataProvider() {}
}',
];
yield 'data provider call without function' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider fooDataProvider
*/
private $prop;
}',
];
yield 'data provider target name already used' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider dataProvider
*/
public function testFoo() {}
public function dataProvider() {}
public function provideFooCases() {}
}',
];
yield 'data provider defined for anonymous function' => [
'<?php
class FooTest extends TestCase {
public function testFoo()
{
/**
* @dataProvider notDataProvider
*/
function () { return true; };
}
public function notDataProvider() {}
}',
];
yield 'multiple data providers for one test function' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider foo1DataProvider
* @dataProvider foo2DataProvider
*/
public function testFoo() {}
public function foo1DataProvider() {}
public function foo2DataProvider() {}
}',
];
yield 'data provider with new name being part of FQCN used in the code' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {
$x = Foo\ProvideFooCases::X_DEFAULT;
}
public function provideFooCases() {}
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider foo
*/
public function testFoo() {
$x = Foo\ProvideFooCases::X_DEFAULT;
}
public function foo() {}
}',
];
yield 'complex example' => [
'<?php
class FooTest extends TestCase {
/** @dataProvider notExistingFunction */
public function testClosure()
{
/** Preparing data */
$x = 0;
/** @dataProvider notDataProvider */
function () { return true; };
}
/**
* @dataProvider reusedDataProvider
* @dataProvider testFooProvider
*/
public function testFoo() {}
/**
* @dataProvider reusedDataProvider
* @dataProvider testBarProvider
*/
public function testBar() {}
public function reusedDataProvider() {}
/** @dataProvider provideBazCases */
public function testBaz() {}
public function provideBazCases() {}
/** @dataProvider provideSomethingCases */
public function testSomething() {}
public function provideSomethingCases() {}
public function testFooProvider() {}
public function testBarProvider() {}
}',
'<?php
class FooTest extends TestCase {
/** @dataProvider notExistingFunction */
public function testClosure()
{
/** Preparing data */
$x = 0;
/** @dataProvider notDataProvider */
function () { return true; };
}
/**
* @dataProvider reusedDataProvider
* @dataProvider testFooProvider
*/
public function testFoo() {}
/**
* @dataProvider reusedDataProvider
* @dataProvider testBarProvider
*/
public function testBar() {}
public function reusedDataProvider() {}
/** @dataProvider provideBazCases */
public function testBaz() {}
public function provideBazCases() {}
/** @dataProvider someDataProvider */
public function testSomething() {}
public function someDataProvider() {}
public function testFooProvider() {}
public function testBarProvider() {}
}',
];
yield 'fixing when string like expected data provider name is present' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {
$foo->provideFooCases(); // do not get fooled that data provider name is already taken
}
public function provideFooCases() {}
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider fooDataProvider
*/
public function testFoo() {
$foo->provideFooCases(); // do not get fooled that data provider name is already taken
}
public function fooDataProvider() {}
}',
];
foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
yield \sprintf('test function with %s modifier', $modifier) => [
\sprintf('<?php
abstract class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
%s function testFoo() %s
public function provideFooCases() {}
}', $modifier, 'abstract' === $modifier ? ';' : '{}'),
\sprintf('<?php
abstract class FooTest extends TestCase {
/**
* @dataProvider fooDataProvider
*/
%s function testFoo() %s
public function fooDataProvider() {}
}', $modifier, 'abstract' === $modifier ? ';' : '{}'),
];
}
foreach (
[
'custom prefix' => [
'theBestPrefixFooCases',
'testFoo',
['prefix' => 'theBestPrefix'],
],
'custom suffix' => [
'provideFooTheBestSuffix',
'testFoo',
['suffix' => 'TheBestSuffix'],
],
'custom prefix and suffix' => [
'theBestPrefixFooTheBestSuffix',
'testFoo',
['prefix' => 'theBestPrefix', 'suffix' => 'TheBestSuffix'],
],
'empty prefix' => [
'fooDataProvider',
'testFoo',
['prefix' => '', 'suffix' => 'DataProvider'],
],
'empty suffix' => [
'dataProviderForFoo',
'testFoo',
['prefix' => 'dataProviderFor', 'suffix' => ''],
],
'prefix and suffix with underscores' => [
'provide_foo_data',
'test_foo',
['prefix' => 'provide_', 'suffix' => '_data'],
],
'empty prefix and suffix with underscores' => [
'foo_data_provider',
'test_foo',
['prefix' => '', 'suffix' => '_data_provider'],
],
'prefix with underscores and empty suffix' => [
'data_provider_foo',
'test_foo',
['prefix' => 'data_provider_', 'suffix' => ''],
],
'prefix with underscores and empty suffix and test function starting with uppercase' => [
'data_provider_Foo',
'test_Foo',
['prefix' => 'data_provider_', 'suffix' => ''],
],
'prefix and suffix with underscores and test function having multiple consecutive underscores' => [
'provide_foo_data',
'test___foo',
['prefix' => 'provide_', 'suffix' => '_data'],
],
'uppercase naming' => [
'PROVIDE_FOO_DATA',
'TEST_FOO',
['prefix' => 'PROVIDE_', 'suffix' => '_DATA'],
],
'camelCase test function and prefix with underscores' => [
'data_provider_FooBar',
'testFooBar',
['prefix' => 'data_provider_', 'suffix' => ''],
],
] as $name => [$dataProvider, $testFunction, $config]
) {
yield $name => [
\sprintf('<?php
class FooTest extends TestCase {
/**
* @dataProvider %s
*/
public function %s() {}
public function %s() {}
}', $dataProvider, $testFunction, $dataProvider),
\sprintf('<?php
class FooTest extends TestCase {
/**
* @dataProvider dtPrvdr
*/
public function %s() {}
public function dtPrvdr() {}
}', $testFunction),
$config,
];
}
}
/**
* @requires PHP ^8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'with an attribute between PHPDoc and test method' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomAttribute]
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider f
*/
#[CustomAttribute]
public function testFoo(): void {}
public function f(): iterable {}
}
PHP,
];
yield 'with data provider as an attribute' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('provideFooCases')]
public function testFoo(): void {}
public function provideFooCases() {}
#[\PHPUnit\Framework\Attributes\DataProvider("provideBarCases")]
public function testBar(): void {}
public function provideBarCases() {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('renameMe')]
public function testFoo(): void {}
public function renameMe() {}
#[\PHPUnit\Framework\Attributes\DataProvider("renameMeToo")]
public function testBar(): void {}
public function renameMeToo() {}
}
PHP,
];
yield 'data provider defined by both annotation and attribute for the same test' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[DataProvider('provideFooCases')]
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
/**
* @dataProvider the_provider_of_the_data
*/
#[DataProvider('the_provider_of_the_data')]
public function testFoo(): void {}
public function the_provider_of_the_data(): iterable {}
}
PHP,
];
yield 'data provider defined by annotation and attribute for different tests' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class FooTest extends TestCase {
/**
* @dataProvider the_provider_of_the_data
*/
public function testFoo(): void {}
#[DataProvider('the_provider_of_the_data')]
public function testBar(): void {}
public function the_provider_of_the_data(): iterable {}
}
PHP,
];
yield 'multiple data providers defined by attributes for one test method' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class PersistenceTest extends TestCase
{
#[DataProvider('provideTypecastBidirectionalCases')]
#[DataProvider('provideTypecastLoadOnlyCases')]
public function testTypecast($v): void {}
public static function provideTypecastBidirectionalCases(): iterable {}
public static function provideTypecastLoadOnlyCases(): iterable {}
}
PHP,
];
yield 'multiple data providers defined by annotation and attributes for one test method' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\DataProvider;
class PersistenceTest extends TestCase
{
/**
* @dataProvider provideTypecastBidirectionalCases
* @dataProvider provideTypecastLoadOnlyCases
*/
#[DataProvider('provideTypecastBidirectionalCases')]
#[DataProvider('provideTypecastLoadOnlyCases')]
public function testTypecast($v): void {}
public static function provideTypecastBidirectionalCases(): iterable {}
public static function provideTypecastLoadOnlyCases(): iterable {}
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php | tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\AbstractPhpUnitFixer
* @covers \PhpCsFixer\Fixer\DocBlockAnnotationTrait
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestClassRequiresCoversFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitTestClassRequiresCoversFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitTestClassRequiresCoversFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'already with annotation: @covers' => [
'<?php
/**
* @covers Foo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'already with annotation: @coversDefaultClass' => [
'<?php
/**
* @coversDefaultClass
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'without docblock #1' => [
'<?php
/**
* @coversNothing
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'without docblock #2 (class is final)' => [
'<?php
/**
* @coversNothing
*/
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'without docblock #2 (class is abstract)' => [
'<?php
abstract class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'with docblock but annotation is missing' => [
'<?php
/**
* Description.
*
* @since v2.2
* @coversNothing
*/
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Description.
*
* @since v2.2
*/
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'with one-line docblock but annotation is missing' => [
'<?php
/**
* Description.
* @coversNothing
*/
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/** Description. */
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'with 2-lines docblock but annotation is missing #1' => [
'<?php
/** Description.
* @coversNothing
*/
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/** Description.
*/
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'with 2-lines docblock but annotation is missing #2' => [
'<?php
/**
* @coversNothing
* Description. */
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Description. */
final class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'with comment instead of docblock' => [
'<?php
/*
* @covers Foo
*/
/**
* @coversNothing
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/*
* @covers Foo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'not a test class' => [
'<?php
class Foo {}
',
];
yield 'multiple classes in one file' => [
'<?php /** */
use \PHPUnit\Framework\TestCase;
/**
* Foo
* @coversNothing
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
class Bar {}
/**
* @coversNothing
*/
class Baz1 extends PHPUnit_Framework_TestCase {}
/**
* @coversNothing
*/
class Baz2 extends \PHPUnit_Framework_TestCase {}
/**
* @coversNothing
*/
class Baz3 extends \PHPUnit\Framework\TestCase {}
/**
* @coversNothing
*/
class Baz4 extends TestCase {}
',
'<?php /** */
use \PHPUnit\Framework\TestCase;
/**
* Foo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
class Bar {}
class Baz1 extends PHPUnit_Framework_TestCase {}
class Baz2 extends \PHPUnit_Framework_TestCase {}
class Baz3 extends \PHPUnit\Framework\TestCase {}
class Baz4 extends TestCase {}
',
];
yield [
'<?php /* comment */
/**
* @coversNothing
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php /* comment */class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
}
/**
* @dataProvider provideWithWhitespacesConfigCases
*/
public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
{
$expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
if (null !== $input) {
$input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
}
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideWithWhitespacesConfigCases(): iterable
{
yield [
'<?php
/**
* @coversNothing
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {}
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'already with attribute CoversClass' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversClass(Foo::class)]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute CoversNothing' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversNothing]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute CoversNothing with leading slash' => [
<<<'PHP'
<?php
#[\PHPUnit\Framework\Attributes\CoversNothing]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with imported attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(Foo::class)]
class FooTest extends TestCase {}
PHP,
];
yield 'already with partially imported attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes;
#[Attributes\CoversClass(Foo::class)]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with aliased attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\CoversClass as PHPUnitCoversClass;
#[PHPUnitCoversClass(Foo::class)]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with partially aliased attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes as PHPUnitAttributes;
#[PHPUnitAttributes\CoversClass(Foo::class)]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'with attribute from different namespace' => [
<<<'PHP'
<?php
use Foo\CoversClass;
use PHPUnit\Framework\Attributes\CoversClass as PHPUnitCoversClass;
/**
* @coversNothing
*/
#[CoversClass(Foo::class)]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
<<<'PHP'
<?php
use Foo\CoversClass;
use PHPUnit\Framework\Attributes\CoversClass as PHPUnitCoversClass;
#[CoversClass(Foo::class)]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'with attribute on final class' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversNothing]
final class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute CoversMethod' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversMethod(Foo::class, 'bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with imported CoversMethod attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversMethod;
#[CoversMethod(Foo::class, 'bar')]
class FooTest extends TestCase {}
PHP,
];
yield 'already with partially imported CoversMethod attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes;
#[Attributes\CoversMethod(Foo::class, 'bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with aliased CoversMethod attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\CoversMethod as PHPUnitCoversMethod;
#[PHPUnitCoversMethod(Foo::class, 'bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with partially aliased CoversMethod attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes as PHPUnitAttributes;
#[PHPUnitAttributes\CoversMethod(Foo::class, 'bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute CoversFunction' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversFunction('bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with imported CoversFunction attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversFunction;
#[CoversFunction('bar')]
class FooTest extends TestCase {}
PHP,
];
yield 'already with partially imported CoversFunction attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes;
#[Attributes\CoversFunction('bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with aliased CoversFunction attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\CoversFunction as PHPUnitCoversFunction;
#[PHPUnitCoversFunction('bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with partially aliased CoversFunction attribute' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes as PHPUnitAttributes;
#[PHPUnitAttributes\CoversFunction('bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'already with attribute CoversTrait' => [
<<<'PHP'
<?php
use PHPUnit\Framework\Attributes\CoversTrait;
#[CoversTrait('Bar')]
class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
}
/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string}>
*/
public static function provideFix82Cases(): iterable
{
yield 'without docblock #2 (class is final)' => [
'<?php
/**
* @coversNothing
*/
readonly final class BarTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
readonly final class BarTest extends \PHPUnit_Framework_TestCase {}
',
];
yield 'without docblock #2 (class is abstract)' => [
'<?php
abstract readonly class FooTest extends \PHPUnit_Framework_TestCase {}
',
null,
];
yield 'with attribute on readonly class' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversNothing]
readonly class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
yield 'with attribute on final readonly class' => [
<<<'PHP'
<?php
#[PHPUnit\Framework\Attributes\CoversNothing]
final readonly class FooTest extends \PHPUnit_Framework_TestCase {}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitNoExpectationAnnotationFixerTest.php | tests/Fixer/PhpUnit/PhpUnitNoExpectationAnnotationFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitNoExpectationAnnotationFixerTest 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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'empty exception message' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class, \'\');
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessage
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting exception' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting rooted exception' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \FooException
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting exception with msg' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class, \'foo@bar\');
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessage foo@bar
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting exception with code' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class, null, 123);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionCode 123
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting exception with msg and code' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class, \'foo\', 123);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessage foo
* @expectedExceptionCode 123
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting exception with msg regex [but too low target]' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessageRegExp /foo.*$/
*/
public function testFnc()
{
aaa();
}
}',
null,
['target' => PhpUnitTargetVersion::VERSION_3_2],
];
yield 'expecting exception with msg regex' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedExceptionRegExp(\FooException::class, \'/foo.*$/\');
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessageRegExp /foo.*$/
*/
public function testFnc()
{
aaa();
}
}',
['target' => PhpUnitTargetVersion::VERSION_4_3],
];
yield 'use_class_const=false' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\'FooException\');
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
*/
public function testFnc()
{
aaa();
}
}',
['use_class_const' => false],
];
yield 'keep rest of docblock' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* Summary.
*
* @param int $param
* @return void
*/
public function testFnc($param)
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* Summary.
*
* @param int $param
* @expectedException FooException
* @return void
*/
public function testFnc($param)
{
aaa();
}
}',
];
yield 'fix method without visibility' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
function testFnc($param)
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
*/
function testFnc($param)
{
aaa();
}
}',
];
yield 'fix final method' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
final function testFnc($param)
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
*/
final function testFnc($param)
{
aaa();
}
}',
];
yield 'ignore when no docblock' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
final function testFoo($param)
{
aaa();
}
/**
*/
final function testFnc($param)
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
final function testFoo($param)
{
aaa();
}
/**
* @expectedException FooException
*/
final function testFnc($param)
{
aaa();
}
}',
];
yield 'valid docblock but for property, not method' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionCode 123
*/
public $foo;
public function bar()
{
/**
* @expectedException FooException
* @expectedExceptionCode 123
*/
$baz = 1;
/**
* @expectedException FooException
* @expectedExceptionCode 123
*/
while (false) {}
}
}',
];
yield 'respect \' and " in expected msg' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* Summary.
*
*/
public function testFnc($param)
{
$this->setExpectedException(\FooException::class, \'Foo \\\' bar " baz\');
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* Summary.
*
* @expectedException FooException
* @expectedExceptionMessage Foo \' bar " baz
*/
public function testFnc($param)
{
aaa();
}
}',
];
yield 'special \ handling' => [
<<<'EOT'
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testElementNonExistentOne()
{
$this->setExpectedException(\Cake\View\Exception\MissingElementException::class, 'A backslash at the end \\');
$this->View->element('non_existent_element');
}
/**
*/
public function testElementNonExistentTwo()
{
$this->setExpectedExceptionRegExp(\Cake\View\Exception\MissingElementException::class, '#^Element file "Element[\\\\/]non_existent_element\\.ctp" is missing\\.$#');
$this->View->element('non_existent_element');
}
}
EOT,
<<<'EOT'
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Cake\View\Exception\MissingElementException
* @expectedExceptionMessage A backslash at the end \
*/
public function testElementNonExistentOne()
{
$this->View->element('non_existent_element');
}
/**
* @expectedException \Cake\View\Exception\MissingElementException
* @expectedExceptionMessageRegExp #^Element file "Element[\\/]non_existent_element\.ctp" is missing\.$#
*/
public function testElementNonExistentTwo()
{
$this->View->element('non_existent_element');
}
}
EOT,
];
yield 'message on newline' => [
<<<'EOT'
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testMessageOnMultilines()
{
$this->setExpectedException(\RuntimeException::class, 'Message on multilines AAA €');
aaa();
}
/**
* @foo
*/
public function testMessageOnMultilinesWithAnotherTag()
{
$this->setExpectedException(\RuntimeException::class, 'Message on multilines BBB è');
bbb();
}
/**
*
* @foo
*/
public function testMessageOnMultilinesWithAnotherSpaceAndTag()
{
$this->setExpectedException(\RuntimeException::class, 'Message on multilines CCC ✔');
ccc();
}
}
EOT,
<<<'EOT'
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Message
* on
* multilines AAA
* €
*/
public function testMessageOnMultilines()
{
aaa();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Message
* on
* multilines BBB
* è
* @foo
*/
public function testMessageOnMultilinesWithAnotherTag()
{
bbb();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Message
* on
* multilines CCC
* ✔
*
* @foo
*/
public function testMessageOnMultilinesWithAnotherSpaceAndTag()
{
ccc();
}
}
EOT,
];
yield 'annotation with double @' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* Double "@" is/was below
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* Double "@" is/was below
* @@expectedException FooException
*/
public function testFnc()
{
aaa();
}
}',
];
yield 'annotation with text before @' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* We are providing invalid input, for that we @expectedException FooException
*/
public function testFnc()
{
aaa();
}
}',
];
yield [
'<?php
abstract class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessage
*/
abstract public function testFnc();
}',
];
yield 'expecting exception in single line comment' => [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/** */
public function testFnc()
{
$this->setExpectedException(\FooException::class);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/** @expectedException FooException */
public function testFnc()
{
aaa();
}
}',
];
yield 'expecting exception with message below' => [
'<?php
class MyTest extends TestCase
{
/**
*/
public function testSomething()
{
$this->setExpectedException(\Foo\Bar::class);
$this->initialize();
}
}',
'<?php
class MyTest extends TestCase
{
/**
* @expectedException Foo\Bar
*
* Testing stuff.
*/
public function testSomething()
{
$this->initialize();
}
}',
];
}
/**
* @dataProvider provideWithWhitespacesConfigCases
*/
public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
{
$expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
if (null !== $input) {
$input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
}
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideWithWhitespacesConfigCases(): iterable
{
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class, \'foo\', 123);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessage foo
* @expectedExceptionCode 123
*/
public function testFnc()
{
aaa();
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
*/
public function testFnc()
{
$this->setExpectedException(\FooException::class, \'foo\', 123);
aaa();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException FooException
* @expectedExceptionMessage foo
* @expectedExceptionCode 123
*/
public function testFnc()
{
aaa();
}
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitMockFixerTest.php | tests/Fixer/PhpUnit/PhpUnitMockFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitMockFixerTest 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<int, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->createMock("Foo");
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->getMockWithoutInvokingTheOriginalConstructor("Foo");
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->createMock("Foo");
$this->createMock($foo(1, 2));
$this->getMock("Foo", ["aaa"]);
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->getMock("Foo");
$this->getMock($foo(1, 2));
$this->getMock("Foo", ["aaa"]);
}
}',
['target' => PhpUnitTargetVersion::VERSION_5_4],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->createMock("Foo");
$this->createMock($foo(1, 2));
$this->createPartialMock("Foo", ["aaa"]);
$this->getMock("Foo", ["aaa"], ["argument"]);
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->getMock("Foo");
$this->getMock($foo(1, 2));
$this->getMock("Foo", ["aaa"]);
$this->getMock("Foo", ["aaa"], ["argument"]);
}
}',
];
yield [
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
$this->createMock("Foo",);
$this->createMock("Bar" ,);
$this->createMock("Baz" , );
$this->createMock($foo(1, 2), );
$this->createMock($foo(3, 4, ));
$this->createMock($foo(5, 6, ), );
$this->createPartialMock("Foo", ["aaa"], );
$this->createPartialMock("Foo", ["bbb", ], );
$this->getMock("Foo", ["aaa"], ["argument"], );
$this->getMock("Foo", ["bbb", ], ["argument", ], );
}
}',
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
$this->getMock("Foo",);
$this->getMock("Bar" ,);
$this->getMock("Baz" , );
$this->getMock($foo(1, 2), );
$this->getMock($foo(3, 4, ));
$this->getMock($foo(5, 6, ), );
$this->getMock("Foo", ["aaa"], );
$this->getMock("Foo", ["bbb", ], );
$this->getMock("Foo", ["aaa"], ["argument"], );
$this->getMock("Foo", ["bbb", ], ["argument", ], );
}
}',
];
}
/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->testFix($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: ?string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
$this?->createMock("Foo");
}
}',
'<?php
class FooTest extends TestCase
{
public function testFoo()
{
$this?->getMock("Foo");
}
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php | tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitDedicateAssertFixerTest 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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
self::generateTest('
$this->assertNan($a);
$this->assertNan($a);
$this->assertTrue(test\is_nan($a));
$this->assertTrue(test\a\is_nan($a));
'),
self::generateTest('
$this->assertTrue(is_nan($a));
$this->assertTrue(\is_nan($a));
$this->assertTrue(test\is_nan($a));
$this->assertTrue(test\a\is_nan($a));
'),
];
yield [
self::generateTest('
$this->assertFileExists($a);
$this->assertFileNotExists($a);
$this->assertFileExists($a);
$this->assertFileNotExists($a);
'),
self::generateTest('
$this->assertTrue(file_exists($a));
$this->assertFalse(file_exists($a));
$this->assertTrue(\file_exists($a));
$this->assertFalse(\file_exists($a));
'),
];
yield [
self::generateTest('
$this->assertNull($a);
$this->assertNotNull($a);
$this->assertNull($a);
$this->assertNotNull($a, "my message");
'),
self::generateTest('
$this->assertTrue(is_null($a));
$this->assertFalse(is_null($a));
$this->assertTrue(\is_null($a));
$this->assertFalse(\is_null($a), "my message");
'),
];
yield [
self::generateTest('
$this->assertEmpty($a);
$this->assertNotEmpty($a);
'),
self::generateTest('
$this->assertTrue(empty($a));
$this->ASSERTFALSE(empty($a));
'),
];
yield [
self::generateTest('
$this->assertInfinite($a);
$this->assertFinite($a, "my message");
$this->assertInfinite($a);
$this->assertFinite($a, b"my message");
'),
self::generateTest('
$this->assertTrue(is_infinite($a));
$this->assertFalse(is_infinite($a), "my message");
$this->assertTrue(\is_infinite($a));
$this->assertFalse(\is_infinite($a), b"my message");
'),
];
yield [
self::generateTest('
$this->assertArrayHasKey("test", $a);
$this->assertArrayNotHasKey($b, $a, $c);
'),
self::generateTest('
$this->assertTrue(\array_key_exists("test", $a));
$this->ASSERTFALSE(array_key_exists($b, $a), $c);
'),
];
yield [
self::generateTest('
$this->assertTrue(is_dir($a));
$this->assertTrue(is_writable($a));
$this->assertTrue(is_readable($a));
'),
null,
['target' => PhpUnitTargetVersion::VERSION_5_0],
];
yield [
self::generateTest('
$this->assertTrue(is_dir($a));
$this->assertTrue(is_writable($a));
$this->assertTrue(is_readable($a));
'),
null,
['target' => PhpUnitTargetVersion::VERSION_3_0],
];
yield [
self::generateTest('
$this->assertDirectoryNotExists($a);
$this->assertNotIsWritable($a);
$this->assertNotIsReadable($a);
'),
self::generateTest('
$this->assertFalse(is_dir($a));
$this->assertFalse(is_writable($a));
$this->assertFalse(is_readable($a));
'),
['target' => PhpUnitTargetVersion::VERSION_5_6],
];
yield [
self::generateTest('
$this->assertDirectoryExists($a);
$this->assertIsWritable($a);
$this->assertIsReadable($a);
'),
self::generateTest('
$this->assertTrue(is_dir($a));
$this->assertTrue(is_writable($a));
$this->assertTrue(is_readable($a));
'),
['target' => PhpUnitTargetVersion::VERSION_NEWEST],
];
foreach (['array', 'bool', 'callable', 'double', 'float', 'int', 'integer', 'long', 'numeric', 'object', 'real', 'scalar', 'string'] as $type) {
yield [
self::generateTest(\sprintf('$this->assertInternalType(\'%s\', $a);', $type)),
self::generateTest(\sprintf('$this->assertTrue(is_%s($a));', $type)),
];
yield [
self::generateTest(\sprintf('$this->assertNotInternalType(\'%s\', $a);', $type)),
self::generateTest(\sprintf('$this->assertFalse(is_%s($a));', $type)),
];
}
yield [
self::generateTest('$this->assertInternalType(\'float\', $a, "my message");'),
self::generateTest('$this->assertTrue(is_float( $a), "my message");'),
];
yield [
self::generateTest('$this->assertInternalType(\'float\', $a);'),
self::generateTest('$this->assertTrue(\IS_FLOAT($a));'),
];
yield [
self::generateTest('$this->assertInternalType(#
\'float\'#
, #
$a#
#
)#
;'),
self::generateTest('$this->assertTrue(#
\IS_FLOAT#
(#
$a#
)#
)#
;'),
];
yield [
self::generateTest('static::assertInternalType(\'float\', $a);'),
self::generateTest('static::assertTrue(is_float( $a));'),
];
yield [
self::generateTest('self::assertInternalType(\'float\', $a);'),
self::generateTest('self::assertTrue(is_float( $a));'),
];
yield [
self::generateTest('static::assertNull($a);'),
self::generateTest('static::assertTrue(is_null($a));'),
];
yield [
self::generateTest('self::assertNull($a);'),
self::generateTest('self::assertTrue(is_null($a));'),
];
yield [
self::generateTest('SELF::assertNull($a);'),
self::generateTest('SELF::assertTrue(is_null($a));'),
];
yield [
self::generateTest('self::assertStringContainsString($needle, $haystack);'),
self::generateTest('self::assertTrue(str_contains($haystack, $needle));'),
['target' => PhpUnitTargetVersion::VERSION_NEWEST],
];
yield [
self::generateTest('self::assertStringNotContainsString($needle, $a[$haystack.""](123)[foo()]);'),
self::generateTest('self::assertFalse(str_contains($a[$haystack.""](123)[foo()], $needle));'),
['target' => PhpUnitTargetVersion::VERSION_NEWEST],
];
yield [
self::generateTest('self::assertStringStartsWith($needle, $haystack);'),
self::generateTest('self::assertTrue(str_starts_with($haystack, $needle));'),
];
yield [
self::generateTest('self::assertStringStartsNotWith($needle, $haystack);'),
self::generateTest('self::assertFalse(str_starts_with($haystack, $needle));'),
];
yield [
self::generateTest('self::assertStringStartsNotWith( #3
$needle#4
, #1
$haystack#2
);'),
self::generateTest('self::assertFalse(str_starts_with( #1
$haystack#2
,#3
$needle#4
));'),
];
yield [
self::generateTest('self::assertStringEndsWith($needle, $haystack);'),
self::generateTest('self::assertTrue(str_ends_with($haystack, $needle));'),
];
yield [
self::generateTest('self::assertStringEndsNotWith($needle, $haystack);'),
self::generateTest('self::assertFalse(str_ends_with($haystack, $needle));'),
];
yield '$a instanceof class' => [
self::generateTest('
$this->assertInstanceOf(SomeClass::class, $x);
$this->assertInstanceOf(SomeClass::class, $y, $message);
'),
self::generateTest('
$this->assertTrue($x instanceof SomeClass);
$this->assertTrue($y instanceof SomeClass, $message);
'),
];
yield '$a instanceof class\a\b' => [
self::generateTest('
$this->assertInstanceOf(\PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer::class, $ii);
'),
self::generateTest('
$this->assertTrue($ii instanceof \PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer);
'),
];
yield '$a instanceof $b' => [
self::generateTest('
$this->assertInstanceOf($tty, $abc/* 1 *//* 2 */);
$this->assertInstanceOf($oo, $def, $message);
'),
self::generateTest('
$this->assertTrue($abc instanceof /* 1 */$tty /* 2 */);
$this->assertTrue($def instanceof $oo, $message);
'),
];
yield 'do not fix instance of' => [
self::generateTest('
$this->assertTrue($gg instanceof $ijl . "X", $something);
$this->assertTrue($ff instanceof $klh . $b(1,2,$message), $noMsg);
'),
];
yield '!$a instanceof class' => [
self::generateTest('
$this->assertNotInstanceOf(SomeClass::class, $x);
$this->assertNotInstanceOf(SomeClass::class, $y, $message);
'),
self::generateTest('
$this->assertTrue(!$x instanceof SomeClass);
$this->assertTrue(!$y instanceof SomeClass, $message);
'),
];
yield 'assertFalse with instanceof' => [
self::generateTest('
self::assertNotInstanceOf(Foo::class, $a);
self::assertInstanceOf(Foo::class, $b);
'),
self::generateTest('
self::assertFalse($a instanceof Foo);
self::assertFalse(!$b instanceof Foo);
'),
];
yield 'not a method call' => [
self::generateTest('echo $this->assertTrue;'),
];
yield 'wrong argument count 1' => [
self::generateTest('static::assertTrue(is_null($a, $b));'),
];
yield 'wrong argument count 2' => [
self::generateTest('static::assertTrue(is_int($a, $b));'),
];
yield [
self::generateTest('
$this->assertTrue(is_null);
$this->assertTrue(is_int($a) && $b);
$this->assertFalse(is_nan($a));
$this->assertTrue(is_int($a) || \is_bool($b));
$this->assertTrue($a&&is_int($a));
static::assertTrue(is_null);
self::assertTrue(is_null);
'),
];
yield 'not in class' => [
'<?php self::assertTrue(is_null($a));',
];
// Do not replace is_resource() by assertIsResource().
// is_resource() also checks if the resource is open or closed,
// while assertIsResource() does not.
yield 'Do not replace is_resource' => [
self::generateTest('self::assertTrue(is_resource($resource));'),
];
foreach (self::provideTestAssertCountCases() as $index => $case) {
foreach (['count', 'sizeof'] as $function) {
yield $function.' - '.$index => array_map(
static fn (string $code): string => \sprintf($code, $function),
$case,
);
}
}
foreach (self::provideTestAssertCountCasingCases() as $case) {
foreach (['COUNT', 'SIZEOF'] as $function) {
yield [
$case[0],
\sprintf($case[1], $function),
];
}
}
yield [
self::generateTest('$this->assertNan($a, );'),
self::generateTest('$this->assertTrue(is_nan($a), );'),
];
yield [
self::generateTest('$this->assertNan($a);'),
self::generateTest('$this->assertTrue(is_nan($a, ));'),
];
yield [
self::generateTest('$this->assertNan($a, );'),
self::generateTest('$this->assertTrue(is_nan($a, ), );'),
];
yield [
self::generateTest('$this->assertInternalType(\'array\', $a,);'),
self::generateTest('$this->assertTrue(is_array($a,),);'),
];
yield [
self::generateTest('$this->assertNan($b);'),
self::generateTest('$this->assertTrue(\is_nan($b,));'),
];
yield [
self::generateTest('$this->assertFileExists($f, \'message\',);'),
self::generateTest('$this->assertTrue(file_exists($f,), \'message\',);'),
];
yield [
self::generateTest('$this->assertNan($y , );'),
self::generateTest('$this->assertTrue(is_nan($y) , );'),
];
yield 'str_starts_with with trailing ","' => [
self::generateTest('self::assertStringStartsWith($needle, $haystack);'),
self::generateTest('self::assertTrue(str_starts_with($haystack, $needle,));'),
];
}
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('/^\[php_unit_dedicate_assert\] Invalid configuration: The option "target" .*\.$/');
$this->fixer->configure(['target' => '_unknown_']); // @phpstan-ignore-line
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->fixer->configure(['target' => PhpUnitTargetVersion::VERSION_NEWEST]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
self::generateTest('$a = $this->assertTrue(...);'),
];
}
/**
* @return iterable<int, array{string, string}>
*/
private static function provideTestAssertCountCasingCases(): iterable
{
yield [
self::generateTest('$this->assertCount(1, $a);'),
self::generateTest('$this->assertSame(1, %s($a));'),
];
yield [
self::generateTest('$this->assertCount(1, $a);'),
self::generateTest('$this->assertSame(1, \%s($a));'),
];
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
private static function provideTestAssertCountCases(): iterable
{
// positive fixing
yield 'assert same' => [
self::generateTest('$this->assertCount(1, $a);'),
self::generateTest('$this->assertSame(1, %s($a));'),
];
yield 'assert equals' => [
self::generateTest('$this->assertCount(2, $b);'),
self::generateTest('$this->assertEquals(2, %s($b));'),
];
// negative fixing
yield 'assert not same' => [
self::generateTest('$this->assertNotCount(11, $c);'),
self::generateTest('$this->assertNotSame(11, %s($c));'),
];
yield 'assert not equals' => [
self::generateTest('$this->assertNotCount(122, $d);'),
self::generateTest('$this->assertNotEquals(122, %s($d));'),
];
// other cases
yield 'assert same with namespace' => [
self::generateTest('$this->assertCount(1, $a);'),
self::generateTest('$this->assertSame(1, \%s($a));'),
];
yield 'no spacing' => [
self::generateTest('$this->assertCount(1,$a);'),
self::generateTest('$this->assertSame(1,%s($a));'),
];
yield 'lot of spacing' => [
self::generateTest('$this->assertCount(
1
,
'.'
'.'
$a
'.'
)
;'),
self::generateTest('$this->assertSame(
1
,
%s
(
$a
)
)
;'),
];
yield 'lot of fix cases' => [
self::generateTest('
$this->assertCount(1, $a);
$this->assertCount(2, $a);
$this->assertCount(3, $a);
$this->assertNotCount(4, $a);
$this->assertCount(5, $a, "abc");
$this->assertCount(6, $a, "def");
'),
self::generateTest('
$this->assertSame(1, %1$s($a));
$this->assertSame(2, %1$s($a));
$this->assertEquals(3, %1$s($a));
$this->assertNotSame(4, %1$s($a));
$this->assertEquals(5, %1$s($a), "abc");
$this->assertSame(6, \%1$s($a), "def");
'),
];
yield 'comment handling' => [
self::generateTest('$this->assertCount(# 0
1# 1
,# 2
# 3
# 4
$a# 5
# 6
)# 7
;# 8'),
self::generateTest('$this->assertSame(# 0
1# 1
,# 2
%s# 3
(# 4
$a# 5
)# 6
)# 7
;# 8'),
];
yield 'expected variable' => [
self::generateTest('$this->assertCount($b, $a);'),
self::generateTest('$this->assertSame($b, %s($a));'),
];
yield 'do not fix 1' => [
self::generateTest('$this->assertSame($b[1], %s($a));'),
];
yield 'do not fix 2' => [
self::generateTest('$this->assertSame(b(), %s($a));'),
];
yield 'do not fix 3' => [
self::generateTest('$this->assertSame(1.0, %s($a));'),
];
yield 'do not fix 4' => [
self::generateTest('$this->assertSame(1);'),
];
yield 'do not fix 5' => [
self::generateTest('$this->assertSame(1, "%s");'),
];
yield 'do not fix 6' => [
self::generateTest('$this->test(); // $this->assertSame($b, %s($a));'),
];
yield 'do not fix 7' => [
self::generateTest('$this->assertSame(2, count($array) - 1);'),
];
yield 'do not fix 8' => [
self::generateTest('
Foo::assertSame(1, sizeof($a));
$this->assertSame(1, sizeof2(2));
$this->assertSame(1, sizeof::foo);
'),
];
}
private static function generateTest(string $content): string
{
return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitExpectationFixerTest.php | tests/Fixer/PhpUnit/PhpUnitExpectationFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitExpectationFixerTest 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<int, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->expectException(\'RuntimeException\');
$this->expectExceptionMessage(\'msg\'/*B*/);
$this->expectExceptionCode(/*C*/123);
zzz();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123);
zzz();
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->expectException(\'RuntimeException\'/*B*/ /*B2*/);
$this->expectExceptionCode(/*C*/123);
zzz();
}
function testFnc2()
{
aaa();
$this->expectException(\'RuntimeException\');
$this->expectExceptionMessage(/*B*/ null /*B2*/ + 1);
$this->expectExceptionCode(/*C*/123);
zzz();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/,/*C*/123);
zzz();
}
function testFnc2()
{
aaa();
$this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/ + 1,/*C*/123);
zzz();
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
$this->expectException(
\Exception::class
);
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
$this->setExpectedException(
\Exception::class
);
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
$this->expectException(
\Exception::class
);
$this->expectExceptionMessage(
"foo"
);
$this->expectExceptionCode(
123
);
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
$this->setExpectedException(
\Exception::class,
"foo",
123
);
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessage("Msg");
$this->expectExceptionCode(123);
foo();
}
public function testBar()
{
$this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
bar();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->setExpectedException("RuntimeException", "Msg", 123);
foo();
}
public function testBar()
{
$this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
bar();
}
}',
['target' => PhpUnitTargetVersion::VERSION_5_2],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessage("Msg");
$this->expectExceptionCode(123);
foo();
}
public function testBar()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessageRegExp("/Msg.*/");
$this->expectExceptionCode(123);
bar();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->setExpectedException("RuntimeException", "Msg", 123);
foo();
}
public function testBar()
{
$this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
bar();
}
}',
['target' => PhpUnitTargetVersion::VERSION_5_6],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessage("Msg");
$this->expectExceptionCode(123);
foo();
}
public function testBar()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessageMatches("/Msg.*/");
$this->expectExceptionCode(123);
bar();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->setExpectedException("RuntimeException", "Msg", 123);
foo();
}
public function testBar()
{
$this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
bar();
}
}',
['target' => PhpUnitTargetVersion::VERSION_8_4],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->expectExceptionMessageMatches("/Msg.*/");
foo();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->expectExceptionMessageRegExp("/Msg.*/");
foo();
}
}',
['target' => PhpUnitTargetVersion::VERSION_8_4],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
// turns wrong into wrong: has a single argument only, but ...
$this->expectExceptionMessageMatches("/Msg.*/");
$this->expectExceptionMessageMatches("fail-case");
foo();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
// turns wrong into wrong: has a single argument only, but ...
$this->expectExceptionMessageRegExp("/Msg.*/", "fail-case");
foo();
}
}',
['target' => PhpUnitTargetVersion::VERSION_8_4],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessage("Msg");
$this->expectExceptionCode(123);
foo();
}
public function testBar()
{
$this->expectException("RuntimeException");
$this->expectExceptionMessageMatches("/Msg.*/");
$this->expectExceptionCode(123);
bar();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$this->setExpectedException("RuntimeException", "Msg", 123);
foo();
}
public function testBar()
{
$this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
bar();
}
}',
['target' => PhpUnitTargetVersion::VERSION_NEWEST],
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->expectException("RuntimeException");
$this->expectExceptionMessage("msg"/*B*/);
$this->expectExceptionCode(/*C*/123);
zzz();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->setExpectedException("RuntimeException", "msg"/*B*/, /*C*/123, );
zzz();
}
}',
];
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->expectException("RuntimeException");
$this->expectExceptionMessage("msg"/*B*/);
$this->expectExceptionCode(/*C*/123/*D*/);
zzz();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this->setExpectedException("RuntimeException", "msg"/*B*/, /*C*/123, /*D*/);
zzz();
}
}',
];
}
/**
* @dataProvider provideWithWhitespacesConfigCases
*/
public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
{
$expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
if (null !== $input) {
$input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
}
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideWithWhitespacesConfigCases(): iterable
{
$expectedTemplate = '
function testFnc%d()
{
aaa();
$this->expectException(\'RuntimeException\');
$this->expectExceptionMessage(\'msg\'/*B*/);
$this->expectExceptionCode(/*C*/123);
zzz();
}
';
$inputTemplate = '
function testFnc%d()
{
aaa();
$this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123);
zzz();
}
';
$input = $expected = '<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
';
for ($i = 0; $i < 8; ++$i) {
$expected .= \sprintf($expectedTemplate, $i);
$input .= \sprintf($inputTemplate, $i);
}
$expected .= "\n}";
$input .= "\n}";
yield [$expected, $input];
}
/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->testFix($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: ?string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this?->expectException("RuntimeException");
$this->expectExceptionMessage("message");
$this->expectExceptionCode(123);
zzz();
}
}',
'<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
function testFnc()
{
aaa();
$this?->setExpectedException("RuntimeException", "message", 123);
zzz();
}
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php | tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer>
*
* @author Gert de Pagter
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitTestAnnotationFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'Annotation is used, and it should not be' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
public function testItDoesSomething() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function itDoesSomething() {}
}',
['style' => 'prefix'],
];
yield 'Annotation is not used, but should be' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function itDoesSomething() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testItDoesSomething() {}
}',
['style' => 'annotation'],
];
yield 'Annotation is not used, but should be, class is extra indented' => [
'<?php
if (1) {
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function itDoesSomething() {}
}
}',
'<?php
if (1) {
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testItDoesSomething() {}
}
}',
['style' => 'annotation'],
];
yield 'Annotation is not used, but should be, and there is already a docBlock' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @dataProvider blabla
*
* @test
*/
public function itDoesSomething() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @dataProvider blabla
*/
public function testItDoesSomething() {}
}',
['style' => 'annotation'],
];
yield 'Annotation is used, but should not be, and it depends on other tests' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
public function testAaa () {}
public function helperFunction() {}
/**
* @depends testAaa
*
*
*/
public function testBbb () {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function aaa () {}
public function helperFunction() {}
/**
* @depends aaa
*
* @test
*/
public function bbb () {}
}',
['style' => 'prefix'],
];
yield 'Annotation is not used, but should be, and it depends on other tests' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function aaa () {}
/**
* @depends aaa
*
* @test
*/
public function bbb () {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testAaa () {}
/**
* @depends testAaa
*/
public function testBbb () {}
}',
['style' => 'annotation'],
];
yield 'Annotation is removed, the function is one word and we want it to use camel case' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
public function testWorks() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function works() {}
}',
];
yield 'Annotation is added, and it is snake case' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function it_has_snake_case() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test_it_has_snake_case() {}
}',
['style' => 'annotation'],
];
yield 'Annotation gets added, it has an @depends, and we use snake case' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function works_fine () {}
/**
* @depends works_fine
*
* @test
*/
public function works_fine_too() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test_works_fine () {}
/**
* @depends test_works_fine
*/
public function test_works_fine_too() {}
}',
['style' => 'annotation'],
];
yield 'Class has both camel and snake case, annotated functions and not, and wants to add annotations' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function snake_cased () {}
/**
* @test
*/
public function camelCased () {}
/**
* Description.
*
* @depends camelCased
*
* @test
*/
public function depends_on_someone () {}
//It even has a comment
public function a_helper_function () {}
/**
* @depends depends_on_someone
*
* @test
*/
public function moreDepends() {}
/**
* @depends depends_on_someone
*
* @test
*/
public function alreadyAnnotated() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test_snake_cased () {}
public function testCamelCased () {}
/**
* Description.
*
* @depends testCamelCased
*/
public function test_depends_on_someone () {}
//It even has a comment
public function a_helper_function () {}
/**
* @depends test_depends_on_someone
*/
public function testMoreDepends() {}
/**
* @depends test_depends_on_someone
*
* @test
*/
public function alreadyAnnotated() {}
}',
['style' => 'annotation'],
];
yield 'Annotation has to be added to multiple functions' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function itWorks() {}
/**
* @test
*/
public function itDoesSomething() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testItWorks() {}
public function testItDoesSomething() {}
}',
['style' => 'annotation'],
];
yield 'Class with big doc blocks and multiple functions has to remove annotations' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* This test is part of the database group and has a provider.
*
* @param int $paramOne
* @param bool $paramTwo
*
*
* @dataProvider provides
* @group Database
*/
public function testDatabase ($paramOne, $paramTwo) {}
/**
* Provider for the database test function
*
* @return array
*/
public function provides() {}
/**
* I am just a helper function but I have test in my name.
* I also have a doc Block
*
* @return Foo\Bar
*/
public function help_test() {}
protected function setUp() {}
/**
* I depend on the database function, but I already
* had test in my name and a docblock
*
* @depends testDatabase
*/
public function testDepends () {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* This test is part of the database group and has a provider.
*
* @param int $paramOne
* @param bool $paramTwo
*
* @test
* @dataProvider provides
* @group Database
*/
public function database ($paramOne, $paramTwo) {}
/**
* Provider for the database test function
*
* @return array
*/
public function provides() {}
/**
* I am just a helper function but I have test in my name.
* I also have a doc Block
*
* @return Foo\Bar
*/
public function help_test() {}
protected function setUp() {}
/**
* I depend on the database function, but I already
* had test in my name and a docblock
*
* @depends database
*/
public function testDepends () {}
}',
];
yield 'Test Annotation has to be removed, but its just one line' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** */
public function testItWorks() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** @test */
public function itWorks() {}
}',
];
yield 'Test annotation has to be added, but there is already a one line doc block' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @group Database
*
* @test
*/
public function itTestsDatabase() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** @group Database */
public function testItTestsDatabase() {}
}',
['style' => 'annotation'],
];
yield 'Test annotation has to be added, but there is already a one line doc block which is a sentence' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* I really like this test, it helps a lot
*
* @test
*/
public function itTestsDatabase() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** I really like this test, it helps a lot */
public function testItTestsDatabase() {}
}',
['style' => 'annotation'],
];
yield 'Test annotation has to be added, but there is already a one line comment present' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
//I really like this test, it helps a lot
/**
* @test
*/
public function itTestsDatabase() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
//I really like this test, it helps a lot
public function testItTestsDatabase() {}
}',
['style' => 'annotation'],
];
yield 'Test annotation has to be added, there is a one line doc block which is an @depends tag' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function itTestsDatabase() {}
/**
* @depends itTestsDatabase
*
* @test
*/
public function itDepends() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testItTestsDatabase() {}
/** @depends testItTestsDatabase */
public function testItDepends() {}
}',
['style' => 'annotation'],
];
yield 'Annotation gets removed, but the function has a @testWith' => [
'<?php
final class ProcessLinterProcessBuilderTest extends TestCase
{
/**
*
* @param string $executable
* @param string $file
* @param string $expected
*
* @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
* ["C:\Program Files\php\php.exe", "foo bar\baz.php", "\"C:\Program Files\php\php.exe\" -l \"foo bar\baz.php\""]
* @requires OS Linux|Darwin
*/
public function testPrepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
{
$builder = new ProcessLinterProcessBuilder($executable);
$this->assertSame(
$expected,
$builder->build($file)->getCommandLine()
);
}
}',
'<?php
final class ProcessLinterProcessBuilderTest extends TestCase
{
/**
* @test
* @param string $executable
* @param string $file
* @param string $expected
*
* @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
* ["C:\Program Files\php\php.exe", "foo bar\baz.php", "\"C:\Program Files\php\php.exe\" -l \"foo bar\baz.php\""]
* @requires OS Linux|Darwin
*/
public function prepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
{
$builder = new ProcessLinterProcessBuilder($executable);
$this->assertSame(
$expected,
$builder->build($file)->getCommandLine()
);
}
}',
];
yield 'Annotation gets added, but there is already an @testWith in the doc block' => [
'<?php
final class ProcessLinterProcessBuilderTest extends TestCase
{
/**
* @param string $executable
* @param string $file
* @param string $expected
*
* @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
* ["C:\Program Files\php\php.exe", "foo bar\baz.php", "\"C:\Program Files\php\php.exe\" -l \"foo bar\baz.php\""]
* @requires OS Linux|Darwin
*
* @test
*/
public function prepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
{
$builder = new ProcessLinterProcessBuilder($executable);
$this->assertSame(
$expected,
$builder->build($file)->getCommandLine()
);
}
}',
'<?php
final class ProcessLinterProcessBuilderTest extends TestCase
{
/**
* @param string $executable
* @param string $file
* @param string $expected
*
* @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
* ["C:\Program Files\php\php.exe", "foo bar\baz.php", "\"C:\Program Files\php\php.exe\" -l \"foo bar\baz.php\""]
* @requires OS Linux|Darwin
*/
public function testPrepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
{
$builder = new ProcessLinterProcessBuilder($executable);
$this->assertSame(
$expected,
$builder->build($file)->getCommandLine()
);
}
}',
['style' => 'annotation'],
];
yield 'Annotation gets properly removed, even when it is in a weird place' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* I am a comment about the function
*/
public function testIHateMyTestSuite() {}
/**
* I am another comment about a function
*/
public function testThisMakesNoSense() {}
/**
* This comment has more issues
*/
public function testItUsesTabs() {}
/**
* @depends testItUsesTabs
*/
public function testItDependsReally() {}
/**
* @depends testItUsesTabs
*/
public function testItDependsSomeMore() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* I am a comment @test about the function
*/
public function iHateMyTestSuite() {}
/**
* I am another comment about a function @test
*/
public function thisMakesNoSense() {}
/**
* This comment has @test more issues
*/
public function itUsesTabs() {}
/**
* @depends itUsesTabs @test
*/
public function itDependsReally() {}
/**
* @test @depends itUsesTabs
*/
public function itDependsSomeMore() {}
}',
];
yield 'Annotation gets added when a single line has doc block has multiple tags already' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* There is some text here @group Database @group Integration
*
* @test
*/
public function whyDoThis() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** There is some text here @group Database @group Integration */
public function testWhyDoThis() {}
}',
['style' => 'annotation'],
];
yield 'Annotation gets removed when a single line doc block has the tag, but there are other things as well' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** There is some text here @group Database @group Integration */
public function testWhyDoThis() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** There is some @test text here @group Database @group Integration */
public function testWhyDoThis() {}
}',
];
yield 'Annotation is used, and should be' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function itDoesSomething() {}
}',
null,
['style' => 'annotation'],
];
yield 'Annotation is not used, and should not be' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testItDoesSomethingWithoutPhpDoc() {}
/**
* No annotation, just text
*/
public function testItDoesSomethingWithPhpDoc() {}
public function testingItDoesSomethingWithoutPhpDoc() {}
/**
* No annotation, just text
*/
public function testingItDoesSomethingWithPhpDoc() {}
}',
];
yield 'Annotation is added when it is already present in a weird place' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* I am a comment @test about the function
*
* @test
*/
public function iHateMyTestSuite() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* I am a comment @test about the function
*/
public function iHateMyTestSuite() {}
}',
['style' => 'annotation'],
];
yield 'Docblock does not get converted to a multi line doc block if it already has @test annotation' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/** @test */
public function doesSomeThings() {}
}',
null,
['style' => 'annotation'],
];
yield 'Annotation does not get added if class is not a test' => [
'<?php
class Waterloo
{
public function testDoesSomeThings() {}
}',
null,
['style' => 'annotation'],
];
yield 'Annotation does not get removed if class is not a test' => [
'<?php
class Waterloo
{
/**
* @test
*/
public function doesSomeThings() {}
}',
];
yield 'Annotation does not get added if there are no tests in the test class' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function setUp() {}
public function itHelpsSomeTests() {}
public function someMoreChanges() {}
}',
null,
['style' => 'annotation'],
];
yield 'Abstract test gets annotation removed' => [
'<?php
abstract class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
abstract function testFooBar();
}',
'<?php
abstract class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
abstract function fooBar();
}',
['style' => 'prefix'],
];
yield 'Annotation present, but method already have test prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
public function testarossaIsFromItaly() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function testarossaIsFromItaly() {}
}',
['style' => 'prefix'],
];
yield 'Annotation present, but method is test prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
public function test() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test() {}
}',
['style' => 'prefix'],
];
yield 'Abstract test gets annotation added' => [
'<?php
abstract class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
abstract function fooBar();
}',
'<?php
abstract class Test extends \PhpUnit\FrameWork\TestCase
{
abstract function testFooBar();
}',
['style' => 'annotation'],
];
yield 'Annotation gets added, but there is a number after the testprefix so it keeps the prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test123fooBar() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test123fooBar() {}
}',
['style' => 'annotation'],
];
yield 'Annotation missing, but there is a lowercase character after the test prefix so it keeps the prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function testarossaIsFromItaly() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testarossaIsFromItaly() {}
}',
['style' => 'annotation'],
];
yield 'Annotation present, but there is a lowercase character after the test prefix so it keeps the prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function testarossaIsFromItaly() {}
}',
null,
['style' => 'annotation'],
];
yield 'Annotation missing, method qualifies as test, but test prefix cannot be removed' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test() {}
}',
['style' => 'annotation'],
];
yield 'Annotation missing, method qualifies as test, but test_ prefix cannot be removed' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test_() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test_() {}
}',
['style' => 'annotation'],
];
yield 'Annotation present, method qualifies as test, but test_ prefix cannot be removed' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test_() {}
}',
null,
['style' => 'annotation'],
];
yield 'Annotation missing, method after fix still has "test" prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test_foo() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test_test_foo() {}
}',
['style' => 'annotation'],
];
yield 'do not touch single line @depends annotation when already correct' => [
'<?php class FooTest extends \PHPUnit\Framework\TestCase
{
public function testOne() {}
/** @depends testOne */
public function testTwo() {}
/** @depends testTwo */
public function testThree() {}
}',
];
}
/**
* @param _AutogeneratedInputConfiguration $config
*
* @dataProvider provideWithWhitespacesConfigCases
*/
public function testWithWhitespacesConfig(string $expected, ?string $input = null, array $config = []): void
{
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->fixer->configure($config);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideWithWhitespacesConfigCases(): iterable
{
yield [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
*
*/
public function testFooTest() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @test
*/
public function fooTest() {}
}
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @param _AutogeneratedInputConfiguration $configuration
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, string $input, array $configuration): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, 1?: ?string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
#[OneTest]
public function itWorks() {}
/**
* @test
*/
#[TwoTest]
public function itDoesSomething() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
#[OneTest]
public function testItWorks() {}
#[TwoTest]
public function testItDoesSomething() {}
}',
['style' => 'annotation'],
];
yield [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
#[OneTest]
#[Internal]
public function itWorks() {}
/**
* @test
*/
#[TwoTest]
#[Internal]
public function itDoesSomething() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
#[OneTest]
#[Internal]
public function testItWorks() {}
#[TwoTest]
#[Internal]
public function testItDoesSomething() {}
}',
['style' => 'annotation'],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitFqcnAnnotationFixerTest.php | tests/Fixer/PhpUnit/PhpUnitFqcnAnnotationFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitFqcnAnnotationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitFqcnAnnotationFixer>
*
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitFqcnAnnotationFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'EOF'
<?php
/**
* @covers \Foo
* @covers ::fooMethod
* @coversDefaultClass \Bar
*/
class FooTest extends TestCase {
/**
* @ExpectedException Value
* @expectedException \X
* @expectedException
* @expectedException \Exception
* @expectedException \Some\Exception\ClassName
* @expectedExceptionCode 123
* @expectedExceptionMessage Foo bar
*
* @uses \Baz
* @uses \selfieGenerator
* @uses self::someFunction
* @uses static::someOtherFunction
*/
}
EOF,
<<<'EOF'
<?php
/**
* @covers Foo
* @covers ::fooMethod
* @coversDefaultClass Bar
*/
class FooTest extends TestCase {
/**
* @ExpectedException Value
* @expectedException X
* @expectedException
* @expectedException \Exception
* @expectedException Some\Exception\ClassName
* @expectedExceptionCode 123
* @expectedExceptionMessage Foo bar
*
* @uses Baz
* @uses selfieGenerator
* @uses self::someFunction
* @uses static::someOtherFunction
*/
}
EOF,
];
yield [
'<?php
class Foo {
/**
* @expectedException Some\Exception\ClassName
* @covers Foo
* @uses Baz
* @uses self::someFunction
*/
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitDataProviderReturnTypeFixerTest.php | tests/Fixer/PhpUnit/PhpUnitDataProviderReturnTypeFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderReturnTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderReturnTypeFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitDataProviderReturnTypeFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'data provider with iterable return type' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public function provideFooCases() : iterable {}
}',
];
yield 'data provider without return type' => self::mapToTemplate(
': iterable',
'',
);
yield 'data provider with array return type' => self::mapToTemplate(
': iterable',
': array',
);
yield 'data provider with return type and comment' => self::mapToTemplate(
': /* foo */ iterable',
': /* foo */ array',
);
yield 'data provider with return type namespaced class' => self::mapToTemplate(
': iterable',
': Foo\Bar',
);
yield 'data provider with iterable return type in different case' => self::mapToTemplate(
': iterable',
': Iterable',
);
yield 'multiple data providers' => [
'<?php class FooTest extends TestCase {
/**
* @dataProvider provider4
* @dataProvider provider1
* @dataProvider provider5
* @dataProvider provider6
* @dataProvider provider2
* @dataProvider provider3
*/
public function testFoo() {}
public function provider1(): iterable {}
public function provider2(): iterable {}
public function provider3(): iterable {}
public function provider4(): iterable {}
public function provider5(): iterable {}
public function provider6(): iterable {}
}',
'<?php class FooTest extends TestCase {
/**
* @dataProvider provider4
* @dataProvider provider1
* @dataProvider provider5
* @dataProvider provider6
* @dataProvider provider2
* @dataProvider provider3
*/
public function testFoo() {}
public function provider1() {}
public function provider2() {}
public function provider3() {}
public function provider4() {}
public function provider5() {}
public function provider6() {}
}',
];
yield 'advanced case' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
* @dataProvider provideFooCases2
*/
public function testFoo()
{
/**
* @dataProvider someFunction
*/
$foo = /** foo */ function ($x) { return $x + 1; };
/**
* @dataProvider someFunction2
*/
/* foo */someFunction2();
}
/**
* @dataProvider provideFooCases3
*/
public function testBar() {}
public function provideFooCases(): iterable {}
public function provideFooCases2(): iterable {}
public function provideFooCases3(): iterable {}
public function someFunction() {}
public function someFunction2() {}
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
* @dataProvider provideFooCases2
*/
public function testFoo()
{
/**
* @dataProvider someFunction
*/
$foo = /** foo */ function ($x) { return $x + 1; };
/**
* @dataProvider someFunction2
*/
/* foo */someFunction2();
}
/**
* @dataProvider provideFooCases3
*/
public function testBar() {}
public function provideFooCases() {}
public function provideFooCases2() {}
public function provideFooCases3() {}
public function someFunction() {}
public function someFunction2() {}
}',
];
foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
yield \sprintf('test function with %s modifier', $modifier) => [
\sprintf('<?php
abstract class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
%s function testFoo() %s
public function provideFooCases(): iterable {}
}
', $modifier, 'abstract' === $modifier ? ';' : '{}'),
\sprintf('<?php
abstract class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
%s function testFoo() %s
public function provideFooCases() {}
}
', $modifier, 'abstract' === $modifier ? ';' : '{}'),
];
}
}
/**
* @requires PHP <8.0
*
* @dataProvider provideFixPre80Cases
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'data provider with return type namespaced class starting with iterable' => self::mapToTemplate(
': iterable \ Foo',
);
yield 'data provider with return type namespaced class and comments' => self::mapToTemplate(
': iterable/* Some info */\/* More info */Bar',
);
}
/**
* @requires PHP ^8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'with an attribute between PHPDoc and test method' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomAttribute]
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomAttribute]
public function testFoo(): void {}
public function provideFooCases() {}
}
PHP,
];
yield 'with data provider as an attribute' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('addTypeToMe')]
public function testFoo(): void {}
public function addTypeToMe(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('addTypeToMe')]
public function testFoo(): void {}
public function addTypeToMe() {}
}
PHP,
];
$withAttributesTemplate = <<<'PHP'
<?php
namespace N;
use PHPUnit\Framework as PphUnitAlias;
use PHPUnit\Framework\Attributes;
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('provider1')]
#[\PHPUnit\Framework\Attributes\DataProvider('doNotGetFooledByConcatenation' . 'notProvider1')]
#[PHPUnit\Framework\Attributes\DataProvider('notProvider2')]
#[
\PHPUnit\Framework\Attributes\BackupGlobals(true),
\PHPUnit\Framework\Attributes\DataProvider('provider2'),
\PHPUnit\Framework\Attributes\Group('foo'),
]
#[Attributes\DataProvider('provider3')]
#[PphUnitAlias\Attributes\DataProvider('provider4')]
#[\PHPUnit\Framework\Attributes\DataProvider]
#[\PHPUnit\Framework\Attributes\DataProvider('provider5')]
#[\PHPUnit\Framework\Attributes\DataProvider(123)]
public function testSomething(int $x): void {}
public function provider1()%1$s {}
public function provider2()%1$s {}
public function provider3()%1$s {}
public function provider4()%1$s {}
public function provider5()%1$s {}
public function notProvider1() {}
public function notProvider2() {}
}
PHP;
yield 'with multiple data providers as an attributes' => [
\sprintf($withAttributesTemplate, ': iterable'),
\sprintf($withAttributesTemplate, ''),
];
}
/**
* @param array{string, 1?: string} ...$types
*
* @return array{string, 1?: string}
*/
private static function mapToTemplate(string ...$types): array
{
// @phpstan-ignore-next-line return.type
return array_map(
static fn (string $type): string => \sprintf(
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
/**
* @dataProvider provider
*/
public function testBar() {}
public function provideFooCases()%1$s {}
public function provider()%1$s {}
public function notProvider(): array {}
}
PHP,
$type,
),
$types,
);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitStrictFixerTest.php | tests/Fixer/PhpUnit/PhpUnitStrictFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitStrictFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
$this->fixer->configure(['assertions' => array_keys(self::getMethodsMap())]); // @phpstan-ignore-line
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php $self->foo();'];
yield [self::generateTest('$self->foo();')];
yield [self::generateTest('$this->assertEquals;')];
foreach (self::getMethodsMap() as $methodBefore => $methodAfter) {
yield [self::generateTest("\$sth->{$methodBefore}(1, 1);")];
yield [self::generateTest("\$sth->{$methodAfter}(1, 1);")];
yield [self::generateTest("\$this->{$methodBefore}(1, 2, 'message', \$toMuch);")];
yield [
self::generateTest("\$this->{$methodAfter}(1, 2);"),
self::generateTest("\$this->{$methodBefore}(1, 2);"),
];
yield [
self::generateTest("\$this->{$methodAfter}(1, 2); \$this->{$methodAfter}(1, 2);"),
self::generateTest("\$this->{$methodBefore}(1, 2); \$this->{$methodBefore}(1, 2);"),
];
yield [
self::generateTest("\$this->{$methodAfter}(1, 2, 'description');"),
self::generateTest("\$this->{$methodBefore}(1, 2, 'description');"),
];
yield [
self::generateTest("\$this->/*aaa*/{$methodAfter} \t /**bbb*/ ( /*ccc*/1 , 2);"),
self::generateTest("\$this->/*aaa*/{$methodBefore} \t /**bbb*/ ( /*ccc*/1 , 2);"),
];
yield [
self::generateTest("\$this->{$methodAfter}(\$expectedTokens->count() + 10, \$tokens->count() ? 10 : 20 , 'Test');"),
self::generateTest("\$this->{$methodBefore}(\$expectedTokens->count() + 10, \$tokens->count() ? 10 : 20 , 'Test');"),
];
yield [
self::generateTest("self::{$methodAfter}(1, 2);"),
self::generateTest("self::{$methodBefore}(1, 2);"),
];
yield [
self::generateTest("static::{$methodAfter}(1, 2);"),
self::generateTest("static::{$methodBefore}(1, 2);"),
];
yield [
self::generateTest("STATIC::{$methodAfter}(1, 2);"),
self::generateTest("STATIC::{$methodBefore}(1, 2);"),
];
}
foreach (self::getMethodsMap() as $methodBefore => $methodAfter) {
yield [
self::generateTest("static::{$methodAfter}(1, 2,);"),
self::generateTest("static::{$methodBefore}(1, 2,);"),
];
yield [
self::generateTest("self::{$methodAfter}(1, \$a, '', );"),
self::generateTest("self::{$methodBefore}(1, \$a, '', );"),
];
}
// Only method calls with 2 or 3 arguments should be fixed.
foreach (self::getMethodsMap() as $candidate => $replacement) {
yield \sprintf('do not change call to "%s" without arguments.', $candidate) => [
self::generateTest(\sprintf('$this->%s();', $candidate)),
];
foreach ([1, 4, 5, 10] as $argumentCount) {
yield \sprintf('do not change call to "%s" with #%d arguments.', $candidate, $argumentCount) => [
self::generateTest(
\sprintf(
'$this->%s(%s);',
$candidate,
substr(str_repeat('$a, ', $argumentCount), 0, -2),
),
),
];
}
}
}
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('/^\[php_unit_strict\] Invalid configuration: The option "assertions" .*\.$/');
$this->fixer->configure(['assertions' => ['__TEST__']]); // @phpstan-ignore-line
}
/**
* @return array<string, string>
*/
private static function getMethodsMap(): array
{
return [
'assertAttributeEquals' => 'assertAttributeSame',
'assertAttributeNotEquals' => 'assertAttributeNotSame',
'assertEquals' => 'assertSame',
'assertNotEquals' => 'assertNotSame',
];
}
private static function generateTest(string $content): string
{
return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitTargetVersionTest.php | tests/Fixer/PhpUnit/PhpUnitTargetVersionTest.php | <?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\PhpUnit;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\Tests\TestCase;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitTargetVersionTest extends TestCase
{
/**
* @dataProvider provideFulfillsCases
*
* @param null|class-string<\Throwable> $exception
*/
public function testFulfills(bool $expected, string $candidate, string $target, ?string $exception = null): void
{
if (null !== $exception) {
$this->expectException($exception);
}
self::assertSame(
$expected,
PhpUnitTargetVersion::fulfills($candidate, $target),
);
}
/**
* @return iterable<int, array{0: bool, 1: string, 2: string, 3?: string}>
*/
public static function provideFulfillsCases(): iterable
{
yield [true, PhpUnitTargetVersion::VERSION_NEWEST, PhpUnitTargetVersion::VERSION_5_6];
yield [true, PhpUnitTargetVersion::VERSION_NEWEST, PhpUnitTargetVersion::VERSION_5_2];
yield [true, PhpUnitTargetVersion::VERSION_5_6, PhpUnitTargetVersion::VERSION_5_6];
yield [true, PhpUnitTargetVersion::VERSION_5_6, PhpUnitTargetVersion::VERSION_5_2];
yield [true, PhpUnitTargetVersion::VERSION_5_2, PhpUnitTargetVersion::VERSION_5_2];
yield [false, PhpUnitTargetVersion::VERSION_5_2, PhpUnitTargetVersion::VERSION_5_6];
yield [false, PhpUnitTargetVersion::VERSION_5_2, PhpUnitTargetVersion::VERSION_NEWEST, \LogicException::class];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitMethodCasingFixerTest.php | tests/Fixer/PhpUnit/PhpUnitMethodCasingFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitMethodCasingFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'skip non phpunit methods' => [
'<?php class MyClass {
public function testMyApp() {}
public function test_my_app() {}
}',
];
yield 'skip non test methods' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function not_a_test() {}
public function notATestEither() {}
}',
];
foreach (self::pairs() as $key => [$camelCase, $snakeCase]) {
yield $key.' to camel case' => [$camelCase, $snakeCase];
yield $key.' to snake case' => [$snakeCase, $camelCase, ['case' => 'snake_case']];
}
yield 'mixed case to camel case' => [
'<?php class MyTest extends TestCase { function testShouldNotFooWhenBar() {} }',
'<?php class MyTest extends TestCase { function test_should_notFoo_When_Bar() {} }',
];
yield 'mixed case to snake case' => [
'<?php class MyTest extends TestCase { function test_should_not_foo_when_bar() {} }',
'<?php class MyTest extends TestCase { function test_should_notFoo_When_Bar() {} }',
['case' => 'snake_case'],
];
yield 'does not fix if it would cause a duplicate method name' => [
<<<'PHP'
<?php
class MyTest extends TestCase {
function test_Foo() {}
function testFoo() {}
function testBar() {}
}
PHP,
<<<'PHP'
<?php
class MyTest extends TestCase {
function test_Foo() {}
function testFoo() {}
function test_Bar() {}
}
PHP,
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield '@depends annotation with class name in Snake_Case' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function testMyApp () {}
/**
* @depends Foo_Bar_Test::testMyApp
*/
#[SimpleTest]
public function testMyAppToo() {}
}',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function test_my_app () {}
/**
* @depends Foo_Bar_Test::test_my_app
*/
#[SimpleTest]
public function test_my_app_too() {}
}',
];
yield '@depends annotation with class name in Snake_Case and attributes in between' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function testMyApp () {}
/**
* @depends Foo_Bar_Test::testMyApp
*/
#[SimpleTest]
#[Deprecated]
public function testMyAppToo() {}
}',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function test_my_app () {}
/**
* @depends Foo_Bar_Test::test_my_app
*/
#[SimpleTest]
#[Deprecated]
public function test_my_app_too() {}
}',
];
yield 'test method with imported Test attribute' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function my_app_too() {}
}',
];
yield 'test method with fully qualified Test attribute' => [
'<?php class MyTest extends \PHPUnit\Framework\TestCase {
#[\PHPUnit\Framework\Attributes\Test]
public function testMyApp() {}
}',
'<?php class MyTest extends \PHPUnit\Framework\TestCase {
#[\PHPUnit\Framework\Attributes\Test]
public function test_my_app() {}
}',
];
yield 'test method with multiple attributes' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[\PHPUnit\Framework\Attributes\After, Test]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[\PHPUnit\Framework\Attributes\After, Test]
public function my_app_too() {}
}',
];
yield 'test method with multiple custom attributes' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[SimpleTest, Test]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[SimpleTest, Test]
public function my_app_too() {}
}',
];
yield 'mixed test methods with and without attributes' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
public function testMyApp() {}
#[Test]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
public function test_my_app() {}
#[Test]
public function my_app_too() {}
}',
];
yield 'snake_case conversion with attribute' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function this_is_a_test_snake_case() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function this_is_a_Test_Snake_Case() {}
}',
['case' => 'snake_case'],
];
yield 'camelCase to snake_case conversion with attribute' => [
'<?php
use \PHPUnit\Framework\Attributes\Test;
class MyTest extends \PhpUnit\FrameWork\TestCase {
#[Test]
public function this_is_a_test_snake_case() {}
}',
'<?php
use \PHPUnit\Framework\Attributes\Test;
class MyTest extends \PhpUnit\FrameWork\TestCase {
#[Test]
public function this_is_a_TestSnakeCase() {}
}',
['case' => 'snake_case'],
];
yield 'method with attribute and docblock' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
/** @return void */
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
/** @return void */
public function my_app_too() {}
}',
];
yield 'method with attribute and docblock and multiple lines' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
/** @return void */
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
/** @return void */
public function my_app_too() {}
}',
];
yield 'method with multiple non-PHPUnit attributes' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Author("John"), Test, Deprecated]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Author("John"), Test, Deprecated]
public function my_app_too() {}
}',
];
yield 'test attribute with fully qualified namespace without import' => [
'<?php
class MyTest extends \PHPUnit\Framework\TestCase {
#[\PHPUnit\Framework\Attributes\Test]
public function myAppToo() {}
}',
'<?php
class MyTest extends \PHPUnit\Framework\TestCase {
#[\PHPUnit\Framework\Attributes\Test]
public function my_app_too() {}
}',
];
yield 'method with both attribute and @test annotation' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
/** @test */
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
/** @test */
public function my_app_too() {}
}',
];
yield 'method with parameters and attribute' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function myAppToo(int $param): void {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function my_app_too(int $param): void {}
}',
];
yield 'test attribute in namespaced class' => [
'<?php
namespace App\Tests;
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function myAppToo() {}
}',
'<?php
namespace App\Tests;
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function my_app_too() {}
}',
];
yield 'test attribute with alias' => [
'<?php
use PHPUnit\Framework\Attributes\Test as CheckTest;
class MyTest extends \PHPUnit\Framework\TestCase {
#[CheckTest]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test as CheckTest;
class MyTest extends \PHPUnit\Framework\TestCase {
#[CheckTest]
public function my_app_too() {}
}',
];
yield 'multiple attributes spanning multiple lines' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[
Test,
DataProvider("testData"),
Group("testData")
]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[
Test,
DataProvider("testData"),
Group("testData")
]
public function my_app_too() {}
}',
];
yield 'multiple attributes in separate attribute blocks with comments' => [
'<?php
class MyTest extends \PHPUnit\Framework\TestCase {
#[FirstAttribute]
/* a comment */
#[PHPUnit\Framework\Attributes\Test]
// another comment
#[LastAttribute]
public function myAppToo() {}
}',
'<?php
class MyTest extends \PHPUnit\Framework\TestCase {
#[FirstAttribute]
/* a comment */
#[PHPUnit\Framework\Attributes\Test]
// another comment
#[LastAttribute]
public function my_app_too() {}
}',
];
yield 'test attribute with trailing comma' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test, ]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test, ]
public function my_app_too() {}
}',
];
yield 'method with both name prefix and attribute' => [
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function testMyAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\Test;
class MyTest extends \PHPUnit\Framework\TestCase {
#[Test]
public function test_my_app_too() {}
}',
];
yield 'attribute with different casing' => [
'<?php
use PHPUnit\Framework\Attributes\TEST;
class MyTest extends \PHPUnit\Framework\TestCase {
#[TEST]
public function myAppToo() {}
}',
'<?php
use PHPUnit\Framework\Attributes\TEST;
class MyTest extends \PHPUnit\Framework\TestCase {
#[TEST]
public function my_app_too() {}
}',
];
yield 'do not touch anonymous class' => [
<<<'PHP'
<?php
class MyTest extends \PHPUnit\Framework\TestCase {
#[PHPUnit\Framework\Attributes\Test]
public function methodFoo(): void
{
$class = new class () {
final public function method_bar(): void {}
};
}
}
PHP,
];
yield 'do not touch setUp method (camel case)' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase
{
#[\Override]
protected function setUp(): void {}
}
PHP,
null,
['case' => 'camel_case'],
];
yield 'do not touch setUp method (snake case)' => [
<<<'PHP'
<?php
class FooTest extends \PHPUnit\Framework\TestCase
{
#[\Override]
protected function setUp(): void {}
}
PHP,
null,
['case' => 'snake_case'],
];
yield 'do not touch setUp method for custom test case (camel case)' => [
<<<'PHP'
<?php
class FooTest extends YabbaDabbaDooTestCase
{
#[\Override]
protected function setUp(): void {}
}
PHP,
null,
['case' => 'camel_case'],
];
yield 'do not touch setUp method for custom test case (snake case)' => [
<<<'PHP'
<?php
class FooTest extends YabbaDabbaDooTestCase
{
#[\Override]
protected function setUp(): void {}
}
PHP,
null,
['case' => 'snake_case'],
];
}
/**
* @return iterable<string, array{string, string}>
*/
private static function pairs(): iterable
{
yield 'default sample' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase { public function testMyApp() {} }',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase { public function test_my_app() {} }',
];
yield 'annotation' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase { /** @test */ public function myApp() {} }',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase { /** @test */ public function my_app() {} }',
];
yield '@depends annotation' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function testMyApp () {}
/**
* @depends testMyApp
*/
public function testMyAppToo() {}
}',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function test_my_app () {}
/**
* @depends test_my_app
*/
public function test_my_app_too() {}
}',
];
yield '@depends annotation with class name in PascalCase' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function testMyApp () {}
/**
* @depends FooBarTest::testMyApp
*/
public function testMyAppToo() {}
}',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function test_my_app () {}
/**
* @depends FooBarTest::test_my_app
*/
public function test_my_app_too() {}
}',
];
yield '@depends annotation with class name in Snake_Case' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function testMyApp () {}
/**
* @depends Foo_Bar_Test::testMyApp
*/
public function testMyAppToo() {}
}',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
public function test_my_app () {}
/**
* @depends Foo_Bar_Test::test_my_app
*/
public function test_my_app_too() {}
}',
];
yield '@depends and @test annotation' => [
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
/**
* @test
*/
public function myApp () {}
/**
* @test
* @depends myApp
*/
public function myAppToo() {}
/** not a test method */
public function my_app_not() {}
public function my_app_not_2() {}
}',
'<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
/**
* @test
*/
public function my_app () {}
/**
* @test
* @depends my_app
*/
public function my_app_too() {}
/** not a test method */
public function my_app_not() {}
public function my_app_not_2() {}
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitInternalClassFixerTest.php | tests/Fixer/PhpUnit/PhpUnitInternalClassFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitInternalClassFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitInternalClassFixer>
*
* @author Gert de Pagter <BackEndTea@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitInternalClassFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitInternalClassFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'It does not change normal classes' => [
'<?php
class Hello
{
}
',
];
yield 'It marks a test class as internal' => [
'<?php
/**
* @internal
*/
class Test extends TestCase
{
}
',
'<?php
class Test extends TestCase
{
}
',
];
yield 'It adds an internal tag to a class that already has a doc block' => [
'<?php
/**
* @coversNothing
* @internal
*/
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*/
class Test extends TestCase
{
}
',
];
yield 'It does not change a class that is already internal' => [
'<?php
/**
* @internal
*/
class Test extends TestCase
{
}
',
];
yield 'It does not change a class that is already internal and has other annotations' => [
'<?php
/**
* @author me
* @coversNothing
* @internal
* @group large
*/
class Test extends TestCase
{
}
',
];
yield 'It works on other indentation levels' => [
'<?php
if (class_exists("Foo\Bar")) {
/**
* @internal
*/
class Test Extends TestCase
{
}
}
',
'<?php
if (class_exists("Foo\Bar")) {
class Test Extends TestCase
{
}
}
',
];
yield 'It works on other indentation levels when the class has other annotations' => [
'<?php
if (class_exists("Foo\Bar")) {
/**
* @author me again
*
*
* @covers \Other\Class
* @internal
*/
class Test Extends TestCase
{
}
}
',
'<?php
if (class_exists("Foo\Bar")) {
/**
* @author me again
*
*
* @covers \Other\Class
*/
class Test Extends TestCase
{
}
}
',
];
yield 'It works for tab ident' => [
'<?php
if (class_exists("Foo\Bar")) {
/**
* @author me again
*
*
* @covers \Other\Class
* @internal
*/
class Test Extends TestCase
{
}
}
',
'<?php
if (class_exists("Foo\Bar")) {
/**
* @author me again
*
*
* @covers \Other\Class
*/
class Test Extends TestCase
{
}
}
',
];
yield 'It always adds @internal to the bottom of the doc block' => [
'<?php
/**
* @coversNothing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* @internal
*/
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
class Test extends TestCase
{
}
',
];
yield 'It does not change a class with a single line internal doc block' => [
'<?php
/** @internal */
class Test extends TestCase
{
}
',
];
yield 'It adds an internal tag to a class that already has a one linedoc block' => [
'<?php
/**
* @coversNothing
* @internal
*/
class Test extends TestCase
{
}
',
'<?php
/** @coversNothing */
class Test extends TestCase
{
}
',
];
yield 'By default it will not mark an abstract class as internal' => [
'<?php
abstract class Test extends TestCase
{
}
',
];
yield 'If abstract is added as an option, abstract classes will be marked internal' => [
'<?php
/**
* @internal
*/
abstract class Test extends TestCase
{
}
',
'<?php
abstract class Test extends TestCase
{
}
',
[
'types' => ['abstract'],
],
];
yield 'If final is not added as an option, final classes will not be marked internal' => [
'<?php
final class Test extends TestCase
{
}
',
null,
[
'types' => ['abstract'],
],
];
yield 'If normal is not added as an option, normal classes will not be marked internal' => [
'<?php
class Test extends TestCase
{
}
',
null,
[
'types' => ['abstract'],
],
];
yield 'It works correctly with multiple classes in one file, even when one of them is not allowed' => [
'<?php
/**
* @internal
*/
class Test extends TestCase
{
}
abstract class Test2 extends TestCase
{
}
class FooBar
{
}
/**
* @internal
*/
class Test extends TestCase
{
}
',
'<?php
class Test extends TestCase
{
}
abstract class Test2 extends TestCase
{
}
class FooBar
{
}
class Test extends TestCase
{
}
',
];
yield 'It skips instantiation of anonymous class as it is not a class declaration' => [
<<<'PHP'
<?php
$test = new class ('test') extends AbstractSomethingTestCase {
public function getSomething(): string
{
return 'something';
}
};
PHP,
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'it adds a docblock above when there is an attribute' => [
'<?php
/**
* @internal
*/
#[SimpleTest]
class Test extends TestCase
{
}
',
'<?php
#[SimpleTest]
class Test extends TestCase
{
}
',
];
yield 'it adds the internal tag along other tags when there is an attribute' => [
'<?php
/**
* @coversNothing
* @internal
*/
#[SimpleTest]
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*/
#[SimpleTest]
class Test extends TestCase
{
}
',
];
yield 'it adds a docblock above when there are attributes' => [
'<?php
/**
* @internal
*/
#[SimpleTest]
#[Annotated]
class Test extends TestCase
{
}
',
'<?php
#[SimpleTest]
#[Annotated]
class Test extends TestCase
{
}
',
];
yield 'it adds the internal tag along other tags when there are attributes' => [
'<?php
/**
* @coversNothing
* @internal
*/
#[SimpleTest]
#[Annotated]
class Test extends TestCase
{
}
',
'<?php
/**
* @coversNothing
*/
#[SimpleTest]
#[Annotated]
class Test extends TestCase
{
}
',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, 1: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix82Cases(): iterable
{
yield 'If final is not added as an option, final classes will not be marked internal' => [
'<?php
final readonly class Test extends TestCase
{}
',
null,
[
'types' => ['normal'],
],
];
yield [
'<?php
/**
* @internal
*/
readonly final class Test extends TestCase {}',
'<?php
readonly final class Test extends TestCase {}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitDataProviderStaticFixerTest.php | tests/Fixer/PhpUnit/PhpUnitDataProviderStaticFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitDataProviderStaticFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'do not fix when containing dynamic calls by default' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFoo1Cases
*/
public function testFoo1() {}
public function provideFoo1Cases() { $this->init(); }
}',
];
yield 'fix single' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public static function provideFooCases() { $x->getData(); }
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public function provideFooCases() { $x->getData(); }
}',
];
yield 'fix multiple' => [
'<?php
class FooTest extends TestCase {
/** @dataProvider provider1 */
public function testFoo1() {}
/** @dataProvider provider2 */
public function testFoo2() {}
/** @dataProvider provider3 */
public function testFoo3() {}
/** @dataProvider provider4 */
public function testFoo4() {}
public static function provider1() {}
public function provider2() { $this->init(); }
public static function provider3() {}
public static function provider4() {}
}',
'<?php
class FooTest extends TestCase {
/** @dataProvider provider1 */
public function testFoo1() {}
/** @dataProvider provider2 */
public function testFoo2() {}
/** @dataProvider provider3 */
public function testFoo3() {}
/** @dataProvider provider4 */
public function testFoo4() {}
public function provider1() {}
public function provider2() { $this->init(); }
public function provider3() {}
public static function provider4() {}
}',
];
yield 'fix with multilines' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public
static function
provideFooCases() { $x->getData(); }
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
public
function
provideFooCases() { $x->getData(); }
}',
];
yield 'fix when data provider is abstract' => [
'<?php
abstract class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
abstract public static function provideFooCases();
}',
'<?php
abstract class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
public function testFoo() {}
abstract public function provideFooCases();
}',
];
yield 'fix when containing dynamic calls and with `force` disabled' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases1
* @dataProvider provideFooCases2
*/
public function testFoo() {}
public function provideFooCases1() { return $this->getFoo(); }
public static function provideFooCases2() { /* no dynamic calls */ }
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases1
* @dataProvider provideFooCases2
*/
public function testFoo() {}
public function provideFooCases1() { return $this->getFoo(); }
public function provideFooCases2() { /* no dynamic calls */ }
}',
['force' => false],
];
yield 'fix when containing dynamic calls and with `force` enabled' => [
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases1
* @dataProvider provideFooCases2
*/
public function testFoo() {}
public static function provideFooCases1() { return $this->getFoo(); }
public static function provideFooCases2() { /* no dynamic calls */ }
}',
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases1
* @dataProvider provideFooCases2
*/
public function testFoo() {}
public function provideFooCases1() { return $this->getFoo(); }
public function provideFooCases2() { /* no dynamic calls */ }
}',
['force' => true],
];
}
/**
* @requires PHP ^8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'with an attribute between PHPDoc and test method' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomAttribute]
public function testFoo(): void {}
public static function provideFooCases(): iterable {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
#[CustomAttribute]
public function testFoo(): void {}
public function provideFooCases(): iterable {}
}
PHP,
];
yield 'with data provider as an attribute' => [
<<<'PHP'
<?php
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('addStaticToMe')]
public function testFoo(): void {}
public static function addStaticToMe() {}
}
PHP,
<<<'PHP'
<?php
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('addStaticToMe')]
public function testFoo(): void {}
public function addStaticToMe() {}
}
PHP,
];
$withAttributesTemplate = <<<'PHP'
<?php
namespace N;
use PHPUnit\Framework as PphUnitAlias;
use PHPUnit\Framework\Attributes;
class FooTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('provider1')]
#[\PHPUnit\Framework\Attributes\DataProvider('doNotGetFooledByConcatenation' . 'notProvider1')]
#[PHPUnit\Framework\Attributes\DataProvider('notProvider2')]
#[
\PHPUnit\Framework\Attributes\BackupGlobals(true),
\PHPUnit\Framework\Attributes\DataProvider('provider2'),
\PHPUnit\Framework\Attributes\Group('foo'),
]
#[Attributes\DataProvider('provider3')]
#[PphUnitAlias\Attributes\DataProvider('provider4')]
#[\PHPUnit\Framework\Attributes\DataProvider]
#[\PHPUnit\Framework\Attributes\DataProvider('provider5')]
#[\PHPUnit\Framework\Attributes\DataProvider(123)]
public function testSomething(int $x): void {}
public%1$s function provider1() {}
public%1$s function provider2() {}
public%1$s function provider3() {}
public%1$s function provider4() {}
public%1$s function provider5() {}
public function notProvider1() {}
public function notProvider2() {}
}
PHP;
yield 'with multiple data providers as an attributes' => [
\sprintf($withAttributesTemplate, ' static'),
\sprintf($withAttributesTemplate, ''),
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitDedicateAssertInternalTypeFixerTest.php | tests/Fixer/PhpUnit/PhpUnitDedicateAssertInternalTypeFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertInternalTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertInternalTypeFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertInternalTypeFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitDedicateAssertInternalTypeFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'skip cases' => [
'<?php
final class MyTest extends \PhpUnit\FrameWork\TestCase
{
public function testMe()
{
$this->assertInternalType(gettype($expectedVar), $var);
$this->assertNotInternalType(gettype($expectedVar), $var);
$this->assertInternalType("foo", $var);
$this->assertNotInternalType("bar", $var);
$this->assertInternalType();
$this->assertNotInternalType();
$this->assertInternalType("array" . "foo", $var);
$this->assertNotInternalType(\'bool\' . "bar", $var);
}
}
',
];
yield 'expected normal cases' => [
'<?php
final class MyTest extends \PhpUnit\FrameWork\TestCase
{
public function testMe()
{
$this->assertIsArray($var);
$this->assertIsBool($var);
$this->assertIsBool($var);
$this->assertIsFloat($var);
$this->assertIsFloat($var);
$this->assertIsInt($var);
$this->assertIsInt($var);
$this->assertNull($var);
$this->assertIsNumeric($var);
$this->assertIsObject($var);
$this->assertIsFloat($var);
$this->assertIsResource($var);
$this->assertIsString($var);
$this->assertIsScalar($var);
$this->assertIsCallable($var);
$this->assertIsIterable($var);
$this->assertIsNotArray($var);
$this->assertIsNotBool($var);
$this->assertIsNotBool($var);
$this->assertIsNotFloat($var);
$this->assertIsNotFloat($var);
$this->assertIsNotInt($var);
$this->assertIsNotInt($var);
$this->assertNotNull($var);
$this->assertIsNotNumeric($var);
$this->assertIsNotObject($var);
$this->assertIsNotFloat($var);
$this->assertIsNotResource($var);
$this->assertIsNotString($var);
$this->assertIsNotScalar($var);
$this->assertIsNotCallable($var);
$this->assertIsNotIterable($var);
}
}
',
'<?php
final class MyTest extends \PhpUnit\FrameWork\TestCase
{
public function testMe()
{
$this->assertInternalType(\'array\', $var);
$this->assertInternalType("boolean", $var);
$this->assertInternalType("bool", $var);
$this->assertInternalType("double", $var);
$this->assertInternalType("float", $var);
$this->assertInternalType("integer", $var);
$this->assertInternalType("int", $var);
$this->assertInternalType("null", $var);
$this->assertInternalType("numeric", $var);
$this->assertInternalType("object", $var);
$this->assertInternalType("real", $var);
$this->assertInternalType("resource", $var);
$this->assertInternalType("string", $var);
$this->assertInternalType("scalar", $var);
$this->assertInternalType("callable", $var);
$this->assertInternalType("iterable", $var);
$this->assertNotInternalType("array", $var);
$this->assertNotInternalType("boolean", $var);
$this->assertNotInternalType("bool", $var);
$this->assertNotInternalType("double", $var);
$this->assertNotInternalType("float", $var);
$this->assertNotInternalType("integer", $var);
$this->assertNotInternalType("int", $var);
$this->assertNotInternalType("null", $var);
$this->assertNotInternalType("numeric", $var);
$this->assertNotInternalType("object", $var);
$this->assertNotInternalType("real", $var);
$this->assertNotInternalType("resource", $var);
$this->assertNotInternalType("string", $var);
$this->assertNotInternalType("scalar", $var);
$this->assertNotInternalType("callable", $var);
$this->assertNotInternalType("iterable", $var);
}
}
',
];
yield 'false positive cases' => [
'<?php
final class MyTest extends \PhpUnit\FrameWork\TestCase
{
public function testMe()
{
$this->assertInternalType = 42;
$this->assertNotInternalType = 43;
}
}
',
];
yield 'anonymous class false positive case' => [
'<?php
final class MyTest extends \PhpUnit\FrameWork\TestCase
{
public function testMe()
{
$class = new class {
private function assertInternalType()
{}
private function foo(){
$this->assertInternalType("array", $var);
}
};
}
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php | tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitAssertNewNamesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitAssertNewNamesFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitAssertNewNamesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1: string}>
*/
public static function provideFixCases(): iterable
{
yield [
self::generateTest(
'
$this->assertFileDoesNotExist($a);
$this->assertIsNotReadable($a);
$this->assertIsNotWritable($a);
$this->assertDirectoryDoesNotExist($a);
$this->assertDirectoryIsNotReadable($a);
$this->assertDirectoryIsNotWriteable($a);
$this->assertFileIsNotReadable($a);
$this->assertFileIsNotWriteable($a);
$this->assertMatchesRegularExpression($a);
$this->assertDoesNotMatchRegularExpression($a);
',
),
self::generateTest(
'
$this->assertFileNotExists($a);
$this->assertNotIsReadable($a);
$this->assertNotIsWritable($a);
$this->assertDirectoryNotExists($a);
$this->assertDirectoryNotIsReadable($a);
$this->assertDirectoryNotIsWritable($a);
$this->assertFileNotIsReadable($a);
$this->assertFileNotIsWritable($a);
$this->assertRegExp($a);
$this->assertNotRegExp($a);
',
),
];
}
private static function generateTest(string $content): string
{
return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixerTest.php | tests/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixerTest.php | <?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\PhpUnit;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockShortWillReturnFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitMockShortWillReturnFixer>
*
* @author Michał Adamski <michal.adamski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpUnitMockShortWillReturnFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'do not fix' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->will;
$someMock->method("someMethod")->will("Smith");
$someMock->method("someMethod")->will($this->returnSelf);
$someMock->method("someMethod")->will($this->doSomething(7));
}
}',
];
yield 'will return simple scenarios' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->willReturn(10);
$someMock->method("someMethod")->willReturn(20);
$someMock->method("someMethod")->willReturn(30);
$someMock->method("someMethod")->willReturn(40);
$someMock->method("someMethod")->willReturn(50);
$someMock->method("someMethod")->willReturn(60);
$someMock->method("someMethod")->willReturn(-10);
$someMock->method("someMethod")->willReturn(10.10);
$someMock->method("someMethod")->willReturn(-10.10);
$someMock->method("someMethod")->willReturn("myValue");
$someMock->method("someMethod")->willReturn($myValue);
$testMock->method("test_method")->willReturn(DEFAULT_VALUE);
$testMock->method("test_method")->willReturn(self::DEFAULT_VALUE);
$someMock->method("someMethod")->willReturn([]);
$someMock->method("someMethod")->willReturn([[]]);
$someMock->method("someMethod")->willReturn(array());
$someMock->method("someMethod")->willReturn(new stdClass());
$someMock->method("someMethod")->willReturn(new \DateTime());
$someMock->method("someMethod")->willReturnSelf();
$someMock->method("someMethod")->willReturnArgument(2);
$someMock->method("someMethod")->willReturnCallback("str_rot13");
$someMock->method("someMethod")->willReturnMap(["a", "b", "c", "d"]);
$someMock->method("someMethod")->willReturn(1);
$someMock->method("someMethod")->willReturn(2);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->will($this->returnValue(10));
$someMock->method("someMethod")->will($THIS->returnValue(20));
$someMock->method("someMethod")->will(self::returnValue(30));
$someMock->method("someMethod")->will(Self::returnValue(40));
$someMock->method("someMethod")->will(static::returnValue(50));
$someMock->method("someMethod")->will(STATIC::returnValue(60));
$someMock->method("someMethod")->will($this->returnValue(-10));
$someMock->method("someMethod")->will($this->returnValue(10.10));
$someMock->method("someMethod")->will($this->returnValue(-10.10));
$someMock->method("someMethod")->will($this->returnValue("myValue"));
$someMock->method("someMethod")->will($this->returnValue($myValue));
$testMock->method("test_method")->will($this->returnValue(DEFAULT_VALUE));
$testMock->method("test_method")->will($this->returnValue(self::DEFAULT_VALUE));
$someMock->method("someMethod")->will($this->returnValue([]));
$someMock->method("someMethod")->will($this->returnValue([[]]));
$someMock->method("someMethod")->will($this->returnValue(array()));
$someMock->method("someMethod")->will($this->returnValue(new stdClass()));
$someMock->method("someMethod")->will($this->returnValue(new \DateTime()));
$someMock->method("someMethod")->will($this->returnSelf());
$someMock->method("someMethod")->will($this->returnArgument(2));
$someMock->method("someMethod")->will($this->returnCallback("str_rot13"));
$someMock->method("someMethod")->will($this->returnValueMap(["a", "b", "c", "d"]));
$someMock->method("someMethod")->WILL($this->returnValue(1));
$someMock->method("someMethod")->will($this->ReturnVALUE(2));
}
}',
];
yield 'will return with multi lines and messy indents' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock
->method("someMethod")
->willReturn(
10
);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock
->method("someMethod")
->will(
$this->returnValue(10)
);
}
}',
];
yield 'will return with multi lines, messy indents and comments inside' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock
->method("someMethod")
->willReturn(
// foo
10
// bar
);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock
->method("someMethod")
->will(
// foo
$this->returnValue(10)
// bar
);
}
}',
];
yield 'will return with block comments in weird places' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->/* a */willReturn/* b */(/* c */ 10 /* d */);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->/* a */will/* b */(/* c */ $this->returnValue(10) /* d */);
}
}',
];
yield 'will return with comments persisted not touched even if put in unexpected places' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")// a
->/* b */willReturn/* c */(/* d */ /** e */
// f
'.'
// g
'.'
/* h */
10 /* i */);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")// a
->/* b */will/* c */(/* d */ $this/** e */
-> // f
returnValue
// g
(
/* h */
10) /* i */);
}
}',
];
yield 'will return with multi lines, messy indents and comments in weird places' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock
->method(
"someMethod"
)
->
/* a */
willReturn
/*
b
c
d
e
*/ (
// f g h i
/* j */ '.'
'.'
10
/* k */
/* l */);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock
->method(
"someMethod"
)
->
/* a */
will
/*
b
c
d
e
*/ (
// f g h i
/* j */ $this
->returnValue
(10)
/* k */
/* l */);
}
}',
];
yield [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->willReturn( 10 , );
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->will($this->returnValue( 10 , ));
}
}',
];
yield 'with trailing commas' => [
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->willReturn( 10 , );
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock->method("someMethod")->will($this->returnValue( 10 , ) , );
}
}',
];
}
/**
* @requires PHP 8.0
*/
public function testFix80(): void
{
$this->doTest(
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock?->method("someMethod")?->willReturn(10);
}
}',
'<?php
class FooTest extends TestCase {
public function testFoo() {
$someMock?->method("someMethod")?->will($this?->returnValue(10));
}
}',
);
}
/**
* @requires PHP 8.1
*/
public function testFix81(): void
{
$this->doTest(
'<?php
class FooTest extends TestCase {
public function testFoo() {
$a = $someMock?->method("someMethod")->will($this?->returnValue(...));
}
}',
);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ConstantNotation/NativeConstantInvocationFixerTest.php | tests/Fixer/ConstantNotation/NativeConstantInvocationFixerTest.php | <?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\ConstantNotation;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Preg;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NativeConstantInvocationFixerTest extends AbstractFixerTestCase
{
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $configuration, string $exceptionExpression): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($exceptionExpression);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'unknown configuration key' => [
['foo' => 'bar'],
'[native_constant_invocation] Invalid configuration: The option "foo" does not exist.',
];
yield 'invalid exclude configuration element - null' => [
['include' => [null]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "null".',
];
yield 'invalid exclude configuration element - false' => [
['include' => [false]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
];
yield 'invalid exclude configuration element - true' => [
['include' => [true]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
];
yield 'invalid exclude configuration element - int' => [
['include' => [1]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "int".',
];
yield 'invalid exclude configuration element - array' => [
['include' => [[]]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "array".',
];
yield 'invalid exclude configuration element - float' => [
['include' => [0.1]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "float".',
];
yield 'invalid exclude configuration element - object' => [
['include' => [new \stdClass()]],
'[native_constant_invocation] Invalid configuration: The option "include" with value array is expected to be of type "string[]", but one of the elements is of type "stdClass".',
];
yield 'invalid exclude configuration element - not-trimmed' => [
['include' => [' M_PI ']],
'[native_constant_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.',
];
}
/**
* @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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php var_dump(NULL, FALSE, TRUE, 1);'];
yield ['<?php echo CUSTOM_DEFINED_CONSTANT_123;'];
yield ['<?php echo m_pi; // Constant are case sensitive'];
yield ['<?php namespace M_PI;'];
yield ['<?php namespace Foo; use M_PI;'];
yield ['<?php class M_PI {}'];
yield ['<?php class Foo extends M_PI {}'];
yield ['<?php class Foo implements M_PI {}'];
yield ['<?php interface M_PI {};'];
yield ['<?php trait M_PI {};'];
yield ['<?php class Foo { const M_PI = 1; }'];
yield ['<?php class Foo { use M_PI; }'];
yield ['<?php class Foo { public $M_PI = 1; }'];
yield ['<?php class Foo { function M_PI($M_PI) {} }'];
yield ['<?php class Foo { function bar() { $M_PI = M_PI() + self::M_PI(); } }'];
yield ['<?php class Foo { function bar() { $this->M_PI(self::M_PI); } }'];
yield ['<?php namespace Foo; use Bar as M_PI;'];
yield ['<?php echo Foo\M_PI\Bar;'];
yield ['<?php M_PI::foo();'];
yield ['<?php function x(M_PI $foo, M_PI &$bar, M_PI ...$baz) {}'];
yield ['<?php $foo instanceof M_PI;'];
yield ['<?php class x implements FOO, M_PI, BAZ {}'];
yield ['<?php class Foo { use Bar, M_PI { Bar::baz insteadof M_PI; } }'];
yield ['<?php M_PI: goto M_PI;'];
yield [
'<?php echo \M_PI;',
'<?php echo M_PI;',
];
yield [
'<?php namespace Foo; use M_PI; echo \M_PI;',
'<?php namespace Foo; use M_PI; echo M_PI;',
];
yield [
// Here we are just testing the algorithm.
// A user likely would add this M_PI to its excluded list.
'<?php namespace M_PI; const M_PI = 1; return \M_PI;',
'<?php namespace M_PI; const M_PI = 1; return M_PI;',
];
yield [
'<?php foo(\E_DEPRECATED | \E_USER_DEPRECATED);',
'<?php foo(E_DEPRECATED | E_USER_DEPRECATED);',
];
yield ['<?php function foo(): M_PI {}'];
yield ['<?php use X\Y\{FOO, BAR as BAR2, M_PI};'];
yield [
'<?php
try {
foo(\JSON_ERROR_DEPTH|\JSON_PRETTY_PRINT|JOB_QUEUE_PRIORITY_HIGH);
} catch (\Exception | \InvalidArgumentException|\UnexpectedValueException|LogicException $e) {
}
',
'<?php
try {
foo(\JSON_ERROR_DEPTH|JSON_PRETTY_PRINT|\JOB_QUEUE_PRIORITY_HIGH);
} catch (\Exception | \InvalidArgumentException|\UnexpectedValueException|LogicException $e) {
}
',
];
yield [
'<?php echo \FOO_BAR_BAZ . \M_PI;',
'<?php echo FOO_BAR_BAZ . M_PI;',
['include' => ['FOO_BAR_BAZ']],
];
yield [
'<?php class Foo { public function bar($foo) { return \FOO_BAR_BAZ . \M_PI; } }',
'<?php class Foo { public function bar($foo) { return FOO_BAR_BAZ . M_PI; } }',
['include' => ['FOO_BAR_BAZ']],
];
yield [
'<?php echo PHP_SAPI . FOO_BAR_BAZ . \M_PI;',
'<?php echo PHP_SAPI . FOO_BAR_BAZ . M_PI;',
[
'fix_built_in' => false,
'include' => ['M_PI'],
],
];
yield [
'<?php class Foo { public function bar($foo) { return PHP_SAPI . FOO_BAR_BAZ . \M_PI; } }',
'<?php class Foo { public function bar($foo) { return PHP_SAPI . FOO_BAR_BAZ . M_PI; } }',
[
'fix_built_in' => false,
'include' => ['M_PI'],
],
];
yield [
'<?php echo \PHP_SAPI . M_PI;',
'<?php echo PHP_SAPI . M_PI;',
['exclude' => ['M_PI']],
];
yield [
'<?php class Foo { public function bar($foo) { return \PHP_SAPI . M_PI; } }',
'<?php class Foo { public function bar($foo) { return PHP_SAPI . M_PI; } }',
['exclude' => ['M_PI']],
];
yield 'null true false are case insensitive' => [
<<<'EOT'
<?php
var_dump(
\null,
\NULL,
\Null,
\nUlL,
\false,
\FALSE,
true,
TRUE,
\M_PI,
\M_pi,
m_pi,
m_PI
);
EOT,
<<<'EOT'
<?php
var_dump(
null,
NULL,
Null,
nUlL,
false,
FALSE,
true,
TRUE,
M_PI,
M_pi,
m_pi,
m_PI
);
EOT,
[
'fix_built_in' => false,
'include' => [
'null',
'false',
'M_PI',
'M_pi',
],
'exclude' => [],
],
];
$uniqueConstantName = uniqid(self::class);
$uniqueConstantName = Preg::replace('/\W+/', '_', $uniqueConstantName);
$uniqueConstantName = strtoupper($uniqueConstantName);
$dontFixMe = 'DONTFIXME_'.$uniqueConstantName;
$fixMe = 'FIXME_'.$uniqueConstantName;
\define($dontFixMe, 1);
\define($fixMe, 1);
yield 'do not include user constants unless explicitly listed' => [
<<<EOT
<?php
var_dump(
\\null,
{$dontFixMe},
\\{$fixMe}
);
EOT,
<<<EOT
<?php
var_dump(
null,
{$dontFixMe},
{$fixMe}
);
EOT,
[
'fix_built_in' => true,
'include' => [
$fixMe,
],
'exclude' => [],
],
];
yield 'do not fix imported constants' => [
<<<'EOT'
<?php
namespace Foo;
use const M_EULER;
var_dump(
null,
\M_PI,
M_EULER
);
EOT,
<<<'EOT'
<?php
namespace Foo;
use const M_EULER;
var_dump(
null,
M_PI,
M_EULER
);
EOT,
[
'fix_built_in' => false,
'include' => [
'M_PI',
'M_EULER',
],
'exclude' => [],
],
];
yield 'scoped only' => [
<<<'EOT'
<?php
namespace space1 {
echo \PHP_VERSION;
}
namespace {
echo PHP_VERSION;
}
EOT,
<<<'EOT'
<?php
namespace space1 {
echo PHP_VERSION;
}
namespace {
echo PHP_VERSION;
}
EOT,
['scope' => 'namespaced'],
];
yield 'scoped only no namespace' => [
<<<'EOT'
<?php
echo PHP_VERSION . PHP_EOL;
EOT,
null,
['scope' => 'namespaced'],
];
yield 'strict option' => [
'<?php
echo \PHP_VERSION . \PHP_EOL; // built-in constants to have backslash
echo MY_FRAMEWORK_MAJOR_VERSION . MY_FRAMEWORK_MINOR_VERSION; // non-built-in constants not to have backslash
echo \Dont\Touch\Namespaced\CONSTANT;
',
'<?php
echo \PHP_VERSION . PHP_EOL; // built-in constants to have backslash
echo \MY_FRAMEWORK_MAJOR_VERSION . MY_FRAMEWORK_MINOR_VERSION; // non-built-in constants not to have backslash
echo \Dont\Touch\Namespaced\CONSTANT;
',
['strict' => true],
];
}
/**
* @requires PHP <8.0
*/
public function testFixPre80(): void
{
$this->doTest(
'<?php
echo \/**/M_PI;
echo \ M_PI;
echo \#
#
M_PI;
echo \M_PI;
',
'<?php
echo \/**/M_PI;
echo \ M_PI;
echo \#
#
M_PI;
echo M_PI;
',
);
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected): void
{
$this->fixer->configure(['strict' => true]);
$this->doTest($expected);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
try {
} catch (\Exception) {
}',
];
yield ['<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}'];
yield ['<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}'];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected): void
{
$this->fixer->configure(['strict' => true]);
$this->doTest($expected);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php enum someEnum: int
{
case E_ALL = 123;
}',
];
}
/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected): void
{
$this->fixer->configure(['strict' => true]);
$this->doTest($expected);
}
/**
* @return iterable<int, array{0: string}>
*/
public static function provideFix82Cases(): iterable
{
yield ['<?php class Foo { public (\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var; }'];
yield ['<?php function foo ((\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var) {}'];
}
/**
* @dataProvider provideFix83Cases
*
* @requires PHP 8.3
*/
public function testFix83(string $expected, string $input): void
{
$this->fixer->configure(['strict' => true]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1: string}>
*/
public static function provideFix83Cases(): iterable
{
yield [
'<?php class Foo {
public const string C1 = \PHP_EOL;
protected const string|int C2 = \PHP_EOL;
private const string|(A&B) C3 = BAR;
public const EnumA C4 = EnumA::FOO;
private const array CONNECTION_TIMEOUT = [\'foo\'];
}',
'<?php class Foo {
public const string C1 = PHP_EOL;
protected const string|int C2 = \PHP_EOL;
private const string|(A&B) C3 = \BAR;
public const EnumA C4 = EnumA::FOO;
private const array CONNECTION_TIMEOUT = [\'foo\'];
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/NoMultilineWhitespaceAroundDoubleArrowFixerTest.php | tests/Fixer/ArrayNotation/NoMultilineWhitespaceAroundDoubleArrowFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\NoMultilineWhitespaceAroundDoubleArrowFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\NoMultilineWhitespaceAroundDoubleArrowFixer>
*
* @author Carlos Cirello <carlos.cirello.nl@gmail.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoMultilineWhitespaceAroundDoubleArrowFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
$arr = array(
$a => array(1),
$a => array(0 => array())
);',
'<?php
$arr = array(
$a =>
array(1),
$a =>
array(0 =>
array())
);',
];
yield [
'<?php
$a = array(
"aaaaaa" => "b",
"c" => "d",
"eeeeee" => array(),
"ggg" => array(),
"hh" => [],
);',
'<?php
$a = array(
"aaaaaa" => "b",
"c"
=>
"d",
"eeeeee" => array(),
"ggg" =>
array(),
"hh" =>
[],
);',
];
yield [
'<?php
$hello = array(
"foo" =>
// hello there
"value",
"hi" =>
/*
* Description.
*/1,
"ha" =>
/**
* Description.
*/
array()
);',
];
yield [
'<?php
$fn = fn() => null;',
'<?php
$fn = fn()
=>
null;',
];
yield [
'<?php
$foo = [
1 /* foo */ => $one,
2 => $two
];',
'<?php
$foo = [
1 /* foo */
=>
$one,
2
=>
$two
];',
];
yield [
'<?php
$foo = [
1 // foo
=> $one,
2 => $two,
];',
'<?php
$foo = [
1 // foo
=>
$one,
2
=>
$two,
];',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/YieldFromArrayToYieldsFixerTest.php | tests/Fixer/ArrayNotation/YieldFromArrayToYieldsFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\YieldFromArrayToYieldsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\YieldFromArrayToYieldsFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class YieldFromArrayToYieldsFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php function f() { yield from foo(); }',
];
yield [
'<?php function f() { yield 1; yield 2; yield 3; }',
'<?php function f() { yield from [1, 2, 3]; }',
];
yield [
'<?php function f() { yield 11; yield 22; yield 33; }',
'<?php function f() { yield from array(11, 22, 33); }',
];
yield [
'<?php function f() { yield 11; yield 22; yield 33; }',
'<?php function f() { yield from array (11, 22, 33); }',
];
yield [
'<?php function f() { /* ugly comment */yield 11; yield 22; yield 33; }',
'<?php function f() { yield from array/* ugly comment */(11, 22, 33); }',
];
yield [
'<?php function f() { /** ugly doc */yield 11; yield 22; yield 33; }',
'<?php function f() { yield from array/** ugly doc */(11, 22, 33); }',
];
yield [
'<?php function f() { yield 111; yield 222; yield 333; }',
'<?php function f() { yield from [111, 222, 333,]; }',
];
yield [
'<?php function f() {
'.'
yield [1, 2];
yield [3, 4];
'.'
}',
'<?php function f() {
yield from [
[1, 2],
[3, 4],
];
}',
];
yield [
'<?php function f() {
'.'
yield array(1, 2);
yield array(3, 4);
'.'
}',
'<?php function f() {
yield from [
array(1, 2),
array(3, 4),
];
}',
];
yield [
'<?php function f() {
'.'
yield 1;
yield 2;
yield 3;
'.'
}',
'<?php function f() {
yield from [
1,
2,
3,
];
}',
];
yield [
'<?php function f() {
'.'
// uno
yield 1;
// dos
yield 2;
// tres
yield 3;
'.'
}',
'<?php function f() {
yield from [
// uno
1,
// dos
2,
// tres
3,
];
}',
];
yield [
'<?php function f() {
'.'
yield random_key() => true;
yield "foo" => foo(1, 2);
yield "bar" => function ($x, $y) { return max($x, $y); };
yield "baz" => function () { yield [1, 2]; };
'.'
}',
'<?php function f() {
yield from [
random_key() => true,
"foo" => foo(1, 2),
"bar" => function ($x, $y) { return max($x, $y); },
"baz" => function () { yield [1, 2]; },
];
}',
];
yield [
'<?php
function f1() { yield 0; yield 1; yield 2; }
function f2() { yield 3; yield 4; yield 5; }
function f3() { yield 6; yield 7; yield 8; }
',
'<?php
function f1() { yield from [0, 1, 2]; }
function f2() { yield from [3, 4, 5]; }
function f3() { yield from [6, 7, 8]; }
',
];
yield [
'<?php
function f1() { yield 0; yield 1; }
function f2() { yield 2; yield 3; }
function f3() { yield 4; yield 5; }
function f4() { yield 6; yield 7; }
function f5() { yield 8; yield 9; }
',
'<?php
function f1() { yield from array(0, 1); }
function f2() { yield from [2, 3]; }
function f3() { yield from array(4, 5); }
function f4() { yield from [6, 7]; }
function f5() { yield from array(8, 9); }
',
];
yield [
'<?php
function foo() {
return [
1,
yield from [2, 3],
4,
];
}
',
];
yield [
'<?php
function foo() {
yield from [
"this element is regular string",
yield from ["here", "are", "nested", "strings"],
"next elements will be an arrow function reference",
fn() => [yield 1, yield from [2, 3]],
fn() => [yield from [1, 2], yield 3],
fn() => [yield from array(1, 2), yield 3]
];
}
',
];
yield [
'<?php function foo() {
yield 0; yield 1;
yield 2; yield 3;
yield from [4, yield from [5, 6], 7];
yield 8; yield 9;
}',
'<?php function foo() {
yield from [0, 1];
yield from [2, 3];
yield from [4, yield from [5, 6], 7];
yield from [8, 9];
}',
];
yield [
'<?php function foo()
{
foreach ([] as $x) {}
yield 1; yield 2; yield 3;
}',
'<?php function foo()
{
foreach ([] as $x) {}
yield from [1, 2, 3];
}',
];
yield 'skip empty arrays' => [
'<?php
function foo1()
{
yield from [/*empty*/ ];
}
function foo2()
{
yield from [
// Inline comment,
# and another one
];
}
function foo3()
{
yield from array(/*empty*/ );
}
function bar()
{
yield from [];
yield from array();
}
function baz()
{
yield from [];
yield 1; yield 2;
yield from [];
}',
'<?php
function foo1()
{
yield from [/*empty*/ ];
}
function foo2()
{
yield from [
// Inline comment,
# and another one
];
}
function foo3()
{
yield from array(/*empty*/ );
}
function bar()
{
yield from [];
yield from array();
}
function baz()
{
yield from [];
yield from [1, 2];
yield from [];
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/TrimArraySpacesFixerTest.php | tests/Fixer/ArrayNotation/TrimArraySpacesFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer>
*
* @author Jared Henderson <jared@netrivet.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class TrimArraySpacesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php $foo = array("foo");',
'<?php $foo = array( "foo" );',
];
yield [
'<?php $foo = ["foo"];',
'<?php $foo = [ "foo" ];',
];
yield [
'<?php $foo = array();',
'<?php $foo = array( );',
];
yield [
'<?php $foo = [];',
'<?php $foo = [ ];',
];
yield [
'<?php $foo = array("foo", "bar");',
'<?php $foo = array( "foo", "bar" );',
];
yield [
'<?php $foo = array("foo", "bar", );',
'<?php $foo = array( "foo", "bar", );',
];
yield [
'<?php $foo = ["foo", "bar", ];',
'<?php $foo = [ "foo", "bar", ];',
];
yield [
"<?php \$foo = array('foo', 'bar');",
"<?php \$foo = array(\t'foo', 'bar'\t);",
];
yield [
"<?php \$foo = array('foo', 'bar');",
"<?php \$foo = array( \t 'foo', 'bar'\t );",
];
yield [
'<?php $foo = array("foo", "bar");',
'<?php $foo = array( "foo", "bar" );',
];
yield [
'<?php $foo = ["foo", "bar"];',
'<?php $foo = [ "foo", "bar" ];',
];
yield [
'<?php $foo = ["foo", "bar"];',
'<?php $foo = [ "foo", "bar" ];',
];
yield [
"<?php \$foo = ['foo', 'bar'];",
"<?php \$foo = [\t'foo', 'bar'\t];",
];
yield [
"<?php \$foo = ['foo', 'bar'];",
"<?php \$foo = [ \t \t 'foo', 'bar'\t \t ];",
];
yield [
'<?php $foo = array("foo", "bar"); $bar = array("foo", "bar");',
'<?php $foo = array( "foo", "bar" ); $bar = array( "foo", "bar" );',
];
yield [
'<?php $foo = ["foo", "bar"]; $bar = ["foo", "bar"];',
'<?php $foo = [ "foo", "bar" ]; $bar = [ "foo", "bar" ];',
];
yield [
'<?php $foo = array("foo" => "bar");',
'<?php $foo = array( "foo" => "bar" );',
];
yield [
'<?php $foo = ["foo" => "bar"];',
'<?php $foo = [ "foo" => "bar" ];',
];
yield [
'<?php $foo = array($y ? true : false);',
'<?php $foo = array( $y ? true : false );',
];
yield [
'<?php $foo = [$y ? true : false];',
'<?php $foo = [ $y ? true : false ];',
];
yield [
'<?php $foo = array(array("foo"), array("bar"));',
'<?php $foo = array( array( "foo" ), array( "bar" ) );',
];
yield [
'<?php $foo = [["foo"], ["bar"]];',
'<?php $foo = [ [ "foo" ], [ "bar" ] ];',
];
yield [
'<?php function(array $foo = array("bar")) {};',
'<?php function(array $foo = array( "bar" )) {};',
];
yield [
'<?php function(array $foo = ["bar"]) {};',
'<?php function(array $foo = [ "bar" ]) {};',
];
yield [
'<?php $foo = array(function() {return "foo";});',
'<?php $foo = array( function() {return "foo";} );',
];
yield [
'<?php $foo = [function() {return "foo";}];',
'<?php $foo = [ function() {return "foo";} ];',
];
yield [
"<?php \$foo = [function( \$a = \tarray('foo') ) { return 'foo' ;}];",
"<?php \$foo = [ function( \$a = \tarray( 'foo' ) ) { return 'foo' ;} ];",
];
yield [
"<?php \$foo = array(function( ) {\treturn 'foo' \t;\t});",
"<?php \$foo = array( function( ) {\treturn 'foo' \t;\t} );",
];
yield [
"<?php \$foo = [function()\t{\t \treturn 'foo';\t}];",
"<?php \$foo = [ function()\t{\t \treturn 'foo';\t} ];",
];
yield [
"<?php \$foo \t = array(function(\$a,\$b,\$c=array(3, 4))\t{\t \treturn 'foo';\t});",
"<?php \$foo \t = array( function(\$a,\$b,\$c=array( 3, 4 ))\t{\t \treturn 'foo';\t} );",
];
yield [
'<?php $foo = array($bar->method(), Foo::doSomething());',
'<?php $foo = array( $bar->method(), Foo::doSomething() );',
];
yield [
'<?php $foo = [$bar->method(), Foo::doSomething()];',
'<?php $foo = [ $bar->method(), Foo::doSomething() ];',
];
yield [
"<?php \$foo = [\$bar->method( \$a,\$b, \$c,\t\t \$d ), Foo::doSomething()];",
"<?php \$foo = [ \$bar->method( \$a,\$b, \$c,\t\t \$d ), Foo::doSomething() ];",
];
yield [
"<?php \$foo =\t array(\$bar->method( \$a,\$b, \$c,\t\t \$d ), \$bar -> doSomething( ['baz']));",
"<?php \$foo =\t array( \$bar->method( \$a,\$b, \$c,\t\t \$d ), \$bar -> doSomething( [ 'baz']) );",
];
yield [
'<?php $foo = array(array("foo"), array("bar"));',
'<?php $foo = array( array("foo"), array("bar") );',
];
yield [
'<?php $foo = [["foo"], ["bar"]];',
'<?php $foo = [ ["foo"], ["bar"] ];',
];
yield [
'<?php $foo = array(array("foo"), array("bar"));',
'<?php $foo = array(array( "foo" ), array( "bar" ));',
];
yield [
'<?php $foo = [["foo"], ["bar"]];',
'<?php $foo = [[ "foo" ], [ "bar" ]];',
];
yield [
'<?php $foo = array(/* empty array */);',
'<?php $foo = array( /* empty array */ );',
];
yield [
'<?php $foo = [/* empty array */];',
'<?php $foo = [ /* empty array */ ];',
];
yield [
'<?php someFunc(array(/* empty array */));',
'<?php someFunc(array( /* empty array */ ));',
];
yield [
'<?php someFunc([/* empty array */]);',
'<?php someFunc([ /* empty array */ ]);',
];
yield [
'<?php
someFunc(array(
/* empty array */
));',
];
yield [
'<?php
someFunc([
/* empty array */
]);',
];
yield [
'<?php
someFunc(array(
/* empty
array */));',
'<?php
someFunc(array(
/* empty
array */ ));',
];
yield [
'<?php
someFunc([
/* empty
array */]);',
'<?php
someFunc([
/* empty
array */ ]);',
];
yield [
'<?php
$a = array( // My array of:
1, // - first item
2, // - second item
);',
];
yield [
'<?php
$a = [ // My array of:
1, // - first item
2, // - second item
];',
];
yield [
'<?php
$a = array(
// My array of:
1, // - first item
2, // - second item
);',
];
yield [
'<?php
$a = [
// My array of:
1, // - first item
2, // - second item
];',
];
yield [
'<?php
$foo = array(/* comment */
1
);',
'<?php
$foo = array( /* comment */
1
);',
];
yield [
'<?php
$foo = [/* comment */
1
];',
'<?php
$foo = [ /* comment */
1
];',
];
// don't fix array syntax within comments
yield [
'<?php someFunc([/* array( "foo", "bar", [ "foo" ] ) */]);',
'<?php someFunc([ /* array( "foo", "bar", [ "foo" ] ) */ ]);',
];
yield [
'<?php $foo = array($bar[ 4 ]);',
'<?php $foo = array( $bar[ 4 ] );',
];
yield [
'<?php $foo = [$bar[ 4 ]];',
'<?php $foo = [ $bar[ 4 ] ];',
];
yield [
'<?php // array( "foo", "bar" );',
];
// multiple single line nested arrays on one line
yield [
'<?php $foo = array("foo", "bar", [1, 2, array(3)]); $baz = ["hash", 1, array("test")];',
'<?php $foo = array( "foo", "bar", [ 1, 2, array( 3 )] ); $baz = [ "hash", 1, array( "test") ];',
];
yield [
"<?php \$foo = array( \n'bar'\n );",
];
yield [
"<?php \$foo = [ \n'bar'\n ];",
];
yield [
"<?php \$foo = array( \n'a', 'b',\n'c');",
"<?php \$foo = array( \n'a', 'b',\n'c' );",
];
yield [
"<?php \$foo = [ \n'a', 'b',\n'c'];",
"<?php \$foo = [ \n'a', 'b',\n'c' ];",
];
yield [
"<?php \$foo = array('a', 'b',\n'c'\n);",
"<?php \$foo = array( 'a', 'b',\n'c'\n);",
];
yield [
"<?php \$foo = ['a', 'b',\n'c'\n];",
"<?php \$foo = [ 'a', 'b',\n'c'\n];",
];
// don't fix array syntax within string
yield [
'<?php $foo = [\'$bar = array( "foo" );\', array(1, 5)];',
'<?php $foo = [ \'$bar = array( "foo" );\', array(1, 5 ) ];',
];
// crazy nested garbage pile #1
yield [
"<?php \$foo = array(/* comment \$bar = array([ ], array( 'foo' ) ), */ function(\$a = array('foo'), \$b = [/* comment [] */]) {}, array('foo' => 'bar', 'baz' => \$x[ 4], 'hash' => array(1,2,3)));",
"<?php \$foo = array( /* comment \$bar = array([ ], array( 'foo' ) ), */ function(\$a = array( 'foo' ), \$b = [ /* comment [] */ ]) {}, array( 'foo' => 'bar', 'baz' => \$x[ 4], 'hash' => array(1,2,3 )) );",
];
// crazy nested garbage pile #2
yield [
'<?php $a = [array("foo", "bar ", [1, 4, function($x = ["foobar", 2]) {}, [/* array( 1) */]]), array("foo", [$y[ 3]()], \'bar\')];',
'<?php $a = [ array("foo", "bar ", [ 1, 4, function($x = [ "foobar", 2 ]) {}, [/* array( 1) */] ] ), array("foo", [ $y[ 3]() ], \'bar\') ];',
];
yield [
'<?php
$foo = array(
1 => 2, // comment
);
',
];
yield [
'<?php
function a()
{
yield array("a" => 1, "b" => 2);
}',
'<?php
function a()
{
yield array( "a" => 1, "b" => 2 );
}',
];
yield [
'<?php
function a()
{
yield ["a" => 1, "b" => 2];
}',
'<?php
function a()
{
yield [ "a" => 1, "b" => 2 ];
}',
];
yield 'array destructuring' => [
"<?php ['url' => \$url] = \$data;",
"<?php [ 'url' => \$url ] = \$data;",
];
yield 'array destructuring with comments' => [
"<?php [/* foo */ 'url' => \$url /* bar */] = \$data;",
"<?php [ /* foo */ 'url' => \$url /* bar */ ] = \$data;",
];
yield 'multiline array destructuring' => [
'<?php
[
\'url\' => $url,
\'token\' => $token,
] = $data;
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixerTest.php | tests/Fixer/ArrayNotation/NoWhitespaceBeforeCommaInArrayFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer>
*
* @author Adam Marczuk <adam@marczuk.info>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoWhitespaceBeforeCommaInArrayFixerTest 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<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
// old style array
yield [
'<?php $x = array(1, "2",3);',
'<?php $x = array(1 , "2",3);',
];
// old style array with comments
yield [
'<?php $x = array /* comment */ (1, "2", 3);',
'<?php $x = array /* comment */ (1 , "2", 3);',
];
// old style array with comments
yield [
'<?php $x = array(1#
,#
"2", 3);',
'<?php $x = array(1#
,#
"2" , 3);',
];
// short array
yield [
'<?php $x = [1, "2", 3,$y];',
'<?php $x = [1 , "2", 3 ,$y];',
];
// don't change function calls
yield [
'<?php $x = [ 1, "2",getValue(1,2 ,3 ),$y];',
'<?php $x = [ 1 , "2",getValue(1,2 ,3 ) ,$y];',
];
// don't change function declarations
yield [
'<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
'<?php $x = [1 , "2", function( $x ,$y) { return $x + $y; }, $y];',
];
// don't change function declarations but change array inside
yield [
'<?php $x = [ 1, "2","c" => function( $x ,$y) { return [$x, $y]; }, $y];',
'<?php $x = [ 1 , "2","c" => function( $x ,$y) { return [$x , $y]; }, $y];',
];
// don't change anonymous class implements list but change array inside
yield [
'<?php $x = [ 1, "2","c" => new class implements Foo , Bar { const FOO = ["x", "y"]; }, $y];',
'<?php $x = [ 1 , "2","c" => new class implements Foo , Bar { const FOO = ["x" , "y"]; }, $y];',
];
// associative array (old)
yield [
'<?php $x = array( "a" => $a, "b" => "b",3=>$this->foo(), "d" => 30);',
'<?php $x = array( "a" => $a , "b" => "b",3=>$this->foo() , "d" => 30);',
];
// associative array (short)
yield [
'<?php $x = [ "a" => $a, "b"=>"b",3 => $this->foo(), "d" =>30 ];',
'<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo() , "d" =>30 ];',
];
// nested arrays
yield [
'<?php $x = ["a" => $a, "b" => "b", 3=> [5,6, 7], "d" => array(1, 2,3,4)];',
'<?php $x = ["a" => $a , "b" => "b", 3=> [5 ,6, 7] , "d" => array(1, 2,3 ,4)];',
];
// multi line array
yield [
'<?php $x = [ "a" =>$a,
"b"=>
"b",
3 => $this->foo(),
"d" => 30 ];',
'<?php $x = [ "a" =>$a ,
"b"=>
"b",
3 => $this->foo() ,
"d" => 30 ];',
];
// multi line array
yield [
'<?php $a = [
"foo",
"bar",
];',
'<?php $a = [
"foo" ,
"bar"
,
];',
];
// nested multiline
yield [
'<?php $a = array(array(
array(T_OPEN_TAG),
array(T_VARIABLE, "$x"),
), 1);',
];
yield [
'<?php $a = array( // comment
123,
);',
];
yield [
"<?php \$x = array(<<<'EOF'
<?php \$a = '\\foo\\bar\\\\';
EOF
, <<<'EOF'
<?php \$a = \"\\foo\\bar\\\\\";
EOF
);",
];
yield [
"<?php \$x = array(<<<'EOF'
<?php \$a = '\\foo\\bar\\\\';
EOF, <<<'EOF'
<?php \$a = \"\\foo\\bar\\\\\";
EOF
);",
"<?php \$x = array(<<<'EOF'
<?php \$a = '\\foo\\bar\\\\';
EOF
, <<<'EOF'
<?php \$a = \"\\foo\\bar\\\\\";
EOF
);",
['after_heredoc' => true],
];
yield [
'<?php $x = array(...$foo, ...$bar);',
'<?php $x = array(...$foo , ...$bar);',
];
yield [
'<?php $x = [...$foo, ...$bar];',
'<?php $x = [...$foo , ...$bar];',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/WhitespaceAfterCommaInArrayFixerTest.php | tests/Fixer/ArrayNotation/WhitespaceAfterCommaInArrayFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer>
*
* @author Adam Marczuk <adam@marczuk.info>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class WhitespaceAfterCommaInArrayFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
{
if (null !== $configuration) {
$this->fixer->configure($configuration);
}
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
// old style array
yield [
'<?php $x = array( 1 , "2", 3);',
'<?php $x = array( 1 ,"2",3);',
];
// old style array with comments
yield [
'<?php $x = array /* comment */ ( 1 , "2", 3);',
'<?php $x = array /* comment */ ( 1 , "2",3);',
];
// short array
yield [
'<?php $x = [ 1 , "2", 3 , $y];',
'<?php $x = [ 1 , "2",3 ,$y];',
];
// don't change function calls
yield [
'<?php $x = [1, "2", getValue(1,2 ,3 ) , $y];',
'<?php $x = [1, "2",getValue(1,2 ,3 ) ,$y];',
];
// don't change function declarations
yield [
'<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
'<?php $x = [1, "2",function( $x ,$y) { return $x + $y; },$y];',
];
// don't change function declarations but change array inside
yield [
'<?php $x = [1, "2", "c" => function( $x ,$y) { return [$x , $y]; }, $y ];',
'<?php $x = [1, "2","c" => function( $x ,$y) { return [$x ,$y]; },$y ];',
];
// don't change anonymous class implements list but change array inside
yield [
'<?php $x = [1, "2", "c" => new class implements Foo ,Bar { const FOO = ["x", "y"]; }, $y ];',
'<?php $x = [1, "2","c" => new class implements Foo ,Bar { const FOO = ["x","y"]; },$y ];',
];
// associative array (old)
yield [
'<?php $x = array("a" => $a , "b" => "b", 3=>$this->foo(), "d" => 30 );',
'<?php $x = array("a" => $a , "b" => "b",3=>$this->foo(), "d" => 30 );',
];
// associative array (short)
yield [
'<?php $x = [ "a" => $a , "b"=>"b", 3 => $this->foo(), "d" =>30];',
'<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo(), "d" =>30];',
];
// nested arrays
yield [
'<?php $x = ["a" => $a, "b" => "b", 3=> [5, 6, 7] , "d" => array(1, 2, 3 , 4)];',
'<?php $x = ["a" => $a, "b" => "b",3=> [5,6, 7] , "d" => array(1, 2,3 ,4)];',
];
// multi line array
yield [
'<?php $x = ["a" =>$a,
"b"=> "b",
3 => $this->foo(),
"d" => 30];',
];
// multi line array
yield [
'<?php $a = [
"foo" ,
"bar",
];',
];
// nested multiline
yield [
'<?php $a = array(array(
array(T_OPEN_TAG),
array(T_VARIABLE, "$x"),
), 1, );',
'<?php $a = array(array(
array(T_OPEN_TAG),
array(T_VARIABLE,"$x"),
),1,);',
];
yield [
'<?php $a = array( // comment
123,
);',
];
yield [
'<?php $x = array(...$foo, ...$bar);',
'<?php $x = array(...$foo,...$bar);',
];
yield [
'<?php $x = [...$foo, ...$bar];',
'<?php $x = [...$foo,...$bar];',
];
yield [
'<?php [0, 1, 2, 3, 4, 5, 6];',
'<?php [0,1, 2, 3, 4, 5, 6];',
['ensure_single_space' => true],
];
yield [
'<?php [0, 1, 2, 3, 4, 5];',
"<?php [0,\t1,\t\t\t2,\t 3, \t4, \t 5];",
['ensure_single_space' => true],
];
yield [
'<?php [
0, # less than one
1, // one
42, /* more than one */
1000500100900, /** much more than one */
];',
null,
['ensure_single_space' => true],
];
yield [
'<?php [0, /* comment */ 1, /** PHPDoc */ 2];',
'<?php [0, /* comment */ 1, /** PHPDoc */ 2];',
['ensure_single_space' => true],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/NoTrailingCommaInSinglelineArrayFixerTest.php | tests/Fixer/ArrayNotation/NoTrailingCommaInSinglelineArrayFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer>
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoTrailingCommaInSinglelineArrayFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php $x = array();'];
yield ['<?php $x = array("foo");'];
yield [
'<?php $x = array("foo");',
'<?php $x = array("foo", );',
];
yield ["<?php \$x = array(\n'foo', \n);"];
yield ["<?php \$x = array('foo', \n);"];
yield ["<?php \$x = array(array('foo'), \n);", "<?php \$x = array(array('foo',), \n);"];
yield ["<?php \$x = array(array('foo',\n), \n);"];
yield [
'<?php
$test = array("foo", <<<TWIG
foo
TWIG
, $twig, );',
];
yield [
'<?php
$test = array(
"foo", <<<TWIG
foo
TWIG
, $twig, );',
];
yield [
'<?php
$test = array("foo", <<<\'TWIG\'
foo
TWIG
, $twig, );',
];
yield [
'<?php
$test = array(
"foo", <<<\'TWIG\'
foo
TWIG
, $twig, );',
];
// Short syntax
yield ['<?php $x = array([]);'];
yield ['<?php $x = [[]];'];
yield ['<?php $x = ["foo"];', '<?php $x = ["foo",];'];
yield ['<?php $x = bar(["foo"]);', '<?php $x = bar(["foo",]);'];
yield ["<?php \$x = bar([['foo'],\n]);"];
yield ["<?php \$x = ['foo', \n];"];
yield ['<?php $x = array([]);', '<?php $x = array([],);'];
yield ['<?php $x = [[]];', '<?php $x = [[],];'];
yield ['<?php $x = [$y[""]];', '<?php $x = [$y[""],];'];
yield [
'<?php
$test = ["foo", <<<TWIG
foo
TWIG
, $twig, ];',
];
yield [
'<?php
$test = [
"foo", <<<TWIG
foo
TWIG
, $twig, ];',
];
yield [
'<?php
$test = ["foo", <<<\'TWIG\'
foo
TWIG
, $twig, ];',
];
yield [
'<?php
$test = [
"foo", <<<\'TWIG\'
foo
TWIG
, $twig, ];',
];
yield [
'<?php $x = array(...$foo);',
'<?php $x = array(...$foo, );',
];
yield [
'<?php $x = [...$foo];',
'<?php $x = [...$foo, ];',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/ReturnToYieldFromFixerTest.php | tests/Fixer/ArrayNotation/ReturnToYieldFromFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\ReturnToYieldFromFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\ReturnToYieldFromFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ReturnToYieldFromFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php return [function() { return [1, 2, 3]; }];'];
yield ['<?php return [fn() => [1, 2, 3]];'];
yield [
'<?php
function foo(): iterable { return $z; }
return [1,2] ?> X <?php { echo 2; }',
];
yield ['<?php function foo() { return [1, 2, 3]; }'];
yield ['<?php function foo(): MyAwesomeIterableType { return [1, 2, 3]; }'];
yield ['<?php function foo(): iterable { if (true) { return [1]; } else { return [2]; } }'];
yield ['<?php function foo(): ?iterable { return [1, 2, 3]; }'];
yield ['<?php
abstract class Foo {
abstract public function bar(): iterable;
public function baz(): array { return []; }
}
'];
yield [
'<?php return [function(): iterable { yield from [1, 2, 3]; }];',
'<?php return [function(): iterable { return [1, 2, 3]; }];',
];
yield [
'<?php class Foo {
function bar(): iterable { yield from [1, 2, 3]; }
}',
'<?php class Foo {
function bar(): iterable { return [1, 2, 3]; }
}',
];
yield [
'<?php function foo(): iterable { yield from [1, 2, 3];;;;;;;; }',
'<?php function foo(): iterable { return [1, 2, 3];;;;;;;; }',
];
yield [
'<?php function foo(): iterable { yield from array(1, 2, 3); }',
'<?php function foo(): iterable { return array(1, 2, 3); }',
];
yield [
'<?php function foo(): iterable { $x = 0; yield from [1, 2, 3]; }',
'<?php function foo(): iterable { $x = 0; return [1, 2, 3]; }',
];
yield [
'<?php function foo(): iterable { $x = 0; yield from array(1, 2, 3); }',
'<?php function foo(): iterable { $x = 0; return array(1, 2, 3); }',
];
yield [
'<?php function foo(): ITERABLE { yield from [1, 2, 3]; }',
'<?php function foo(): ITERABLE { return [1, 2, 3]; }',
];
yield [
'<?php $f = function(): iterable { yield from [1, 2, 3]; };',
'<?php $f = function(): iterable { return [1, 2, 3]; };',
];
yield [
'<?php
function foo(): array { return [3, 4]; }
function bar(): iterable { yield from [1, 2]; }
function baz(): int { return 5; }
',
'<?php
function foo(): array { return [3, 4]; }
function bar(): iterable { return [1, 2]; }
function baz(): int { return 5; }
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function foo(): null|iterable { return [1, 2, 3]; }',
];
yield [
'<?php function foo(): iterable|null { return [1, 2, 3]; }',
];
yield [
'<?php function foo(): ITERABLE|null { return [1, 2, 3]; }',
];
yield [
'<?php function foo(): Bar|iterable { return [1, 2, 3]; }',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php | tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer>
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
* @author Gregor Harlan <gharlan@web.de>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ArraySyntaxFixerTest extends AbstractFixerTestCase
{
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('#^\[array_syntax\] Invalid configuration: The option "a" does not exist\. Defined options are: "syntax"\.$#');
$this->fixer->configure(['a' => 1]);
}
/**
* @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, _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'default configuration' => [
'<?php $a = []; $b = [];',
'<?php $a = []; $b = array();',
[],
];
yield [
'<?php $x = array();',
'<?php $x = [];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(); $y = array();',
'<?php $x = []; $y = [];',
['syntax' => 'long'],
];
yield [
'<?php $x = array( );',
'<?php $x = [ ];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(\'foo\');',
'<?php $x = [\'foo\'];',
['syntax' => 'long'],
];
yield [
'<?php $x = array( \'foo\' );',
'<?php $x = [ \'foo\' ];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(($y ? true : false));',
'<?php $x = [($y ? true : false)];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(($y ? array(true) : array(false)));',
'<?php $x = [($y ? [true] : [false])];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(($y ? array(true) : array( false )));',
'<?php $x = [($y ? [true] : [ false ])];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(($y ? array("t" => true) : array("f" => false)));',
'<?php $x = [($y ? ["t" => true] : ["f" => false])];',
['syntax' => 'long'], ];
yield [
'<?php print_r(array(($y ? true : false)));',
'<?php print_r([($y ? true : false)]);',
['syntax' => 'long'],
];
yield [
'<?php $x = array(array(array()));',
'<?php $x = [[[]]];',
['syntax' => 'long'],
];
yield [
'<?php $x = array(array(array())); $y = array(array(array()));',
'<?php $x = [[[]]]; $y = [[[]]];',
['syntax' => 'long'],
];
yield [
'<?php function(array $foo = array()) {};',
'<?php function(array $foo = []) {};',
['syntax' => 'long'],
];
yield [
'<?php $x = array(1, 2)[0];',
'<?php $x = [1, 2][0];',
['syntax' => 'long'],
];
yield [
'<?php $x[] = 1;',
null,
['syntax' => 'long'],
];
yield [
'<?php $x[ ] = 1;',
null,
['syntax' => 'long'],
];
yield [
'<?php $x[2] = 1;',
null,
['syntax' => 'long'],
];
yield [
'<?php $x["a"] = 1;',
null,
['syntax' => 'long'],
];
yield [
'<?php $x = func()[$x];',
null,
['syntax' => 'long'],
];
yield [
'<?php $x = "foo"[$x];',
null,
['syntax' => 'long'],
];
yield [
'<?php $text = "foo ${aaa[123]} bar $bbb[0] baz";',
null,
['syntax' => 'long'],
];
yield [
'<?php foreach ($array as [$x, $y]) {}',
null,
['syntax' => 'long'],
];
yield [
'<?php foreach ($array as $key => [$x, $y]) {}',
null,
['syntax' => 'long'],
];
yield [
'<?php $x = [];',
'<?php $x = array();',
['syntax' => 'short'],
];
yield [
'<?php $x = []; $y = [];',
'<?php $x = array(); $y = array();',
['syntax' => 'short'],
];
yield [
'<?php $x = [ ];',
'<?php $x = array( );',
['syntax' => 'short'],
];
yield [
'<?php $x = [\'foo\'];',
'<?php $x = array(\'foo\');',
['syntax' => 'short'],
];
yield [
'<?php $x = [ \'foo\' ];',
'<?php $x = array( \'foo\' );',
['syntax' => 'short'],
];
yield [
'<?php $x = [($y ? true : false)];',
'<?php $x = array(($y ? true : false));',
['syntax' => 'short'],
];
yield [
'<?php $x = [($y ? [true] : [false])];',
'<?php $x = array(($y ? array(true) : array(false)));',
['syntax' => 'short'],
];
yield [
'<?php $x = [($y ? [true] : [ false ])];',
'<?php $x = array(($y ? array(true) : array( false )));',
['syntax' => 'short'],
];
yield [
'<?php $x = [($y ? ["t" => true] : ["f" => false])];',
'<?php $x = array(($y ? array("t" => true) : array("f" => false)));',
['syntax' => 'short'],
];
yield [
'<?php print_r([($y ? true : false)]);',
'<?php print_r(array(($y ? true : false)));',
['syntax' => 'short'],
];
yield [
'<?php $x = [[[]]];',
'<?php $x = array(array(array()));',
['syntax' => 'short'],
];
yield [
'<?php $x = [[[]]]; $y = [[[]]];',
'<?php $x = array(array(array())); $y = array(array(array()));',
['syntax' => 'short'],
];
yield [
'<?php function(array $foo = []) {};',
'<?php function(array $foo = array()) {};',
['syntax' => 'short'],
];
yield [
'<?php function(array $foo) {};',
null,
['syntax' => 'short'],
];
yield [
'<?php $a = [ ];',
'<?php $a = array ( );',
['syntax' => 'short'],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ArrayNotation/NormalizeIndexBraceFixerTest.php | tests/Fixer/ArrayNotation/NormalizeIndexBraceFixerTest.php | <?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\ArrayNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ArrayNotation\NormalizeIndexBraceFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\NormalizeIndexBraceFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NormalizeIndexBraceFixerTest extends AbstractFixerTestCase
{
/**
* @requires PHP <8.0
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php echo $arr[$index];',
'<?php echo $arr{$index};',
];
yield [
'<?php echo $nestedArray[$index][$index2][$index3][$index4];',
'<?php echo $nestedArray{$index}{$index2}[$index3]{$index4};',
];
yield [
'<?php echo $array[0]->foo . $collection->items[1]->property;',
'<?php echo $array{0}->foo . $collection->items{1}->property;',
];
}
/**
* @requires PHP 8.4
*
* @dataProvider provideFix84Cases
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'property hooks: property without default value' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar {
set(string $value) {
$this -> foo = strtolower($value);
}
}
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/CombineConsecutiveIssetsFixerTest.php | tests/Fixer/LanguageConstruct/CombineConsecutiveIssetsFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveIssetsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveIssetsFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class CombineConsecutiveIssetsFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php $a = isset($a, $b) ;',
'<?php $a = isset($a) && isset($b);',
];
yield [
'<?php $a = isset($a, $b,$c) ;',
'<?php $a = isset($a) && isset($b,$c);',
];
yield [
'<?php $a = isset($a,$c, $b,$c) ;',
'<?php $a = isset($a,$c) && isset($b,$c);',
];
yield [
'<?php $a = isset($a,$c, $b) ;',
'<?php $a = isset($a,$c) && isset($b);',
];
yield [
'<?php $a = isset($a, $b) || isset($c, $e) ?>',
'<?php $a = isset($a) && isset($b) || isset($c) && isset($e)?>',
];
yield [
'<?php $a = isset($a[a() ? b() : $d], $b) ;',
'<?php $a = isset($a[a() ? b() : $d]) && isset($b);',
];
yield [
'<?php $a = isset($a[$b], $b/**/) ;',
'<?php $a = isset($a[$b]/**/) && isset($b);',
];
yield [
'<?php $a = isset ( $a, $c, $d /*1*/ ) ;',
'<?php $a = isset ( $a /*1*/ ) && isset ( $c ) && isset( $d );',
];
yield 'minimal fix case' => [
'<?php {{isset($a, $b);}}',
'<?php {{isset($a)&&isset($b);}}',
];
yield [
'<?php foo(isset($a, $b, $c) );',
'<?php foo(isset($a) && isset($b) && isset($c));',
];
yield [
'<?php isset($a, $b) && !isset($c) ?>',
'<?php isset($a) && isset($b) && !isset($c) ?>',
];
yield [
'<?php $a = isset($a,$c, $b,$c, $b,$c,$d,$f, $b) ;',
'<?php $a = isset($a,$c) && isset($b,$c) && isset($b,$c,$d,$f) && isset($b);',
];
yield 'comments' => [
'<?php
$a =#0
isset#1
(#2
$a, $b,$c, $d#3
)#4
#5
#6
#7
#8
#9
/*10*/ /**11
*/
'.'
;',
'<?php
$a =#0
isset#1
(#2
$a#3
)#4
&
isset
#6
#7
( #8
$b #9
/*10*/, $c/**11
*/
)&& isset($d)
;',
];
yield [
'<?php
$a = isset($a, $b, $c, $d, $e, $f) ;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
$a = isset($a, $b) ;
',
'<?php
$a = isset($a) && isset($b) && isset($c) && isset($d) && isset($e) && isset($f);
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
echo 1; echo 1; echo 1; echo 1; echo 1; echo 1; echo 1;
$a = isset($a) && isset($b);
',
];
yield [
'<?php $d = isset($z[1], $z[2], $z[3]) || false;',
'<?php $d = isset($z[1]) && isset($z[2]) && isset($z[3]) || false;',
];
yield [
'<?php
$a = isset($a, $b) && isset($c) === false;
$a = isset($a, $b) && isset($c) | false;
$a = isset($a, $b) && isset($c) ^ false;
',
'<?php
$a = isset($a) && isset($b) && isset($c) === false;
$a = isset($a) && isset($b) && isset($c) | false;
$a = isset($a) && isset($b) && isset($c) ^ false;
',
];
// don't fix cases
yield [
'<?php $a = isset($a) && $a->isset(); $b=isset($d);',
];
yield [
'<?php
$a = !isset($a) && isset($b);
$a = !isset($a) && !isset($b);
$a = isset($a) && !isset($b);
//
$a = isset($b) && isset($c) === false;
$a = isset($b) && isset($c) | false;
$a = isset($b) && isset($c) ^ false;
//
$a = false === isset($b) && isset($c);
$a = false | isset($b) && isset($c);
$a = false ^ isset($b) && isset($c);
',
];
yield [
'<?php $a = !isset($container[$a]) && isset($container[$b]) && !isset($container[$c]) && isset($container[$d]);',
];
yield 'anonymous class' => [
'<?php
class A {function isset(){}} // isset($b) && isset($c)
$a = new A(); /** isset($b) && isset($c) */
if (isset($b) && $a->isset()) {}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/ErrorSuppressionFixerTest.php | tests/Fixer/LanguageConstruct/ErrorSuppressionFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer>
*
* @author Jules Pietri <jules@heahprod.com>
* @author Kuba Werłos <werlos@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\ErrorSuppressionFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ErrorSuppressionFixerTest 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<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php trigger_error("This is not a deprecation warning."); @f(); ?>',
];
yield [
'<?php trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
];
yield [
'<?php A\B\trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
null,
[ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => false],
];
yield [
'<?php @\trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
'<?php \trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php echo "test";@trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
'<?php echo "test";trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php //
@Trigger_Error/**/("This is a deprecation warning.", E_USER_DEPRECATED/***/); ?>',
'<?php //
Trigger_Error/**/("This is a deprecation warning.", E_USER_DEPRECATED/***/); ?>',
];
yield [
'<?php new trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php new \trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php $foo->trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php Foo::trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
];
yield [
'<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); mkdir("dir"); ?>',
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
[ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
];
yield [
'<?php $foo->isBar(); ?>',
'<?php @$foo->isBar(); ?>',
[ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
];
yield [
'<?php Foo::isBar(); ?>',
'<?php @Foo::isBar(); ?>',
[ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true],
];
yield [
'<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); ?>',
[ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['mkdir']],
];
yield [
'<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); unlink($path); ?>',
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @mkdir("dir"); @unlink($path); ?>',
[ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['mkdir']],
];
yield [
'<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED); @trigger_error("This is not a deprecation warning.", E_USER_WARNING); ?>',
[ErrorSuppressionFixer::OPTION_MUTE_DEPRECATION_ERROR => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES => true, ErrorSuppressionFixer::OPTION_NOISE_REMAINING_USAGES_EXCLUDE => ['trigger_error']],
];
yield [
'<?php @trigger_error("This is a deprecation warning.", E_USER_DEPRECATED, );',
'<?php trigger_error("This is a deprecation warning.", E_USER_DEPRECATED, );',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php \A\B/* */\trigger_error("This is not a deprecation warning.", E_USER_DEPRECATED); ?>',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php $a = trigger_error(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/GetClassToClassKeywordFixerTest.php | tests/Fixer/LanguageConstruct/GetClassToClassKeywordFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\GetClassToClassKeywordFixer
*
* @requires PHP 8.0
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\GetClassToClassKeywordFixer>
*
* @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class GetClassToClassKeywordFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'
<?php
$before = $before::class;
$after = $after::class;
',
'
<?php
$before = get_class($before);
$after = get_class($after);
',
];
yield [
'<?php $abc::class;',
'<?php get_class($abc);',
];
yield [
'<?php $a::class ;',
'<?php get_class( $a );',
];
yield [
'<?php $b::class;',
'<?php \get_class($b);',
];
yield [
'<?php $c::class;',
'<?php GET_class($c);',
];
yield [
'<?php $d::class/* a */;',
'<?php get_class($d/* a */);',
];
yield [
'<?php $e::class /** b */;',
'<?php get_class($e /** b */);',
];
yield [
'<?php $f::class ;',
'<?php get_class ( $f );',
];
yield [
'<?php $g::class/* x */ /* y */;',
'<?php \get_class(/* x */ $g /* y */);',
];
yield [
'<?php $h::class;',
'<?php get_class(($h));',
];
yield [
"<?php\necho \$bar::class\n \n;\n",
"<?php\necho get_class(\n \$bar\n);\n",
];
yield [
'<?php get_class;',
];
yield [
'<?php get_class($this);',
];
yield [
'<?php get_class();',
];
yield [
'<?php get_class(/* $a */);',
];
yield [
'<?php get_class(/** $date */);',
];
yield [
'<?php $a = get_class(12);',
];
yield [
'<?php get_class($a.$b);',
];
yield [
'<?php get_class($a === $b);',
];
yield [
'<?php get_class($foo->bar);',
];
yield [
'<?php get_class($$foo);',
];
yield [
'<?php get_class($arr[$bar]);',
];
yield [
'<?php \a\get_class($foo);',
];
yield [
'<?php
class A
{
public function get_class($foo) {}
}
',
];
yield [
'<?php get_class($a, $b);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php | tests/Fixer/LanguageConstruct/SingleSpaceAroundConstructFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAroundConstructFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAroundConstructFixer>
*
* @author Andreas Möller <am@localheinz.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAroundConstructFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleSpaceAroundConstructFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideInvalidConfigurationCases
*
* @param mixed $construct
*/
public function testInvalidConfiguration($construct): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->fixer->configure([
'constructs_followed_by_a_single_space' => [
$construct,
],
]);
}
/**
* @return iterable<string, array{mixed}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'null' => [null];
yield 'false' => [false];
yield 'true' => [true];
yield 'int' => [0];
yield 'float' => [3.14];
yield 'array' => [[]];
yield 'object' => [new \stdClass()];
yield 'unknown' => ['foo'];
}
/**
* @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, _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php abstract class Foo {}; if($a){}',
'<?php abstract class Foo {}; if($a){}',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php abstract class Foo {};',
'<?php abstract
class Foo {};',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php abstract /* foo */class Foo {};',
'<?php abstract/* foo */class Foo {};',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php abstract /* foo */class Foo {};',
'<?php abstract /* foo */class Foo {};',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php
abstract class Foo
{
abstract function bar();
}',
'<?php
abstract class Foo
{
abstract function bar();
}',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php
abstract class Foo
{
abstract function bar();
}',
'<?php
abstract class Foo
{
abstract
function bar();
}',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php
abstract class Foo
{
abstract /* foo */function bar();
}',
'<?php
abstract class Foo
{
abstract /* foo */function bar();
}',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php
abstract class Foo
{
abstract /* foo */function bar();
}',
'<?php
abstract class Foo
{
abstract/* foo */function bar();
}',
['constructs_followed_by_a_single_space' => ['abstract']],
];
yield [
'<?php while (true) { break; }',
null,
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php while (true) { break /* foo */; }',
'<?php while (true) { break/* foo */; }',
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php while (true) { break /* foo */; }',
'<?php while (true) { break /* foo */; }',
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php while (true) { break 1; }',
'<?php while (true) { break 1; }',
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php while (true) { break 1; }',
'<?php while (true) { break
1; }',
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php while (true) { break /* foo */1; }',
'<?php while (true) { break/* foo */1; }',
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php while (true) { break /* foo */1; }',
'<?php while (true) { break /* foo */1; }',
['constructs_followed_by_a_single_space' => ['break']],
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as$bar) {}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as $bar) {}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as $bar) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as
$bar) {}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo
as $bar) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach ($foo as /* foo */$bar) {}',
'<?php foreach ($foo as/* foo */$bar) {}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php foreach ($foo/* foo */ as $bar) {}',
'<?php foreach ($foo/* foo */as $bar) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach ($foo as /* foo */$bar) {}',
'<?php foreach ($foo as /* foo */$bar) {}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php foreach ($foo /* foo */ as $bar) {}',
'<?php foreach ($foo /* foo */ as $bar) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach (range(1, 12) as $num) {}',
'<?php foreach (range(1, 12)as $num) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach (range(1, 12) as $num) {}',
'<?php foreach (range(1, 12) as $num) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach ([1, 2, 3, 4] as $int) {}',
'<?php foreach ([1, 2, 3, 4]as $int) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php foreach ([1, 2, 3, 4] as $int) {}',
'<?php foreach ([1, 2, 3, 4]
as $int) {}',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as
bar;
}
}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as /* foo */bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as/* foo */bar;
}
}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as /* foo */bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as /* foo */bar;
}
}',
[
'constructs_preceded_by_a_single_space' => [],
'constructs_followed_by_a_single_space' => ['as'],
],
];
yield [
'<?php
namespace Foo;
use Bar as Baz;
final class Qux extends Baz {}
',
'<?php
namespace Foo;
use Bar as Baz;
final class Qux extends Baz {}
',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
namespace Foo;
use Bar as Baz;
final class Qux extends Baz {}
',
'<?php
namespace Foo;
use Bar
as Baz;
final class Qux extends Baz {}
',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
namespace Foo;
use Bar /** foo */ as Baz;
final class Qux extends Baz {}
',
'<?php
namespace Foo;
use Bar /** foo */as Baz;
final class Qux extends Baz {}
',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}
',
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}
',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}
',
'<?php
class Foo
{
use Bar {
Bar::baz
as bar;
}
}
',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz/** foo */ as bar;
}
}
',
'<?php
class Foo
{
use Bar {
Bar::baz/** foo */as bar;
}
}
',
[
'constructs_preceded_by_a_single_space' => ['as'],
'constructs_followed_by_a_single_space' => [],
],
];
yield [
'<?php
switch ($i) {
case $j:
break;
}',
'<?php
switch ($i) {
case$j:
break;
}',
['constructs_followed_by_a_single_space' => ['case']],
];
yield [
'<?php
switch ($i) {
case 0:
break;
}',
'<?php
switch ($i) {
case 0:
break;
}',
['constructs_followed_by_a_single_space' => ['case']],
];
yield [
'<?php
switch ($i) {
case 0:
break;
}',
'<?php
switch ($i) {
case
0:
break;
}',
['constructs_followed_by_a_single_space' => ['case']],
];
yield [
'<?php
switch ($i) {
case /* foo */0:
break;
}',
'<?php
switch ($i) {
case/* foo */0:
break;
}',
['constructs_followed_by_a_single_space' => ['case']],
];
yield [
'<?php try {} catch (\Exception $exception) {}',
'<?php try {} catch(\Exception $exception) {}',
['constructs_followed_by_a_single_space' => ['catch']],
];
yield [
'<?php try {} catch (\Exception $exception) {}',
'<?php try {} catch (\Exception $exception) {}',
['constructs_followed_by_a_single_space' => ['catch']],
];
yield [
'<?php try {} catch (\Exception $exception) {}',
'<?php try {} catch
(\Exception $exception) {}',
['constructs_followed_by_a_single_space' => ['catch']],
];
yield [
'<?php try {} catch /* foo */(Exception $exception) {}',
'<?php try {} catch/* foo */(Exception $exception) {}',
['constructs_followed_by_a_single_space' => ['catch']],
];
yield [
'<?php try {} catch /* foo */(Exception $exception) {}',
'<?php try {} catch /* foo */(Exception $exception) {}',
['constructs_followed_by_a_single_space' => ['catch']],
];
yield [
'<?php class Foo {}',
'<?php class Foo {}',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php class Foo {}',
'<?php class
Foo {}',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php class /* foo */Foo {}',
'<?php class /* foo */Foo {}',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php class /* foo */Foo {}',
'<?php class/* foo */Foo {}',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php $foo = stdClass::class;',
null,
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php $foo = new class {};',
'<?php $foo = new class {};',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php $foo = new class {};',
'<?php $foo = new class{};',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php $foo = new class /* foo */{};',
'<?php $foo = new class/* foo */{};',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php $foo = new class /* foo */{};',
'<?php $foo = new class /* foo */{};',
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php $foo = new class(){};',
null,
['constructs_followed_by_a_single_space' => ['class']],
];
yield [
'<?php return
$a ? new class(){ public function foo() { echo 1; }}
: 1
;',
null,
['constructs_followed_by_a_single_space' => ['return']],
];
yield [
'<?php while (true) { continue; }',
null,
['constructs_followed_by_a_single_space' => ['continue']],
];
yield [
'<?php while (true) { continue /* foo */; }',
'<?php while (true) { continue/* foo */; }',
['constructs_followed_by_a_single_space' => ['continue']],
];
yield [
'<?php while (true) { continue /* foo */; }',
'<?php while (true) { continue /* foo */; }',
['constructs_followed_by_a_single_space' => ['continue']],
];
yield [
'<?php while (true) { continue 1; }',
'<?php while (true) { continue 1; }',
['constructs_followed_by_a_single_space' => ['continue']],
];
yield [
'<?php while (true) { continue 1; }',
'<?php while (true) { continue
1; }',
['constructs_followed_by_a_single_space' => ['continue']],
];
yield [
'<?php while (true) { continue /* foo*/ 1; }',
'<?php while (true) { continue /* foo*/ 1; }',
['constructs_followed_by_a_single_space' => ['continue']],
];
yield [
'<?php class Foo { const FOO = 9000; }',
'<?php class Foo { const FOO = 9000; }',
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php class Foo { const FOO = 9000; }',
'<?php class Foo { const
FOO = 9000; }',
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php class Foo { const /* foo */FOO = 9000; }',
'<?php class Foo { const/* foo */FOO = 9000; }',
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php class Foo { const /* foo */FOO = 9000; }',
'<?php class Foo { const /* foo */FOO = 9000; }',
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php class Foo {
const FOO = [
1
];
const BAR = [
2,
];
}',
'<?php class Foo {
const FOO = [
1
];
const BAR = [
2,
];
}',
['constructs_followed_by_a_single_space' => ['const']],
];
yield ['<?php class Foo {
const
FOO = 9000,
BAR = 10000;
}',
null,
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php
const
A = 3,
B = 3
?>',
null,
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php
const A = 3 ?>
<?php
[ ,
,
,$z
] = foo() ;',
'<?php
const A = 3 ?>
<?php
[ ,
,
,$z
] = foo() ;',
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php
const A
=
1;
',
'<?php
const
A
=
1;
',
['constructs_followed_by_a_single_space' => ['const']],
];
yield [
'<?php use const FOO\BAR;',
'<?php use const FOO\BAR;',
['constructs_followed_by_a_single_space' => ['const_import']],
];
yield [
'<?php use const FOO\BAR;',
'<?php use const
FOO\BAR;',
['constructs_followed_by_a_single_space' => ['const_import']],
];
yield [
'<?php use const /* foo */FOO\BAR;',
'<?php use const/* foo */FOO\BAR;',
['constructs_followed_by_a_single_space' => ['const_import']],
];
yield [
'<?php use const /* foo */FOO\BAR;',
'<?php use const /* foo */FOO\BAR;',
['constructs_followed_by_a_single_space' => ['const_import']],
];
yield [
'<?php clone $foo;',
'<?php clone$foo;',
['constructs_followed_by_a_single_space' => ['clone']],
];
yield [
'<?php clone $foo;',
'<?php clone $foo;',
['constructs_followed_by_a_single_space' => ['clone']],
];
yield [
'<?php clone $foo;',
'<?php clone
$foo;',
['constructs_followed_by_a_single_space' => ['clone']],
];
yield [
'<?php clone /* foo */$foo;',
'<?php clone/* foo */$foo;',
['constructs_followed_by_a_single_space' => ['clone']],
];
yield [
'<?php do {} while (true);',
'<?php do{} while (true);',
['constructs_followed_by_a_single_space' => ['do']],
];
yield [
'<?php DO {} while (true);',
'<?php DO{} while (true);',
['constructs_followed_by_a_single_space' => ['do']],
];
yield [
'<?php do {} while (true);',
'<?php do {} while (true);',
['constructs_followed_by_a_single_space' => ['do']],
];
yield [
'<?php do {} while (true);',
'<?php do
{} while (true);',
['constructs_followed_by_a_single_space' => ['do']],
];
yield [
'<?php do /* foo*/{} while (true);',
'<?php do/* foo*/{} while (true);',
['constructs_followed_by_a_single_space' => ['do']],
];
yield [
'<?php do /* foo*/{} while (true);',
'<?php do /* foo*/{} while (true);',
['constructs_followed_by_a_single_space' => ['do']],
];
yield [
'<?php echo $foo;',
'<?php echo$foo;',
['constructs_followed_by_a_single_space' => ['echo']],
];
yield [
'<?php echo 9000;',
'<?php echo 9000;',
['constructs_followed_by_a_single_space' => ['echo']],
];
yield [
'<?php echo 9000;',
'<?php echo
9000;',
['constructs_followed_by_a_single_space' => ['echo']],
];
yield [
'<?php ECHO /* foo */9000;',
'<?php ECHO/* foo */9000;',
['constructs_followed_by_a_single_space' => ['echo']],
];
yield [
'<?php if (true) {} else {}',
'<?php if (true) {} else{}',
['constructs_followed_by_a_single_space' => ['else']],
];
yield [
'<?php if (true) {} else {}',
'<?php if (true) {} else {}',
['constructs_followed_by_a_single_space' => ['else']],
];
yield [
'<?php if (true){} else {}',
'<?php if (true){} else
{}',
['constructs_followed_by_a_single_space' => ['else']],
];
yield [
'<?php if (true) {} else /* foo */{}',
'<?php if (true) {} else/* foo */{}',
['constructs_followed_by_a_single_space' => ['else']],
];
yield [
'<?php if ($a222) {} else {}',
'<?php if ($a222) {}else{}',
['constructs_preceded_by_a_single_space' => ['else']],
];
yield [
"<?php if (\$b333) {}//foo X\nelse {}",
null,
['constructs_preceded_by_a_single_space' => ['else']],
];
yield [
'<?php if (true) {}elseif (false) {}',
'<?php if (true) {}elseif(false) {}',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true) {} elseif (false) {}',
'<?php if (true) {} elseif (false) {}',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true) {} elseif (false) {}',
'<?php if (true) {} elseif
(false) {}',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true) {} elseif /* foo */(false) {}',
'<?php if (true) {} elseif/* foo */(false) {}',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true): elseif (false): endif;',
'<?php if (true): elseif(false): endif;',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true): elseif (false): endif;',
'<?php if (true): elseif (false): endif;',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true): elseif (false): endif;',
'<?php if (true): elseif
(false): endif;',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if (true): elseif /* foo */(false): endif;',
'<?php if (true): elseif/* foo */(false): endif;',
['constructs_followed_by_a_single_space' => ['elseif']],
];
yield [
'<?php if ($y1x) {} elseif (false) {}',
'<?php if ($y1x) {}elseif(false) {}',
['constructs_preceded_by_a_single_space' => ['elseif']],
];
yield [
'<?php if ($y2y) {} elseif (false) {}',
'<?php if ($y2y) {} elseif (false) {}',
['constructs_preceded_by_a_single_space' => ['elseif']],
];
yield [
"<?php if (\$y3t) {}//foo Y\nelseif (false) {}",
null,
['constructs_preceded_by_a_single_space' => ['elseif']],
];
yield [
'<?php class Foo extends \InvalidArgumentException {}',
'<?php class Foo extends \InvalidArgumentException {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php class Foo extends \InvalidArgumentException {}',
'<?php class Foo extends
\InvalidArgumentException {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php class Foo extends /* foo */\InvalidArgumentException {}',
'<?php class Foo extends/* foo */\InvalidArgumentException {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php class Foo extends /* foo */\InvalidArgumentException {}',
'<?php class Foo extends /* foo */\InvalidArgumentException {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends Bar1 {}',
'<?php interface Foo extends Bar1 {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends Bar2 {}',
'<?php interface Foo extends
Bar2 {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends /* foo */Bar3 {}',
'<?php interface Foo extends/* foo */Bar3 {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends /* foo */Bar4 {}',
'<?php interface Foo extends /* foo */Bar4 {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends Bar5, Baz, Qux {}',
'<?php interface Foo extends Bar5, Baz, Qux {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends Bar6, Baz, Qux {}',
'<?php interface Foo extends
Bar6, Baz, Qux {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends /* foo */Bar7, Baz, Qux {}',
'<?php interface Foo extends/* foo */Bar7, Baz, Qux {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
'<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php interface Foo extends
Bar9,
Baz,
Qux
{}',
null,
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php $foo = new class extends \InvalidArgumentException {};',
'<?php $foo = new class extends \InvalidArgumentException {};',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php $foo = new class extends \InvalidArgumentException {};',
'<?php $foo = new class extends
\InvalidArgumentException {};',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
'<?php $foo = new class extends/* foo */\InvalidArgumentException {};',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
'<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
['constructs_followed_by_a_single_space' => ['extends']],
];
yield [
'<?php final class Foo {}',
'<?php final class Foo {}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php final class Foo {}',
'<?php final
class Foo {}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php final /* foo */class Foo {}',
'<?php final/* foo */class Foo {}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php final /* foo */class Foo {}',
'<?php final /* foo */class Foo {}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php
class Foo
{
final function bar() {}
}',
'<?php
class Foo
{
final function bar() {}
}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php
class Foo
{
final function bar() {}
}',
'<?php
class Foo
{
final
function bar() {}
}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php
class Foo
{
final /* foo */function bar() {}
}',
'<?php
class Foo
{
final/* foo */function bar() {}
}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php
class Foo
{
final /* foo */function bar() {}
}',
'<?php
class Foo
{
final /* foo */function bar() {}
}',
['constructs_followed_by_a_single_space' => ['final']],
];
yield [
'<?php try {} finally {}',
'<?php try {} finally{}',
['constructs_followed_by_a_single_space' => ['finally']],
];
yield [
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/NullableTypeDeclarationFixerTest.php | tests/Fixer/LanguageConstruct/NullableTypeDeclarationFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\NullableTypeDeclarationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\NullableTypeDeclarationFixer>
*
* @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\NullableTypeDeclarationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NullableTypeDeclarationFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*
* @requires PHP 8.0
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string}>
*/
public static function provideFixCases(): iterable
{
yield 'scalar with null' => [
"<?php\nfunction foo(?int \$bar): void {}\n",
"<?php\nfunction foo(int|null \$bar): void {}\n",
];
yield 'class with null' => [
"<?php\nfunction bar(?\\stdClass \$crate): int {}\n",
"<?php\nfunction bar(null | \\stdClass \$crate): int {}\n",
];
yield 'static null' => [
'<?php
class Foo
{
public function bar(?array $config = null): ?static {}
}
',
'<?php
class Foo
{
public function bar(null|array $config = null): null|static {}
}
',
];
yield 'multiple parameters' => [
"<?php\nfunction baz(?Foo \$foo, int|string \$value, ?array \$config = null): ?int {}\n",
"<?php\nfunction baz(null|Foo \$foo, int|string \$value, null|array \$config = null): int|null {}\n",
];
yield 'class properties' => [
'<?php
class Dto
{
public ?string $name;
public ?array $parameters;
public ?int $count;
public ?Closure $callable;
}
',
'<?php
class Dto
{
public null|string $name;
public array|null $parameters;
public int|null $count;
public null|Closure $callable;
}
',
];
yield 'skips more than two atomic types' => [
"<?php\nstatic fn (int|null|string \$bar): bool => true;\n",
];
yield 'skips already fixed' => [
"<?php\n\$bar = function (?string \$input): int {};\n",
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->fixer->configure(['syntax' => 'union']);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'scalar with null' => [
"<?php\nfunction foo(null|int \$bar): void {}\n",
"<?php\nfunction foo(?int \$bar): void {}\n",
];
yield 'class with null' => [
"<?php\nfunction bar(null|\\stdClass \$crate): int {}\n",
"<?php\nfunction bar(?\\stdClass \$crate): int {}\n",
];
yield 'static null' => [
'<?php
class Foo
{
public function bar(null|array $config = null): null|static {}
}
',
'<?php
class Foo
{
public function bar(?array $config = null): ?static {}
}
',
];
yield 'multiple parameters' => [
"<?php\nfunction baz(null|Foo \$foo, int|string \$value, null|array \$config = null): null|int {}\n",
"<?php\nfunction baz(?Foo \$foo, int|string \$value, ?array \$config = null): ?int {}\n",
];
yield 'class properties' => [
'<?php
class Dto
{
public null|\Closure $callable;
public null|string $name;
public null|array $parameters;
public null|int $count;
}
',
'<?php
class Dto
{
public ?\Closure $callable;
public ?string $name;
public ?array $parameters;
public ?int $count;
}
',
];
yield 'space after ?' => [
'<?php
class Foo
{
private null|int $x;
public static function from(null|int $x): null|static {}
}
',
'<?php
class Foo
{
private ? int $x;
public static function from(? int $x): ? static {}
}
',
];
yield 'skips already fixed' => [
"<?php\n\$bar = function (null | string \$input): int {};\n",
];
yield 'no space before ?' => [
<<<'PHP'
<?php
class Foo
{
public null|int $a;
protected null|array $b;
private null|string $c;
private static null|bool $d;
}
PHP,
<<<'PHP'
<?php
class Foo
{
public?int $a;
protected?array $b;
private?string $c;
private static?bool $d;
}
PHP,
];
yield 'multiline function declaration' => [
<<<'PHP'
<?php function foo(
null|string $bar,
string $baz
) {}
PHP,
<<<'PHP'
<?php function foo(
?string $bar,
string $baz
) {}
PHP,
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix81Cases(): iterable
{
yield 'readonly property' => [
'<?php
class Qux
{
public readonly ?int $baz;
}
',
'<?php
class Qux
{
public readonly int|null $baz;
}
',
];
yield 'readonly property with union syntax expected' => [
'<?php
class Qux
{
public readonly null|int $baz;
}
',
'<?php
class Qux
{
public readonly ?int $baz;
}
',
['syntax' => 'union'],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix82Cases(): iterable
{
yield 'skips DNF types' => [
'<?php
class Infinite
{
private static (A&B)|null $dft;
}
',
];
yield 'standalone null' => [
'<?php
class Foo
{
public function bar(null|array $config = null): null {}
public function baz(null|ARRAY $config = NULL): NULL {}
}
',
'<?php
class Foo
{
public function bar(?array $config = null): null {}
public function baz(?ARRAY $config = NULL): NULL {}
}
',
['syntax' => 'union'],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix84Cases(): iterable
{
$asymmetricVisibilityWithQuestionMarks = <<<'PHP'
<?php class Foo
{
public public(set) ?int $a;
public protected(set) ?int $b;
public private(set) ?int $c;
public(set) ?int $d;
protected(set) ?int $e;
private(set) ?int $f;
}
PHP;
$asymmetricVisibilityWithNullUnionType = <<<'PHP'
<?php class Foo
{
public public(set) null|int $a;
public protected(set) null|int $b;
public private(set) null|int $c;
public(set) null|int $d;
protected(set) null|int $e;
private(set) null|int $f;
}
PHP;
yield 'asymmetric visibility - fix `null|T` to `?T`' => [
$asymmetricVisibilityWithQuestionMarks,
$asymmetricVisibilityWithNullUnionType,
];
yield 'asymmetric visibility - fix `T|null` to `?T`' => [
$asymmetricVisibilityWithQuestionMarks,
str_replace('null|int', 'int|null', $asymmetricVisibilityWithNullUnionType),
];
yield 'asymmetric visibility - fix `?T` to `null|T`' => [
$asymmetricVisibilityWithNullUnionType,
$asymmetricVisibilityWithQuestionMarks,
['syntax' => 'union'],
];
$abstractAndFinalPropertiesWithQuestionMarks = <<<'PHP'
<?php abstract class Foo
{
abstract public ?int $abstractPublic { set; }
public abstract ?int $publicAbstract { set; }
final protected ?int $finalProtected { set(?int $x) { $this->finalProtected = $x; } }
protected final ?int $protectedFinal { set(?int $i) { $this->protectedFinal = $x; } }
}
PHP;
$abstractAndFinalPropertiesWithNullUnionType = <<<'PHP'
<?php abstract class Foo
{
abstract public null|int $abstractPublic { set; }
public abstract null|int $publicAbstract { set; }
final protected null|int $finalProtected { set(?int $x) { $this->finalProtected = $x; } }
protected final null|int $protectedFinal { set(?int $i) { $this->protectedFinal = $x; } }
}
PHP;
yield 'abstract and final properties - fix `null|T` to `?T`' => [
$abstractAndFinalPropertiesWithQuestionMarks,
$abstractAndFinalPropertiesWithNullUnionType,
];
yield 'abstract and final properties - fix `T|null` to `?T`' => [
$abstractAndFinalPropertiesWithQuestionMarks,
str_replace('null|int', 'int|null', $abstractAndFinalPropertiesWithNullUnionType),
];
yield 'abstract and final properties - fix `?T` to `null|T`' => [
$abstractAndFinalPropertiesWithNullUnionType,
$abstractAndFinalPropertiesWithQuestionMarks,
['syntax' => 'union'],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix85Cases
*
* @requires PHP 8.5
*/
public function testFix85(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix85Cases(): iterable
{
$finalPromotedPropertiesWithQuestionMarks = <<<'PHP'
<?php class Foo
{
public function __construct(
final public ?int $a,
final protected ?int $b,
final private ?int $c,
public final ?int $d,
protected final ?int $e,
private final ?int $f,
) {}
}
PHP;
$finalPromotedPropertiesWithNullUnionType = <<<'PHP'
<?php class Foo
{
public function __construct(
final public null|int $a,
final protected null|int $b,
final private null|int $c,
public final null|int $d,
protected final null|int $e,
private final null|int $f,
) {}
}
PHP;
yield 'final promoted properties - fix `null|T` to `?T`' => [
$finalPromotedPropertiesWithQuestionMarks,
$finalPromotedPropertiesWithNullUnionType,
];
yield 'final promoted properties - fix `T|null` to `?T`' => [
$finalPromotedPropertiesWithQuestionMarks,
str_replace('null|int', 'int|null', $finalPromotedPropertiesWithNullUnionType),
];
yield 'final promoted properties - fix `?T` to `null|T`' => [
$finalPromotedPropertiesWithNullUnionType,
$finalPromotedPropertiesWithQuestionMarks,
['syntax' => 'union'],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/DeclareParenthesesFixerTest.php | tests/Fixer/LanguageConstruct/DeclareParenthesesFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\DeclareParenthesesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\DeclareParenthesesFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DeclareParenthesesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield 'spaces around parentheses' => [
'<?php declare(strict_types = 1);',
'<?php declare ( strict_types = 1 );',
];
yield 'newlines (\n) around parentheses' => [
'<?php declare(strict_types = 1);',
'<?php declare
(
strict_types = 1
);',
];
yield 'newlines (\r\n) around parentheses' => [
'<?php declare(strict_types = 1);',
"<?php declare\r
(\r
strict_types = 1\r
);",
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/ClassKeywordRemoveFixerTest.php | tests/Fixer/LanguageConstruct/ClassKeywordRemoveFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordRemoveFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordRemoveFixer>
*
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ClassKeywordRemoveFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string, 2?: string, 3?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
"<?php
use Foo\\Bar\\Thing;
echo 'Foo\\Bar\\Thing';
",
'<?php
use Foo\Bar\Thing;
echo Thing::class;
',
];
yield [
'<?php
use Foo\Bar;
'."
echo 'Foo\\Bar\\Thing';
",
'<?php
use Foo\Bar;
'.'
echo Bar\Thing::class;
',
];
yield [
"<?php
namespace Foo;
use Foo\\Bar;
echo 'Foo\\Bar\\Baz';
",
'<?php
namespace Foo;
use Foo\Bar;
echo \Foo\Bar\Baz::class;
',
];
yield [
"<?php
use Foo\\Bar\\Thing as Alias;
echo 'Foo\\Bar\\Thing';
",
'<?php
use Foo\Bar\Thing as Alias;
echo Alias::class;
',
];
yield [
"<?php
use Foo\\Bar\\Dummy;
use Foo\\Bar\\Thing as Alias;
echo 'Foo\\Bar\\Dummy';
echo 'Foo\\Bar\\Thing';
",
'<?php
use Foo\Bar\Dummy;
use Foo\Bar\Thing as Alias;
echo Dummy::class;
echo Alias::class;
',
];
yield [
"<?php
echo 'DateTime';
",
'<?php
echo \DateTime::class;
',
];
yield [
"<?php
echo 'Thing';
",
'<?php
echo Thing::class;
',
];
yield [
"<?php
class Foo {
public function amazingFunction() {
echo 'Thing';
}
}
",
'<?php
class Foo {
public function amazingFunction() {
echo Thing::class;
}
}
',
];
yield [
"<?php
namespace A\\B;
use Foo\\Bar;
echo 'Foo\\Bar';
",
'<?php
namespace A\B;
use Foo\Bar;
echo Bar::class;
',
];
yield [
"<?php
namespace A\\B {
class D {
}
}
namespace B\\B {
class D {
}
}
namespace C {
use A\\B\\D;
var_dump('A\\B\\D');
}
namespace C1 {
use B\\B\\D;
var_dump('B\\B\\D');
}
",
'<?php
namespace A\B {
class D {
}
}
namespace B\B {
class D {
}
}
namespace C {
use A\B\D;
var_dump(D::class);
}
namespace C1 {
use B\B\D;
var_dump(D::class);
}
',
];
yield [
'<?php
namespace Foo;
class Bar extends Baz {
public function a() {
return self::class;
}
public function b() {
return static::class;
}
public function c() {
return parent::class;
}
}
',
];
yield [
"<?php
namespace Foo;
var_dump('Foo\\Bar\\Baz');
",
'<?php
namespace Foo;
var_dump(Bar\Baz::class);
',
];
yield [
"<?php
namespace Foo\\Bar;
var_dump('Foo\\Bar\\Baz');
",
'<?php
namespace Foo\Bar;
var_dump(Baz::class);
',
];
yield [
"<?php
use Foo\\Bar\\{ClassA, ClassB, ClassC as C};
use function Foo\\Bar\\{fn_a, fn_b, fn_c};
use const Foo\\Bar\\{ConstA, ConstB, ConstC};
echo 'Foo\\Bar\\ClassB';
echo 'Foo\\Bar\\ClassC';
",
'<?php
use Foo\Bar\{ClassA, ClassB, ClassC as C};
use function Foo\Bar\{fn_a, fn_b, fn_c};
use const Foo\Bar\{ConstA, ConstB, ConstC};
echo ClassB::class;
echo C::class;
',
];
yield [
"<?php
namespace {
var_dump('Foo');
}
namespace A {
use B\\C;
var_dump('B\\C');
}
namespace {
var_dump('Bar\\Baz');
}
namespace B {
use A\\C\\D;
var_dump('A\\C\\D');
}
namespace {
var_dump('Qux\\Quux');
}
",
'<?php
namespace {
var_dump(Foo::class);
}
namespace A {
use B\C;
var_dump(C::class);
}
namespace {
var_dump(Bar\Baz::class);
}
namespace B {
use A\C\D;
var_dump(D::class);
}
namespace {
var_dump(Qux\Quux::class);
}
',
];
}
/**
* @requires PHP <8.0
*/
public function testFixPrePHP8x0(): void
{
$this->doTest(
"<?php echo 'DateTime'
# a
/* b */?>
",
'<?php echo \
DateTime:: # a
/* b */ class?>
',
);
}
/**
* @requires PHP 8.0
*/
public function testNotFixPHP8(): void
{
$this->doTest(
"<?php
echo 'Thing';
echo \$thing::class;
",
'<?php
echo Thing::class;
echo $thing::class;
',
);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/NoUnsetOnPropertyFixerTest.php | tests/Fixer/LanguageConstruct/NoUnsetOnPropertyFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\NoUnsetOnPropertyFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\NoUnsetOnPropertyFixer>
*
* @author Gert de Pagter <BackEndTea@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUnsetOnPropertyFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'It replaces an unset on a property with = null' => [
'<?php $foo->bar = null;',
'<?php unset($foo->bar);',
];
yield 'It replaces an unset on a property with = null II' => [
'<?php $foo->bar = null ;',
'<?php unset($foo->bar );',
];
yield 'It replaces an unset on a static property with = null' => [
'<?php TestClass::$bar = null;',
'<?php unset(TestClass::$bar);',
];
yield 'It does not replace unset on a variable with = null' => [
'<?php $b->a; unset($foo);',
];
yield 'It replaces multiple unsets on variables with = null' => [
'<?php $foo->bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;',
'<?php unset($foo->bar, $bar->foo, $bar->baz, $a->ba);',
];
yield 'It replaces multiple unsets, but not those that arent properties' => [
'<?php $foo->bar = null; $bar->foo = null; unset($bar);',
'<?php unset($foo->bar, $bar->foo, $bar);',
];
yield 'It replaces multiple unsets, but not those that arent properties in multiple places' => [
'<?php unset($foo); $bar->foo = null; unset($bar);',
'<?php unset($foo, $bar->foo, $bar);',
];
yield 'It replaces $this -> and self:: replacements' => [
'<?php $this->bar = null; self::$foo = null; unset($bar);',
'<?php unset($this->bar, self::$foo, $bar);',
];
yield 'It does not replace unsets on arrays' => [
'<?php unset($bar->foo[0]);',
];
yield 'It works in a more complex unset' => [
'<?php unset($bar->foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b);',
'<?php unset($bar->foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b);',
];
yield 'It works with consecutive unsets' => [
'<?php $foo->bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;',
'<?php unset($foo->bar, $foo, $bar, $baz, $this->ab);',
];
yield 'It works when around messy whitespace' => [
'<?php
unset($a); $this->b = null;
$this->a = null; unset($b);
',
'<?php
unset($a, $this->b);
unset($this->a, $b);
',
];
yield 'It works with weirdly placed comments' => [
'<?php unset/*foo*/(/*bar*/$bar->foo[0]); self::$foo = null/*baz*/; /*hello*/\Test\Baz::$fooBar = null/*comment*/; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b);
unset/*foo*/(/*bar*/$bar);',
'<?php unset/*foo*/(/*bar*/$bar->foo[0], self::$foo/*baz*/, /*hello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b);
unset/*foo*/(/*bar*/$bar);',
];
yield 'It does not mess with consecutive unsets' => [
'<?php unset($a, $b, $c);
$this->a = null;',
'<?php unset($a, $b, $c);
unset($this->a);',
];
yield 'It does not replace function call with class constant inside' => [
'<?php unset($foos[array_search(BadFoo::NAME, $foos)]);',
];
yield 'It does not replace function call with class constant and property inside' => [
'<?php unset($this->property[array_search(\Types::TYPE_RANDOM, $this->property)]);',
];
yield 'It does not break complex expressions' => [
'<?php
unset(a()[b()["a"]]);
unset(a()[b()]);
unset(a()["a"]);
unset(c($a)->a);
',
];
yield 'It replaces an unset on a property with = null 1' => [
'<?php $foo->bar = null;',
'<?php unset($foo->bar,);',
];
yield 'It replaces multiple unsets, but not those that arent properties 1' => [
'<?php $foo->bar = null; $bar->foo = null; unset($bar,);',
'<?php unset($foo->bar, $bar->foo, $bar,);',
];
yield 'It replaces an unset on a static property with = null 1' => [
'<?php TestClass::$bar = null;',
'<?php unset(TestClass::$bar,);',
];
yield 'It does not replace unset on a variable with = null 1' => [
'<?php $b->a; unset($foo,);',
];
yield 'It replaces multiple unsets on variables with = null 1' => [
'<?php $foo->bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;',
'<?php unset($foo->bar, $bar->foo, $bar->baz, $a->ba,);',
];
yield 'It replaces multiple unsets, but not those that arent properties in multiple places 1' => [
'<?php unset($foo); $bar->foo = null; unset($bar,);',
'<?php unset($foo, $bar->foo, $bar,);',
];
yield 'It replaces $this -> and self:: replacements 1' => [
'<?php $this->bar = null; self::$foo = null; unset($bar,);',
'<?php unset($this->bar, self::$foo, $bar,);',
];
yield 'It does not replace unsets on arrays 1' => [
'<?php unset($bar->foo[0],);',
];
yield 'It works in a more complex unset 1' => [
'<?php unset($bar->foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b,);',
'<?php unset($bar->foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b,);',
];
yield 'It works with consecutive unsets 1' => [
'<?php $foo->bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;',
'<?php unset($foo->bar, $foo, $bar, $baz, $this->ab,);',
];
yield 'It works when around messy whitespace 1' => [
'<?php
unset($a); $this->b = null;
$this->a = null; unset($b,);
',
'<?php
unset($a, $this->b,);
unset($this->a, $b,);
',
];
yield 'It works with weirdly placed comments 11' => [
'<?php unset/*foo*/(/*bar*/$bar->foo[0]); self::$foo = null/*baz*/; /*hello*/\Test\Baz::$fooBar = null/*comment*/; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b,);
unset/*foo*/(/*bar*/$bar,);',
'<?php unset/*foo*/(/*bar*/$bar->foo[0], self::$foo/*baz*/, /*hello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b,);
unset/*foo*/(/*bar*/$bar,);',
];
yield 'It does not mess with consecutive unsets 1' => [
'<?php unset($a, $b, $c,);
$this->a = null;',
'<?php unset($a, $b, $c,);
unset($this->a,);',
];
yield 'It does not replace function call with class constant inside 1' => [
'<?php unset($foos[array_search(BadFoo::NAME, $foos)],);',
];
yield 'It does not replace function call with class constant and property inside 1' => [
'<?php unset($this->property[array_search(\Types::TYPE_RANDOM, $this->property)],);',
];
yield [
'<?php $foo->bar = null ;',
'<?php unset($foo->bar, );',
];
yield [
'<?php $foo->bar = null ;',
'<?php unset($foo->bar ,);',
];
yield [
'<?php $foo->bar = null ;',
'<?php unset($foo->bar , );',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'It does not replace unsets on arrays with special notation' => [
'<?php unset($bar->foo{0});',
];
yield 'It does not replace unsets on arrays with special notation 1' => [
'<?php unset($bar->foo{0},);',
];
yield 'It does not break curly access expressions' => [
'<?php unset(a(){"a"});',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/ClassKeywordFixerTest.php | tests/Fixer/LanguageConstruct/ClassKeywordFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ClassKeywordFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: ?string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
echo \PhpCsFixer\FixerDefinition\CodeSample::class;
echo \'Foo\Bar\Baz\';
echo \PhpCsFixer\FixerDefinition\CodeSample::class;
echo \PhpCsFixer\FixerDefinition\CodeSample::class;
',
'<?php
echo "PhpCsFixer\FixerDefinition\CodeSample";
echo \'Foo\Bar\Baz\';
echo \'PhpCsFixer\FixerDefinition\CodeSample\';
echo \'\PhpCsFixer\FixerDefinition\CodeSample\';
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/DirConstantFixerTest.php | tests/Fixer/LanguageConstruct/DirConstantFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractFunctionReferenceFixer
* @covers \PhpCsFixer\Fixer\LanguageConstruct\DirConstantFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\DirConstantFixer>
*
* @author Vladimir Reznichenko <kalessil@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DirConstantFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
$multiLinePatternToFix = <<<'FIX'
<?php $x =
dirname
(
__FILE__
)
;
FIX;
$multiLinePatternFixed = <<<'FIXED'
<?php $x =
__DIR__
;
FIXED;
yield ['<?php $x = "dirname";'];
yield ['<?php $x = dirname(__FILE__.".dist");'];
yield ['<?php $x = ClassA::dirname(__FILE__);'];
yield ['<?php $x = ScopeA\dirname(__FILE__);'];
yield ['<?php $x = namespace\dirname(__FILE__);'];
yield ['<?php $x = $object->dirname(__FILE__);'];
yield ['<?php $x = new \dirname(__FILE__);'];
yield ['<?php $x = new dirname(__FILE__);'];
yield ['<?php $x = new ScopeB\dirname(__FILE__);'];
yield ['<?php dirnameSmth(__FILE__);'];
yield ['<?php smth_dirname(__FILE__);'];
yield ['<?php "SELECT ... dirname(__FILE__) ...";'];
yield ['<?php "SELECT ... DIRNAME(__FILE__) ...";'];
yield ['<?php "test" . "dirname" . "in concatenation";'];
yield [
'<?php $x = dirname(__DIR__);',
'<?php $x = dirname(dirname(__FILE__));',
];
yield [
'<?php $x = __DIR__;',
'<?php $x = dirname(__FILE__);',
];
yield [
'<?php $x = /* A */ __DIR__ /* B */;',
'<?php $x = dirname ( /* A */ __FILE__ ) /* B */;',
];
yield [
'<?php $x = __DIR__;',
'<?php $x = \dirname(__FILE__);',
];
yield [
'<?php $x = __DIR__.".dist";',
'<?php $x = dirname(__FILE__).".dist";',
];
yield [
'<?php $x = __DIR__.".dist";',
'<?php $x = \dirname(__FILE__).".dist";',
];
yield [
'<?php $x = /* 0 *//* 1 */ /** x2*//*3*//** 4*/__DIR__/**5*//*xx*/;',
'<?php $x = /* 0 */dirname/* 1 */ /** x2*/(/*3*//** 4*/__FILE__/**5*/)/*xx*/;',
];
yield [
'<?php
interface Test
{
public function dirname($a);
}',
];
yield [
'<?php
interface Test
{
public function &dirname($a);
}',
];
yield [
"<?php echo __DIR__\n?>",
"<?php echo dirname\n(\n__FILE__\n)\n?>",
];
yield [
"<?php echo __DIR__/*1*/\n?>",
"<?php echo dirname\n(\n__FILE__/*1*/\n)\n?>",
];
yield [
$multiLinePatternFixed,
$multiLinePatternToFix,
];
yield [
'<?php $x = __DIR__;',
'<?php $x = \dirname(
__FILE__ '.'
);',
];
yield [
'<?php
$x = dirname(dirname("a".__FILE__));
$x = dirname(dirname(__FILE__."a"));
$x = dirname(dirname("a".__FILE__."a"));
',
];
yield [
'<?php $x = __DIR__.".dist";',
'<?php $x = dirname(__FILE__, ).".dist";',
];
yield [
'<?php $x = __DIR__/* a */ /* b */ .".dist";',
'<?php $x = \dirname(__FILE__/* a */, /* b */) .".dist";',
];
yield [
'<?php $x = __DIR__;',
'<?php $x = \dirname(
__FILE__ , '.'
);',
];
}
/**
* @requires PHP <8.0
*/
public function testFixPre80(): void
{
$this->doTest(
'<?php $x =# A
# A1
# B
# C
__DIR__# D
# E
;# F
',
'<?php $x =# A
\
# A1
dirname# B
(# C
__FILE__# D
)# E
;# F
',
);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/DeclareEqualNormalizeFixerTest.php | tests/Fixer/LanguageConstruct/DeclareEqualNormalizeFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DeclareEqualNormalizeFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input, array $configuration): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'minimal case remove whitespace (default config)' => [
'<?php declare(ticks=1);',
'<?php declare(ticks= 1);',
[],
];
yield 'minimal case remove whitespace (no space config)' => [
'<?php declare(ticks=1);',
'<?php declare(ticks = 1);',
['space' => 'none'],
];
yield 'minimal case add whitespace' => [
'<?php declare(ticks = 1);',
'<?php declare(ticks=1);',
['space' => 'single'],
];
yield 'to much whitespace case add whitespace' => [
'<?php declare(ticks = 1);',
"<?php declare(ticks\n\t = 1);",
['space' => 'single'],
];
yield 'repeating case remove whitespace (default config)' => [
'<?php declare(ticks=1);declare(ticks=1)?>',
'<?php declare(ticks= 1);declare(ticks= 1)?>',
[],
];
yield 'repeating case add whitespace' => [
'<?php declare ( ticks = 1 );declare( ticks = 1) ?>',
'<?php declare ( ticks=1 );declare( ticks =1) ?>',
['space' => 'single'],
];
yield 'minimal case add whitespace comments, single' => [
'<?php declare(ticks#
= #
1#
);',
'<?php declare(ticks#
=#
1#
);',
['space' => 'single'],
];
yield 'minimal case add whitespace comments, none' => [
'<?php declare(ticks#
=#
1#
);',
null,
['space' => 'none'],
];
yield 'declare having multiple directives, single' => [
'<?php declare(strict_types=1, ticks=1);',
'<?php declare(strict_types = 1, ticks = 1);',
[],
];
yield 'declare having multiple directives, none' => [
'<?php declare(strict_types = 1, ticks = 1);',
'<?php declare(strict_types=1, ticks=1);',
['space' => 'single'],
];
}
/**
* @param array<array-key, mixed> $config
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $config, string $expectedMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage(\sprintf('[declare_equal_normalize] Invalid configuration: %s', $expectedMessage));
$this->fixer->configure($config);
}
/**
* @return iterable<int, array{array<array-key, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
[1, 2],
'The options "0", "1" do not exist.',
];
yield [
['space' => 'tab'],
'The option "space" with value "tab" is invalid.',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixerTest.php | tests/Fixer/LanguageConstruct/SingleSpaceAfterConstructFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAfterConstructFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAfterConstructFixer>
*
* @author Andreas Möller <am@localheinz.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\SingleSpaceAfterConstructFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleSpaceAfterConstructFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideConfigureRejectsInvalidControlStatementCases
*
* @param mixed $construct
*/
public function testConfigureRejectsInvalidControlStatement($construct): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->fixer->configure([
'constructs' => [
$construct,
],
]);
}
/**
* @return iterable<string, array{mixed}>
*/
public static function provideConfigureRejectsInvalidControlStatementCases(): iterable
{
yield 'null' => [null];
yield 'false' => [false];
yield 'true' => [true];
yield 'int' => [0];
yield 'float' => [3.14];
yield 'array' => [[]];
yield 'object' => [new \stdClass()];
yield 'unknown' => ['foo'];
}
/**
* @dataProvider provideFixWithAbstractCases
*/
public function testFixWithAbstract(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'abstract',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithAbstractCases(): iterable
{
yield [
'<?php abstract class Foo {}; if($a){}',
'<?php abstract class Foo {}; if($a){}',
];
yield [
'<?php abstract class Foo {};',
'<?php abstract
class Foo {};',
];
yield [
'<?php abstract /* foo */class Foo {};',
'<?php abstract/* foo */class Foo {};',
];
yield [
'<?php abstract /* foo */class Foo {};',
'<?php abstract /* foo */class Foo {};',
];
yield [
'<?php
abstract class Foo
{
abstract function bar();
}',
'<?php
abstract class Foo
{
abstract function bar();
}',
];
yield [
'<?php
abstract class Foo
{
abstract function bar();
}',
'<?php
abstract class Foo
{
abstract
function bar();
}',
];
yield [
'<?php
abstract class Foo
{
abstract /* foo */function bar();
}',
'<?php
abstract class Foo
{
abstract /* foo */function bar();
}',
];
yield [
'<?php
abstract class Foo
{
abstract /* foo */function bar();
}',
'<?php
abstract class Foo
{
abstract/* foo */function bar();
}',
];
}
/**
* @dataProvider provideFixWithBreakCases
*/
public function testFixWithBreak(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'break',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixWithBreakCases(): iterable
{
yield [
'<?php while (true) { break; }',
];
yield [
'<?php while (true) { break /* foo */; }',
'<?php while (true) { break/* foo */; }',
];
yield [
'<?php while (true) { break /* foo */; }',
'<?php while (true) { break /* foo */; }',
];
yield [
'<?php while (true) { break 1; }',
'<?php while (true) { break 1; }',
];
yield [
'<?php while (true) { break 1; }',
'<?php while (true) { break
1; }',
];
yield [
'<?php while (true) { break /* foo */1; }',
'<?php while (true) { break/* foo */1; }',
];
yield [
'<?php while (true) { break /* foo */1; }',
'<?php while (true) { break /* foo */1; }',
];
}
/**
* @dataProvider provideFixWithAsCases
*/
public function testFixWithAs(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'as',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithAsCases(): iterable
{
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as$bar) {}',
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as $bar) {}',
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as
$bar) {}',
];
yield [
'<?php foreach ($foo as /* foo */$bar) {}',
'<?php foreach ($foo as/* foo */$bar) {}',
];
yield [
'<?php foreach ($foo as /* foo */$bar) {}',
'<?php foreach ($foo as /* foo */$bar) {}',
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}',
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as
bar;
}
}',
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as /* foo */bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as/* foo */bar;
}
}',
];
yield [
'<?php
class Foo
{
use Bar {
Bar::baz as /* foo */bar;
}
}',
'<?php
class Foo
{
use Bar {
Bar::baz as /* foo */bar;
}
}',
];
}
/**
* @dataProvider provideFixWithCaseCases
*/
public function testFixWithCase(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'case',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithCaseCases(): iterable
{
yield [
'<?php
switch ($i) {
case $j:
break;
}',
'<?php
switch ($i) {
case$j:
break;
}',
];
yield [
'<?php
switch ($i) {
case 0:
break;
}',
'<?php
switch ($i) {
case 0:
break;
}',
];
yield [
'<?php
switch ($i) {
case 0:
break;
}',
'<?php
switch ($i) {
case
0:
break;
}',
];
yield [
'<?php
switch ($i) {
case /* foo */0:
break;
}',
'<?php
switch ($i) {
case/* foo */0:
break;
}',
];
}
/**
* @dataProvider provideFixWithCatchCases
*/
public function testFixWithCatch(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'catch',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithCatchCases(): iterable
{
yield [
'<?php try {} catch (\Exception $exception) {}',
'<?php try {} catch(\Exception $exception) {}',
];
yield [
'<?php try {} catch (\Exception $exception) {}',
'<?php try {} catch (\Exception $exception) {}',
];
yield [
'<?php try {} catch (\Exception $exception) {}',
'<?php try {} catch
(\Exception $exception) {}',
];
yield [
'<?php try {} catch /* foo */(Exception $exception) {}',
'<?php try {} catch/* foo */(Exception $exception) {}',
];
yield [
'<?php try {} catch /* foo */(Exception $exception) {}',
'<?php try {} catch /* foo */(Exception $exception) {}',
];
}
/**
* @dataProvider provideFixWithClassCases
*/
public function testFixWithClass(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'class',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixWithClassCases(): iterable
{
yield [
'<?php class Foo {}',
'<?php class Foo {}',
];
yield [
'<?php class Foo {}',
'<?php class
Foo {}',
];
yield [
'<?php class /* foo */Foo {}',
'<?php class /* foo */Foo {}',
];
yield [
'<?php class /* foo */Foo {}',
'<?php class/* foo */Foo {}',
];
yield [
'<?php $foo = stdClass::class;',
];
yield [
'<?php $foo = new class {};',
'<?php $foo = new class {};',
];
yield [
'<?php $foo = new class {};',
'<?php $foo = new class{};',
];
yield [
'<?php $foo = new class /* foo */{};',
'<?php $foo = new class/* foo */{};',
];
yield [
'<?php $foo = new class /* foo */{};',
'<?php $foo = new class /* foo */{};',
];
yield [
'<?php $foo = new class(){};',
];
yield [
'<?php return
$a ? new class(){ public function foo() { echo 1; }}
: 1
;',
];
}
/**
* @dataProvider provideFixWithContinueCases
*/
public function testFixWithContinue(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'continue',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixWithContinueCases(): iterable
{
yield [
'<?php while (true) { continue; }',
];
yield [
'<?php while (true) { continue /* foo */; }',
'<?php while (true) { continue/* foo */; }',
];
yield [
'<?php while (true) { continue /* foo */; }',
'<?php while (true) { continue /* foo */; }',
];
yield [
'<?php while (true) { continue 1; }',
'<?php while (true) { continue 1; }',
];
yield [
'<?php while (true) { continue 1; }',
'<?php while (true) { continue
1; }',
];
yield [
'<?php while (true) { continue /* foo*/ 1; }',
'<?php while (true) { continue /* foo*/ 1; }',
];
}
/**
* @dataProvider provideFixWithConstCases
*/
public function testFixWithConst(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'const',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixWithConstCases(): iterable
{
yield [
'<?php class Foo { const FOO = 9000; }',
'<?php class Foo { const FOO = 9000; }',
];
yield [
'<?php class Foo { const FOO = 9000; }',
'<?php class Foo { const
FOO = 9000; }',
];
yield [
'<?php class Foo { const /* foo */FOO = 9000; }',
'<?php class Foo { const/* foo */FOO = 9000; }',
];
yield [
'<?php class Foo { const /* foo */FOO = 9000; }',
'<?php class Foo { const /* foo */FOO = 9000; }',
];
yield ['<?php class Foo {
const
FOO = 9000,
BAR = 10000;
}',
];
yield [
'<?php
const
A = 3,
B = 3
?>',
];
yield [
'<?php
const A = 3 ?>
<?php
[ ,
,
,$z
] = foo() ;',
'<?php
const A = 3 ?>
<?php
[ ,
,
,$z
] = foo() ;',
];
yield [
'<?php
const A
=
1;
',
'<?php
const
A
=
1;
',
];
}
/**
* @dataProvider provideFixWithConstImportCases
*/
public function testFixWithConstImport(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'const_import',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithConstImportCases(): iterable
{
yield [
'<?php use const FOO\BAR;',
'<?php use const FOO\BAR;',
];
yield [
'<?php use const FOO\BAR;',
'<?php use const
FOO\BAR;',
];
yield [
'<?php use const /* foo */FOO\BAR;',
'<?php use const/* foo */FOO\BAR;',
];
yield [
'<?php use const /* foo */FOO\BAR;',
'<?php use const /* foo */FOO\BAR;',
];
}
/**
* @dataProvider provideFixWithCloneCases
*/
public function testFixWithClone(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'clone',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithCloneCases(): iterable
{
yield [
'<?php clone $foo;',
'<?php clone$foo;',
];
yield [
'<?php clone $foo;',
'<?php clone $foo;',
];
yield [
'<?php clone $foo;',
'<?php clone
$foo;',
];
yield [
'<?php clone /* foo */$foo;',
'<?php clone/* foo */$foo;',
];
}
/**
* @dataProvider provideFixWithDoCases
*/
public function testFixWithDo(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'do',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithDoCases(): iterable
{
yield [
'<?php do {} while (true);',
'<?php do{} while (true);',
];
yield [
'<?php DO {} while (true);',
'<?php DO{} while (true);',
];
yield [
'<?php do {} while (true);',
'<?php do {} while (true);',
];
yield [
'<?php do {} while (true);',
'<?php do
{} while (true);',
];
yield [
'<?php do /* foo*/{} while (true);',
'<?php do/* foo*/{} while (true);',
];
yield [
'<?php do /* foo*/{} while (true);',
'<?php do /* foo*/{} while (true);',
];
}
/**
* @dataProvider provideFixWithEchoCases
*/
public function testFixWithEcho(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'echo',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithEchoCases(): iterable
{
yield [
'<?php echo $foo;',
'<?php echo$foo;',
];
yield [
'<?php echo 9000;',
'<?php echo 9000;',
];
yield [
'<?php echo 9000;',
'<?php echo
9000;',
];
yield [
'<?php ECHO /* foo */9000;',
'<?php ECHO/* foo */9000;',
];
}
/**
* @dataProvider provideFixWithElseCases
*/
public function testFixWithElse(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'else',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithElseCases(): iterable
{
yield [
'<?php if (true) {} else {}',
'<?php if (true) {} else{}',
];
yield [
'<?php if (true) {} else {}',
'<?php if (true) {} else {}',
];
yield [
'<?php if (true) {} else {}',
'<?php if (true) {} else
{}',
];
yield [
'<?php if (true) {} else /* foo */{}',
'<?php if (true) {} else/* foo */{}',
];
}
/**
* @dataProvider provideFixWithElseIfCases
*/
public function testFixWithElseIf(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'elseif',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithElseIfCases(): iterable
{
yield [
'<?php if (true) {} elseif (false) {}',
'<?php if (true) {} elseif(false) {}',
];
yield [
'<?php if (true) {} elseif (false) {}',
'<?php if (true) {} elseif (false) {}',
];
yield [
'<?php if (true) {} elseif (false) {}',
'<?php if (true) {} elseif
(false) {}',
];
yield [
'<?php if (true) {} elseif /* foo */(false) {}',
'<?php if (true) {} elseif/* foo */(false) {}',
];
}
/**
* @dataProvider provideFixWithExtendsCases
*/
public function testFixWithExtends(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'extends',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixWithExtendsCases(): iterable
{
yield [
'<?php class Foo extends \InvalidArgumentException {}',
'<?php class Foo extends \InvalidArgumentException {}',
];
yield [
'<?php class Foo extends \InvalidArgumentException {}',
'<?php class Foo extends
\InvalidArgumentException {}',
];
yield [
'<?php class Foo extends /* foo */\InvalidArgumentException {}',
'<?php class Foo extends/* foo */\InvalidArgumentException {}',
];
yield [
'<?php class Foo extends /* foo */\InvalidArgumentException {}',
'<?php class Foo extends /* foo */\InvalidArgumentException {}',
];
yield [
'<?php interface Foo extends Bar1 {}',
'<?php interface Foo extends Bar1 {}',
];
yield [
'<?php interface Foo extends Bar2 {}',
'<?php interface Foo extends
Bar2 {}',
];
yield [
'<?php interface Foo extends /* foo */Bar3 {}',
'<?php interface Foo extends/* foo */Bar3 {}',
];
yield [
'<?php interface Foo extends /* foo */Bar4 {}',
'<?php interface Foo extends /* foo */Bar4 {}',
];
yield [
'<?php interface Foo extends Bar5, Baz, Qux {}',
'<?php interface Foo extends Bar5, Baz, Qux {}',
];
yield [
'<?php interface Foo extends Bar6, Baz, Qux {}',
'<?php interface Foo extends
Bar6, Baz, Qux {}',
];
yield [
'<?php interface Foo extends /* foo */Bar7, Baz, Qux {}',
'<?php interface Foo extends/* foo */Bar7, Baz, Qux {}',
];
yield [
'<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
'<?php interface Foo extends /* foo */Bar8, Baz, Qux {}',
];
yield [
'<?php interface Foo extends
Bar9,
Baz,
Qux
{}',
];
yield [
'<?php $foo = new class extends \InvalidArgumentException {};',
'<?php $foo = new class extends \InvalidArgumentException {};',
];
yield [
'<?php $foo = new class extends \InvalidArgumentException {};',
'<?php $foo = new class extends
\InvalidArgumentException {};',
];
yield [
'<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
'<?php $foo = new class extends/* foo */\InvalidArgumentException {};',
];
yield [
'<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
'<?php $foo = new class extends /* foo */\InvalidArgumentException {};',
];
}
/**
* @dataProvider provideFixWithFinalCases
*/
public function testFixWithFinal(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'final',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithFinalCases(): iterable
{
yield [
'<?php final class Foo {}',
'<?php final class Foo {}',
];
yield [
'<?php final class Foo {}',
'<?php final
class Foo {}',
];
yield [
'<?php final /* foo */class Foo {}',
'<?php final/* foo */class Foo {}',
];
yield [
'<?php final /* foo */class Foo {}',
'<?php final /* foo */class Foo {}',
];
yield [
'<?php
class Foo
{
final function bar() {}
}',
'<?php
class Foo
{
final function bar() {}
}',
];
yield [
'<?php
class Foo
{
final function bar() {}
}',
'<?php
class Foo
{
final
function bar() {}
}',
];
yield [
'<?php
class Foo
{
final /* foo */function bar() {}
}',
'<?php
class Foo
{
final/* foo */function bar() {}
}',
];
yield [
'<?php
class Foo
{
final /* foo */function bar() {}
}',
'<?php
class Foo
{
final /* foo */function bar() {}
}',
];
}
/**
* @dataProvider provideFixWithFinallyCases
*/
public function testFixWithFinally(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'finally',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithFinallyCases(): iterable
{
yield [
'<?php try {} finally {}',
'<?php try {} finally{}',
];
yield [
'<?php try {} finally {}',
'<?php try {} finally {}',
];
yield [
'<?php try {} finally {}',
'<?php try {} finally
{}',
];
yield [
'<?php try {} finally /* foo */{}',
'<?php try {} finally/* foo */{}',
];
yield [
'<?php try {} finally /* foo */{}',
'<?php try {} finally /* foo */{}',
];
}
/**
* @dataProvider provideFixWithForCases
*/
public function testFixWithFor(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'for',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithForCases(): iterable
{
yield [
'<?php for ($i = 0; $i < 3; ++$i) {}',
'<?php for($i = 0; $i < 3; ++$i) {}',
];
yield [
'<?php for ($i = 0; $i < 3; ++$i) {}',
'<?php for ($i = 0; $i < 3; ++$i) {}',
];
yield [
'<?php for ($i = 0; $i < 3; ++$i) {}',
'<?php for
($i = 0; $i < 3; ++$i) {}',
];
yield [
'<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
'<?php for/* foo */($i = 0; $i < 3; ++$i) {}',
];
yield [
'<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
'<?php for /* foo */($i = 0; $i < 3; ++$i) {}',
];
}
/**
* @dataProvider provideFixWithForeachCases
*/
public function testFixWithForeach(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'foreach',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithForeachCases(): iterable
{
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach($foo as $bar) {}',
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach ($foo as $bar) {}',
];
yield [
'<?php foreach ($foo as $bar) {}',
'<?php foreach
($foo as $bar) {}',
];
yield [
'<?php foreach /* foo */($foo as $bar) {}',
'<?php foreach/* foo */($foo as $bar) {}',
];
yield [
'<?php foreach /* foo */($foo as $bar) {}',
'<?php foreach /* foo */($foo as $bar) {}',
];
}
/**
* @dataProvider provideFixWithFunctionCases
*/
public function testFixWithFunction(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'function',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithFunctionCases(): iterable
{
yield [
'<?php function foo() {}',
'<?php function foo() {}',
];
yield [
'<?php function foo() {}',
'<?php function
foo() {}',
];
yield [
'<?php function /* foo */foo() {}',
'<?php function/* foo */foo() {}',
];
yield [
'<?php function /* foo */foo() {}',
'<?php function /* foo */foo() {}',
];
yield [
'<?php
class Foo
{
function bar() {}
}
',
'<?php
class Foo
{
function bar() {}
}
',
];
yield [
'<?php
class Foo
{
function bar() {}
}
',
'<?php
class Foo
{
function
bar() {}
}
',
];
yield [
'<?php
class Foo
{
function /* foo */bar() {}
}
',
'<?php
class Foo
{
function/* foo */bar() {}
}
',
];
yield [
'<?php
class Foo
{
function /* foo */bar() {}
}
',
'<?php
class Foo
{
function /* foo */bar() {}
}
',
];
}
/**
* @dataProvider provideFixWithFunctionImportCases
*/
public function testFixWithFunctionImport(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'function_import',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithFunctionImportCases(): iterable
{
yield [
'<?php use function Foo\bar;',
'<?php use function Foo\bar;',
];
yield [
'<?php use function Foo\bar;',
'<?php use function
Foo\bar;',
];
yield [
'<?php use function /* foo */Foo\bar;',
'<?php use function/* foo */Foo\bar;',
];
yield [
'<?php use function /* foo */Foo\bar;',
'<?php use function /* foo */Foo\bar;',
];
}
/**
* @dataProvider provideFixWithGlobalCases
*/
public function testFixWithGlobal(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'global',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithGlobalCases(): iterable
{
yield [
'<?php function foo() { global $bar; }',
'<?php function foo() { global$bar; }',
];
yield [
'<?php function foo() { global $bar; }',
'<?php function foo() { global $bar; }',
];
yield [
'<?php function foo() { global $bar; }',
'<?php function foo() { global
$bar; }',
];
yield [
'<?php function foo() { global /* foo */$bar; }',
'<?php function foo() { global/* foo */$bar; }',
];
yield [
'<?php function foo() { global /* foo */$bar; }',
'<?php function foo() { global /* foo */$bar; }',
];
}
/**
* @dataProvider provideFixWithGotoCases
*/
public function testFixWithGoto(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'goto',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithGotoCases(): iterable
{
yield [
'<?php goto foo; foo: echo "Bar";',
'<?php goto foo; foo: echo "Bar";',
];
yield [
'<?php goto foo; foo: echo "Bar";',
'<?php goto
foo; foo: echo "Bar";',
];
yield [
'<?php goto /* foo */foo; foo: echo "Bar";',
'<?php goto/* foo */foo; foo: echo "Bar";',
];
}
/**
* @dataProvider provideFixWithIfCases
*/
public function testFixWithIf(string $expected, ?string $input = null): void
{
$this->fixer->configure([
'constructs' => [
'if',
],
]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixWithIfCases(): iterable
{
yield [
'<?php if ($foo === $bar) {}',
'<?php if($foo === $bar) {}',
];
yield [
'<?php if ($foo === $bar) {}',
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/ExplicitIndirectVariableFixerTest.php | tests/Fixer/LanguageConstruct/ExplicitIndirectVariableFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\ExplicitIndirectVariableFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\ExplicitIndirectVariableFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ExplicitIndirectVariableFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string}>
*/
public static function provideFixCases(): iterable
{
yield 'variable variable function call' => [
'<?php echo ${$foo}($bar);',
'<?php echo $$foo($bar);',
];
yield 'variable variable array fetch' => [
'<?php echo ${$foo}[\'bar\'][\'baz\'];',
'<?php echo $$foo[\'bar\'][\'baz\'];',
];
yield 'dynamic property access' => [
'<?php echo $foo->{$bar}[\'baz\'];',
'<?php echo $foo->$bar[\'baz\'];',
];
yield 'dynamic property access with method call' => [
'<?php echo $foo->{$bar}[\'baz\']();',
'<?php echo $foo->$bar[\'baz\']();',
];
yield 'variable variable with comments between dollar signs' => [
'<?php echo $
/* C1 */
// C2
{$foo}
// C3
;',
'<?php echo $
/* C1 */
// C2
$foo
// C3
;',
];
yield 'dynamic static property access using variable variable' => [
'<?php echo Foo::${$bar};',
'<?php echo Foo::$$bar;',
];
yield 'dynamic static property access using variable variable with method call' => [
'<?php echo Foo::${$bar}->baz();',
'<?php echo Foo::$$bar->baz();',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'dynamic property fetch with nullsafe operator' => [
'<?php echo $foo?->{$bar}["baz"];',
'<?php echo $foo?->$bar["baz"];',
];
yield 'dynamic property fetch with nullsafe operator and method call' => [
'<?php echo $foo?->{$bar}["baz"]();',
'<?php echo $foo?->$bar["baz"]();',
];
}
/**
* @dataProvider provideFix83Cases
*
* @requires PHP 8.3
*/
public function testFix83(string $expected, ?string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string}>
*/
public static function provideFix83Cases(): iterable
{
yield 'dynamic class const fetch with variable variable' => [
'<?php echo Foo::{${$bar}};',
'<?php echo Foo::{$$bar};',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/FunctionToConstantFixerTest.php | tests/Fixer/LanguageConstruct/FunctionToConstantFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class FunctionToConstantFixerTest 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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'Minimal case, alternative casing, alternative statement end.' => [
'<?php echo PHP_VERSION?>',
'<?php echo PHPversion()?>',
];
yield 'With embedded comment.' => [
'<?php echo PHP_VERSION/**/?>',
'<?php echo phpversion(/**/)?>',
];
yield 'With white space.' => [
'<?php echo PHP_VERSION ;',
'<?php echo phpversion ( ) ;',
];
yield 'With multi line whitespace.' => [
'<?php echo
PHP_VERSION
'.'
'.'
;',
'<?php echo
phpversion
(
)
;',
];
yield 'Global namespaced.' => [
'<?php echo \PHP_VERSION;',
'<?php echo \phpversion();',
];
yield 'Wrong number of arguments.' => [
'<?php phpversion($a);',
];
yield 'Wrong namespace.' => [
'<?php A\B\phpversion();',
];
yield 'Class creating.' => [
'<?php new phpversion();',
];
yield 'Class static method call.' => [
'<?php A::phpversion();',
];
yield 'Class method call.' => [
'<?php $a->phpversion();',
];
yield 'Overridden function.' => [
'<?php if (!function_exists("phpversion")){function phpversion(){}}?>',
];
yield 'phpversion only' => [
'<?php echo PHP_VERSION; echo php_sapi_name(); echo pi();',
'<?php echo phpversion(); echo php_sapi_name(); echo pi();',
['functions' => ['phpversion']],
];
yield 'php_sapi_name only' => [
'<?php echo phpversion(); echo PHP_SAPI; echo pi();',
'<?php echo phpversion(); echo php_sapi_name(); echo pi();',
['functions' => ['php_sapi_name']],
];
yield 'php_sapi_name in conditional' => [
'<?php if ("cli" === PHP_SAPI && $a){ echo 123;}',
'<?php if ("cli" === php_sapi_name() && $a){ echo 123;}',
['functions' => ['php_sapi_name']],
];
yield 'pi only' => [
'<?php echo phpversion(); echo php_sapi_name(); echo M_PI;',
'<?php echo phpversion(); echo php_sapi_name(); echo pi();',
['functions' => ['pi']],
];
yield 'multi line pi' => [
'<?php
$a =
$b
|| $c < M_PI
;',
'<?php
$a =
$b
|| $c < pi()
;',
['functions' => ['pi']],
];
yield 'phpversion and pi' => [
'<?php echo PHP_VERSION; echo php_sapi_name(); echo M_PI;',
'<?php echo phpversion(); echo php_sapi_name(); echo M_PI;',
['functions' => ['pi', 'phpversion']],
];
yield 'diff argument count than native allows' => [
'<?php
echo phpversion(1);
echo php_sapi_name(1,2);
echo pi(1);
',
];
yield 'get_class => T_CLASS' => [
'<?php
class A
{
public function echoClassName($notMe)
{
echo get_class($notMe);
echo self::class/** 1 *//* 2 */;
echo self::class;
}
}
class B
{
use A;
}
',
'<?php
class A
{
public function echoClassName($notMe)
{
echo get_class($notMe);
echo get_class(/** 1 *//* 2 */);
echo GET_Class();
}
}
class B
{
use A;
}
',
];
yield 'get_class with leading backslash' => [
'<?php self::class;',
'<?php \get_class();',
];
yield [
'<?php class A { function B(){ echo static::class; }}',
'<?php class A { function B(){ echo get_called_class(); }}',
['functions' => ['get_called_class']],
];
yield [
'<?php class A { function B(){
echo#.
#0
static::class#1
#2
#3
#4
#5
#6
;#7
}}
',
'<?php class A { function B(){
echo#.
#0
get_called_class#1
#2
(#3
#4
)#5
#6
;#7
}}
',
['functions' => ['get_called_class']],
];
yield 'get_called_class with leading backslash' => [
'<?php class A { function B(){echo static::class; }}',
'<?php class A { function B(){echo \get_called_class(); }}',
['functions' => ['get_called_class']],
];
yield 'get_called_class overridden' => [
'<?php echo get_called_class(1);',
null,
['functions' => ['get_called_class']],
];
yield [
'<?php class Foo{ public function Bar(){ echo static::class ; }}',
'<?php class Foo{ public function Bar(){ echo get_class( $This ); }}',
['functions' => ['get_class_this']],
];
yield [
'<?php class Foo{ public function Bar(){ echo static::class; get_class(1, 2); get_class($a); get_class($a, $b);}}',
'<?php class Foo{ public function Bar(){ echo get_class($this); get_class(1, 2); get_class($a); get_class($a, $b);}}',
['functions' => ['get_class_this']],
];
yield [
'<?php class Foo{ public function Bar(){ echo static::class /* 0 */ /* 1 */ ;}}',
'<?php class Foo{ public function Bar(){ echo \get_class( /* 0 */ $this /* 1 */ );}}',
['functions' => ['get_class_this']],
];
yield [
'<?php class Foo{ public function Bar(){ echo static::class; echo self::class; }}',
'<?php class Foo{ public function Bar(){ echo \get_class((($this))); echo get_class(); }}',
['functions' => ['get_class_this', 'get_class']],
];
yield [
'<?php
class Foo{ public function Bar(){ echo $reflection = new \ReflectionClass(get_class($this->extension)); }}
class Foo{ public function Bar(){ echo $reflection = new \ReflectionClass(get_class($this() )); }}
',
null,
['functions' => ['get_class_this']],
];
yield [
"<?php namespace Foo;\nfunction &PHPversion(){}",
];
}
/**
* @param array<array-key, mixed> $config
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $config, string $expectedExceptionMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
$this->fixer->configure($config);
}
/**
* @return iterable<int, array{array<array-key, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['functions' => ['a']],
'[function_to_constant] Invalid configuration: The option "functions" with value array is invalid.',
];
yield [
['functions' => [false => 1]],
'[function_to_constant] Invalid configuration: The option "functions" with value array is expected to be of type "string[]", but one of the elements is of type "int".',
];
yield [
['functions' => ['abc' => true]],
'[function_to_constant] Invalid configuration: The option "functions" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
];
yield [
['pi123'],
'[function_to_constant] Invalid configuration: The option "0" does not exist. Defined options are: "functions".',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'first callable class' => [
'<?php $a = get_class(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/IsNullFixerTest.php | tests/Fixer/LanguageConstruct/IsNullFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer>
*
* @author Vladimir Reznichenko <kalessil@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class IsNullFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
$multiLinePatternToFix = <<<'FIX'
<?php $x =
is_null
(
json_decode
(
$x
)
)
;
FIX;
$multiLinePatternFixed = <<<'FIXED'
<?php $x =
null === json_decode
(
$x
)
;
FIXED;
yield ['<?php $x = "is_null";'];
yield ['<?php $x = ClassA::is_null(json_decode($x));'];
yield ['<?php $x = ScopeA\is_null(json_decode($x));'];
yield ['<?php $x = namespace\is_null(json_decode($x));'];
yield ['<?php $x = $object->is_null(json_decode($x));'];
yield ['<?php $x = new \is_null(json_decode($x));'];
yield ['<?php $x = new is_null(json_decode($x));'];
yield ['<?php $x = new ScopeB\is_null(json_decode($x));'];
yield ['<?php is_nullSmth(json_decode($x));'];
yield ['<?php smth_is_null(json_decode($x));'];
yield ['<?php namespace Foo; function &is_null($x) { return null === $x; }'];
yield ['<?php "SELECT ... is_null(json_decode($x)) ...";'];
yield ['<?php "test" . "is_null" . "in concatenation";'];
yield ['<?php $x = null === json_decode($x);', '<?php $x = is_null(json_decode($x));'];
yield ['<?php $x = null !== json_decode($x);', '<?php $x = !is_null(json_decode($x));'];
yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null(json_decode($x));'];
yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! is_null( json_decode($x) );'];
yield ['<?php $x = null === json_decode($x);', '<?php $x = \is_null(json_decode($x));'];
yield ['<?php $x = null !== json_decode($x);', '<?php $x = !\is_null(json_decode($x));'];
yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! \is_null(json_decode($x));'];
yield ['<?php $x = null !== json_decode($x);', '<?php $x = ! \is_null( json_decode($x) );'];
yield ['<?php $x = null === json_decode($x).".dist";', '<?php $x = is_null(json_decode($x)).".dist";'];
yield ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !is_null(json_decode($x)).".dist";'];
yield ['<?php $x = null === json_decode($x).".dist";', '<?php $x = \is_null(json_decode($x)).".dist";'];
yield ['<?php $x = null !== json_decode($x).".dist";', '<?php $x = !\is_null(json_decode($x)).".dist";'];
yield [$multiLinePatternFixed, $multiLinePatternToFix];
yield [
'<?php $x = /**/null === /**/ /** x*//**//** */json_decode($x)/***//*xx*/;',
'<?php $x = /**/is_null/**/ /** x*/(/**//** */json_decode($x)/***/)/*xx*/;',
];
yield [
'<?php $x = null === (null === $x ? z(null === $y) : z(null === $z));',
'<?php $x = is_null(is_null($x) ? z(is_null($y)) : z(is_null($z)));',
];
yield [
'<?php $x = null === ($x = array());',
'<?php $x = is_null($x = array());',
];
yield [
'<?php null === a(null === a(null === a(null === b())));',
'<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b())))))));',
];
yield [
'<?php $d= null === ($a)/*=?*/?>',
"<?php \$d= is_null(\n(\$a)/*=?*/\n)?>",
];
yield [
'<?php is_null()?>',
];
// edge cases: is_null wrapped into a binary operations
yield [
'<?php $result = (false === (null === $a)); ?>',
'<?php $result = (false === is_null($a)); ?>',
];
yield [
'<?php $result = ((null === $a) === false); ?>',
'<?php $result = (is_null($a) === false); ?>',
];
yield [
'<?php $result = (false !== (null === $a)); ?>',
'<?php $result = (false !== is_null($a)); ?>',
];
yield [
'<?php $result = ((null === $a) !== false); ?>',
'<?php $result = (is_null($a) !== false); ?>',
];
yield [
'<?php $result = (false == (null === $a)); ?>',
'<?php $result = (false == is_null($a)); ?>',
];
yield [
'<?php $result = ((null === $a) == false); ?>',
'<?php $result = (is_null($a) == false); ?>',
];
yield [
'<?php $result = (false != (null === $a)); ?>',
'<?php $result = (false != is_null($a)); ?>',
];
yield [
'<?php $result = ((null === $a) != false); ?>',
'<?php $result = (is_null($a) != false); ?>',
];
yield [
'<?php $result = (false <> (null === $a)); ?>',
'<?php $result = (false <> is_null($a)); ?>',
];
yield [
'<?php $result = ((null === $a) <> false); ?>',
'<?php $result = (is_null($a) <> false); ?>',
];
yield [
'<?php if (null === $x) echo "foo"; ?>',
'<?php if (is_null($x)) echo "foo"; ?>',
];
// check with logical operator
yield [
'<?php if (null === $x && $y) echo "foo"; ?>',
'<?php if (is_null($x) && $y) echo "foo"; ?>',
];
yield [
'<?php if (null === $x || $y) echo "foo"; ?>',
'<?php if (is_null($x) || $y) echo "foo"; ?>',
];
yield [
'<?php if (null === $x xor $y) echo "foo"; ?>',
'<?php if (is_null($x) xor $y) echo "foo"; ?>',
];
yield [
'<?php if (null === $x and $y) echo "foo"; ?>',
'<?php if (is_null($x) and $y) echo "foo"; ?>',
];
yield [
'<?php if (null === $x or $y) echo "foo"; ?>',
'<?php if (is_null($x) or $y) echo "foo"; ?>',
];
yield [
'<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
'<?php if ((is_null($u) or $v) and ($w || is_null($x)) xor (!is_null($y) and $z)) echo "foo"; ?>',
];
// edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
yield [
'<?php null === ($a ? $x : $y);',
'<?php is_null($a ? $x : $y);',
];
yield [
'<?php $a === (null === $x);',
'<?php $a === is_null($x);',
];
yield [
'<?php $a === (null === ($a ? $x : $y));',
'<?php $a === is_null($a ? $x : $y);',
];
yield [
'<?php null !== ($a ?? null);',
'<?php !is_null($a ?? null);',
];
yield [
'<?php null === $x;',
'<?php is_null($x, );',
];
yield [
'<?php null === $x;',
'<?php is_null( $x , );',
];
yield [
'<?php null === a(null === a(null === a(null === b(), ), ), );',
'<?php \is_null(a(\is_null(a(\is_null(a(\is_null(b(), ), ), ), ), ), ), );',
];
yield [
'<?php if ((null === $u or $v) and ($w || null === $x) xor (null !== $y and $z)) echo "foo"; ?>',
'<?php if ((is_null($u, ) or $v) and ($w || is_null($x, )) xor (!is_null($y, ) and $z)) echo "foo"; ?>',
];
// edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses
yield [
'<?php null === ($a ? $x : $y );',
'<?php is_null($a ? $x : $y, );',
];
yield [
'<?php $a === (null === $x);',
'<?php $a === is_null($x, );',
];
yield [
'<?php $a === (null === ($a ? $x : $y ));',
'<?php $a === is_null($a ? $x : $y, );',
];
yield [
'<?php $a === (int) (null === $x) + (int) (null !== $y);',
'<?php $a === (int) is_null($x) + (int) !is_null($y);',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'first-class callable' => [
'<?php array_filter([], is_null(...));',
];
yield 'first-class callable with leading slash' => [
'<?php array_filter([], \is_null(...));',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/LanguageConstruct/CombineConsecutiveUnsetsFixerTest.php | tests/Fixer/LanguageConstruct/CombineConsecutiveUnsetsFixerTest.php | <?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\LanguageConstruct;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class CombineConsecutiveUnsetsFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php //1
unset($foo/*;*/, /*;*/$bar, $c , $foobar , $foobar2);
//test
/* more comment test*/
'.'
',
'<?php //1
unset($foo/*;*/);
unset(/*;*/$bar, $c ); //test
unset($foobar ); /* more comment test*/
unset( $foobar2);
',
];
yield [
'<?php unset($d , $e);/*unset( $d2);unset($e );;*/ ',
'<?php unset($d );/*unset( $d2);unset($e );;*/ uNseT($e);',
];
yield [
'<?php UNSET($a, $b,$c/**/); ',
'<?php UNSET($a); unset($b,$c/**/);',
];
yield [
'<?php
$config = array();
if ($config) {
}
unset($config[\'autoescape_service\'], $config[\'autoescape_service_method\']);
',
];
yield [
'<?php //2
unset($foo, $bar, $foobar, $foobar2, $foobar3);/*1*/
/*2*/
//3
/*4*/
/*5*/ '.'
',
'<?php //2
unset($foo);/*1*/
unset($bar);/*2*/
unset($foobar);//3
unset($foobar2);/*4*/
/*5*/ unset($foobar3);
',
];
yield [
'<?php
unset($foo3, $bar, $test,$test1);
/* c1 */
'.'
'.'
// c2
'.'
',
'<?php
unset($foo3);
/* c1 */
unset($bar);
'.'
// c2
unset($test,$test1);
',
];
yield [
'<?php unset($x, $b , $d); /**/ ?> b',
'<?php unset($x); /**/ unset ($b , $d) ?> b',
];
yield [
'<?php unset($x) ?>',
];
yield [
'<?php unset($y, $u); ?>',
'<?php unset($y);unset($u) ?>',
];
yield [
'<?php
unset($a[0], $a[\'a\'], $a["b"], $a->b, $a->b->c, $a->b[0]->c[\'a\']);
'.'
'.'
'.'
'.'
'.'
',
'<?php
unset($a[0]);
unset($a[\'a\']);
unset($a["b"]);
unset($a->b);
unset($a->b->c);
unset($a->b[0]->c[\'a\']);
',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php (unset)$f;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/RandomApiMigrationFixerTest.php | tests/Fixer/Alias/RandomApiMigrationFixerTest.php | <?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\Alias;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractFunctionReferenceFixer
* @covers \PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer>
*
* @author Vladimir Reznichenko <kalessil@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class RandomApiMigrationFixerTest extends AbstractFixerTestCase
{
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(string $message, array $configuration): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($message);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{string, array<string, mixed>}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'not supported function' => [
'[random_api_migration] Invalid configuration: Function "is_null" is not handled by the fixer.',
['replacements' => ['is_null' => 'random_int']],
];
yield 'wrong replacement' => [
'[random_api_migration] Invalid configuration: The option "replacements" with value array is expected to be of type "string[]", but one of the elements is of type "null".',
['replacements' => ['rand' => null]],
];
}
public function testConfigure(): void
{
$this->fixer->configure(['replacements' => ['rand' => 'random_int']]);
self::assertSame(
['replacements' => [
'rand' => 'random_int',
]],
\Closure::bind(static fn (RandomApiMigrationFixer $fixer): array => $fixer->configuration, null, RandomApiMigrationFixer::class)($this->fixer),
);
}
/**
* @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<int, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php random_int(0, getrandmax());',
'<?php rand();',
['replacements' => ['rand' => 'random_int']],
];
yield [
'<?php random_int#1
#2
(0, getrandmax()#3
#4
)#5
;',
'<?php rand#1
#2
(#3
#4
)#5
;',
['replacements' => ['rand' => 'random_int']],
];
yield ['<?php $smth->srand($a);'];
yield ['<?php srandSmth($a);'];
yield ['<?php smth_srand($a);'];
yield ['<?php new srand($a);'];
yield ['<?php new Smth\srand($a);'];
yield ['<?php Smth\srand($a);'];
yield ['<?php namespace\srand($a);'];
yield ['<?php Smth::srand($a);'];
yield ['<?php new srand\smth($a);'];
yield ['<?php srand::smth($a);'];
yield ['<?php srand\smth($a);'];
yield ['<?php "SELECT ... srand(\$a) ...";'];
yield ['<?php "SELECT ... SRAND($a) ...";'];
yield ["<?php 'test'.'srand' . 'in concatenation';"];
yield ['<?php "test" . "srand"."in concatenation";'];
yield [
'<?php
class SrandClass
{
const srand = 1;
public function srand($srand)
{
if (!defined("srand") || $srand instanceof srand) {
echo srand;
}
}
}
class srand extends SrandClass{
const srand = "srand";
}
',
];
yield ['<?php mt_srand($a);', '<?php srand($a);'];
yield ['<?php \mt_srand($a);', '<?php \srand($a);'];
yield ['<?php $a = &mt_srand($a);', '<?php $a = &srand($a);'];
yield ['<?php $a = &\mt_srand($a);', '<?php $a = &\srand($a);'];
yield ['<?php /* foo */ mt_srand /** bar */ ($a);', '<?php /* foo */ srand /** bar */ ($a);'];
yield ['<?php a(mt_getrandmax ());', '<?php a(getrandmax ());'];
yield ['<?php a(mt_rand());', '<?php a(rand());'];
yield ['<?php a(mt_srand());', '<?php a(srand());'];
yield ['<?php a(\mt_srand());', '<?php a(\srand());'];
yield [
'<?php rand(rand($a));',
null,
['replacements' => ['rand' => 'random_int']],
];
yield [
'<?php random_int($d, random_int($a,$b));',
'<?php rand($d, rand($a,$b));',
['replacements' => ['rand' => 'random_int']],
];
yield [
'<?php random_int($a, \Other\Scope\mt_rand($a));',
'<?php rand($a, \Other\Scope\mt_rand($a));',
['replacements' => ['rand' => 'random_int']],
];
yield [
'<?php $a = random_int(1,2) + random_int(3,4);',
'<?php $a = rand(1,2) + mt_rand(3,4);',
['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
];
yield [
'<?php
interface Test
{
public function getrandmax();
public function &rand();
}',
null,
['replacements' => ['rand' => 'random_int']],
];
yield [
'<?php rand($d, rand($a,$b));',
null,
['replacements' => ['rand' => 'rand']],
];
yield [
'<?php $a = random_int(1,2,) + random_int(3,4,);',
'<?php $a = rand(1,2,) + mt_rand(3,4,);',
['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
];
yield [
'<?php mt_srand($a,);',
'<?php srand($a,);',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'simple 8.1' => [
'<?php $f = srand(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/EregToPregFixerTest.php | tests/Fixer/Alias/EregToPregFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\EregToPregFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\EregToPregFixer>
*
* @author Matteo Beccati <matteo@beccati.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class EregToPregFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php $x = 1;'];
yield ['<?php $x = "ereg";'];
yield ['<?php $x = ereg("[A-Z]"."foo", $m);'];
yield ['<?php $x = ereg("^*broken", $m);'];
yield ['<?php $x = Foo::split("[A-Z]", $m);'];
yield ['<?php $x = $foo->split("[A-Z]", $m);'];
yield ['<?php $x = Foo\split("[A-Z]", $m);'];
yield [
'<?php $x = preg_match(\'/[A-Z]/D\');',
'<?php $x = ereg(\'[A-Z]\');',
];
yield [
'<?php $x = preg_match(\'/[A-Z]/D\', $m);',
'<?php $x = ereg(\'[A-Z]\', $m);',
];
yield [
'<?php $x = preg_match("/[A-Z]/D", $m);',
'<?php $x = ereg("[A-Z]", $m);',
];
yield [
'<?php $x = preg_match("/[A-Z]/Di", $m);',
'<?php $x = eregi("[A-Z]", $m);',
];
yield [
'<?php $x = preg_match("#/[AZ]#D", $m);',
'<?php $x = ereg("/[AZ]", $m);',
];
yield [
'<?php $x = preg_match("#[AZ]/#D", $m);',
'<?php $x = ereg("[AZ]/", $m);',
];
yield [
'<?php $x = preg_match("!#[A]/!D", $m);',
'<?php $x = ereg("#[A]/", $m);',
];
yield [
'<?php $x = preg_match("!##[A\!]//!D", $m);',
'<?php $x = ereg("##[A!]//", $m);',
];
yield [
'<?php $x = preg_match("/##[A!!]\/\//D", $m);',
'<?php $x = ereg("##[A!!]//", $m);',
];
yield [
'<?php $x = preg_match("#\#\#[A!!]///#D", $m);',
'<?php $x = ereg("##[A!!]///", $m);',
];
yield [
'<?php $x = preg_replace("/[A-Z]/D", "", $m);',
'<?php $x = ereg_replace("[A-Z]", "", $m);',
];
yield [
'<?php $x = preg_replace("/[A-Z]/Di", "", $m);',
'<?php $x = eregi_replace("[A-Z]", "", $m);',
];
yield [
'<?php $x = preg_split("/[A-Z]/D", $m);',
'<?php $x = split("[A-Z]", $m);',
];
yield [
'<?php $x = preg_split("/[A-Z]/Di", $m);',
'<?php $x = spliti("[A-Z]", $m);',
];
yield 'binary lowercase' => [
'<?php $x = preg_split(b"/[A-Z]/Di", $m);',
'<?php $x = spliti(b"[A-Z]", $m);',
];
yield 'binary uppercase' => [
'<?php $x = preg_split(B"/[A-Z]/Di", $m);',
'<?php $x = spliti(B"[A-Z]", $m);',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php $x = spliti(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/PowToExponentiationFixerTest.php | tests/Fixer/Alias/PowToExponentiationFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractFunctionReferenceFixer
* @covers \PhpCsFixer\Fixer\Alias\PowToExponentiationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\PowToExponentiationFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PowToExponentiationFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php 1**2;',
'<?php pow(1,2);',
];
yield [
'<?php 1**2?>',
'<?php pow(1,2)?>',
];
yield [
'<?php 1.2**2.3;',
'<?php pow(1.2,2.3);',
];
yield [
'<?php echo (-2)** 3;',
'<?php echo pow(-2, 3);',
];
yield [
'<?php echo (-2)**( -3);',
'<?php echo pow(-2, -3);',
];
yield [
'<?php echo (-2)**( 1-3);',
'<?php echo pow(-2, 1-3);',
];
yield [
'<?php echo (-2)**( -1-3);',
'<?php echo pow(-2, -1-3);',
];
yield [
'<?php $a = 3** +2;',
'<?php $a = pow(3, +2);',
];
yield [
'<?php $a--**++$b;',
'<?php pow($a--,++$b);',
];
yield [
'<?php 1//
#
**2/**/ /** */;',
'<?php pow(1//
#
,2/**/ /** */);',
];
yield [
'<?php /**/a(3/**/,4)**$pow;//pow(1,2);',
'<?php pow/**/(a(3/**/,4),$pow);//pow(1,2);',
];
yield [
'<?php \a\pow(5,6);7**8?>',
'<?php \a\pow(5,6);pow(7,8)?>',
];
yield [
'<?php (9**10)**(11**12);',
'<?php pow(pow(9,10),pow(11,12));',
];
yield [
'<?php (1 + 2)**( 3 * 4);',
'<?php pow(1 + 2, 3 * 4);',
];
yield [
'<?php ($b = 4)** 3;',
'<?php pow($b = 4, 3);',
];
yield [
'<?php 13**14;',
'<?php \pow(13,14);',
];
yield [
'<?php $a = 15 + (16** 17)** 18;',
'<?php $a = 15 + \pow(\pow(16, 17), 18);',
];
yield [
'<?php $a = $b** $c($d + 1);',
'<?php $a = pow($b, $c($d + 1));',
];
yield [
'<?php $a = ($a+$b)** ($c-$d);',
'<?php $a = pow(($a+$b), ($c-$d));',
];
yield [
"<?php \$a = 2**( '1'.'2'). 2;",
"<?php \$a = pow(2, '1'.'2'). 2;",
];
yield [
'<?php A::B** 2;\A\B\C::B** 2;',
'<?php pow(A::B, 2);pow(\A\B\C::B, 2);',
];
yield [
'<?php $obj->{$bar}** $obj->{$foo};',
'<?php pow($obj->{$bar}, $obj->{$foo});',
];
yield [
'<?php echo ${$bar}** ${$foo};',
'<?php echo pow(${$bar}, ${$foo});',
];
yield [
'<?php echo $a[2^3+1]->test(1,2)** $b[2+$c];',
'<?php echo pow($a[2^3+1]->test(1,2), $b[2+$c]);',
];
yield [
'<?php (int)"2"**(float)"3.0";',
'<?php pow((int)"2",(float)"3.0");',
];
yield [
'<?php namespace\Foo::BAR** 2;',
'<?php pow(namespace\Foo::BAR, 2);',
];
yield [
'<?php (-1)**( (-2)**( (-3)**( (-4)**( (-5)**( (-6)**( (-7)**( (-8)**( (-9)** 3))))))));',
'<?php pow(-1, pow(-2, pow(-3, pow(-4, pow(-5, pow(-6, pow(-7, pow(-8, pow(-9, 3)))))))));',
];
yield [
'<?php
$z = 1**2;
$a = 1**( 2**( 3**( 4**( 5**( 6**( 7**( 8**( 9** 3))))))));
$b = 1**( 2**( 3**( 4**( 5**( 6**( 7**( 8**( 9** 3))))))));
$d = 1**2;
',
'<?php
$z = pow(1,2);
$a = \pow(1, \poW(2, \pOw(3, \pOW(4, \Pow(5, \PoW(6, \POw(7, \POW(8, \pow(9, 3)))))))));
$b = \pow(1, \pow(2, \pow(3, \pow(4, \pow(5, \pow(6, \pow(7, \pow(8, \pow(9, 3)))))))));
$d = pow(1,2);
',
];
yield [
'<?php $b = 3** __LINE__;',
'<?php $b = pow(3, __LINE__);',
];
yield [
'<?php
($a-$b)**(
($a-$b)**(
($a-$b)**(
($a-$b)**(
($a-$b)**($a-$b)
))));',
'<?php
pow($a-$b,
pow($a-$b,
pow($a-$b,
pow($a-$b,
pow($a-$b,$a-$b)
))));',
];
yield [
'<?php (-1)**( $a** pow(1,2,3, ($a-3)** 4));',
'<?php pow(-1, pow($a, pow(1,2,3, pow($a-3, 4))));',
];
yield [
'<?php 1**2 /**/ ?>',
'<?php pow(1,2) /**/ ?>',
];
yield [
'<?php ($$a)**( $$b);',
'<?php pow($$a, $$b);',
];
yield [
'<?php [1, 2, 3, 4][$x]** 2;',
'<?php pow([1, 2, 3, 4][$x], 2);',
];
yield [
'<?php echo +$a** 2;',
'<?php echo pow(+$a, 2);',
];
yield [
'<?php
interface Test
{
public function pow($a, $b);
}',
];
yield [
'<?php
interface Test
{
public function &pow($a, $b);
}',
];
yield [
'<?php echo $a[1]** $b[2+5];',
'<?php echo pow($a[1], $b[2+5]);',
];
yield [
'<?php pow($b, ...$a);',
];
yield [
'<?php echo +$a** 2;',
'<?php echo pow(+$a, 2,);',
];
yield [
'<?php echo +$a** 2/*1*//*2*/;',
'<?php echo pow(+$a, 2/*1*/,/*2*/);',
];
yield [
'<?php echo 10_0** 2;',
'<?php echo pow(10_0, 2);',
];
yield [
'<?php pow(); ++$a;++$a;++$a;++$a;++$a;++$a;// pow(1,2);',
];
yield [
'<?php pow(5); ++$a;++$a;++$a;++$a;++$a;++$a;# pow(1,2);',
];
yield [
'<?php pow(5,1,1); ++$a;++$a;++$a;++$a;++$a;++$a;/* pow(1,2); */',
];
yield [
'<?php \a\pow(4,3); ++$a;++$a;++$a;++$a;++$a;++$a;/** pow(1,2); */',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php echo $a{1}** $b{2+5};',
'<?php echo pow($a{1}, $b{2+5});',
];
}
/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php echo $a[2^3+1]?->test(1,2)** $b[2+$c];',
'<?php echo pow($a[2^3+1]?->test(1,2), $b[2+$c]);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/MbStrFunctionsFixerTest.php | tests/Fixer/Alias/MbStrFunctionsFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class MbStrFunctionsFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php $x = "strlen";'];
yield ['<?php $x = Foo::strlen("bar");'];
yield ['<?php $x = new strlen("bar");'];
yield ['<?php $x = new \strlen("bar");'];
yield ['<?php $x = new Foo\strlen("bar");'];
yield ['<?php $x = Foo\strlen("bar");'];
yield ['<?php $x = strlen::call("bar");'];
yield ['<?php $x = $foo->strlen("bar");'];
yield ['<?php $x = strlen();']; // number of arguments mismatch
yield ['<?php $x = strlen($a, $b);']; // number of arguments mismatch
yield ['<?php $x = mb_strlen("bar");', '<?php $x = strlen("bar");'];
yield ['<?php $x = \mb_strlen("bar");', '<?php $x = \strlen("bar");'];
yield ['<?php $x = mb_strtolower(mb_strstr("bar", "a"));', '<?php $x = strtolower(strstr("bar", "a"));'];
yield ['<?php $x = mb_strtolower( \mb_strstr ("bar", "a"));', '<?php $x = strtolower( \strstr ("bar", "a"));'];
yield ['<?php $x = mb_substr("bar", 2, 1);', '<?php $x = substr("bar", 2, 1);'];
yield [
'<?php
interface Test
{
public function &strlen($a);
public function strtolower($a);
}',
];
yield [
'<?php $a = mb_str_split($a);',
'<?php $a = str_split($a);',
];
yield [
<<<'PHP'
<?php
namespace Foo;
use function Bar\strlen;
use function mb_strtolower;
use function mb_strtoupper;
use function \mb_str_split;
return strlen($x) > 10 ? mb_strtolower($x) : mb_strtoupper($x);
PHP,
<<<'PHP'
<?php
namespace Foo;
use function Bar\strlen;
use function strtolower;
use function strtoupper;
use function \str_split;
return strlen($x) > 10 ? strtolower($x) : strtoupper($x);
PHP,
];
}
/**
* @requires PHP 8.3
*
* @dataProvider provideFix83Cases
*/
public function testFix83(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, null|string}>
*/
public static function provideFix83Cases(): iterable
{
yield 'mb_str_pad()' => [
'<?php $x = mb_str_pad("bar", 2, "0", STR_PAD_LEFT);',
'<?php $x = str_pad("bar", 2, "0", STR_PAD_LEFT);',
];
}
/**
* @requires PHP 8.4
*
* @dataProvider provideFix84Cases
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, null|string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'mb_trim 1 argument' => [
'<?php $x = mb_trim(" foo ");',
'<?php $x = trim(" foo ");',
];
yield 'mb_trim 2 arguments' => [
'<?php $x = mb_trim("____foo__", "_");',
'<?php $x = trim("____foo__", "_");',
];
yield 'ltrim' => [
'<?php $x = mb_ltrim(" foo ");',
'<?php $x = ltrim(" foo ");',
];
yield 'rtrim' => [
'<?php $x = mb_rtrim(" foo ");',
'<?php $x = rtrim(" foo ");',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/NoAliasLanguageConstructCallFixerTest.php | tests/Fixer/Alias/NoAliasLanguageConstructCallFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\NoAliasLanguageConstructCallFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\NoAliasLanguageConstructCallFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoAliasLanguageConstructCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php exit;',
'<?php die;',
];
yield [
'<?php exit ("foo");',
'<?php die ("foo");',
];
yield [
'<?php exit (1); EXIT(1);',
'<?php DIE (1); EXIT(1);',
];
yield [
'<?php
echo "die";
// die;
/* die(1); */
echo $die;
echo $die(1);
echo $$die;
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/ArrayPushFixerTest.php | tests/Fixer/Alias/ArrayPushFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\ArrayPushFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\ArrayPushFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ArrayPushFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'minimal' => [
'<?php $a[] =$b;',
'<?php array_push($a,$b);',
];
yield 'simple' => [
'<?php $a[] = $b;',
'<?php array_push($a, $b);',
];
yield 'simple, spaces' => [
'<?php $a [] =$b ;',
'<?php array_push( $a , $b );',
];
yield 'simple 25x times' => [
'<?php '.str_repeat('$a[] = $b;', 25),
'<?php '.str_repeat('array_push($a, $b);', 25),
];
yield 'simple namespaced' => [
'<?php $a[] = $b1;',
'<?php \array_push($a, $b1);',
];
yield '; before' => [
'<?php ; $a1[] = $b2 ?>',
'<?php ; array_push($a1, $b2) ?>',
];
yield ') before' => [
'<?php
if ($c) $a[] = $b;
while (--$c > 0) $a[] = $c;
',
'<?php
if ($c) array_push($a, $b);
while (--$c > 0) array_push($a, $c);
',
];
yield '} before' => [
'<?php $b3 = []; { $a = 1; } $b5[] = 1;',
'<?php $b3 = []; { $a = 1; } \array_push($b5, 1);',
];
yield '{ before' => [
'<?php { $a[] = $b8; }',
'<?php { array_push($a, $b8); }',
];
yield 'comments and PHPDoc' => [
'<?php /* */ $a2[] = $b3 /** */;',
'<?php /* */ array_push($a2, $b3) /** */;',
];
yield [
'<?php $a4[1][] = $b6[2];',
'<?php array_push($a4[1], $b6[2]);',
];
yield 'case insensitive and precedence' => [
'<?php
$a[] = $b--;
$a[] = ++$b;
$a[] = !$b;
$a[] = $b + $c;
$a[] = 1 ** $c / 2 || !b && c(1,2,3) ^ $a[1];
',
'<?php
array_push($a, $b--);
ARRAY_push($a, ++$b);
array_PUSH($a, !$b);
ARRAY_PUSH($a, $b + $c);
\array_push($a, 1 ** $c / 2 || !b && c(1,2,3) ^ $a[1]);
',
];
yield 'simple traditional array' => [
'<?php $a[] = array($b, $c);',
'<?php array_push($a, array($b, $c));',
];
yield 'simple short array' => [
'<?php $a[] = [$b];',
'<?php array_push($a, [$b]);',
];
yield 'multiple element short array' => [
'<?php $a[] = [[], [], $b, $c];',
'<?php array_push($a, [[], [], $b, $c]);',
];
yield 'second argument wrapped in `(` `)`' => [
'<?php $a::$c[] = ($b);',
'<?php array_push($a::$c, ($b));',
];
yield [
'<?php $a::$c[] = $b;',
'<?php array_push($a::$c, $b);',
];
yield [
'<?php $a[foo(1,2,3)][] = $b[foo(1,2,3)];',
'<?php array_push($a[foo(1,2,3)], $b[foo(1,2,3)]);',
];
yield [
'<?php \A\B::$foo[] = 1;',
'<?php array_push(\A\B::$foo, 1);',
];
yield [
'<?php static::$foo[] = 1;',
'<?php array_push(static::$foo, 1);',
];
yield [
'<?php namespace\A::$foo[] = 1;',
'<?php array_push(namespace\A::$foo, 1);',
];
yield [
'<?php foo()->bar[] = 1;',
'<?php array_push(foo()->bar, 1);',
];
yield [
'<?php foo()[] = 1;',
'<?php array_push(foo(), 1);',
];
yield [
'<?php $a->$c[] = $b;',
'<?php array_push($a->$c, $b);',
];
yield [
'<?php $a->$c[1]->$d[$a--]->$a[7][] = $b;',
'<?php array_push($a->$c[1]->$d[$a--]->$a[7], $b);',
];
yield 'push multiple' => [
'<?php array_push($a6, $b9, $c);',
];
yield 'push multiple II' => [
'<?php ; array_push($a6a, $b9->$a(1,2), $c);',
];
yield 'push multiple short' => [
'<?php array_push($a6, [$b,$c], []);',
];
yield 'returns number of elements in the array I' => [
'<?php foo(array_push($a7, $b10)); // ;array_push($a, $b);',
];
yield 'returns number of elements in the array II' => [
'<?php $a = 3 * array_push($a8, $b11);',
];
yield 'returns number of elements in the array III' => [
'<?php $a = foo($z, array_push($a9, $b12));',
];
yield 'returns number of elements in the array IV' => [
'<?php $z = array_push($a00, $b13);',
];
yield 'function declare in different namespace' => [
'<?php namespace Foo; function array_push($a11, $b14){};',
];
yield 'overridden detect I' => [
'<?php namespace Foo; array_push(1, $a15);',
];
yield 'overridden detect II' => [
'<?php namespace Foo; array_push($a + 1, $a16);',
];
yield 'different namespace and not a function call' => [
'<?php
A\array_push($a, $b17);
A::array_push($a, $b18);
$a->array_push($a, $b19);
',
];
yield 'open echo' => [
'<?= array_push($a, $b20) ?> <?= array_push($a, $b20); ?>',
];
yield 'ellipsis' => [
'<?php array_push($a, ...$b21);',
];
$precedenceCases = [
'$b and $c',
'$b or $c',
'$b xor $c',
];
foreach ($precedenceCases as $precedenceCase) {
yield [
\sprintf(
'<?php array_push($a, %s);',
$precedenceCase,
),
];
}
yield [
'<?php
while (foo()) $a[] = $b;
foreach (foo() as $C) $a[] = $b;
if (foo()) $a[] = $b;
if ($b) {} elseif (foo()) $a[] = $b;
',
'<?php
while (foo()) array_push($a, $b);
foreach (foo() as $C) array_push($a, $b);
if (foo()) array_push($a, $b);
if ($b) {} elseif (foo()) array_push($a, $b);
',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php $a5{1*3}[2+1][] = $b4{2+1};',
'<?php array_push($a5{1*3}[2+1], $b4{2+1});',
];
yield [
'<?php $a5{1*3}[2+1][] = $b7{2+1};',
'<?php array_push($a5{1*3}[2+1], $b7{2+1});',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php array_push($b?->c[2], $b19);',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'simple 8.1' => [
'<?php
$a[] = $b;
$a = array_push(...);
',
'<?php
array_push($a, $b);
$a = array_push(...);
',
];
}
/**
* @dataProvider provideFixPre84Cases
*
* @requires PHP <8.4
*/
public function testFixPre84(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixPre84Cases(): iterable
{
yield [
'<?php $a->$c[1]->$d{$a--}->$a[7][] = $b;',
'<?php array_push($a->$c[1]->$d{$a--}->$a[7], $b);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php | tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php | <?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\Alias;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer>
*
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoMixedEchoPrintFixerTest 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<int, array{string, null|string, _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
print "test";
',
null,
['use' => 'print'],
];
yield [
'<?php
print ("test");
',
null,
['use' => 'print'],
];
yield [
'<?php
print("test");
',
null,
['use' => 'print'],
];
// `echo` can take multiple parameters (although such usage is rare) while `print` can take only one argument,
// @see https://php.net/manual/en/function.echo.php and @see https://php.net/manual/en/function.print.php
yield [
'<?php
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
',
null,
['use' => 'print'],
];
yield [
'<?php
print "test";
',
'<?php
echo "test";
',
['use' => 'print'],
];
yield [
'<?php
print ("test");
',
'<?php
echo ("test");
',
['use' => 'print'],
];
yield [
'<?php
print("test");
',
'<?php
echo("test");
',
['use' => 'print'],
];
yield [
'<?php
print foo(1, 2);
',
'<?php
echo foo(1, 2);
',
['use' => 'print'],
];
yield [
'<?php
print ["foo", "bar", "baz"][$x];
',
'<?php
echo ["foo", "bar", "baz"][$x];
',
['use' => 'print'],
];
yield [
'<?php
print $foo ? "foo" : "bar";
',
'<?php
echo $foo ? "foo" : "bar";
',
['use' => 'print'],
];
yield [
"<?php print 'foo' ?>...<?php echo 'bar', 'baz' ?>",
"<?php echo 'foo' ?>...<?php echo 'bar', 'baz' ?>",
['use' => 'print'],
];
yield [
'<?php
if ($foo) {
print "foo";
}
print "bar";
',
'<?php
if ($foo) {
echo "foo";
}
echo "bar";
',
['use' => 'print'],
];
yield [
'<?=$foo?>',
null,
['use' => 'print'],
];
foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
yield [
\sprintf($codeSnippet, 'print'),
\sprintf($codeSnippet, 'echo'),
['use' => 'print'],
];
}
yield [
'<?php
echo "test";
',
null,
['use' => 'echo'],
];
yield [
'<?php
echo ("test");
',
null,
['use' => 'echo'],
];
yield [
'<?php
echo("test");
',
null,
['use' => 'echo'],
];
// https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/1502#issuecomment-156436229
yield [
'<?php
($some_var) ? print "true" : print "false";
',
null,
['use' => 'echo'],
];
// echo has no return value while print has a return value of 1 so it can be used in expressions.
// https://www.w3schools.com/php/php_echo_print.asp
yield [
'<?php
$ret = print "test";
',
null,
['use' => 'echo'],
];
yield [
'<?php
@print foo();
',
null,
['use' => 'echo'],
];
yield [
'<?php
function testFunction() {
return print("test");
}
$a = testFunction();
$b += print($a);
$c=\'\';
$c .= $b.print($a);
$d = print($c) > 0 ? \'a\' : \'b\';
switch(print(\'a\')) {}
if (1 === print($a)) {}
',
null,
['use' => 'echo'],
];
yield [
'<?php
some_function_call();
echo "test";
',
'<?php
some_function_call();
print "test";
',
['use' => 'echo'],
];
yield [
'<?php
echo "test";
',
'<?php
print "test";
',
['use' => 'echo'],
];
yield [
'<?php
echo ("test");
',
'<?php
print ("test");
',
['use' => 'echo'],
];
yield [
'<?php
echo("test");
',
'<?php
print("test");
',
['use' => 'echo'],
];
yield [
'<?php
echo foo(1, 2);
',
'<?php
print foo(1, 2);
',
['use' => 'echo'],
];
yield [
'<?php
echo $foo ? "foo" : "bar";
',
'<?php
print $foo ? "foo" : "bar";
',
['use' => 'echo'],
];
yield [
'<?php
if ($foo) {
echo "foo";
}
echo "bar";
',
'<?php
if ($foo) {
print "foo";
}
print "bar";
',
['use' => 'echo'],
];
foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
yield [
\sprintf($codeSnippet, 'echo'),
\sprintf($codeSnippet, 'print'),
['use' => 'echo'],
];
}
}
public function testConfigure(): void
{
$this->fixer->configure([]);
self::assertCandidateTokenType(\T_PRINT, $this->fixer);
}
/**
* @param array<array-key, mixed> $wrongConfig
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches($expectedMessage);
$this->fixer->configure($wrongConfig);
}
/**
* @return iterable<int, array{array<array-key, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['a' => 'b'],
'#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#',
];
yield [
['a' => 'b', 'b' => 'c'],
'#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#',
];
yield [
[1],
'#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#',
];
yield [
['use' => '_invalid_'],
'#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#',
];
}
private static function assertCandidateTokenType(int $expected, NoMixedEchoPrintFixer $fixer): void
{
self::assertSame(
$expected,
\Closure::bind(static fn (NoMixedEchoPrintFixer $fixer): int => $fixer->candidateTokenType, null, NoMixedEchoPrintFixer::class)($fixer),
);
}
/**
* @return iterable<non-empty-string>
*/
private static function getCodeSnippetsToConvertBothWays(): iterable
{
yield 'inside of HTML' => '<div><?php %1$s "foo" ?></div>';
yield 'foreach without curly brackets' => '<?php
%1$s "There will be foos: ";
foreach ($foos as $foo)
%1$s $foo;
%1$s "End of foos";
';
yield 'if and else without curly brackets' => '<?php
if ($foo)
%1$s "One";
elseif ($bar)
%1$s "Two";
else
%1$s "Three";
';
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/SetTypeToCastFixerTest.php | tests/Fixer/Alias/SetTypeToCastFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\SetTypeToCastFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\SetTypeToCastFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SetTypeToCastFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'null cast' => [
'<?php $foo = null;',
'<?php settype($foo, "null");',
];
yield 'null cast comments' => [
'<?php
# 0
$foo = null# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
;',
'<?php
# 0
settype# 1
# 2
(# 3
# 4
$foo# 5
# 6
,# 7
# 8
"null"# 9
)# 10
;',
];
yield 'array + spacing' => [
'<?php $foo = (array) $foo;',
'<?php settype ( $foo , \'array\');',
];
yield 'bool + casing' => [
'<?php $foo = (bool) $foo;',
'<?php settype ( $foo , "Bool");',
];
yield 'boolean with extra space' => [
'<?php $foo = (bool) $foo;',
'<?php settype ( $foo , "boolean");',
];
yield 'double' => [
'<?php $foo = (float) $foo;',
'<?php settype($foo, "double");',
];
yield 'float' => [
'<?php $foo = (float) $foo;',
'<?php settype($foo, "float");',
];
yield 'float in loop' => [
'<?php while(a()){$foo = (float) $foo;}',
'<?php while(a()){settype($foo, "float");}',
];
yield 'int full caps' => [
'<?php $foo = (int) $foo;',
'<?php settype($foo, "INT");',
];
yield 'integer (simple)' => [
'<?php $foo = (int) $foo;',
'<?php settype($foo, "integer");',
];
yield 'object' => [
'<?php echo 1; $foo = (object) $foo;',
'<?php echo 1; settype($foo, "object");',
];
yield 'string' => [
'<?php $foo = (string) $foo;',
'<?php settype($foo, "string");',
];
yield 'string in function body' => [
'<?php function A(){ $foo = (string) $foo; return $foo; }',
'<?php function A(){ settype($foo, "string"); return $foo; }',
];
yield 'integer + no space' => [
'<?php $foo = (int) $foo;',
'<?php settype($foo,"integer");',
];
yield 'no space comments' => [
'<?php /*0*//*1*/$foo = (int) $foo/*2*//*3*//*4*//*5*//*6*//*7*/;/*8*/',
'<?php /*0*//*1*/settype/*2*/(/*3*/$foo/*4*/,/*5*/"integer"/*6*/)/*7*/;/*8*/',
];
yield 'comments with line breaks' => [
'<?php #0
#1
$foo = (int) $foo#2
#3
#4
#5
#6
#7
#8
;#9',
'<?php #0
#1
settype#2
#3
(#4
$foo#5
,#6
"integer"#7
)#8
;#9',
];
// do not fix cases
yield 'first argument is not a variable' => [
'<?php
namespace A\B; // comment
function settype($a, $b){} // "
settype(1, "double");
',
];
yield 'first argument is variable followed by operation' => [
'<?php
namespace A\B; // comment
function settype($a, $b){} // "
settype($foo + 1, "integer"); // function must be overridden, so do not fix it
',
];
yield 'wrong numbers of arguments' => [
'<?php settype($foo, "integer", $a);',
];
yield 'other namespace I' => [
'<?php a\b\settype($foo, "integer", $a);',
];
yield 'other namespace II' => [
'<?php \b\settype($foo, "integer", $a);',
];
yield 'static call' => [
'<?php A::settype($foo, "integer");',
];
yield 'member call' => [
'<?php $a->settype($foo, "integer");',
];
yield 'unknown type' => [
'<?php $a->settype($foo, "foo");',
];
yield 'return value used I' => [
'<?php $a = settype($foo, "integer");',
];
yield 'return value used II' => [
'<?php a(settype($foo, "integer"));',
];
yield 'return value used III' => [
'<?php $a = "123"; $b = [settype($a, "integer")];',
];
yield 'return value used IV' => [
'<?php $a = "123"; $b = [3 => settype($a, "integer")];',
];
yield 'return value used V' => [
'<?= settype($foo, "object");',
];
yield 'wrapped statements, fixable after removing the useless parenthesis brace' => [
'<?php
settype(/*1*//*2*/($a), \'int\');
settype($b, (\'int\'));
settype(($c), (\'int\'));
settype((($d)), ((\'int\')));
',
];
yield 'wrapped statements, not-fixable, even after removing the useless parenthesis brace' => [
'<?php
namespace A\B; // comment
function settype($a, $b){} // "
settype($foo1, (("integer")."1"));
settype($foo2, ("1".("integer")));
settype($foo3, ((("integer")."1")));
settype((($foo)+1), "integer");
',
];
yield 'nested is not an issue for this fixer, as these non may be fixed' => [
'<?php
settype($foo, settype($foo, "double"));
settype(settype($foo, "double"), "double");
',
];
yield 'unknown type II' => [
'<?php settype($foo, "stringX");',
];
yield 'boolean' => [
'<?php $foo = (bool) $foo;',
'<?php settype($foo, "boolean", );',
];
yield 'comments with line breaks II' => [
'<?php #0
#1
$foo = (int) $foo#2
#3
#4
#5
#6
#7
#8
#9
;#10',
'<?php #0
#1
settype#2
#3
(#4
$foo#5
,#6
"integer"#7
,#8
)#9
;#10',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/BacktickToShellExecFixerTest.php | tests/Fixer/Alias/BacktickToShellExecFixerTest.php | <?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\Alias;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\BacktickToShellExecFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\BacktickToShellExecFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class BacktickToShellExecFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'plain' => [
'<?php shell_exec("ls -lah");',
'<?php `ls -lah`;',
];
yield 'with variables' => [
'<?php shell_exec("$var1 ls ${var2} -lah {$var3} file1.txt {$var4[0]} file2.txt {$var5->call()}");',
'<?php `$var1 ls ${var2} -lah {$var3} file1.txt {$var4[0]} file2.txt {$var5->call()}`;',
];
yield 'with single quote' => [
<<<'EOT'
<?php
`echo a\'b`;
`echo 'ab'`;
EOT,
];
yield 'with double quote' => [
<<<'EOT'
<?php
`echo a\"b`;
`echo 'a"b'`;
EOT,
];
yield 'with backtick' => [
<<<'EOT'
<?php
`echo 'a\`b'`;
`echo a\\\`b`;
EOT,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/NoAliasFunctionsFixerTest.php | tests/Fixer/Alias/NoAliasFunctionsFixerTest.php | <?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\Alias;
use PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer>
*
* @author Vladimir Reznichenko <kalessil@gmail.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoAliasFunctionsFixerTest 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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
$defaultSets = [
'@internal',
'@IMAP',
'@pg',
];
foreach (self::provideAllCases() as $set => $cases) {
if (\in_array($set, $defaultSets, true)) {
yield from $cases;
} else {
foreach ($cases as $case) {
yield [$case[0]];
}
}
}
// static case to fix - in case previous generation is broken
yield [
'<?php is_int($a);',
'<?php is_integer($a);',
];
yield [
'<?php socket_set_option($a, $b, $c, $d);',
'<?php socket_setopt($a, $b, $c, $d);',
];
yield 'dns -> mxrr' => [
'<?php getmxrr();',
'<?php dns_get_mx();',
];
yield [
'<?php $b=is_int(count(implode($b,$a)));',
'<?php $b=is_integer(sizeof(join($b,$a)));',
];
yield [
'<?php
interface JoinInterface
{
public function &join();
}
abstract class A
{
abstract public function join($a);
public function is_integer($a)
{
$fputs = "is_double(\$a);\n"; // key_exists($b, $c);
echo $fputs."\$is_writable";
\B::close();
Scope\is_long();
namespace\is_long();
$a->pos();
new join();
new \join();
new ScopeB\join(mt_rand(0, 100));
}
}',
];
yield '@internal' => [
'<?php
$a = rtrim($b);
$a = imap_header($imap_stream, 1);
mbereg_search_getregs();
',
'<?php
$a = chop($b);
$a = imap_header($imap_stream, 1);
mbereg_search_getregs();
',
['sets' => ['@internal']],
];
yield '@IMAP' => [
'<?php
$a = chop($b);
$a = imap_headerinfo($imap_stream, 1);
mb_ereg_search_getregs();
',
'<?php
$a = chop($b);
$a = imap_header($imap_stream, 1);
mb_ereg_search_getregs();
',
['sets' => ['@IMAP']],
];
yield '@mbreg' => [
'<?php
$a = chop($b);
$a = imap_header($imap_stream, 1);
mb_ereg_search_getregs();
mktime();
',
'<?php
$a = chop($b);
$a = imap_header($imap_stream, 1);
mbereg_search_getregs();
mktime();
',
['sets' => ['@mbreg']],
];
yield '@all' => [
'<?php
$a = rtrim($b);
$a = imap_headerinfo($imap_stream, 1);
mb_ereg_search_getregs();
time();
time();
$foo = exif_read_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
mktime($a);
echo gmmktime(1, 2, 3, 4, 5, 6);
',
'<?php
$a = chop($b);
$a = imap_header($imap_stream, 1);
mbereg_search_getregs();
mktime();
gmmktime();
$foo = read_exif_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
mktime($a);
echo gmmktime(1, 2, 3, 4, 5, 6);
',
['sets' => ['@all']],
];
yield '@IMAP, @mbreg' => [
'<?php
$a = chop($b);
$a = imap_headerinfo($imap_stream, 1);
mb_ereg_search_getregs();
',
'<?php
$a = chop($b);
$a = imap_header($imap_stream, 1);
mbereg_search_getregs();
',
['sets' => ['@IMAP', '@mbreg']],
];
yield '@time' => [
'<?php
time();
time();
MKTIME($A);
ECHO GMMKTIME(1, 2, 3, 4, 5, 6);
',
'<?php
MKTIME();
GMMKTIME();
MKTIME($A);
ECHO GMMKTIME(1, 2, 3, 4, 5, 6);
',
['sets' => ['@time']],
];
yield '@exif' => [
'<?php
$foo = exif_read_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
',
'<?php
$foo = read_exif_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
',
['sets' => ['@exif']],
];
/** @var value-of<_AutogeneratedInputConfiguration['sets']> $set */
foreach (self::provideAllCases() as $set => $cases) {
foreach ($cases as $case) {
yield [
$case[0],
$case[1] ?? null,
['sets' => [$set]],
];
}
}
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'simple 8.1' => [
'<?php $a = is_double(...);',
];
}
/**
* @return iterable<string, list<array{0: string, 1?: string}>>
*/
private static function provideAllCases(): iterable
{
$reflectionConstant = new \ReflectionClassConstant(NoAliasFunctionsFixer::class, 'SETS');
/** @var array<string, array<string, string>> $allAliases */
$allAliases = $reflectionConstant->getValue();
$sets = $allAliases;
unset($sets['@time']); // Tested manually
$sets = array_keys($sets);
foreach ($sets as $set) {
\assert(\array_key_exists($set, $allAliases));
$aliases = $allAliases[$set];
$cases = [];
foreach ($aliases as $alias => $master) {
// valid cases
$cases[] = ["<?php \$smth->{$alias}(\$a);"];
$cases[] = ["<?php {$alias}Smth(\$a);"];
$cases[] = ["<?php smth_{$alias}(\$a);"];
$cases[] = ["<?php new {$alias}(\$a);"];
$cases[] = ["<?php new Smth\\{$alias}(\$a);"];
$cases[] = ["<?php Smth\\{$alias}(\$a);"];
$cases[] = ["<?php namespace\\{$alias}(\$a);"];
$cases[] = ["<?php Smth::{$alias}(\$a);"];
$cases[] = ["<?php new {$alias}\\smth(\$a);"];
$cases[] = ["<?php {$alias}::smth(\$a);"];
$cases[] = ["<?php {$alias}\\smth(\$a);"];
$cases[] = ['<?php "SELECT ... '.$alias.'(\$a) ...";'];
$cases[] = ['<?php "SELECT ... '.strtoupper($alias).'($a) ...";'];
$cases[] = ["<?php 'test'.'{$alias}' . 'in concatenation';"];
$cases[] = ['<?php "test" . "'.$alias.'"."in concatenation";'];
$cases[] = [
'<?php
class '.ucfirst($alias).'ing
{
const '.$alias.' = 1;
public function '.$alias.'($'.$alias.')
{
if (defined("'.$alias.'") || $'.$alias.' instanceof '.$alias.') {
echo '.$alias.';
}
}
}
class '.$alias.' extends '.ucfirst($alias).'ing{
const '.$alias.' = "'.$alias.'";
}
',
];
// cases to be fixed
$cases[] = [
"<?php {$master}(\$a);",
"<?php {$alias}(\$a);",
];
$cases[] = [
"<?php \\{$master}(\$a);",
"<?php \\{$alias}(\$a);",
];
$cases[] = [
"<?php {$master}
(\$a);",
"<?php {$alias}
(\$a);",
];
$cases[] = [
"<?php /* foo */ {$master} /** bar */ (\$a);",
"<?php /* foo */ {$alias} /** bar */ (\$a);",
];
$cases[] = [
"<?php a({$master}());",
"<?php a({$alias}());",
];
$cases[] = [
"<?php a(\\{$master}());",
"<?php a(\\{$alias}());",
];
}
yield $set => $cases;
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Alias/ModernizeStrposFixerTest.php | tests/Fixer/Alias/ModernizeStrposFixerTest.php | <?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\Alias;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\Alias\ModernizeStrposFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Alias\ModernizeStrposFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\ModernizeStrposFixer>
*
* @author Alexander M. Turek <me@derrabus.de>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\ModernizeStrposFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ModernizeStrposFixerTest extends AbstractFixerTestCase
{
public function testConfigure(): void
{
$this->fixer->configure(['modernize_stripos' => true]);
self::assertSame(
['modernize_stripos' => true],
\Closure::bind(static fn (ModernizeStrposFixer $fixer): array => $fixer->configuration, null, ModernizeStrposFixer::class)($this->fixer),
);
}
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage('[modernize_strpos] Invalid configuration: The option "invalid" does not exist. Defined options are: "modernize_stripos".');
$this->fixer->configure(['invalid' => true]);
}
/**
* @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{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'yoda ===' => [
'<?php if ( str_starts_with($haystack1, $needle)) {}',
'<?php if (0 === strpos($haystack1, $needle)) {}',
];
yield 'case insensitive yoda ===' => [
'<?php if ( str_starts_with(strtolower($haystack1), strtolower($needle))) {}',
'<?php if (0 === stripos($haystack1, $needle)) {}',
['modernize_stripos' => true],
];
yield 'not zero yoda !==' => [
'<?php if ( !str_starts_with($haystack2, $needle)) {}',
'<?php if (0 !== strpos($haystack2, $needle)) {}',
];
yield 'case insensitive not zero yoda !==' => [
'<?php if ( !str_starts_with(strtolower($haystack2), strtolower($needle))) {}',
'<?php if (0 !== stripos($haystack2, $needle)) {}',
['modernize_stripos' => true],
];
yield 'false yoda ===' => [
'<?php if ( !str_contains($haystack, $needle)) {}',
'<?php if (false === strpos($haystack, $needle)) {}',
];
yield 'case insensitive false yoda ===' => [
'<?php if ( !str_contains(strtolower($haystack), strtolower($needle))) {}',
'<?php if (false === stripos($haystack, $needle)) {}',
['modernize_stripos' => true],
];
yield [
'<?php if (str_starts_with($haystack3, $needle) ) {}',
'<?php if (strpos($haystack3, $needle) === 0) {}',
];
yield [
'<?php if (str_starts_with(strtolower($haystack3), strtolower($needle)) ) {}',
'<?php if (stripos($haystack3, $needle) === 0) {}',
['modernize_stripos' => true],
];
yield 'casing call' => [
'<?php if (str_starts_with($haystack4, $needle) ) {}',
'<?php if (STRPOS($haystack4, $needle) === 0) {}',
];
yield 'case insensitive casing call' => [
'<?php if (str_starts_with(strtolower($haystack4), strtolower($needle)) ) {}',
'<?php if (STRIPOS($haystack4, $needle) === 0) {}',
['modernize_stripos' => true],
];
yield 'leading namespace' => [
'<?php if (\str_starts_with($haystack5, $needle) ) {}',
'<?php if (\strpos($haystack5, $needle) === 0) {}',
];
yield 'case insensitive leading namespace' => [
'<?php if (\str_starts_with(\strtolower($haystack5), \strtolower($needle)) ) {}',
'<?php if (\stripos($haystack5, $needle) === 0) {}',
['modernize_stripos' => true],
];
yield 'leading namespace with yoda' => [
'<?php if ( \str_starts_with($haystack5, $needle)) {}',
'<?php if (0 === \strpos($haystack5, $needle)) {}',
];
yield 'case insensitive leading namespace with yoda' => [
'<?php if ( \str_starts_with(\strtolower($haystack5), \strtolower($needle))) {}',
'<?php if (0 === \stripos($haystack5, $needle)) {}',
['modernize_stripos' => true],
];
yield [
'<?php if (!str_starts_with($haystack6, $needle) ) {}',
'<?php if (strpos($haystack6, $needle) !== 0) {}',
];
yield [
'<?php if (!str_starts_with(strtolower($haystack6), strtolower($needle)) ) {}',
'<?php if (stripos($haystack6, $needle) !== 0) {}',
['modernize_stripos' => true],
];
yield [
'<?php if (!\str_starts_with($haystack6, $needle) ) {}',
'<?php if (\strpos($haystack6, $needle) !== 0) {}',
];
yield [
'<?php if (!\str_starts_with(\strtolower($haystack6), \strtolower($needle)) ) {}',
'<?php if (\stripos($haystack6, $needle) !== 0) {}',
['modernize_stripos' => true],
];
yield [
'<?php if ( !\str_starts_with($haystack6, $needle)) {}',
'<?php if (0 !== \strpos($haystack6, $needle)) {}',
];
yield [
'<?php if ( !\str_starts_with(\strtolower($haystack6), \strtolower($needle))) {}',
'<?php if (0 !== \stripos($haystack6, $needle)) {}',
['modernize_stripos' => true],
];
yield 'casing operand' => [
'<?php if (str_contains($haystack7, $needle) ) {}',
'<?php if (strpos($haystack7, $needle) !== FALSE) {}',
];
yield 'case insensitive casing operand' => [
'<?php if (str_contains(strtolower($haystack7), strtolower($needle)) ) {}',
'<?php if (stripos($haystack7, $needle) !== FALSE) {}',
['modernize_stripos' => true],
];
yield [
'<?php if (!str_contains($haystack8, $needle) ) {}',
'<?php if (strpos($haystack8, $needle) === false) {}',
];
yield [
'<?php if (!str_contains(strtolower($haystack8), strtolower($needle)) ) {}',
'<?php if (stripos($haystack8, $needle) === false) {}',
['modernize_stripos' => true],
];
yield [
'<?php if ( !str_starts_with($haystack9, $needle)) {}',
'<?php if (0 !== strpos($haystack9, $needle)) {}',
];
yield [
'<?php if ( !str_starts_with(strtolower($haystack9), strtolower($needle))) {}',
'<?php if (0 !== stripos($haystack9, $needle)) {}',
['modernize_stripos' => true],
];
yield [
'<?php $a = !str_starts_with($haystack9a, $needle) ;',
'<?php $a = strpos($haystack9a, $needle) !== 0;',
];
yield [
'<?php $a = !str_starts_with(strtolower($haystack9a), strtolower($needle)) ;',
'<?php $a = stripos($haystack9a, $needle) !== 0;',
['modernize_stripos' => true],
];
yield 'comments inside, no spacing' => [
'<?php if (/* foo *//* bar */str_contains($haystack10,$a)) {}',
'<?php if (/* foo */false/* bar */!==strpos($haystack10,$a)) {}',
];
yield 'case insensitive comments inside, no spacing' => [
'<?php if (/* foo *//* bar */str_contains(strtolower($haystack10),strtolower($a))) {}',
'<?php if (/* foo */false/* bar */!==stripos($haystack10,$a)) {}',
['modernize_stripos' => true],
];
yield [
'<?php $a = !str_contains($haystack11, $needle)?>',
'<?php $a = false === strpos($haystack11, $needle)?>',
];
yield [
'<?php $a = !str_contains(strtolower($haystack11), strtolower($needle))?>',
'<?php $a = false === stripos($haystack11, $needle)?>',
['modernize_stripos' => true],
];
yield [
'<?php $a = $input && str_contains($input, $method) ? $input : null;',
'<?php $a = $input && strpos($input, $method) !== FALSE ? $input : null;',
];
yield [
'<?php $a = $input && str_contains(strtolower($input), strtolower($method)) ? $input : null;',
'<?php $a = $input && stripos($input, $method) !== FALSE ? $input : null;',
['modernize_stripos' => true],
];
yield [
'<?php !str_starts_with(strtolower($file), strtolower($needle.\DIRECTORY_SEPARATOR));',
'<?php 0 !== stripos($file, $needle.\DIRECTORY_SEPARATOR);',
['modernize_stripos' => true],
];
yield [
'<?php !str_starts_with(strtolower($file.\DIRECTORY_SEPARATOR), strtolower($needle.\DIRECTORY_SEPARATOR));',
'<?php 0 !== stripos($file.\DIRECTORY_SEPARATOR, $needle.\DIRECTORY_SEPARATOR);',
['modernize_stripos' => true],
];
yield [
'<?php !str_starts_with(strtolower($file.\DIRECTORY_SEPARATOR), strtolower($needle));',
'<?php 0 !== stripos($file.\DIRECTORY_SEPARATOR, $needle);',
['modernize_stripos' => true],
];
yield [
'<?php str_starts_with(strtolower(/** comm */ $file.\DIRECTORY_SEPARATOR /* comm2 */), /* comm3 */ strtolower($needle // comm4
));',
'<?php 0 === stripos(/** comm */ $file.\DIRECTORY_SEPARATOR /* comm2 */, /* comm3 */ $needle // comm4
);',
['modernize_stripos' => true],
];
// do not fix
yield [
'<?php
$x = 1;
$x = "strpos";
// if (false === strpos($haystack12, $needle)) {}
/** if (false === strpos($haystack13, $needle)) {} */
',
];
yield [
'<?php
$x = 1;
$x = "stripos";
// if (false === strpos($haystack12, $needle)) {}
/** if (false === strpos($haystack13, $needle)) {} */
',
];
yield 'disabled stripos (default)' => [
'<?php if (stripos($haystack3, $needle) === 0) {}',
];
yield 'disabled stripos' => [
'<?php if (stripos($haystack3, $needle) === 0) {}',
null,
['modernize_stripos' => false],
];
yield 'different namespace' => [
'<?php if (a\strpos($haystack14, $needle) === 0) {}',
];
yield 'case insensitive different namespace' => [
'<?php if (a\stripos($haystack14, $needle) === 0) {}',
];
yield 'different namespace with yoda' => [
'<?php if (0 === a\strpos($haystack14, $needle)) {}',
];
yield 'case insensitive different namespace with yoda' => [
'<?php if (0 === a\stripos($haystack14, $needle)) {}',
];
yield 'non condition (hardcoded)' => [
'<?php $x = strpos(\'foo\', \'f\');',
];
yield 'case insensitive non condition (hardcoded)' => [
'<?php $x = stripos(\'foo\', \'f\');',
];
yield 'non condition' => [
'<?php $x = strpos($haystack15, $needle) ?>',
];
yield 'case insensitive non condition' => [
'<?php $x = stripos($haystack15, $needle) ?>',
];
yield 'none zero int' => [
'<?php if (1 !== strpos($haystack16, $needle)) {}',
];
yield 'case insensitive none zero int' => [
'<?php if (1 !== stripos($haystack16, $needle)) {}',
];
yield 'greater condition' => [
'<?php if (strpos($haystack17, $needle) > 0) {}',
];
yield 'case insensitive greater condition' => [
'<?php if (stripos($haystack17, $needle) > 0) {}',
];
yield 'lesser condition' => [
'<?php if (0 < strpos($haystack18, $needle)) {}',
];
yield 'case insensitive lesser condition' => [
'<?php if (0 < stripos($haystack18, $needle)) {}',
];
yield 'no argument' => [
'<?php $z = strpos();',
];
yield 'case insensitive no argument' => [
'<?php $z = stripos();',
];
yield 'one argument' => [
'<?php if (0 === strpos($haystack1)) {}',
];
yield 'case insensitive one argument' => [
'<?php if (0 === stripos($haystack1)) {}',
];
yield '3 arguments' => [
'<?php if (0 === strpos($haystack1, $a, $b)) {}',
];
yield 'case insensitive 3 arguments' => [
'<?php if (0 === stripos($haystack1, $a, $b)) {}',
];
yield 'higher precedence 1' => [
'<?php if (4 + 0 !== strpos($haystack9, $needle)) {}',
];
yield 'case insensitive higher precedence 1' => [
'<?php if (4 + 0 !== stripos($haystack9, $needle)) {}',
];
yield 'higher precedence 2' => [
'<?php if (!false === strpos($haystack, $needle)) {}',
];
yield 'case insensitive higher precedence 2' => [
'<?php if (!false === stripos($haystack, $needle)) {}',
];
yield 'higher precedence 3' => [
'<?php $a = strpos($haystack, $needle) === 0 + 1;',
];
yield 'case insensitive higher precedence 3' => [
'<?php $a = stripos($haystack, $needle) === 0 + 1;',
];
yield 'higher precedence 4' => [
'<?php $a = strpos($haystack, $needle) === 0 > $b;',
];
yield 'case insensitive higher precedence 4' => [
'<?php $a = stripos($haystack, $needle) === 0 > $b;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/NoTrailingWhitespaceInStringFixerTest.php | tests/Fixer/StringNotation/NoTrailingWhitespaceInStringFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\NoTrailingWhitespaceInStringFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\NoTrailingWhitespaceInStringFixer>
*
* @author Gregor Harlan
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoTrailingWhitespaceInStringFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield [
"<?php \$a = ' foo\r bar\r\n\nbaz\n ';",
"<?php \$a = ' foo \r bar \r\n \nbaz \n ';",
];
yield [
"<?php \$a = \" foo\r bar\r\n\nbaz\n \";",
"<?php \$a = \" foo \r bar \r\n \nbaz \n \";",
];
yield [
"<?php \$a = \" \$foo\n\";",
"<?php \$a = \" \$foo \n\";",
];
yield [
" foo\r bar\r\nbaz\n",
" foo \r bar \r\nbaz \n ",
];
yield [
"\n<?php foo() ?>\n foo",
" \n<?php foo() ?> \n foo",
];
yield [
"<?php foo() ?>\n<?php foo() ?>\n",
"<?php foo() ?> \n<?php foo() ?> \n",
];
yield [
"<?php foo() ?>\n\nfoo",
"<?php foo() ?>\n \nfoo",
];
yield [
"<?php foo() ?>foo\n",
"<?php foo() ?>foo \n",
];
yield [
'',
' ',
];
yield [
'<?php echo 1; ?>',
'<?php echo 1; ?> ',
];
yield [
'
<?php
$a = <<<EOD
foo
bar
$a
$b
baz
EOD;
',
'
<?php
$a = <<<EOD
foo '.'
bar
$a '.'
$b
'.'
baz '.'
EOD;
',
];
yield [
'
<?php
$a = <<<\'EOD\'
foo
bar
baz
EOD;
',
'
<?php
$a = <<<\'EOD\'
foo '.'
bar
'.'
baz '.'
EOD;
',
];
yield [
'
<?php
$a = <<<\'EOD\'
foo
bar
baz
EOD;
',
'
<?php
$a = <<<\'EOD\'
foo '.'
bar
'.'
baz '.'
EOD;
',
];
yield 'binary string' => [
"<?php \$a = b\" \$foo\n\";",
"<?php \$a = b\" \$foo \n\";",
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/StringLengthToEmptyFixerTest.php | tests/Fixer/StringNotation/StringLengthToEmptyFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\StringLengthToEmptyFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\StringLengthToEmptyFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class StringLengthToEmptyFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php $a = \'\' === $b;',
'<?php $a = 0 === strlen($b);',
];
yield 'casing' => [
'<?php $a1 = \'\' === $b ?>',
'<?php $a1 = 0 === \STRlen($b) ?>',
];
yield 'nested' => [
'<?php $a2 = \'\' === foo(\'\' === foo(\'\' === foo(\'\' === foo(\'\' === foo(\'\' === foo(\'\' === $b))))));',
'<?php $a2 = 0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen($b)))))))))))));',
];
yield [
'<?php $a3 = \'\' !== $b;',
'<?php $a3 = 0 !== strlen($b);',
];
yield [
'<?php $a4 = 0 <= strlen($b);',
];
yield [
'<?php $a5 = \'\' === $b;',
'<?php $a5 = 0 >= strlen($b);',
];
yield [
'<?php $a6 = \'\' !== $b;',
'<?php $a6 = 0 < strlen($b);',
];
yield [
'<?php $a7 = 0 > strlen($b);',
];
yield [
'<?php $a8 = 1 === strlen($b);',
];
yield [
'<?php $a9 = 1 !== strlen($b);',
];
yield [
'<?php $a10 = \'\' !== $b;',
'<?php $a10 = 1 <= strlen($b);',
];
yield [
'<?php $a11 = 1 >= strlen($b);',
];
yield [
'<?php $a12 = 1 < strlen($b);',
];
yield [
'<?php $a13 = \'\' === $b;',
'<?php $a13 = 1 > strlen($b);',
];
yield [
'<?php $a14 = $b === \'\';',
'<?php $a14 = strlen($b) === 0;',
];
yield [
'<?php $a15 = $b !== \'\';',
'<?php $a15 = strlen($b) !== 0;',
];
yield [
'<?php $a16 = $b === \'\';',
'<?php $a16 = strlen($b) <= 0;',
];
yield [
'<?php $a17 = strlen($b) >= 0;',
];
yield [
'<?php $a18 = strlen($b) < 0;',
];
yield [
'<?php $a19 = $b !== \'\';',
'<?php $a19 = strlen($b) > 0;',
];
yield [
'<?php $a20 = strlen($b) === 1;',
];
yield [
'<?php $a21 = strlen($b) !== 1;',
];
yield [
'<?php $a22 = strlen($b) <= 1;',
];
yield [
'<?php $a23 = $b !== \'\';',
'<?php $a23 = strlen($b) >= 1;',
];
yield [
'<?php $a24 = $b === \'\';',
'<?php $a24 = strlen($b) < 1;',
];
yield [
'<?php $a25 = strlen($b) > 1;',
];
yield [
'<?php $e = 0 === foo() ? -1 : \'\' === $a;',
'<?php $e = 0 === foo() ? -1 : 0 === strlen($a);',
];
yield [
'<?php $x = /* 1 */ $b /* 2 */ ->a !== \'\';',
'<?php $x = strlen(/* 1 */ $b /* 2 */ ->a) >= 1;',
];
yield [
'<?php $y = $b[0] === \'\';',
'<?php $y = strlen($b[0]) < 1;',
];
yield [
'<?php $y1 = $b[0]->$a[$a++](1) /* 1 */ === \'\';',
'<?php $y1 = strlen($b[0]->$a[$a++](1) /* 1 */ ) < 1;',
];
yield [
'<?php $z = \'\' === $b[1]->foo(++$i, static function () use ($z){ return $z + 1;});',
'<?php $z = 0 === strlen($b[1]->foo(++$i, static function () use ($z){ return $z + 1;}));',
];
yield [
'<?php if ((string) $node !== \'\') { echo 1; }',
'<?php if (\strlen((string) $node) > 0) { echo 1; }',
];
yield 'do not fix' => [
'<?php
//-----------------------------------
// operator precedence
$a01 = 0 === strlen($b) ** $c;
$a03 = 0 === strlen($b) % $c;
$a04 = 0 === strlen($b) / $c;
$a05 = 0 === strlen($b) * $c;
$a06 = 0 === strlen($b) + $c;
$a07 = 0 === strlen($b) - $c;
$a08 = 0 === strlen($b) . $c;
$a09 = 0 === strlen($b) >> $c;
$a10 = 0 === strlen($b) << $c;
$a01n = strlen($b) === 0 ** $c;
$a03n = strlen($b) === 0 % $c;
$a04n = strlen($b) === 0 / $c;
$a05n = strlen($b) === 0 * $c;
$a06n = strlen($b) === 0 + $c;
$a07n = strlen($b) === 0 - $c;
$a08n = strlen($b) === 0 . $c;
$a09n = strlen($b) === 0 >> $c;
$a10n = strlen($b) === 0 << $c;
$b = "a";
$c = 0 === strlen($b) - 1;
var_dump($c);
$c = "" === $b - 1;
var_dump($c);
//-----------------------------------
// type juggle
$d = false;
$e = 0 === strlen($d) ? -1 : 0;
var_dump($e);
$e = "" === $d ? -1 : 0;
var_dump($e);
//-----------------------------------
// wrong argument count
$f = strlen(1,2);
$g = \strlen(1,2,3);
//-----------------------------------
// others
$h = 0 === (string) strlen($b);
$i = 0 === @strlen($b);
$j = 0 === !strlen($b);
$jj = 2 === strlen($b);
$jk = __DIR__ === strlen($b);
$jl = \'X\' !== strlen($b);
$jj = strlen($b) === 2;
$jk = strlen($b) === __DIR__;
$jl = strlen($b) !== \'X\';
//-----------------------------------
// not global calls
$k = 0 === $a->strlen($b);
$l = 0 === Foo::strlen($b);
//-----------------------------------
// comments
// $a = 0 === strlen($b);
# $a = 0 === strlen($b);
/* $a = 0 === strlen($b); */
/** $a = 0 === strlen($b); */
',
];
// cases where `(` and `)` must be kept
yield [
'<?php $e = ($a = trim($b)) !== \'\';',
'<?php $e = \strlen($a = trim($b)) > 0;',
];
yield [
'<?php if (\'\' === ($value = foo())) { echo 2; }',
'<?php if (0 === \strlen($value = foo())) { echo 2; }',
];
yield [
'<?php
$a02 = 0 === strlen($b) instanceof stdClass;
$a02n = strlen($b) === 0 instanceof stdClass;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/SimpleToComplexStringVariableFixerTest.php | tests/Fixer/StringNotation/SimpleToComplexStringVariableFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\SimpleToComplexStringVariableFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\SimpleToComplexStringVariableFixer>
*
* @author Dave van der Brugge <dmvdbrugge@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SimpleToComplexStringVariableFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'basic fix' => [
<<<'EXPECTED'
<?php
$name = "World";
echo "Hello {$name}!";
EXPECTED,
<<<'INPUT'
<?php
$name = "World";
echo "Hello ${name}!";
INPUT,
];
yield 'heredoc' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo <<<TEST
Hello {$name}!
TEST;
EXPECTED,
<<<'INPUT'
<?php
$name = 'World';
echo <<<TEST
Hello ${name}!
TEST;
INPUT,
];
yield 'implicit' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo "Hello $name!";
EXPECTED,
];
yield 'implicit again' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo "Hello { $name }!";
EXPECTED,
];
yield 'escaped' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo "Hello \${name}";
EXPECTED,
];
yield 'double dollar' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo "Hello \${$name}";
EXPECTED,
<<<'INPUT'
<?php
$name = 'World';
echo "Hello $${name}";
INPUT,
];
yield 'double dollar heredoc' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo <<<TEST
Hello \${$name}!
TEST;
EXPECTED,
<<<'INPUT'
<?php
$name = 'World';
echo <<<TEST
Hello $${name}!
TEST;
INPUT,
];
yield 'double dollar single quote' => [
<<<'EXPECTED'
<?php
$name = 'World';
echo 'Hello $${name}';
EXPECTED,
];
yield 'array elements' => [
<<<'PHP'
<?php
"Hello {$array[0]}!";
"Hello {$array['index']}!";
PHP,
<<<'PHP'
<?php
"Hello ${array[0]}!";
"Hello ${array['index']}!";
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/HeredocToNowdocFixerTest.php | tests/Fixer/StringNotation/HeredocToNowdocFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\HeredocToNowdocFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\HeredocToNowdocFixer>
*
* @author Gregor Harlan <gharlan@web.de>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class HeredocToNowdocFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'EOF'
<?php $a = <<<'TEST'
Foo $bar \n
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<'TEST'
TEST;
EOF,
<<<'EOF'
<?php $a = <<<TEST
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<'TEST'
Foo \\ $bar \n
TEST;
EOF,
<<<'EOF'
<?php $a = <<<TEST
Foo \\\\ \$bar \\n
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<'TEST'
Foo
TEST;
EOF,
<<<'EOF'
<?php $a = <<<"TEST"
Foo
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<TEST
Foo $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<TEST
Foo \\$bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<TEST
Foo \n $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<<TEST
Foo \x00 $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php
$html = <<< 'HTML'
a
HTML;
EOF,
<<<'EOF'
<?php
$html = <<< HTML
a
HTML;
EOF,
];
yield [
<<<'EOF'
<?php $a = <<< 'TEST'
Foo
TEST;
EOF,
<<<'EOF'
<?php $a = <<< "TEST"
Foo
TEST;
EOF,
];
yield [
<<<EOF
<?php echo <<<'TEST'\r\nFoo\r\nTEST;
EOF,
<<<EOF
<?php echo <<<TEST\r\nFoo\r\nTEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<'TEST'
Foo $bar \n
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<'TEST'
TEST;
EOF,
<<<'EOF'
<?php $a = b<<<TEST
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<'TEST'
Foo \\ $bar \n
TEST;
EOF,
<<<'EOF'
<?php $a = b<<<TEST
Foo \\\\ \$bar \\n
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<'TEST'
Foo
TEST;
EOF,
<<<'EOF'
<?php $a = b<<<"TEST"
Foo
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<TEST
Foo $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<TEST
Foo \\$bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<TEST
Foo \n $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<<TEST
Foo \x00 $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php
$html = b<<< 'HTML'
a
HTML;
EOF,
<<<'EOF'
<?php
$html = b<<< HTML
a
HTML;
EOF,
];
yield [
<<<'EOF'
<?php $a = b<<< 'TEST'
Foo
TEST;
EOF,
<<<'EOF'
<?php $a = b<<< "TEST"
Foo
TEST;
EOF,
];
yield [
<<<EOF
<?php echo b<<<'TEST'\r\nFoo\r\nTEST;
EOF,
<<<EOF
<?php echo b<<<TEST\r\nFoo\r\nTEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<'TEST'
Foo $bar \n
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<'TEST'
TEST;
EOF,
<<<'EOF'
<?php $a = B<<<TEST
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<'TEST'
Foo \\ $bar \n
TEST;
EOF,
<<<'EOF'
<?php $a = B<<<TEST
Foo \\\\ \$bar \\n
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<'TEST'
Foo
TEST;
EOF,
<<<'EOF'
<?php $a = B<<<"TEST"
Foo
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<TEST
Foo $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<TEST
Foo \\$bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<TEST
Foo \n $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<<TEST
Foo \x00 $bar
TEST;
EOF,
];
yield [
<<<'EOF'
<?php
$html = B<<< 'HTML'
a
HTML;
EOF,
<<<'EOF'
<?php
$html = B<<< HTML
a
HTML;
EOF,
];
yield [
<<<'EOF'
<?php $a = B<<< 'TEST'
Foo
TEST;
EOF,
<<<'EOF'
<?php $a = B<<< "TEST"
Foo
TEST;
EOF,
];
yield [
<<<EOF
<?php echo B<<<'TEST'\r\nFoo\r\nTEST;
EOF,
<<<EOF
<?php echo B<<<TEST\r\nFoo\r\nTEST;
EOF,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/StringLineEndingFixerTest.php | tests/Fixer/StringNotation/StringLineEndingFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\StringLineEndingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\StringLineEndingFixer>
*
* @author Ilija Tovilo <ilija.tovilo@me.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class StringLineEndingFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, string}>
*/
public static function provideFixCases(): iterable
{
$heredocTemplate = "<?php\n\$a=\n<<<EOT\n%s\n\nEOT;\n";
$nowdocTemplate = "<?php\n\$a=\n<<<'EOT'\n%s\n\nEOT;\n";
$input = '/**
* @SWG\Get(
* path="/api/v0/cards",
* operationId="listCards",
* tags={"Банковские карты"},
* summary="Возвращает список банковских карт."
* )
*/';
yield [
"<?php \$a = 'my\nmulti\nline\nstring';\r\n",
"<?php \$a = 'my\r\nmulti\nline\r\nstring';\r\n",
];
yield [
"<?php \$a = \"my\nmulti\nline\nstring\";\r\n",
"<?php \$a = \"my\r\nmulti\nline\r\nstring\";\r\n",
];
yield [
"<?php \$a = \"my\nmulti\nline\nstring\nwith\n\$b\ninterpolation\";\r\n",
"<?php \$a = \"my\r\nmulti\nline\r\nstring\nwith\r\n\$b\ninterpolation\";\r\n",
];
yield [
\sprintf($heredocTemplate, $input),
\sprintf($heredocTemplate, str_replace("\n", "\r", $input)),
];
yield [
\sprintf($heredocTemplate, $input),
\sprintf($heredocTemplate, str_replace("\n", "\r\n", $input)),
];
yield [
\sprintf($nowdocTemplate, $input),
\sprintf($nowdocTemplate, str_replace("\n", "\r", $input)),
];
yield [
\sprintf($nowdocTemplate, $input),
\sprintf($nowdocTemplate, str_replace("\n", "\r\n", $input)),
];
yield [
\sprintf(str_replace('<<<', 'b<<<', $nowdocTemplate), $input),
\sprintf(str_replace('<<<', 'b<<<', $nowdocTemplate), str_replace("\n", "\r\n", $input)),
];
yield [
\sprintf(str_replace('<<<', 'B<<<', $nowdocTemplate), $input),
\sprintf(str_replace('<<<', 'B<<<', $nowdocTemplate), str_replace("\n", "\r\n", $input)),
];
yield [
\sprintf(str_replace('<<<', 'b<<<', $heredocTemplate), $input),
\sprintf(str_replace('<<<', 'b<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
];
yield [
\sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), $input),
\sprintf(str_replace('<<<', 'B<<<', $heredocTemplate), str_replace("\n", "\r\n", $input)),
];
yield 'not T_CLOSE_TAG, do T_INLINE_HTML' => [
"<?php foo(); ?>\r\nA\n\n",
"<?php foo(); ?>\r\nA\r\n\r\n",
];
yield [
"<?php \$a = b'my\nmulti\nline\nstring';\r\n",
"<?php \$a = b'my\r\nmulti\nline\r\nstring';\r\n",
];
}
public function testWithWhitespacesConfig(): void
{
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
$this->doTest(
"<?php \$a = 'my\r\nmulti\r\nline\r\nstring';",
"<?php \$a = 'my\nmulti\nline\nstring';",
);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/ExplicitStringVariableFixerTest.php | tests/Fixer/StringNotation/ExplicitStringVariableFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ExplicitStringVariableFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
$input = $expected = '<?php';
for ($inc = 1; $inc < 15; ++$inc) {
$expected .= " \$var{$inc} = \"My name is {\$name}!\";";
$input .= " \$var{$inc} = \"My name is \$name!\";";
}
yield [
$expected,
$input,
];
yield [
'<?php $a = "My name is {$name}!";',
'<?php $a = "My name is $name!";',
];
yield [
'<?php "My name is {$james}{$bond}!";',
'<?php "My name is $james$bond!";',
];
yield [
'<?php $a = <<<EOF
My name is {$name}!
EOF;
',
'<?php $a = <<<EOF
My name is $name!
EOF;
',
];
yield [
'<?php $a = "{$b}";',
'<?php $a = "$b";',
];
yield [
'<?php $a = "{$b} start";',
'<?php $a = "$b start";',
];
yield [
'<?php $a = "end {$b}";',
'<?php $a = "end $b";',
];
yield [
'<?php $a = <<<EOF
{$b}
EOF;
',
'<?php $a = <<<EOF
$b
EOF;
',
];
yield ['<?php $a = \'My name is $name!\';'];
yield ['<?php $a = "My name is " . $name;'];
yield ['<?php $a = "My name is {$name}!";'];
yield [
'<?php $a = <<<EOF
My name is {$name}!
EOF;
',
];
yield ['<?php $a = "My name is {$user->name}";'];
yield [
'<?php $a = <<<EOF
My name is {$user->name}
EOF;
',
];
yield [
'<?php $a = <<<\'EOF\'
$b
EOF;
',
];
yield [
'<?php $a = "My name is {$object->property} !";',
'<?php $a = "My name is $object->property !";',
];
yield [
'<?php $a = "My name is {$array[1]} !";',
'<?php $a = "My name is $array[1] !";',
];
yield [
'<?php $a = "My name is {$array[\'foo\']} !";',
'<?php $a = "My name is $array[foo] !";',
];
yield [
'<?php $a = "My name is {$array[$foo]} !";',
'<?php $a = "My name is $array[$foo] !";',
];
yield [
'<?php $a = "My name is {$array[$foo]}[{$bar}] !";',
'<?php $a = "My name is $array[$foo][$bar] !";',
];
yield [
'<?php $a = "Closure not allowed {$closure}() text";',
'<?php $a = "Closure not allowed $closure() text";',
];
yield [
'<?php $a = "Complex object chaining not allowed {$object->property}->method()->array[1] text";',
'<?php $a = "Complex object chaining not allowed $object->property->method()->array[1] text";',
];
yield [
'<?php $a = "Complex array chaining not allowed {$array[1]}[2][MY_CONSTANT] text";',
'<?php $a = "Complex array chaining not allowed $array[1][2][MY_CONSTANT] text";',
];
yield [
'<?php $a = "Concatenation: {$james}{$bond}{$object->property}{$array[1]}!";',
'<?php $a = "Concatenation: $james$bond$object->property$array[1]!";',
];
yield [
'<?php $a = "{$a->b} start";',
'<?php $a = "$a->b start";',
];
yield [
'<?php $a = "end {$a->b}";',
'<?php $a = "end $a->b";',
];
yield [
'<?php $a = "{$a[1]} start";',
'<?php $a = "$a[1] start";',
];
yield [
'<?php $a = "end {$a[1]}";',
'<?php $a = "end $a[1]";',
];
yield [
'<?php $a = b"{$a->b} start";',
'<?php $a = b"$a->b start";',
];
yield [
'<?php $a = b"end {$a->b}";',
'<?php $a = b"end $a->b";',
];
yield [
'<?php $a = b"{$a[1]} start";',
'<?php $a = b"$a[1] start";',
];
yield [
'<?php $a = b"end {$a[1]}";',
'<?php $a = b"end $a[1]";',
];
yield [
'<?php $a = B"{$a->b} start";',
'<?php $a = B"$a->b start";',
];
yield [
'<?php $a = B"end {$a->b}";',
'<?php $a = B"end $a->b";',
];
yield [
'<?php $a = B"{$a[1]} start";',
'<?php $a = B"$a[1] start";',
];
yield [
'<?php $a = B"end {$a[1]}";',
'<?php $a = B"end $a[1]";',
];
yield [
'<?php $a = "*{$a[0]}{$b[1]}X{$c[2]}{$d[3]}";',
'<?php $a = "*$a[0]$b[1]X$c[2]$d[3]";',
];
yield [
'<?php $a = `echo $foo`;',
];
yield [
'<?php $a = "My name is {$name}!"; $a = `echo $foo`; $a = "{$a->b} start";',
'<?php $a = "My name is $name!"; $a = `echo $foo`; $a = "$a->b start";',
];
yield [
'<?php $mobileNumberVisible = "***-***-{$last4Digits[0]}{$last4Digits[1]}-{$last4Digits[2]}{$last4Digits[3]}";',
'<?php $mobileNumberVisible = "***-***-$last4Digits[0]$last4Digits[1]-$last4Digits[2]$last4Digits[3]";',
];
yield [
'<?php $pair = "{$foo} {$bar[0]}";',
'<?php $pair = "$foo {$bar[0]}";',
];
yield [
'<?php $pair = "{$foo}{$bar[0]}";',
'<?php $pair = "$foo{$bar[0]}";',
];
yield [
'<?php $a = "My name is {$array[-1]} !";',
'<?php $a = "My name is $array[-1] !";',
];
yield [
'<?php $a = "{$a[-1]} start";',
'<?php $a = "$a[-1] start";',
];
yield [
'<?php $a = "end {$a[-1]}";',
'<?php $a = "end $a[-1]";',
];
yield [
'<?php $a = b"end {$a[-1]}";',
'<?php $a = b"end $a[-1]";',
];
yield [
'<?php $a = B"end {$a[-1]}";',
'<?php $a = B"end $a[-1]";',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/NoBinaryStringFixerTest.php | tests/Fixer/StringNotation/NoBinaryStringFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\NoBinaryStringFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\NoBinaryStringFixer>
*
* @author ntzm
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoBinaryStringFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php echo \'hello world\';',
'<?php echo b\'hello world\';',
];
yield [
'<?php $a=\'hello world\';',
'<?php $a=b\'hello world\';',
];
yield [
'<?php echo (\'hello world\');',
'<?php echo (b\'hello world\');',
];
yield [
'<?php echo "hi".\'hello world\';',
'<?php echo "hi".b\'hello world\';',
];
yield [
'<?php echo "hello world";',
'<?php echo b"hello world";',
];
yield [
'<?php echo \'hello world\';',
'<?php echo B\'hello world\';',
];
yield [
'<?php echo "hello world";',
'<?php echo B"hello world";',
];
yield [
'<?php echo /* foo */"hello world";',
'<?php echo /* foo */B"hello world";',
];
yield [
"<?php echo <<<EOT\nfoo\nEOT;\n",
"<?php echo b<<<EOT\nfoo\nEOT;\n",
];
yield [
"<?php echo <<<EOT\nfoo\nEOT;\n",
"<?php echo B<<<EOT\nfoo\nEOT;\n",
];
yield [
"<?php echo <<<'EOT'\nfoo\nEOT;\n",
"<?php echo b<<<'EOT'\nfoo\nEOT;\n",
];
yield [
"<?php echo <<<'EOT'\nfoo\nEOT;\n",
"<?php echo B<<<'EOT'\nfoo\nEOT;\n",
];
yield [
"<?php echo <<<\"EOT\"\nfoo\nEOT;\n",
"<?php echo b<<<\"EOT\"\nfoo\nEOT;\n",
];
yield [
"<?php echo <<<\"EOT\"\nfoo\nEOT;\n",
"<?php echo B<<<\"EOT\"\nfoo\nEOT;\n",
];
yield [
'<?php
echo "{$fruit}";
echo " {$fruit}";
',
'<?php
echo b"{$fruit}";
echo b" {$fruit}";
',
];
yield ['<?php echo Bar::foo();'];
yield ['<?php echo bar::foo();'];
yield ['<?php echo "b";'];
yield ['<?php echo b;'];
yield ['<?php echo b."a";'];
yield ['<?php echo b("a");'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/MultilineStringToHeredocFixerTest.php | tests/Fixer/StringNotation/MultilineStringToHeredocFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\MultilineStringToHeredocFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\MultilineStringToHeredocFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class MultilineStringToHeredocFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string}>
*/
public static function provideFixCases(): iterable
{
yield 'empty string' => [
'<?php $a = \'\';',
];
yield 'single line string' => [
'<?php $a = \'a b\';',
];
yield 'single line string with "\n"' => [
'<?php $a = \'a\nb\';',
];
yield 'simple single quoted' => [
<<<'EOF'
<?php
$a = <<<'EOD'
line1
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = 'line1
line2';
EOD,
];
yield 'simple double quoted' => [
<<<'EOF'
<?php
$a = <<<EOD
line1
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = "line1
line2";
EOD,
];
yield 'colliding closing marker - one' => [
<<<'EOF'
<?php
$a = <<<'EOD_'
line1
EOD
line2
EOD_;
EOF,
<<<'EOF'
<?php
$a = 'line1
EOD
line2';
EOF,
];
yield 'colliding closing marker - two' => [
<<<'EOF'
<?php
$a = <<<'EOD__'
line1
EOD
EOD_
line2
EOD__;
EOF,
<<<'EOF'
<?php
$a = 'line1
EOD
EOD_
line2';
EOF,
];
yield 'single quoted unescape' => [
<<<'EOF'
<?php
$a = <<<'EOD'
line1
\
\n
'
\\'
\"
\
EOD;
EOF,
<<<'EOD'
<?php
$a = 'line1
\\
\n
\'
\\\\\'
\"
\
';
EOD,
];
yield 'double quoted unescape' => [
<<<'EOF'
<?php
$a = <<<EOD
line1
\\
\n
"
\\\\"
\'
\
"{$rawPath}"
EOD;
EOF,
<<<'EOD'
<?php
$a = "line1
\\
\n
\"
\\\\\"
\'
\
\"{$rawPath}\"
";
EOD,
];
yield 'single quoted /w variable' => [
<<<'EOF'
<?php
$a = <<<'EOD'
line1$var
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = 'line1$var
line2';
EOD,
];
yield 'double quoted /w simple variable' => [
<<<'EOF'
<?php
$a = <<<EOD
line1$var
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = "line1$var
line2";
EOD,
];
yield 'double quoted /w simple curly variable' => [
<<<'EOF'
<?php
$a = <<<EOD
line1{$var}
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = "line1{$var}
line2";
EOD,
];
yield 'double quoted /w complex curly variable' => [
<<<'EOF'
<?php
$a = <<<EOD
{$arr['foo'][3]}
{ $obj->values[3]->name }
{${getName()}}
EOD;
EOF,
<<<'EOD'
<?php
$a = "{$arr['foo'][3]}
{ $obj->values[3]->name }
{${getName()}}";
EOD,
];
yield 'test stateful fixing loop' => [
<<<'EOF'
<?php
<<<EOD
$a
{$b['x']}
EOD;
<<<'EOD'
c
d
EOD;
<<<EOD
$a
$b
EOD;
<<<EOD
$c
$d
EOD;
'a';
<<<'EOD'
b
c
EOD;
<<<'EOD'
EOD;
<<<EOD
$a $b
EOD;
<<<'EOD'
c d
EOD;
<<<EOD
$a $b
EOD;
<<<EOD
$a
$b
EOD;
<<<'EOD'
$c
$d
EOD;
EOF,
<<<'EOF'
<?php
"$a
{$b['x']}";
'c
d';
"$a
$b";
"$c
$d";
'a';
'b
c';
<<<'EOD'
EOD;
<<<EOD
$a $b
EOD;
<<<'EOD'
c d
EOD;
<<<EOD
$a $b
EOD;
<<<EOD
$a
$b
EOD;
<<<'EOD'
$c
$d
EOD;
EOF,
];
yield 'simple strings prefixed with b/B' => [
<<<'EOF'
<?php
$a = <<<'EOD'
line1
line2
EOD;
$b = <<<EOD
line1
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = b'line1
line2';
$b = B"line1
line2";
EOD,
];
yield 'double quoted /w simple variable prefixed with b/B' => [
<<<'EOF'
<?php
$a = <<<EOD
line1$var
line2
EOD;
$b = <<<EOD
line1$var
line2
EOD;
EOF,
<<<'EOD'
<?php
$a = b"line1$var
line2";
$b = B"line1$var
line2";
EOD,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/EscapeImplicitBackslashesFixerTest.php | tests/Fixer/StringNotation/EscapeImplicitBackslashesFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\EscapeImplicitBackslashesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\EscapeImplicitBackslashesFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\StringNotation\EscapeImplicitBackslashesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class EscapeImplicitBackslashesFixerTest 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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'EOF'
<?php $var = 'String (\\\'\r\n\x0) for My\Prefix\\';
EOF,
];
yield [
<<<'EOF'
<?php $var = 'String (\\\'\\r\\n\\x0) for My\\Prefix\\';
EOF,
<<<'EOF'
<?php $var = 'String (\\\'\r\n\x0) for My\Prefix\\';
EOF,
['single_quoted' => true],
];
yield [
<<<'EOF'
<?php
$var = "\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z";
$var = "\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z \\' \\8\\9 \\xZ \\u";
$var = "$foo \\A \\a \\' \\8\\9 \\xZ \\u ${bar}";
$var = <<<HEREDOC_SYNTAX
\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z
\\"
\\'
\\8\\9
\\xZ
\\u
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
$foo \\A \\a \\" \\' \\8\\9 \\xZ \\u ${bar}
HEREDOC_SYNTAX;
$var = <<<'NOWDOC_SYNTAX'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC_SYNTAX;
EOF,
<<<'EOF'
<?php
$var = "\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z";
$var = "\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z \' \8\9 \xZ \u";
$var = "$foo \A \a \' \8\9 \xZ \u ${bar}";
$var = <<<HEREDOC_SYNTAX
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\"
\'
\8\9
\xZ
\u
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
$foo \A \a \" \' \8\9 \xZ \u ${bar}
HEREDOC_SYNTAX;
$var = <<<'NOWDOC_SYNTAX'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = "\e\f\n\r\t\v \\ \$ \"";
$var = "$foo \e\f\n\r\t\v \\ \$ \" ${bar}";
$var = <<<HEREDOC_SYNTAX
\e\f\n\r\t\v \\ \$
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
$foo \e\f\n\r\t\v \\ \$ ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = "\0 \00 \000 \0000 \00000";
$var = "$foo \0 \00 \000 \0000 \00000 ${bar}";
$var = <<<HEREDOC_SYNTAX
\0 \00 \000 \0000 \00000
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
$foo \0 \00 \000 \0000 \00000 ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = "\xA \x99 \u{0}";
$var = "$foo \xA \x99 \u{0} ${bar}";
$var = <<<HEREDOC_SYNTAX
\xA \x99 \u{0}
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
$foo \xA \x99 \u{0} ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = 'backslash \\ already escaped';
$var = 'code coverage';
$var = "backslash \\ already escaped";
$var = "code coverage";
$var = <<<HEREDOC_SYNTAX
backslash \\ already escaped
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
code coverage
HEREDOC_SYNTAX;
$var = <<<'NOWDOC_SYNTAX'
backslash \\ already escaped
NOWDOC_SYNTAX;
$var = <<<'NOWDOC_SYNTAX'
code coverage
NOWDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = "\A\a \' \8\9 \xZ \u";
$var = "$foo \A\a \' \8\9 \xZ \u ${bar}";
EOF,
null,
['double_quoted' => false],
];
yield [
<<<'EOF'
<?php
$var = <<<HEREDOC_SYNTAX
\A\Z
\a\z
\'
\8\9
\xZ
\u
HEREDOC_SYNTAX;
$var = <<<HEREDOC_SYNTAX
$foo
\A\Z
\a\z
\'
\8\9
\xZ
\u
${bar}
HEREDOC_SYNTAX;
EOF,
null,
['heredoc_syntax' => false],
];
yield [
<<<'EOF'
<?php $var = b'String (\\\'\r\n\x0) for My\Prefix\\';
EOF,
];
yield [
<<<'EOF'
<?php $var = b'String (\\\'\\r\\n\\x0) for My\\Prefix\\';
EOF,
<<<'EOF'
<?php $var = b'String (\\\'\r\n\x0) for My\Prefix\\';
EOF,
['single_quoted' => true],
];
yield [
<<<'EOF'
<?php
$var = b"\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z";
$var = b"\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z \\' \\8\\9 \\xZ \\u";
$var = b"$foo \\A \\a \\' \\8\\9 \\xZ \\u ${bar}";
$var = b<<<HEREDOC_SYNTAX
\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z
\\"
\\'
\\8\\9
\\xZ
\\u
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
$foo \\A \\a \\" \\' \\8\\9 \\xZ \\u ${bar}
HEREDOC_SYNTAX;
$var = b<<<'NOWDOC_SYNTAX'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC_SYNTAX;
EOF,
<<<'EOF'
<?php
$var = b"\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z";
$var = b"\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z \' \8\9 \xZ \u";
$var = b"$foo \A \a \' \8\9 \xZ \u ${bar}";
$var = b<<<HEREDOC_SYNTAX
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\"
\'
\8\9
\xZ
\u
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
$foo \A \a \" \' \8\9 \xZ \u ${bar}
HEREDOC_SYNTAX;
$var = b<<<'NOWDOC_SYNTAX'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = b"\e\f\n\r\t\v \\ \$ \"";
$var = b"$foo \e\f\n\r\t\v \\ \$ \" ${bar}";
$var = b<<<HEREDOC_SYNTAX
\e\f\n\r\t\v \\ \$
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
$foo \e\f\n\r\t\v \\ \$ ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = b"\0 \00 \000 \0000 \00000";
$var = b"$foo \0 \00 \000 \0000 \00000 ${bar}";
$var = b<<<HEREDOC_SYNTAX
\0 \00 \000 \0000 \00000
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
$foo \0 \00 \000 \0000 \00000 ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = b"\xA \x99 \u{0}";
$var = b"$foo \xA \x99 \u{0} ${bar}";
$var = b<<<HEREDOC_SYNTAX
\xA \x99 \u{0}
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
$foo \xA \x99 \u{0} ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = b'backslash \\ already escaped';
$var = b'code coverage';
$var = b"backslash \\ already escaped";
$var = b"code coverage";
$var = b<<<HEREDOC_SYNTAX
backslash \\ already escaped
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
code coverage
HEREDOC_SYNTAX;
$var = b<<<'NOWDOC_SYNTAX'
backslash \\ already escaped
NOWDOC_SYNTAX;
$var = b<<<'NOWDOC_SYNTAX'
code coverage
NOWDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = b"\A\a \' \8\9 \xZ \u";
$var = b"$foo \A\a \' \8\9 \xZ \u ${bar}";
EOF,
null,
['double_quoted' => false],
];
yield [
<<<'EOF'
<?php
$var = b<<<HEREDOC_SYNTAX
\A\Z
\a\z
\'
\8\9
\xZ
\u
HEREDOC_SYNTAX;
$var = b<<<HEREDOC_SYNTAX
$foo
\A\Z
\a\z
\'
\8\9
\xZ
\u
${bar}
HEREDOC_SYNTAX;
EOF,
null,
['heredoc_syntax' => false],
];
yield [
<<<'EOF'
<?php $var = B'String (\\\'\r\n\x0) for My\Prefix\\';
EOF,
];
yield [
<<<'EOF'
<?php $var = B'String (\\\'\\r\\n\\x0) for My\\Prefix\\';
EOF,
<<<'EOF'
<?php $var = B'String (\\\'\r\n\x0) for My\Prefix\\';
EOF,
['single_quoted' => true],
];
yield [
<<<'EOF'
<?php
$var = B"\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z";
$var = B"\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z \\' \\8\\9 \\xZ \\u";
$var = B"$foo \\A \\a \\' \\8\\9 \\xZ \\u ${bar}";
$var = B<<<HEREDOC_SYNTAX
\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z
\\"
\\'
\\8\\9
\\xZ
\\u
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
$foo \\A \\a \\" \\' \\8\\9 \\xZ \\u ${bar}
HEREDOC_SYNTAX;
$var = B<<<'NOWDOC_SYNTAX'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC_SYNTAX;
EOF,
<<<'EOF'
<?php
$var = B"\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z";
$var = B"\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z \' \8\9 \xZ \u";
$var = B"$foo \A \a \' \8\9 \xZ \u ${bar}";
$var = B<<<HEREDOC_SYNTAX
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\"
\'
\8\9
\xZ
\u
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
$foo \A \a \" \' \8\9 \xZ \u ${bar}
HEREDOC_SYNTAX;
$var = B<<<'NOWDOC_SYNTAX'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = B"\e\f\n\r\t\v \\ \$ \"";
$var = B"$foo \e\f\n\r\t\v \\ \$ \" ${bar}";
$var = B<<<HEREDOC_SYNTAX
\e\f\n\r\t\v \\ \$
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
$foo \e\f\n\r\t\v \\ \$ ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = B"\0 \00 \000 \0000 \00000";
$var = B"$foo \0 \00 \000 \0000 \00000 ${bar}";
$var = B<<<HEREDOC_SYNTAX
\0 \00 \000 \0000 \00000
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
$foo \0 \00 \000 \0000 \00000 ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = B"\xA \x99 \u{0}";
$var = B"$foo \xA \x99 \u{0} ${bar}";
$var = B<<<HEREDOC_SYNTAX
\xA \x99 \u{0}
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
$foo \xA \x99 \u{0} ${bar}
HEREDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = B'backslash \\ already escaped';
$var = B'code coverage';
$var = B"backslash \\ already escaped";
$var = B"code coverage";
$var = B<<<HEREDOC_SYNTAX
backslash \\ already escaped
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
code coverage
HEREDOC_SYNTAX;
$var = B<<<'NOWDOC_SYNTAX'
backslash \\ already escaped
NOWDOC_SYNTAX;
$var = B<<<'NOWDOC_SYNTAX'
code coverage
NOWDOC_SYNTAX;
EOF,
];
yield [
<<<'EOF'
<?php
$var = B"\A\a \' \8\9 \xZ \u";
$var = B"$foo \A\a \' \8\9 \xZ \u ${bar}";
EOF,
null,
['double_quoted' => false],
];
yield [
<<<'EOF'
<?php
$var = B<<<HEREDOC_SYNTAX
\A\Z
\a\z
\'
\8\9
\xZ
\u
HEREDOC_SYNTAX;
$var = B<<<HEREDOC_SYNTAX
$foo
\A\Z
\a\z
\'
\8\9
\xZ
\u
${bar}
HEREDOC_SYNTAX;
EOF,
null,
['heredoc_syntax' => false],
];
yield [
<<<'EOF'
<?php
$var = "\\bar";
$var = "\\bar";
$var = "\\\\bar";
$var = "\\\\bar";
$var = "\\\\\\bar";
$var = "\\\\\\bar";
EOF,
<<<'EOF'
<?php
$var = "\bar";
$var = "\\bar";
$var = "\\\bar";
$var = "\\\\bar";
$var = "\\\\\bar";
$var = "\\\\\\bar";
EOF,
];
yield [
<<<'EOF'
<?php
$var = '\\bar';
$var = '\\bar';
$var = '\\\\bar';
$var = '\\\\bar';
$var = '\\\\\\bar';
$var = '\\\\\\bar';
EOF,
<<<'EOF'
<?php
$var = '\bar';
$var = '\\bar';
$var = '\\\bar';
$var = '\\\\bar';
$var = '\\\\\bar';
$var = '\\\\\\bar';
EOF,
['single_quoted' => true],
];
yield [
<<<'EOF'
<?php
$var = <<<TXT
\\bar
\\bar
\\\\bar
\\\\bar
\\\\\\bar
\\\\\\bar
TXT;
EOF,
<<<'EOF'
<?php
$var = <<<TXT
\bar
\\bar
\\\bar
\\\\bar
\\\\\bar
\\\\\\bar
TXT;
EOF,
];
yield [
<<<'EOF'
<?php
$var = <<<'TXT'
\bar
\\bar
\\\bar
\\\\bar
\\\\\bar
\\\\\\bar
TXT;
EOF,
];
yield 'execution operator' => [
'<?php $var = `ls a\\\b`;',
'<?php $var = `ls a\b`;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/HeredocClosingMarkerFixerTest.php | tests/Fixer/StringNotation/HeredocClosingMarkerFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\HeredocClosingMarkerFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\HeredocClosingMarkerFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\StringNotation\HeredocClosingMarkerFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class HeredocClosingMarkerFixerTest 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<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'heredoc' => [
<<<'PHP'
<?php $a = <<<EOD
xxx EOD xxx
EOD;
PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx EOD xxx
TEST;
PHP,
];
yield 'nowdoc' => [
<<<'PHP'
<?php $a = <<<'EOD'
xxx EOD xxx
EOD;
PHP,
<<<'PHP'
<?php $a = <<<'TEST'
xxx EOD xxx
TEST;
PHP,
];
yield 'heredoc /w custom preferred closing marker' => [
<<<'PHP'
<?php $a = <<<EOF
xxx
EOF;
PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx
TEST;
PHP,
['closing_marker' => 'EOF'],
];
yield 'heredoc /w custom explicit style' => [
<<<'PHP'
<?php $a = <<<"EOD"
xxx
EOD;
$b = <<<"EOD"
xxx2
EOD;
$b = <<<'EOD'
xxx3
EOD;
PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx
TEST;
$b = <<<"TEST"
xxx2
TEST;
$b = <<<'TEST'
xxx3
TEST;
PHP,
['explicit_heredoc_style' => true],
];
yield 'heredoc /w b' => [
<<<'PHP'
<?php $a = b<<<EOD
xxx EOD xxx
EOD;
PHP,
<<<'PHP'
<?php $a = b<<<TEST
xxx EOD xxx
TEST;
PHP,
];
yield 'heredoc /w B' => [
<<<'PHP'
<?php $a = B<<<EOD
xxx EOD xxx
EOD;
PHP,
<<<'PHP'
<?php $a = B<<<TEST
xxx EOD xxx
TEST;
PHP,
];
yield 'heredoc and reserved closing marker' => [
<<<'PHP_'
<?php $a = <<<PHP
xxx
PHP;
PHP_,
];
yield 'heredoc and reserved closing marker - different case' => [
<<<'PHP_'
<?php $a = <<<PHP
xxx
PHP;
$a = <<<PHP
PHP;
PHP_,
<<<'PHP'
<?php $a = <<<php
xxx
php;
$a = <<<Php
Php;
PHP,
];
yield 'heredoc and reserved custom closing marker' => [
<<<'PHP'
<?php $a = <<<Žlutý
xxx
Žlutý;
$aNormCase = <<<Žlutý
xxx
Žlutý;
$aNormCase = <<<Žlutý
xxx
Žlutý;
$b = <<<EOD
xxx2
EOD;
$c = <<<EOD
xxx3
EOD;
PHP,
<<<'PHP_'
<?php $a = <<<Žlutý
xxx
Žlutý;
$aNormCase = <<<ŽluTý
xxx
ŽluTý;
$aNormCase = <<<ŽLUTÝ
xxx
ŽLUTÝ;
$b = <<<Žlutý2
xxx2
Žlutý2;
$c = <<<PHP
xxx3
PHP;
PHP_,
['reserved_closing_markers' => ['Žlutý']],
];
yield 'no longer colliding reserved marker recovery' => [
<<<'PHP'
<?php
$a = <<<CSS
CSS;
$a = <<<CSS
CSS;
$a = <<<CSS_
CSS
CSS_;
$a = <<<CSS
CSS_
CSS;
PHP,
<<<'PHP'
<?php
$a = <<<CSS_
CSS_;
$a = <<<CSS__
CSS__;
$a = <<<CSS__
CSS
CSS__;
$a = <<<CSS__
CSS_
CSS__;
PHP,
];
yield 'heredoc /w content starting with preferred closing marker' => [
<<<'PHP'
<?php $a = <<<EOD_
EOD xxx
EOD_;
PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD xxx
TEST;
PHP,
];
yield 'heredoc /w content starting with whitespace and preferred closing marker' => [
<<<'PHP'
<?php $a = <<<EOD_
EOD xxx
EOD_;
PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD xxx
TEST;
PHP,
];
yield 'heredoc /w content starting with preferred closing marker and single quote' => [
<<<'PHP'
<?php $a = <<<EOD_
EOD'
EOD_;
PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD'
TEST;
PHP,
];
yield 'heredoc /w content starting with preferred closing marker and semicolon' => [
<<<'PHP'
<?php $a = <<<EOD_
EOD;
EOD_;
PHP,
<<<'PHP'
<?php $a = <<<TEST
EOD;
TEST;
PHP,
];
yield 'heredoc /w content ending with preferred closing marker' => [
<<<'PHP'
<?php $a = <<<EOD
xxx EOD
EOD;
PHP,
<<<'PHP'
<?php $a = <<<TEST
xxx EOD
TEST;
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/StringImplicitBackslashesFixerTest.php | tests/Fixer/StringNotation/StringImplicitBackslashesFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\StringImplicitBackslashesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\StringImplicitBackslashesFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
* @author Michael Vorisek <https://github.com/mvorisek>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\StringNotation\StringImplicitBackslashesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class StringImplicitBackslashesFixerTest 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, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'EOD'
<?php $var = 'String (\\\'\r\n\x0\) for My\Prefix\\';
EOD,
];
yield [
<<<'EOD'
<?php $var = 'String (\\\'\\r\\n\\x0\\) for My\\Prefix\\';
EOD,
<<<'EOD'
<?php $var = 'String (\\\'\r\n\x0\) for My\Prefix\\';
EOD,
['single_quoted' => 'escape'],
];
yield [
<<<'EOD'
<?php
$var = "\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z";
$var = "\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z \\' \\8\\9 \\xZ \\u";
$var = "$foo \\A \\a \\' \\8\\9 \\xZ \\u ${bar}";
$var = <<<HEREDOC
\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z
\\"
\\'
\\8\\9
\\xZ
\\u
HEREDOC;
$var = <<<HEREDOC
$foo \\A \\a \\" \\' \\8\\9 \\xZ \\u ${bar}
HEREDOC;
$var = <<<'NOWDOC'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC;
EOD,
<<<'EOD'
<?php
$var = "\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z";
$var = "\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z \' \8\9 \xZ \u";
$var = "$foo \A \a \' \8\9 \xZ \u ${bar}";
$var = <<<HEREDOC
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\"
\'
\8\9
\xZ
\u
HEREDOC;
$var = <<<HEREDOC
$foo \A \a \" \' \8\9 \xZ \u ${bar}
HEREDOC;
$var = <<<'NOWDOC'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = "\e\f\n\r\t\v \\ \$ \"";
$var = "$foo \e\f\n\r\t\v \\ \$ \" ${bar}";
$var = <<<HEREDOC
\e\f\n\r\t\v \\ \$
HEREDOC;
$var = <<<HEREDOC
$foo \e\f\n\r\t\v \\ \$ ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = "\0 \00 \000 \0000 \00000";
$var = "$foo \0 \00 \000 \0000 \00000 ${bar}";
$var = <<<HEREDOC
\0 \00 \000 \0000 \00000
HEREDOC;
$var = <<<HEREDOC
$foo \0 \00 \000 \0000 \00000 ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = "\xA \x99 \u{0}";
$var = "$foo \xA \x99 \u{0} ${bar}";
$var = <<<HEREDOC
\xA \x99 \u{0}
HEREDOC;
$var = <<<HEREDOC
$foo \xA \x99 \u{0} ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = 'backslash \ not escaped';
$var = 'code coverage';
$var = "backslash \\ already escaped";
$var = "code coverage";
$var = <<<HEREDOC
backslash \\ already escaped
HEREDOC;
$var = <<<HEREDOC
code coverage
HEREDOC;
$var = <<<'NOWDOC'
backslash \\ already escaped
NOWDOC;
$var = <<<'NOWDOC'
code coverage
NOWDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = "\A\a \' \8\9 \xZ \u";
$var = "$foo \A\a \' \8\9 \xZ \u ${bar}";
EOD,
null,
['double_quoted' => 'unescape'],
];
yield [
<<<'EOD'
<?php
$var = <<<HEREDOC
\A\Z
\a\z
\'
\8\9
\xZ
\u
HEREDOC;
$var = <<<HEREDOC
$foo
\A\Z
\a\z
\'
\8\9
\xZ
\u
${bar}
HEREDOC;
EOD,
null,
['heredoc' => 'unescape'],
];
yield [
<<<'EOD'
<?php $var = b'String (\\\'\r\n\x0) for My\Prefix\\';
EOD,
];
yield [
<<<'EOD'
<?php $var = b'String (\\\'\\r\\n\\x0) for My\\Prefix\\';
EOD,
<<<'EOD'
<?php $var = b'String (\\\'\r\n\x0) for My\Prefix\\';
EOD,
['single_quoted' => 'escape'],
];
yield [
<<<'EOD'
<?php
$var = b"\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z";
$var = b"\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z \\' \\8\\9 \\xZ \\u";
$var = b"$foo \\A \\a \\' \\8\\9 \\xZ \\u ${bar}";
$var = b<<<HEREDOC
\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z
\\"
\\'
\\8\\9
\\xZ
\\u
HEREDOC;
$var = b<<<HEREDOC
$foo \\A \\a \\" \\' \\8\\9 \\xZ \\u ${bar}
HEREDOC;
$var = b<<<'NOWDOC'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC;
EOD,
<<<'EOD'
<?php
$var = b"\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z";
$var = b"\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z \' \8\9 \xZ \u";
$var = b"$foo \A \a \' \8\9 \xZ \u ${bar}";
$var = b<<<HEREDOC
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\"
\'
\8\9
\xZ
\u
HEREDOC;
$var = b<<<HEREDOC
$foo \A \a \" \' \8\9 \xZ \u ${bar}
HEREDOC;
$var = b<<<'NOWDOC'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = b"\e\f\n\r\t\v \\ \$ \"";
$var = b"$foo \e\f\n\r\t\v \\ \$ \" ${bar}";
$var = b<<<HEREDOC
\e\f\n\r\t\v \\ \$
HEREDOC;
$var = b<<<HEREDOC
$foo \e\f\n\r\t\v \\ \$ ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = b"\0 \00 \000 \0000 \00000";
$var = b"$foo \0 \00 \000 \0000 \00000 ${bar}";
$var = b<<<HEREDOC
\0 \00 \000 \0000 \00000
HEREDOC;
$var = b<<<HEREDOC
$foo \0 \00 \000 \0000 \00000 ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = b"\xA \x99 \u{0}";
$var = b"$foo \xA \x99 \u{0} ${bar}";
$var = b<<<HEREDOC
\xA \x99 \u{0}
HEREDOC;
$var = b<<<HEREDOC
$foo \xA \x99 \u{0} ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = b'backslash \ not escaped';
$var = b'code coverage';
$var = b"backslash \\ already escaped";
$var = b"code coverage";
$var = b<<<HEREDOC
backslash \\ already escaped
HEREDOC;
$var = b<<<HEREDOC
code coverage
HEREDOC;
$var = b<<<'NOWDOC'
backslash \\ already escaped
NOWDOC;
$var = b<<<'NOWDOC'
code coverage
NOWDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = b"\A\a \' \8\9 \xZ \u";
$var = b"$foo \A\a \' \8\9 \xZ \u ${bar}";
EOD,
null,
['double_quoted' => 'unescape'],
];
yield [
<<<'EOD'
<?php
$var = b<<<HEREDOC
\A\Z
\a\z
\'
\8\9
\xZ
\u
HEREDOC;
$var = b<<<HEREDOC
$foo
\A\Z
\a\z
\'
\8\9
\xZ
\u
${bar}
HEREDOC;
EOD,
null,
['heredoc' => 'unescape'],
];
yield [
<<<'EOD'
<?php $var = B'String (\\\'\r\n\x0) for My\Prefix\\';
EOD,
];
yield [
<<<'EOD'
<?php $var = B'String (\\\'\\r\\n\\x0) for My\\Prefix\\';
EOD,
<<<'EOD'
<?php $var = B'String (\\\'\r\n\x0) for My\Prefix\\';
EOD,
['single_quoted' => 'escape'],
];
yield [
<<<'EOD'
<?php
$var = B"\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z";
$var = B"\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z \\' \\8\\9 \\xZ \\u";
$var = B"$foo \\A \\a \\' \\8\\9 \\xZ \\u ${bar}";
$var = B<<<HEREDOC
\\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
\\a\\b\\c\\d\\g\\h\\i\\j\\k\\l\\m\\o\\p\\q\\s\\w\\y\\z
\\"
\\'
\\8\\9
\\xZ
\\u
HEREDOC;
$var = B<<<HEREDOC
$foo \\A \\a \\" \\' \\8\\9 \\xZ \\u ${bar}
HEREDOC;
$var = B<<<'NOWDOC'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC;
EOD,
<<<'EOD'
<?php
$var = B"\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z";
$var = B"\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z \' \8\9 \xZ \u";
$var = B"$foo \A \a \' \8\9 \xZ \u ${bar}";
$var = B<<<HEREDOC
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\"
\'
\8\9
\xZ
\u
HEREDOC;
$var = B<<<HEREDOC
$foo \A \a \" \' \8\9 \xZ \u ${bar}
HEREDOC;
$var = B<<<'NOWDOC'
\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
\a\b\c\d\g\h\i\j\k\l\m\o\p\q\s\w\y\z
\'
\8\9
\xZ
\u
NOWDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = B"\e\f\n\r\t\v \\ \$ \"";
$var = B"$foo \e\f\n\r\t\v \\ \$ \" ${bar}";
$var = B<<<HEREDOC
\e\f\n\r\t\v \\ \$
HEREDOC;
$var = B<<<HEREDOC
$foo \e\f\n\r\t\v \\ \$ ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = B"\0 \00 \000 \0000 \00000";
$var = B"$foo \0 \00 \000 \0000 \00000 ${bar}";
$var = B<<<HEREDOC
\0 \00 \000 \0000 \00000
HEREDOC;
$var = B<<<HEREDOC
$foo \0 \00 \000 \0000 \00000 ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = B"\xA \x99 \u{0}";
$var = B"$foo \xA \x99 \u{0} ${bar}";
$var = B<<<HEREDOC
\xA \x99 \u{0}
HEREDOC;
$var = B<<<HEREDOC
$foo \xA \x99 \u{0} ${bar}
HEREDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = B'backslash \ not escaped';
$var = B'code coverage';
$var = B"backslash \\ already escaped";
$var = B"code coverage";
$var = B<<<HEREDOC
backslash \\ already escaped
HEREDOC;
$var = B<<<HEREDOC
code coverage
HEREDOC;
$var = B<<<'NOWDOC'
backslash \\ already escaped
NOWDOC;
$var = B<<<'NOWDOC'
code coverage
NOWDOC;
EOD,
];
yield [
<<<'EOD'
<?php
$var = B"\A\a \' \8\9 \xZ \u";
$var = B"$foo \A\a \' \8\9 \xZ \u ${bar}";
EOD,
null,
['double_quoted' => 'unescape'],
];
yield [
<<<'EOD'
<?php
$var = B<<<HEREDOC
\A\Z
\a\z
\'
\8\9
\xZ
\u
HEREDOC;
$var = B<<<HEREDOC
$foo
\A\Z
\a\z
\'
\8\9
\xZ
\u
${bar}
HEREDOC;
EOD,
null,
['heredoc' => 'unescape'],
];
yield [
<<<'EOD'
<?php
$var = "\\bar";
$var = "\\bar";
$var = "\\\\bar";
$var = "\\\\bar";
$var = "\\\\\\bar";
$var = "\\\\\\bar";
EOD,
<<<'EOD'
<?php
$var = "\bar";
$var = "\\bar";
$var = "\\\bar";
$var = "\\\\bar";
$var = "\\\\\bar";
$var = "\\\\\\bar";
EOD,
];
yield [
<<<'EOD'
<?php
$var = '\\bar';
$var = '\\bar';
$var = '\\\\bar';
$var = '\\\\bar';
$var = '\\\\\\bar';
$var = '\\\\\\bar';
EOD,
<<<'EOD'
<?php
$var = '\bar';
$var = '\\bar';
$var = '\\\bar';
$var = '\\\\bar';
$var = '\\\\\bar';
$var = '\\\\\\bar';
EOD,
['single_quoted' => 'escape'],
];
yield [
<<<'EOD'
<?php
$var = <<<TXT
\\bar
\\bar
\\\\bar
\\\\bar
\\\\\\bar
\\\\\\bar
TXT;
EOD,
<<<'EOD'
<?php
$var = <<<TXT
\bar
\\bar
\\\bar
\\\\bar
\\\\\bar
\\\\\\bar
TXT;
EOD,
];
yield [
<<<'EOD'
<?php
$var = <<<'TXT'
\bar
\\bar
\\\bar
\\\\bar
\\\\\bar
\\\\\\bar
TXT;
EOD,
];
yield 'unescaped backslashes in single quoted string - backslash' => [
<<<'EOD'
<?php
'\\';
'\\\\';
'\\\\\\';
EOD,
];
yield 'unescaped backslashes in single quoted string - reserved double quote' => [
<<<'EOD'
<?php
'\"';
'\"';
'\\\"';
'\\\"';
'\\\\\"';
'\\\\\"';
'\\\\\\\"';
'\\\\\\\"';
EOD,
<<<'EOD'
<?php
'\"';
'\\"';
'\\\"';
'\\\\"';
'\\\\\"';
'\\\\\\"';
'\\\\\\\"';
'\\\\\\\\"';
EOD,
];
yield 'unescaped backslashes in single quoted string - reserved chars' => [
<<<'EOD'
<?php
'\b';
'\b';
'\\\b';
'\\\b';
'\\\\\b';
'\\\\\b';
'\\\\\\\b';
'\\\\\\\b';
'\$v';
'\$v';
'\{$v}';
'\{$v}';
'\n';
'\n';
EOD,
<<<'EOD'
<?php
'\b';
'\\b';
'\\\b';
'\\\\b';
'\\\\\b';
'\\\\\\b';
'\\\\\\\b';
'\\\\\\\\b';
'\$v';
'\\$v';
'\{$v}';
'\\{$v}';
'\n';
'\\n';
EOD,
];
yield 'unescaped backslashes in double quoted string - backslash' => [
<<<'EOD'
<?php
"\\";
"\\\\";
"\\\\\\";
EOD,
null,
['double_quoted' => 'unescape'],
];
yield 'unescaped backslashes in double quoted string - reserved chars' => [
<<<'EOD'
<?php
"\b";
"\b";
"\\\b";
"\\\b";
"\\\\\b";
"\\\\\b";
"\\\\\\\b";
"\\\\\\\b";
"\$v";
"\\$v";
"\{$v}";
"\\{$v}";
"\n";
"\\n";
EOD,
<<<'EOD'
<?php
"\b";
"\\b";
"\\\b";
"\\\\b";
"\\\\\b";
"\\\\\\b";
"\\\\\\\b";
"\\\\\\\\b";
"\$v";
"\\$v";
"\{$v}";
"\\{$v}";
"\n";
"\\n";
EOD,
['double_quoted' => 'unescape'],
];
yield 'unescaped backslashes in heredoc - backslash' => [
<<<'EOD_'
<?php
<<<EOD
\
\
\\\
\\\
\\\\\
\\\\\
\\\\\\\
EOD;
EOD_,
<<<'EOD_'
<?php
<<<EOD
\
\\
\\\
\\\\
\\\\\
\\\\\\
\\\\\\\
EOD;
EOD_,
['heredoc' => 'unescape'],
];
yield 'unescaped backslashes in heredoc - reserved single quote' => [
<<<'EOD_'
<?php
<<<EOD
\'
\'
\\\'
\\\'
\\\\\'
\\\\\'
\\\\\\\'
EOD;
EOD_,
<<<'EOD_'
<?php
<<<EOD
\'
\\'
\\\'
\\\\'
\\\\\'
\\\\\\'
\\\\\\\'
EOD;
EOD_,
['heredoc' => 'unescape'],
];
yield 'unescaped backslashes in heredoc - reserved double quote' => [
<<<'EOD_'
<?php
<<<EOD
\"
\"
\\\"
\\\"
\\\\\"
\\\\\"
\\\\\\\"
EOD;
EOD_,
<<<'EOD_'
<?php
<<<EOD
\"
\\"
\\\"
\\\\"
\\\\\"
\\\\\\"
\\\\\\\"
EOD;
EOD_,
['heredoc' => 'unescape'],
];
yield 'unescaped backslashes in heredoc - reserved chars' => [
<<<'EOD_'
<?php
<<<EOD
\$v
\\$v
\{$v}
\\{$v}
\n
\\n
\b
\b
\\\b
\\\b
\\\\\b
\\\\\b
\\\\\\\b
EOD;
EOD_,
<<<'EOD_'
<?php
<<<EOD
\$v
\\$v
\{$v}
\\{$v}
\n
\\n
\b
\\b
\\\b
\\\\b
\\\\\b
\\\\\\b
\\\\\\\b
EOD;
EOD_,
['heredoc' => 'unescape'],
];
yield 'ignored mixed implicit backslashes in single quoted string' => [
<<<'EOD'
<?php
$var = 'a\b\\c';
EOD,
null,
['single_quoted' => 'ignore'],
];
yield 'ignored mixed implicit backslashes in double quoted string' => [
<<<'EOD'
<?php
$var = "a\b\\c";
EOD,
null,
['double_quoted' => 'ignore'],
];
yield 'ignored mixed implicit backslashes in heredoc' => [
<<<'EOD'
<?php
$var = <<<HEREDOC
a\b\\c
HEREDOC;
EOD,
null,
['heredoc' => 'ignore'],
];
yield 'execution operator' => [
'<?php $var = `ls a\\\b`;',
'<?php $var = `ls a\b`;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/StringNotation/SingleQuoteFixerTest.php | tests/Fixer/StringNotation/SingleQuoteFixerTest.php | <?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\StringNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer>
*
* @author Gregor Harlan <gharlan@web.de>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleQuoteFixerTest 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<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php $a = \'\';',
'<?php $a = "";',
];
yield [
'<?php $a = \'foo bar\';',
'<?php $a = "foo bar";',
];
yield [
'<?php $a = b\'\';',
'<?php $a = b"";',
];
yield [
'<?php $a = B\'\';',
'<?php $a = B"";',
];
yield [
'<?php $a = b\'foo bar\';',
'<?php $a = b"foo bar";',
];
yield [
'<?php $a = B\'foo bar\';',
'<?php $a = B"foo bar";',
];
yield [
'<?php $a = \'foo
bar\';',
'<?php $a = "foo
bar";',
];
yield [
'<?php $a = \'foo\'.\'bar\'."$baz";',
'<?php $a = \'foo\'."bar"."$baz";',
];
yield [
'<?php $a = \'foo "bar"\';',
'<?php $a = "foo \"bar\"";',
];
yield [
<<<'EOF'
<?php $a = '\\foo\\bar\\\\';
EOF,
<<<'EOF'
<?php $a = "\\foo\\bar\\\\";
EOF,
];
yield [
'<?php $a = \'foo $bar7\';',
'<?php $a = "foo \$bar7";',
];
yield [
'<?php $a = \'foo $(bar7)\';',
'<?php $a = "foo \$(bar7)";',
];
yield [
'<?php $a = \'foo \\\($bar8)\';',
'<?php $a = "foo \\\(\$bar8)";',
];
yield ['<?php $a = "foo \" \$$bar";'];
yield ['<?php $a = b"foo \" \$$bar";'];
yield ['<?php $a = B"foo \" \$$bar";'];
yield ['<?php $a = "foo \'bar\'";'];
yield ['<?php $a = b"foo \'bar\'";'];
yield ['<?php $a = B"foo \'bar\'";'];
yield ['<?php $a = "foo $bar";'];
yield ['<?php $a = b"foo $bar";'];
yield ['<?php $a = B"foo $bar";'];
yield ['<?php $a = "foo ${bar}";'];
yield ['<?php $a = b"foo ${bar}";'];
yield ['<?php $a = B"foo ${bar}";'];
yield ['<?php $a = "foo\n bar";'];
yield ['<?php $a = b"foo\n bar";'];
yield ['<?php $a = B"foo\n bar";'];
yield [
<<<'EOF'
<?php $a = "\\\n";
EOF,
];
yield [
'<?php $a = \'foo \\\'bar\\\'\';',
'<?php $a = "foo \'bar\'";',
['strings_containing_single_quote_chars' => true],
];
yield [
<<<'EOT'
<?php
// none
$a = 'start \' end';
// one escaped backslash
$b = 'start \\\' end';
// two escaped backslash
$c = 'start \\\\\' end';
EOT,
<<<'EOT'
<?php
// none
$a = "start ' end";
// one escaped backslash
$b = "start \\' end";
// two escaped backslash
$c = "start \\\\' end";
EOT,
['strings_containing_single_quote_chars' => true],
];
yield [
<<<'EOT'
<?php
// one unescaped backslash
$a = "start \' end";
// one escaped + one unescaped backslash
$b = "start \\\' end";
EOT,
null,
['strings_containing_single_quote_chars' => true],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/HeaderCommentFixerTest.php | tests/Fixer/Comment/HeaderCommentFixerTest.php | <?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\Comment;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException;
use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\HeaderCommentFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class HeaderCommentFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(
string $expected,
?string $input,
array $configuration,
?WhitespacesFixerConfig $whitespacesConfig = null
): void {
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, null|string, _AutogeneratedInputConfiguration, 3?: WhitespacesFixerConfig}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
$a;',
'<?php
/**
* new
*/
$a;',
['header' => ''],
];
yield [
'<?php
declare(strict_types=1);
/*
* tmp
*/
namespace A\B;
echo 1;',
'<?php
declare(strict_types=1);namespace A\B;
echo 1;',
[
'header' => 'tmp',
'location' => 'after_declare_strict',
],
];
yield [
'<?php
declare(strict_types=1);
/**
* tmp
*/
namespace A\B;
echo 1;',
'<?php
declare(strict_types=1);
namespace A\B;
echo 1;',
[
'header' => 'tmp',
'location' => 'after_declare_strict',
'separate' => 'bottom',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/*
* tmp
*/
declare(strict_types=1);
namespace A\B;
echo 1;',
'<?php
declare(strict_types=1);
namespace A\B;
echo 1;',
[
'header' => 'tmp',
'location' => 'after_open',
],
];
yield [
'<?php
/*
* new
*/
',
'<?php
/** test */
',
[
'header' => 'new',
'comment_type' => HeaderCommentFixer::HEADER_COMMENT,
],
];
yield [
'<?php
/**
* new
*/
',
'<?php
/* test */
',
[
'header' => 'new',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/**
* def
*/
',
'<?php
',
[
'header' => 'def',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/*
* xyz
*/
$b;',
'<?php
$b;',
['header' => 'xyz'],
];
yield [
'<?php
/*
* xyz123
*/
$a;',
'<?php
$a;',
[
'header' => 'xyz123',
'separate' => 'none',
],
];
yield [
'<?php
/**
* abc
*/
$c;',
'<?php
$c;',
[
'header' => 'abc',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/*
* ghi
*/
$d;',
'<?php
$d;',
[
'header' => 'ghi',
'separate' => 'both',
],
];
yield [
'<?php
/*
* ghi
*/
$d;',
'<?php
$d;',
[
'header' => 'ghi',
'separate' => 'top',
],
];
yield [
'<?php
/*
* tmp
*/
declare(ticks=1);
echo 1;',
'<?php
declare(ticks=1);
echo 1;',
[
'header' => 'tmp',
'location' => 'after_declare_strict',
],
];
yield [
'<?php
/*
* Foo
*/
echo \'bar\';',
'<?php echo \'bar\';',
['header' => 'Foo'],
];
yield [
'<?php
/*
* x
*/
echo \'a\';',
'<?php
/*
* y
* z
*/
echo \'a\';',
['header' => 'x'],
];
yield [
'<?php
/*
* a
* a
*/
echo \'x\';',
'<?php
/*
* b
* c
*/
echo \'x\';',
['header' => "a\na"],
];
yield [
'<?php
/**
* foo
*/
declare(strict_types=1);
namespace A;
echo 1;',
'<?php
declare(strict_types=1);
/**
* foo
*/
namespace A;
echo 1;',
[
'header' => 'foo',
'location' => 'after_open',
'separate' => 'bottom',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/**
* foo
*/
declare(strict_types=1);
/**
* bar
*/
namespace A;
echo 1;',
'<?php
declare(strict_types=1);
/**
* bar
*/
namespace A;
echo 1;',
[
'header' => 'foo',
'location' => 'after_open',
'separate' => 'bottom',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
declare(strict_types=1);
/*
* Foo
*/
namespace SebastianBergmann\Foo;
class Bar
{
}',
'<?php
/*
* Foo
*/
declare(strict_types=1);
namespace SebastianBergmann\Foo;
class Bar
{
}',
[
'header' => 'Foo',
'separate' => 'none',
],
];
yield [
'<?php
/*
* tmp
*/
/**
* Foo class doc.
*/
class Foo {}',
'<?php
/**
* Foo class doc.
*/
class Foo {}',
['header' => 'tmp'],
];
yield [
'<?php
/*
* tmp
*/
class Foo {}',
'<?php
/*
* Foo class doc.
*/
class Foo {}',
['header' => 'tmp'],
];
yield [
'<?php
/**
* tmp1
*/
/**
* Foo class doc.
*/
class Foo {}',
'<?php
/**
* Foo class doc.
*/
class Foo {}',
[
'header' => 'tmp1',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/**
* tmp2
*/
class Foo {}',
'<?php
/**
* tmp2
*/
class Foo {}',
[
'header' => 'tmp2',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield [
'<?php
/*
* tmp3
*/
class Foo {}',
'<?php
/**
* Foo class doc.
*/
class Foo {}',
[
'header' => 'tmp3',
'separate' => 'top',
],
];
yield [
'<?php
/*
* bar
*/
declare(strict_types=1);
// foo
foo();',
'<?php
/*
* foo
*/
declare(strict_types=1);
// foo
foo();',
[
'header' => 'bar',
'location' => 'after_open',
],
];
yield [
'<?php
/*
* bar
*/
declare(strict_types=1);
/* foo */
foo();',
'<?php
/*
* foo
*/
declare(strict_types=1);
/* foo */
foo();',
[
'header' => 'bar',
'location' => 'after_open',
],
];
yield [
'<?php
/*
* tmp4
*/
declare(strict_types=1) ?>',
'<?php
declare(strict_types=1) ?>',
[
'header' => 'tmp4',
'location' => 'after_declare_strict',
],
];
yield [
'#!/usr/bin/env php
<?php
declare(strict_types=1);
/*
* tmp5
*/
namespace A\B;
echo 1;',
'#!/usr/bin/env php
<?php
declare(strict_types=1);namespace A\B;
echo 1;',
[
'header' => 'tmp5',
'location' => 'after_declare_strict',
],
];
yield [
'Short mixed file A
Hello<?php echo "World!"; ?>',
null,
[
'header' => 'tmp',
'location' => 'after_open',
],
];
yield [
'Short mixed file B
<?php echo "Hello"; ?>World!',
null,
[
'header' => 'tmp',
'location' => 'after_open',
],
];
yield [
'File with anything at the beginning and with multiple opening tags are not supported
<?php
echo 1;
?>Hello World!<?php
script_continues_here();',
null,
[
'header' => 'tmp',
'location' => 'after_open',
],
];
$fileHeaderParts = [
<<<'EOF'
This file is part of the xxx.
(c) Foo Bar <foo@bar.com>
EOF,
<<<'EOF'
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF,
];
$fileHeaderComment = implode('', $fileHeaderParts);
$fileHeaderCommentValidator = implode('', [
'/',
preg_quote($fileHeaderParts[0], '/'),
'(?P<EXTRA>.*)??',
preg_quote($fileHeaderParts[1], '/'),
'/s',
]);
yield 'using validator, but existing comment in wrong place - adding new one' => [
'<?php
/*
* This file is part of the xxx.
*
* (c) Foo Bar <foo@bar.com>
*
* This
* is
* sub
* note.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace A;
echo 1;',
'<?php
declare(strict_types=1);
/*
* This file is part of the xxx.
*
* (c) Foo Bar <foo@bar.com>
*
* This
* is
* sub
* note.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace A;
echo 1;',
[
'header' => $fileHeaderComment,
'validator' => $fileHeaderCommentValidator,
'location' => 'after_open',
],
];
yield 'using validator, existing comment matches' => [
'<?php
/*
* This file is part of the xxx.
*
* (c) Foo Bar <foo@bar.com>
*
* This
* is
* sub
* note.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace A;
echo 1;',
null,
[
'header' => $fileHeaderComment,
'validator' => $fileHeaderCommentValidator,
'location' => 'after_open',
],
];
yield 'using validator, existing comment matches but misplaced' => [
'<?php
/**
* This file is part of the xxx.
*
* (c) Foo Bar <foo@bar.com>
*
* This
* is
* sub
* note.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Foo {}',
'<?php
/**
* This file is part of the xxx.
*
* (c) Foo Bar <foo@bar.com>
*
* This
* is
* sub
* note.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Foo {}',
[
'header' => $fileHeaderComment,
'validator' => $fileHeaderCommentValidator,
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield 'default configuration' => [
'<?php
/*
* a
*/
echo 1;',
'<?php
echo 1;',
['header' => 'a'],
];
yield [
'<?php
/*
* a
*/
echo 1;',
'<?php
echo 1;',
[
'header' => 'a',
'comment_type' => HeaderCommentFixer::HEADER_COMMENT,
],
];
yield [
'<?php
/**
* a
*/
echo 1;',
'<?php
echo 1;',
[
'header' => 'a',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
];
yield ["<?php\nphpinfo();\n?>\n<?", null, ['header' => '']];
yield [" <?php\nphpinfo();\n", null, ['header' => '']];
yield ["<?php\nphpinfo();\n?><hr/>", null, ['header' => '']];
yield [" <?php\n", null, ['header' => '']];
yield ['<?= 1?>', null, ['header' => '']];
yield ["<?= 1?><?php\n", null, ['header' => '']];
yield ["<?= 1?>\n<?php\n", null, ['header' => '']];
yield ["<?php\n// comment 1\n?><?php\n// comment 2\n", null, ['header' => '']];
yield [
"<?php\r\ndeclare(strict_types=1);\r\n/**\r\n * whitemess\r\n */\r\n\r\nnamespace A\\B;\r\n\r\necho 1;",
"<?php\r\ndeclare(strict_types=1);\r\n\r\nnamespace A\\B;\r\n\r\necho 1;",
[
'header' => 'whitemess',
'location' => 'after_declare_strict',
'separate' => 'bottom',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
new WhitespacesFixerConfig("\t", "\r\n"),
];
yield [
"<?php\n\n/*\n * Foo\n */\n\necho 1;",
"<?php\necho 1;",
['header' => 'Foo'],
];
yield [
"<?php\r\n\r\n/*\r\n * Foo\r\n */\r\n\r\necho 1;",
"<?php\r\necho 1;",
['header' => 'Foo'],
new WhitespacesFixerConfig(' ', "\r\n"),
];
yield [
"<?php\r\n\r\n/*\r\n * Bar\r\n */\r\n\r\necho 1;",
"<?php\r\necho 1;",
['header' => 'Bar'],
new WhitespacesFixerConfig(' ', "\r\n"),
];
yield [
"<?php\n\n/*\n * Bar\n */\n\necho 1;",
"<?php\necho 1;",
['header' => 'Bar'],
new WhitespacesFixerConfig(' ', "\n"),
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input, array $configuration): void
{
$this->testFix($expected, $input, $configuration);
}
/**
* @return iterable<int, array{string, null|string, _AutogeneratedInputConfiguration}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php
/*
* tmp
*/
/**
* Foo class doc.
*/
enum Foo {}',
'<?php
/**
* Foo class doc.
*/
enum Foo {}',
['header' => 'tmp'],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
* @param class-string<InvalidFixerConfigurationException> $exception
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(
?array $configuration,
string $exceptionMessage,
string $exception = InvalidFixerConfigurationException::class
): void {
$this->expectException($exception);
$this->expectExceptionMessageMatches("#^\\[header_comment\\] {$exceptionMessage}$#");
if (null !== $configuration) {
$this->fixer->configure($configuration);
} else {
$this->doTest('<?php echo 1;');
}
}
/**
* @return iterable<int, array{null|array<array-key, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
null,
'Configuration is required.',
RequiredFixerConfigurationException::class,
];
yield [[], 'Missing required configuration: The required option "header" is missing.'];
yield [
['header' => 1],
'Invalid configuration: The option "header" with value 1 is expected to be of type "string", but is of type "(int|integer)"\.',
];
yield [
[
'header' => '',
'comment_type' => 'foo',
],
'Invalid configuration: The option "comment_type" with value "foo" is invalid\. Accepted values are: "PHPDoc", "comment"\.',
];
yield [
[
'header' => '',
'comment_type' => new \stdClass(),
],
'Invalid configuration: The option "comment_type" with value stdClass is invalid\. Accepted values are: "PHPDoc", "comment"\.',
];
yield [
[
'header' => '',
'location' => new \stdClass(),
],
'Invalid configuration: The option "location" with value stdClass is invalid\. Accepted values are: "after_open", "after_declare_strict"\.',
];
yield [
[
'header' => '',
'separate' => new \stdClass(),
],
'Invalid configuration: The option "separate" with value stdClass is invalid\. Accepted values are: "both", "top", "bottom", "none"\.',
];
yield [
[
'header' => 'Foo',
'validator' => '/\w+++/',
],
'Provided RegEx is not valid.',
];
yield [
[
'header' => '/** test */',
'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
],
'Cannot use \'\*/\' in header\.$',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/SingleLineCommentStyleFixerTest.php | tests/Fixer/Comment/SingleLineCommentStyleFixerTest.php | <?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\Comment;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\SingleLineCommentStyleFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\SingleLineCommentStyleFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Comment\SingleLineCommentStyleFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleLineCommentStyleFixerTest extends AbstractFixerTestCase
{
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->fixer->configure(['abc']);
}
/**
* @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{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
// lonely line
',
'<?php
/* lonely line */
',
['comment_types' => ['asterisk']],
];
yield [
'<?php
// indented line
',
'<?php
/* indented line */
',
['comment_types' => ['asterisk']],
];
yield [
'<?php
// weird-spaced line
',
'<?php
/* weird-spaced line*/
',
['comment_types' => ['asterisk']],
];
yield [
'<?php // start-end',
'<?php /* start-end */',
['comment_types' => ['asterisk']],
];
yield [
"<?php\n \t \n \t // weird indent\n",
"<?php\n \t \n \t /* weird indent */\n",
['comment_types' => ['asterisk']],
];
yield [
"<?php\n// with spaces after\n \t ",
"<?php\n/* with spaces after */ \t \n \t ",
['comment_types' => ['asterisk']],
];
yield [
'<?php
$a = 1; // after code
',
'<?php
$a = 1; /* after code */
',
['comment_types' => ['asterisk']],
];
yield [
'<?php
/* first */ // second
',
'<?php
/* first */ /* second */
',
['comment_types' => ['asterisk']],
];
yield [
'<?php
/* first */// second',
'<?php
/* first *//*
second
*/',
['comment_types' => ['asterisk']],
];
yield [
'<?php
// one line',
'<?php
/*one line
*/',
['comment_types' => ['asterisk']],
];
yield [
'<?php
// one line',
'<?php
/*
one line*/',
['comment_types' => ['asterisk']],
];
yield [
'<?php
// one line',
"<?php
/* \t "."
\t * one line ".'
*
*/',
['comment_types' => ['asterisk']],
];
yield [
'<?php
//',
'<?php
/***
*
*/',
['comment_types' => ['asterisk']],
];
yield [
'<?php
// s',
'<?php
/***
s *
*/',
['comment_types' => ['asterisk']],
];
yield 'empty comment' => [
'<?php
//
',
'<?php
/**/
',
['comment_types' => ['asterisk']],
];
yield [
'<?php
$a = 1; /* in code */ $b = 2;
',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/*
* in code 2
*/ $a = 1;
',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/***
*
*/ $a = 1;',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/***
s *
*/ $a = 1;',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/*
* first line
* second line
*/',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/*
* first line
*
* second line
*/',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/*first line
second line*/',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php /** inline doc comment */',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php
/**
* Doc comment
*/',
null,
['comment_types' => ['asterisk']],
];
yield [
'<?php # test',
null,
['comment_types' => ['asterisk']],
];
yield [
'<h1>This is an <?php //echo 123;?> example</h1>',
'<h1>This is an <?php #echo 123;?> example</h1>',
['comment_types' => ['hash']],
];
yield [
'<?php
// test
',
'<?php
# test
',
['comment_types' => ['hash']],
];
yield [
'<?php
// test1
//test2
// test3
// test 4
',
'<?php
# test1
#test2
# test3
# test 4
',
['comment_types' => ['hash']],
];
yield [
'<?php //',
'<?php #',
['comment_types' => ['hash']],
];
yield [
'<?php
//#test
',
null,
['comment_types' => ['hash']],
];
yield [
'<?php
/*
#test
*/
',
null,
['comment_types' => ['hash']],
];
yield [
'<?php // a',
'<?php # a',
['comment_types' => ['hash']],
];
yield [
'<?php /* start-end */',
null,
['comment_types' => ['hash']],
];
yield [
'<?php function foo(
#[MyAttr([1, 2])] Type $myParam,
) {} // foo',
null,
['comment_types' => ['hash']],
];
yield [
'<?php
// 1
// 2
/*
* 3.a
* 3.b
*/
/**
* 4
*/
// 5
',
'<?php
/* 1 */
/*
* 2
*/
/*
* 3.a
* 3.b
*/
/**
* 4
*/
# 5
',
];
yield [
'<?php
function foo() {
/* ?> */
return "bar";
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/SingleLineCommentSpacingFixerTest.php | tests/Fixer/Comment/SingleLineCommentSpacingFixerTest.php | <?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\Comment;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleLineCommentSpacingFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'comment list' => [
'<?php
// following:
// 1 :
// 2 :
# Test:
# - abc
# - fgh
# Title:
# | abc1
# | xyz
// Point:
// * first point
// * some other point
// Matrix:
// [1,2]
// [3,4]
',
];
yield [
'<?php /* XYZ */',
'<?php /* XYZ */',
];
yield [
'<?php // /',
'<?php ///',
];
yield [
'<?php // //',
'<?php ////',
];
yield 'hash open slash asterisk close' => [
'<?php # A*/',
'<?php #A*/',
];
yield [
"<?php
// a
# b
/* ABC */
// \t d
#\te
/* f */
",
"<?php
//a
#b
/*ABC*/
// \t d
#\te
/* f */
",
];
yield 'do not fix multi line comments' => [
'<?php
/*
*/
/*A
B*/
',
];
yield 'empty double slash' => [
'<?php //',
];
yield 'empty hash' => [
'<?php #',
];
yield [
'<?php /**/',
];
yield [
'<?php /***/',
];
yield 'do not fix PHPDocs' => [
"<?php /**\n*/ /**\nX1*/ /** Y1 */",
];
yield 'do not fix comments looking like PHPDocs' => [
'<?php /**/ /**X1*/ /** Y1 */',
];
yield 'do not fix annotation' => [
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
new
#[Foo]
class extends stdClass {};
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/MultilineCommentOpeningClosingFixerTest.php | tests/Fixer/Comment/MultilineCommentOpeningClosingFixerTest.php | <?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\Comment;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\MultilineCommentOpeningClosingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\MultilineCommentOpeningClosingFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class MultilineCommentOpeningClosingFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php /** Opening DocBlock */'];
yield [
'<?php /* Opening comment */',
'<?php /*** Opening comment */',
];
yield [
'<?php /*\ Opening false-DocBlock */',
'<?php /**\ Opening false-DocBlock */',
];
yield [
'<?php /** Closing DocBlock */',
'<?php /** Closing DocBlock ***/',
];
yield [
'<?php /* Closing comment */',
'<?php /* Closing comment ***/',
];
yield [
'<?php /**/',
'<?php /***/',
];
yield [
'<?php /**/',
'<?php /********/',
];
yield [
<<<'EOT'
<?php
/*
* WUT
*/
EOT,
<<<'EOT'
<?php
/********
* WUT
********/
EOT,
];
yield [
<<<'EOT'
<?php
/*\
* False DocBlock
*/
EOT,
<<<'EOT'
<?php
/**\
* False DocBlock
*/
EOT,
];
yield [
<<<'EOT'
<?php
# Hash
#*** Hash asterisk
// Slash
//*** Slash asterisk
/*
/**
/***
Weird multiline comment
*/
EOT,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/NoEmptyCommentFixerTest.php | tests/Fixer/Comment/NoEmptyCommentFixerTest.php | <?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\Comment;
use PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoEmptyCommentFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
// fix cases
yield [
'<?php
echo 0;
echo 1;
',
'<?php
echo 0;//
echo 1;
',
];
yield [
'<?php
echo 0;
echo 1;
',
'<?php
echo 0;//
echo 1;
',
];
yield [
'<?php
echo 1;
',
'<?php
echo 1;//
',
];
yield [
'<?php
echo 2;
'.'
echo 1;
',
'<?php
echo 2;
//
echo 1;
',
];
yield [
'<?php
?>',
'<?php
//?>',
];
yield [
'<?php
'.'
',
'<?php
//
',
];
yield [
'<?php
'.'
',
'<?php
#
',
];
yield [
'<?php
'.'
',
'<?php
/**/
',
];
yield [
'<?php
echo 0;echo 1;
',
'<?php
echo 0;/**/echo 1;
',
];
yield [
'<?php
echo 0;echo 1;
',
'<?php
echo 0;/**//**//**/echo 1/**/;
',
];
yield [
'<?php
',
'<?php
//',
];
yield [
'<?php
',
'<?php
/*
*/',
];
yield [
"<?php\n \n \n \n \n ",
"<?php\n //\n //\n //\n /**///\n ",
];
yield [
"<?php\r \r \r \r \r ",
"<?php\r //\r //\r //\r /**///\r ",
];
yield [
"<?php\r\n \r\n \r\n \r\n \r\n ",
"<?php\r\n //\r\n //\r\n //\r\n /**///\r\n ",
];
yield [
"<?php\necho 1;\r\recho 2;",
"<?php\necho 1;\r//\recho 2;",
];
// do not fix cases
yield [
'<?php
// a
// /**/
// #
/* b */ // s
# c',
];
yield [
'<?php
// This comment could be nicely formatted.
//
//
// For that, it could have some empty comment lines inside.
//
## A 1
##
##
## A 2
##
// B 1
//
// B 2
## C 1
##
## C 2
$foo = 1;
//
// a
//
$bar = 2;
',
];
yield [
'<?php
'.'
',
'<?php
/*
*
*/
',
];
yield [
'<?php
'.'
',
'<?php
/********
*
********/
',
];
yield [
'<?php /* a */',
'<?php /* *//* a *//* */',
];
yield [
'<?php
'.'
/* a */
'.'
',
'<?php
//
/* a */
//
',
];
}
/**
* @param string $source valid PHP source code
* @param int $startIndex start index of the comment block
* @param int $endIndex expected index of the last token of the block
* @param bool $isEmpty expected value of empty flag returned
*
* @dataProvider provideGetCommentBlockCases
*/
public function testGetCommentBlock(string $source, int $startIndex, int $endIndex, bool $isEmpty): void
{
Tokens::clearCache();
$tokens = Tokens::fromCode($source);
self::assertTrue($tokens[$startIndex]->isComment(), \sprintf('Misconfiguration of test, expected comment token at index "%d".', $startIndex));
$foundInfo = \Closure::bind(static fn (NoEmptyCommentFixer $fixer): array => $fixer->getCommentBlock($tokens, $startIndex), null, NoEmptyCommentFixer::class)($this->fixer);
self::assertSame($startIndex, $foundInfo['blockStart'], 'Find start index of block failed.');
self::assertSame($endIndex, $foundInfo['blockEnd'], 'Find end index of block failed.');
self::assertSame($isEmpty, $foundInfo['isEmpty'], 'Is empty comment block detection failed.');
}
/**
* @return iterable<int, array{string, int, int, bool}>
*/
public static function provideGetCommentBlockCases(): iterable
{
yield [
'<?php // a',
1,
1,
false,
];
yield [
'<?php
// This comment could be nicely formatted.
//
//
// For that, it could have some empty comment lines inside.
// ',
2,
11,
false,
];
yield [
'<?php
/**///',
1,
1,
true,
];
yield [
'<?php
//
//
#
#
',
5,
8,
true,
];
yield [
'<?php
//
//
//
//
',
5,
8,
true,
];
yield [
'<?php
//
//
//
//
',
1,
3,
true,
];
yield [
str_replace("\n", "\r", "<?php\n//\n//\n\n//\n//\n"),
1,
3,
true,
];
yield [
str_replace("\n", "\r\n", "<?php\n//\n//\n\n//\n//\n"),
1,
3,
true,
];
yield [
'<?php
//
//
',
1,
1,
true,
];
yield [
'<?php
//
//
$a; ',
1,
4,
true,
];
yield [
'<?php
//',
1,
1,
true,
];
$src = '<?php
// a2
// /*4*/
// #6
/* b8 */ // s10
# c12';
foreach ([2, 4, 6] as $i) {
yield [$src, $i, 7, false];
}
yield [$src, 8, 8, false];
yield [$src, 10, 11, false];
yield [$src, 12, 12, false];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/CommentToPhpdocFixerTest.php | tests/Fixer/Comment/CommentToPhpdocFixerTest.php | <?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\Comment;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer>
*
* @author Kuba Werłos <werlos@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Comment\CommentToPhpdocFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class CommentToPhpdocFixerTest 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<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php /* header comment */ $foo = true; /* string $bar */ $bar = "baz";',
];
yield [
'<?php /* header comment */ $foo = true; /* $yoda string @var */',
];
yield [
'<?php /* header comment */ $foo = true; /* $yoda @var string */',
];
yield [
'<?php /* header comment */ $foo = true; /** @var string $bar */ $bar = "baz";',
'<?php /* header comment */ $foo = true; /* @var string $bar */ $bar = "baz";',
];
yield [
'<?php /* header comment */ $foo = true; /** @var string $bar */ $bar = "baz";',
'<?php /* header comment */ $foo = true; /*@var string $bar */ $bar = "baz";',
];
yield [
'<?php /* header comment */ $foo = true;
/** @var string $bar */
$bar = "baz";
',
'<?php /* header comment */ $foo = true;
/*** @var string $bar */
$bar = "baz";
',
];
yield [
'<?php /* header comment */ $foo = true;
/** @var string $bar */
$bar = "baz";
',
'<?php /* header comment */ $foo = true;
// @var string $bar
$bar = "baz";
',
];
yield [
'<?php /* header comment */ $foo = true;
/** @var string $bar */
$bar = "baz";
',
'<?php /* header comment */ $foo = true;
//@var string $bar
$bar = "baz";
',
];
yield [
'<?php /* header comment */ $foo = true;
/** @var string $bar */
$bar = "baz";
',
'<?php /* header comment */ $foo = true;
# @var string $bar
$bar = "baz";
',
];
yield [
'<?php /* header comment */ $foo = true;
/** @var string $bar */
$bar = "baz";
',
'<?php /* header comment */ $foo = true;
#@var string $bar
$bar = "baz";
',
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* @var string $bar
*/
$bar = "baz";
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
/*
* @var string $bar
*/
$bar = "baz";
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* This is my var
* @var string $foo
* stop using it
* @deprecated since 1.2
*/
$foo = 1;
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
// This is my var
// @var string $foo
// stop using it
// @deprecated since 1.2
$foo = 1;
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
for (;;) {
/**
* This is my var
* @var string $foo
*/
$foo = someValue();
}
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
for (;;) {
// This is my var
// @var string $foo
$foo = someValue();
}
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* This is my var
* @var string $foo
* stop using it
* @deprecated since 1.3
*/
$foo = 1;
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
# This is my var
# @var string $foo
# stop using it
# @deprecated since 1.3
$foo = 1;
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* @Column(type="string", length=32, unique=true, nullable=false)
*/
$bar = 'baz';
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
/*
* @Column(type="string", length=32, unique=true, nullable=false)
*/
$bar = 'baz';
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* @ORM\Column(name="id", type="integer")
*/
$bar = 42;
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
/*
* @ORM\Column(name="id", type="integer")
*/
$bar = 42;
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
// This is my var
// /** @var string $foo */
$foo = 1;
EOT,
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
// @todo do something later
$foo = 1;
EOT,
null,
['ignored_tags' => ['todo']],
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
// @TODO do something later
$foo = 1;
EOT,
null,
['ignored_tags' => ['todo']],
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* @todo do something later
* @var int $foo
*/
$foo = 1;
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
// @todo do something later
// @var int $foo
$foo = 1;
EOT,
['ignored_tags' => ['todo']],
];
yield [
<<<'EOT'
<?php /* header comment */ $foo = true;
/**
* @var int $foo
* @todo do something later
*/
$foo = 1;
EOT,
<<<'EOT'
<?php /* header comment */ $foo = true;
// @var int $foo
// @todo do something later
$foo = 1;
EOT,
['ignored_tags' => ['todo']],
];
yield [
'<?php // header
/** /@foo */
namespace Foo\Bar;
',
'<?php // header
///@foo
namespace Foo\Bar;
',
];
yield [
'<?php // header
/**
* / @foo
* / @bar
*/
namespace Foo\Bar;
',
'<?php // header
/// @foo
/// @bar
namespace Foo\Bar;
',
];
yield [
'<?php /* header comment */ $foo = true; class Foo { /** @phpstan-use Bar<Baz> $bar */ use Bar; }',
'<?php /* header comment */ $foo = true; class Foo { /* @phpstan-use Bar<Baz> $bar */ use Bar; }',
];
yield [
'<?php
$foo = new class extends Foo { // @phpstan-ignore method.internal
/** @foo */
public function m(): void {}
};
',
'<?php
$foo = new class extends Foo { // @phpstan-ignore method.internal
// @foo
public function m(): void {}
};
',
];
yield [
'<?php
$foo = new class extends Foo { // @psalm-suppress all
/** @foo */
public function m(): void {}
};
',
'<?php
$foo = new class extends Foo { // @psalm-suppress all
// @foo
public function m(): void {}
};
',
];
yield [
<<<'EOT'
<?php
$foo = new class extends Foo { /**
* @phpstan-return void
* @foo
*/
public function m(): void {}
};
EOT,
<<<'EOT'
<?php
$foo = new class extends Foo { // @phpstan-return void
// @foo
public function m(): void {}
};
EOT,
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility' => [
<<<'PHP'
<?php class Foo {
/** @var int */
public(set) int $a;
/** @var int */
protected(set) int $b;
/** @var int */
private(set) int $c;
}
PHP,
<<<'PHP'
<?php class Foo {
/* @var int */
public(set) int $a;
/* @var int */
protected(set) int $b;
/* @var int */
private(set) int $c;
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Comment/NoTrailingWhitespaceInCommentFixerTest.php | tests/Fixer/Comment/NoTrailingWhitespaceInCommentFixerTest.php | <?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\Comment;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoTrailingWhitespaceInCommentFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/*
//
//
//
//
//
//
//
//
*/
',
'<?php
/*
//
//
'.'
//
//
//
'.'
//
//
'.'
//
*/
',
];
yield [
'<?php
// This is'.'
//'.'
//'.'
// multiline comment.
//',
'<?php
// This is '.'
// '.'
// '.'
// multiline comment. '.'
// ',
];
yield [
'<?php
/*
* This is another'.'
*'.'
*'.'
* multiline comment.'.'
*/',
'<?php
/* '.'
* This is another '.'
* '.'
* '.'
* multiline comment. '.'
*/',
];
yield [
'<?php
/**
* Summary'.'
*'.'
*'.'
* Description.'.'
*
* @annotation
* Foo
*/',
'<?php
/** '.'
* Summary '.'
* '.'
* '.'
* Description. '.'
* '.'
* @annotation '.'
* Foo '.'
*/',
];
yield [
str_replace(
"\n",
"\r\n",
'<?php
/**
* Summary
*'.'
* Description
*/',
),
str_replace(
"\n",
"\r\n",
'<?php
/**
* Summary
* '.'
* Description
*/',
),
];
yield [
str_replace(
"\n",
"\r",
'<?php
/**
* Summary
*'.'
* Description
*/',
),
str_replace(
"\n",
"\r",
'<?php
/**
* Summary
* '.'
* Description
*/',
),
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/DoctrineAnnotation/DoctrineAnnotationIndentationFixerTest.php | tests/Fixer/DoctrineAnnotation/DoctrineAnnotationIndentationFixerTest.php | <?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\DoctrineAnnotation;
use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
* @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
* @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationIndentationFixer
*
* @extends AbstractDoctrineAnnotationFixerTestCase<\PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationIndentationFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationIndentationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DoctrineAnnotationIndentationFixerTest extends AbstractDoctrineAnnotationFixerTestCase
{
/**
* @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<int, array{0: string, 1?: null|string}>
*/
public static function provideFixCases(): iterable
{
$commentCases = [
['
/**
* Foo.
*
* @author John Doe
*
* @Foo
* @Bar
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Foo
* @Bar
*/'],
['
/**
* @Foo(
* foo="foo"
* )
*/', '
/**
* @Foo(
* foo="foo"
* )
*/'],
['
/**
* @Foo(foo="foo", bar={
* "foo": 1,
* "foobar": 2,
* "foobarbaz": 3
* })
*/', '
/**
* @Foo(foo="foo", bar={
* "foo": 1,
* "foobar": 2,
* "foobarbaz": 3
* })
*/'],
['
/**
* @Foo(@Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* }))
*/', '
/**
* @Foo(@Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* }))
*/'],
['
/**
* @Foo(
* @Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* })
* )
*/', '
/**
* @Foo(
* @Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* })
* )
*/'],
['
/**
* @Foo(
* @Bar(
* "baz"
* )
*/'],
['
/**
* Foo(
* Bar()
* "baz"
* )
*/'],
['
/**
* @Foo( @Bar(
* "baz"
* ) )
*/', '
/**
* @Foo( @Bar(
* "baz"
* ) )
*/'],
['
/**
* @Foo(x={
* @Bar
* })
* @Foo\z
*/', '
/**
* @Foo(x={
* @Bar
* })
* @Foo\z
*/'],
['
/**
* Description with a single " character.
*
* @Foo(
* "string "" with inner quote"
* )
*
* @param mixed description with a single " character.
*/', '
/**
* Description with a single " character.
*
* @Foo(
* "string "" with inner quote"
* )
*
* @param mixed description with a single " character.
*/'],
['
/**
* @Foo(@Bar,
* @Baz)
* @Qux
*/', '
/**
* @Foo(@Bar,
* @Baz)
* @Qux
*/'],
['
/**
* @Foo({"bar",
* "baz"})
* @Qux
*/', '
/**
* @Foo({"bar",
* "baz"})
* @Qux
*/'],
['
/**
* // PHPDocumentor 1
* @abstract
* @access
* @code
* @deprec
* @encode
* @exception
* @final
* @ingroup
* @inheritdoc
* @inheritDoc
* @magic
* @name
* @toc
* @tutorial
* @private
* @static
* @staticvar
* @staticVar
* @throw
*
* // PHPDocumentor 2
* @api
* @author
* @category
* @copyright
* @deprecated
* @example
* @filesource
* @global
* @ignore
* @internal
* @license
* @link
* @method
* @package
* @param
* @property
* @property-read
* @property-write
* @return
* @see
* @since
* @source
* @subpackage
* @throws
* @todo
* @TODO
* @usedBy
* @uses
* @var
* @version
*
* // PHPUnit
* @after
* @afterClass
* @backupGlobals
* @backupStaticAttributes
* @before
* @beforeClass
* @codeCoverageIgnore
* @codeCoverageIgnoreStart
* @codeCoverageIgnoreEnd
* @covers
* @coversDefaultClass
* @coversNothing
* @dataProvider
* @depends
* @expectedException
* @expectedExceptionCode
* @expectedExceptionMessage
* @expectedExceptionMessageRegExp
* @group
* @large
* @medium
* @preserveGlobalState
* @requires
* @runTestsInSeparateProcesses
* @runInSeparateProcess
* @small
* @test
* @testdox
* @ticket
* @uses
*
* // PHPCheckStyle
* @SuppressWarnings
*
* // PHPStorm
* @noinspection
*
* // PEAR
* @package_version
*
* // PlantUML
* @enduml
* @startuml
*
* // other
* @fix
* @FIXME
* @fixme
* @override
*/'],
['
/**
* @Foo({
* @Bar()}
* )
*/', '
/**
* @Foo({
* @Bar()}
* )
*/'],
['
/**
* @Foo(foo={
* "bar": 1,
* }, baz={
* "qux": 2,
* })
*/
'],
['
/**
* @Foo(foo={
* "foo",
* }, bar={ "bar" }, baz={
* "baz"
* })
*/
'],
];
yield from self::createTestCases($commentCases);
yield from self::createTestCases($commentCases, ['indent_mixed_lines' => false]);
yield [
'<?php
/**
* @see \User getId()
*/
',
];
yield [
'<?php
/**
* @see \User getId()
*/
',
null,
['indent_mixed_lines' => false],
];
yield from self::createTestCases(
[
['
/**
* Foo.
*
* @author John Doe
*
* @Foo
* @Bar
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Foo
* @Bar
*/'],
['
/**
* @Foo(
* foo="foo"
* )
*/', '
/**
* @Foo(
* foo="foo"
* )
*/'],
['
/**
* @Foo(foo="foo", bar={
* "foo": 1,
* "foobar": 2,
* "foobarbaz": 3
* })
*/', '
/**
* @Foo(foo="foo", bar={
* "foo": 1,
* "foobar": 2,
* "foobarbaz": 3
* })
*/'],
['
/**
* @Foo(@Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* }))
*/', '
/**
* @Foo(@Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* }))
*/'],
['
/**
* @Foo(
* @Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* })
* )
*/', '
/**
* @Foo(
* @Bar({
* "FOO": 1,
* "BAR": 2,
* "BAZ": 3
* })
* )
*/'],
['
/**
* @Foo(
* @Bar(
* "baz"
* )
*/'],
['
/**
* Foo(
* Bar()
* "baz"
* )
*/'],
['
/**
* @Foo( @Bar(
* "baz"
* ) )
*/', '
/**
* @Foo( @Bar(
* "baz"
* ) )
*/'],
['
/**
* @Foo(x={
* @Bar
* })
* @Foo\z
*/', '
/**
* @Foo(x={
* @Bar
* })
* @Foo\z
*/'],
['
/**
* Description with a single " character.
*
* @Foo(
* "string "" with inner quote"
* )
*
* @param mixed description with a single " character.
*/', '
/**
* Description with a single " character.
*
* @Foo(
* "string "" with inner quote"
* )
*
* @param mixed description with a single " character.
*/'],
['
/**
* @Foo(@Bar,
* @Baz)
* @Qux
*/', '
/**
* @Foo(@Bar,
* @Baz)
* @Qux
*/'],
['
/**
* @Foo({"bar",
* "baz"})
* @Qux
*/', '
/**
* @Foo({"bar",
* "baz"})
* @Qux
*/'],
['
/**
* // PHPDocumentor 1
* @abstract
* @access
* @code
* @deprec
* @encode
* @exception
* @final
* @ingroup
* @inheritdoc
* @inheritDoc
* @magic
* @name
* @toc
* @tutorial
* @private
* @static
* @staticvar
* @staticVar
* @throw
*
* // PHPDocumentor 2
* @api
* @author
* @category
* @copyright
* @deprecated
* @example
* @filesource
* @global
* @ignore
* @internal
* @license
* @link
* @method
* @package
* @param
* @property
* @property-read
* @property-write
* @return
* @see
* @since
* @source
* @subpackage
* @throws
* @todo
* @TODO
* @usedBy
* @uses
* @var
* @version
*
* // PHPUnit
* @after
* @afterClass
* @backupGlobals
* @backupStaticAttributes
* @before
* @beforeClass
* @codeCoverageIgnore
* @codeCoverageIgnoreStart
* @codeCoverageIgnoreEnd
* @covers
* @coversDefaultClass
* @coversNothing
* @dataProvider
* @depends
* @expectedException
* @expectedExceptionCode
* @expectedExceptionMessage
* @expectedExceptionMessageRegExp
* @group
* @large
* @medium
* @preserveGlobalState
* @requires
* @runTestsInSeparateProcesses
* @runInSeparateProcess
* @small
* @test
* @testdox
* @ticket
* @uses
*
* // PHPCheckStyle
* @SuppressWarnings
*
* // PHPStorm
* @noinspection
*
* // PEAR
* @package_version
*
* // PlantUML
* @enduml
* @startuml
*
* // other
* @fix
* @FIXME
* @fixme
* @override
*/'],
['
/**
* @Foo({
* @Bar()}
* )
*/'],
],
['indent_mixed_lines' => true],
);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/DoctrineAnnotation/DoctrineAnnotationBracesFixerTest.php | tests/Fixer/DoctrineAnnotation/DoctrineAnnotationBracesFixerTest.php | <?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\DoctrineAnnotation;
use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
* @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
* @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationBracesFixer
*
* @extends AbstractDoctrineAnnotationFixerTestCase<\PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationBracesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationBracesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DoctrineAnnotationBracesFixerTest extends AbstractDoctrineAnnotationFixerTestCase
{
/**
* @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<int, array{0: string, 1?: null|string}>
*/
public static function provideFixCases(): iterable
{
yield from self::createTestCases(
[
['
/**
* @Foo()
*/'],
['
/**
* @Foo ()
*/'],
['
/**
* @Foo
* (
* )
*/'],
['
/**
* Foo.
*
* @author John Doe
*
* @Foo()
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Foo
*/'],
[
'/** @Foo() */',
'/** @Foo */',
],
['
/**
* @Foo(@Bar())
*/', '
/**
* @Foo(@Bar)
*/'],
['
/**
* @Foo(
* @Bar()
* )
*/', '
/**
* @Foo(
* @Bar
* )
*/'],
['
/**
* @Foo(
* @Bar(),
* "baz"
* )
*/', '
/**
* @Foo(
* @Bar,
* "baz"
* )
*/'],
['
/**
* @Foo(
* @Bar\Baz()
* )
*/', '
/**
* @Foo(
* @Bar\Baz
* )
*/'],
['
/**
* @Foo() @Bar\Baz()
*/', '
/**
* @Foo @Bar\Baz
*/'],
['
/**
* @Foo("@Bar")
*/'],
['
/**
* Description with a single " character.
*
* @Foo("string "" with inner quote")
*
* @param mixed description with a single " character.
*/'],
['
/**
* @Foo(@Bar
*/'],
['
/**
* @Foo())@Bar)
*/', '
/**
* @Foo)@Bar)
*/'],
['
/**
* See {@link https://help Help} or {@see BarClass} for details.
*/'],
['
/**
* @var int
*/'],
['
/**
* // PHPDocumentor 1
* @abstract
* @access
* @code
* @deprec
* @encode
* @exception
* @final
* @ingroup
* @inheritdoc
* @inheritDoc
* @magic
* @name
* @toc
* @tutorial
* @private
* @static
* @staticvar
* @staticVar
* @throw
*
* // PHPDocumentor 2
* @api
* @author
* @category
* @copyright
* @deprecated
* @example
* @filesource
* @global
* @ignore
* @internal
* @license
* @link
* @method
* @package
* @param
* @property
* @property-read
* @property-write
* @return
* @see
* @since
* @source
* @subpackage
* @throws
* @todo
* @TODO
* @usedBy
* @uses
* @var
* @version
*
* // PHPUnit
* @after
* @afterClass
* @backupGlobals
* @backupStaticAttributes
* @before
* @beforeClass
* @codeCoverageIgnore
* @codeCoverageIgnoreStart
* @codeCoverageIgnoreEnd
* @covers
* @coversDefaultClass
* @coversNothing
* @dataProvider
* @depends
* @expectedException
* @expectedExceptionCode
* @expectedExceptionMessage
* @expectedExceptionMessageRegExp
* @group
* @large
* @medium
* @preserveGlobalState
* @requires
* @runTestsInSeparateProcesses
* @runInSeparateProcess
* @small
* @test
* @testdox
* @ticket
* @uses
*
* // PHPCheckStyle
* @SuppressWarnings
*
* // PHPStorm
* @noinspection
*
* // PEAR
* @package_version
*
* // PlantUML
* @enduml
* @startuml
*
* // Psalm
* @psalm
* @psalm-param
*
* // PHPStan
* @phpstan
* @phpstan-param
*
* // other
* @fix
* @FIXME
* @fixme
* @fixme: foo
* @override
* @todo: foo
*/'],
],
['syntax' => 'with_braces'],
);
yield [
'<?php
/**
* @see \User getId()
*/
',
];
yield from self::createTestCases(
[
['
/**
* Foo.
*
* @author John Doe
*
* @Baz\Bar
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Baz\Bar ( )
*/'],
[
'/** @Foo */',
'/** @Foo () */',
],
['
/**
* @Foo("bar")
*/'],
['
/**
* @Foo
*/', '
/**
* @Foo
* (
* )
*/'],
['
/**
* @Foo(@Bar)
*/', '
/**
* @Foo(@Bar())
*/'],
['
/**
* @Foo(
* @Bar
* )
*/', '
/**
* @Foo(
* @Bar()
* )
*/'],
['
/**
* @Foo(
* @Bar,
* "baz"
* )
*/', '
/**
* @Foo(
* @Bar(),
* "baz"
* )
*/'],
['
/**
* @Foo(
* @Bar\Baz
* )
*/', '
/**
* @Foo(
* @Bar\Baz()
* )
*/'],
['
/**
* @Foo @Bar\Baz
*/', '
/**
* @Foo() @Bar\Baz()
*/'],
['
/**
* @\Foo @\Bar\Baz
*/', '
/**
* @\Foo() @\Bar\Baz()
*/'],
['
/**
* @Foo("@Bar()")
*/'],
['
/**
* Description with a single " character.
*
* @Foo("string "" with inner quote")
*
* @param mixed description with a single " character.
*/'],
['
/**
* @Foo(
*/'],
['
/**
* @Foo)
*/'],
['
/**
* @Foo(@Bar()
*/'],
['
/**
* @Foo
* @Bar
* @Baz
*/', '
/**
* @Foo()
* @Bar()
* @Baz()
*/'],
['
/**
* @FIXME ()
* @fixme ()
* @TODO ()
* @todo ()
*/'],
['
/**
* // PHPDocumentor 1
* @abstract()
* @access()
* @code()
* @deprec()
* @encode()
* @exception()
* @final()
* @ingroup()
* @inheritdoc()
* @inheritDoc()
* @magic()
* @name()
* @toc()
* @tutorial()
* @private()
* @static()
* @staticvar()
* @staticVar()
* @throw()
*
* // PHPDocumentor 2
* @api()
* @author()
* @category()
* @copyright()
* @deprecated()
* @example()
* @filesource()
* @global()
* @ignore()
* @internal()
* @license()
* @link()
* @method()
* @package()
* @param()
* @property()
* @property-read()
* @property-write()
* @return()
* @see()
* @since()
* @source()
* @subpackage()
* @throws()
* @todo()
* @TODO()
* @usedBy()
* @uses()
* @var()
* @version()
*
* // PHPUnit
* @after()
* @afterClass()
* @backupGlobals()
* @backupStaticAttributes()
* @before()
* @beforeClass()
* @codeCoverageIgnore()
* @codeCoverageIgnoreStart()
* @codeCoverageIgnoreEnd()
* @covers()
* @coversDefaultClass()
* @coversNothing()
* @dataProvider()
* @depends()
* @expectedException()
* @expectedExceptionCode()
* @expectedExceptionMessage()
* @expectedExceptionMessageRegExp()
* @group()
* @large()
* @medium()
* @preserveGlobalState()
* @requires()
* @runTestsInSeparateProcesses()
* @runInSeparateProcess()
* @small()
* @test()
* @testdox()
* @ticket()
* @uses()
*
* // PHPCheckStyle
* @SuppressWarnings()
*
* // PHPStorm
* @noinspection()
*
* // PEAR
* @package_version()
*
* // PlantUML
* @enduml()
* @startuml()
*
* // Psalm
* @psalm()
* @psalm-param()
*
* // PHPStan
* @phpstan()
* @psalm-param()
*
*
* // other
* @fix()
* @FIXME()
* @fixme()
* @fixme: foo()
* @override()
* @todo: foo()
*/'],
],
['syntax' => 'without_braces'],
);
yield [
'<?php
/**
* @see \User getId()
*/
',
null,
['syntax' => 'without_braces'],
];
}
/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix82Cases(): iterable
{
yield [
'<?php
/**
* @author John Doe
*
* @Baz\Bar
*/
readonly class FooClass{}',
'<?php
/**
* @author John Doe
*
* @Baz\Bar ( )
*/
readonly class FooClass{}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/DoctrineAnnotation/DoctrineAnnotationArrayAssignmentFixerTest.php | tests/Fixer/DoctrineAnnotation/DoctrineAnnotationArrayAssignmentFixerTest.php | <?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\DoctrineAnnotation;
use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
* @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
* @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer
*
* @extends AbstractDoctrineAnnotationFixerTestCase<\PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DoctrineAnnotationArrayAssignmentFixerTest extends AbstractDoctrineAnnotationFixerTestCase
{
/**
* @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<int, array{0: string, 1?: null|string}>
*/
public static function provideFixCases(): iterable
{
$commentCases = [
['
/**
* @Foo
*/',
],
['
/**
* @Foo()
*/',
],
['
/**
* @Foo(bar="baz")
*/',
],
[
'
/**
* @Foo({bar="baz"})
*/',
'
/**
* @Foo({bar:"baz"})
*/',
],
[
'
/**
* @Foo({bar = "baz"})
*/',
'
/**
* @Foo({bar : "baz"})
*/',
],
['
/**
* See {@link https://help Help} or {@see BarClass} for details.
*/',
],
];
yield from self::createTestCases($commentCases);
yield from self::createTestCases($commentCases, ['operator' => '=']);
yield [
'<?php
/**
* @see \User getId()
*/
',
];
yield [
'<?php
/**
* @see \User getId()
*/
',
null,
['operator' => '='],
];
yield from self::createTestCases(
[
['
/**
* @Foo
*/',
],
['
/**
* @Foo()
*/',
],
[
'
/**
* @Foo(bar:"baz")
*/',
],
[
'
/**
* @Foo({bar:"baz"})
*/',
'
/**
* @Foo({bar="baz"})
*/',
],
[
'
/**
* @Foo({bar : "baz"})
*/',
'
/**
* @Foo({bar = "baz"})
*/',
],
[
'
/**
* @Foo(foo="bar", {bar:"baz"})
*/',
'
/**
* @Foo(foo="bar", {bar="baz"})
*/',
],
['
/**
* See {@link https://help Help} or {@see BarClass} for details.
*/'],
],
['operator' => ':'],
);
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php class FooClass{
/**
* @Foo({bar = "baz"})
*/
private readonly Foo $foo;
}',
'<?php class FooClass{
/**
* @Foo({bar : "baz"})
*/
private readonly Foo $foo;
}',
];
yield [
'<?php class FooClass{
/**
* @Foo({bar = "baz"})
*/
readonly private Foo $foo;
}',
'<?php class FooClass{
/**
* @Foo({bar : "baz"})
*/
readonly private Foo $foo;
}',
];
yield [
'<?php class FooClass{
/**
* @Foo({bar = "baz"})
*/
readonly Foo $foo;
}',
'<?php class FooClass{
/**
* @Foo({bar : "baz"})
*/
readonly Foo $foo;
}',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility' => [
<<<'PHP'
<?php class Entity {
/**
* @Foo({foo = "foo"})
*/
public public(set) Foo $foo;
/**
* @Bar({bar = "bar"})
*/
public protected(set) Bar $bar;
/**
* @Baz({baz = "baz"})
*/
protected private(set) Baz $baz;
}
PHP,
<<<'PHP'
<?php class Entity {
/**
* @Foo({foo : "foo"})
*/
public public(set) Foo $foo;
/**
* @Bar({bar : "bar"})
*/
public protected(set) Bar $bar;
/**
* @Baz({baz : "baz"})
*/
protected private(set) Baz $baz;
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/DoctrineAnnotation/DoctrineAnnotationSpacesFixerTest.php | tests/Fixer/DoctrineAnnotation/DoctrineAnnotationSpacesFixerTest.php | <?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\DoctrineAnnotation;
use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
* @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
* @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationSpacesFixer
*
* @extends AbstractDoctrineAnnotationFixerTestCase<\PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationSpacesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationSpacesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DoctrineAnnotationSpacesFixerTest extends AbstractDoctrineAnnotationFixerTestCase
{
/**
* @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<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield from self::createMultipleTestCase(
[
['
/**
* @Foo
*/'],
['
/**
* @Foo()
*/'],
['
/**
* Foo.
*
* @author John Doe
*
* @Foo(foo="foo", bar="bar")
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Foo ( foo = "foo" ,bar = "bar" )
*/'],
['
/**
* @Foo(
* foo="foo",
* bar="bar"
* )
*/', '
/**
* @Foo (
* foo = "foo" ,
* bar = "bar"
* )
*/'],
['
/**
* @Foo(
* @Bar("foo", "bar"),
* @Baz
* )
*/', '
/**
* @Foo(
* @Bar ( "foo" ,"bar") ,
* @Baz
* )
*/'],
['
/**
* @Foo({"bar", "baz"})
*/', '
/**
* @Foo( {"bar" ,"baz"} )
*/'],
['
/**
* @Foo(foo="=foo", bar={"foo" : "=foo", "bar" = "=bar"})
*/', '
/**
* @Foo(foo = "=foo" ,bar = {"foo" : "=foo", "bar"="=bar"})
*/'],
[
'/** @Foo(foo="foo", bar={"foo" : "foo", "bar" = "bar"}) */',
'/** @Foo ( foo = "foo" ,bar = {"foo" : "foo", "bar"="bar"} ) */',
],
['
/**
* @Foo(
* foo="foo",
* bar={
* "foo" : "foo",
* "bar" = "bar"
* }
* )
*/', '
/**
* @Foo(
* foo = "foo"
* ,
* bar = {
* "foo":"foo",
* "bar"="bar"
* }
* )
*/'],
['
/**
* @Foo(
* foo="foo",
* bar={
* "foo" : "foo",
* "bar" = "bar"
* }
* )
*/', '
/**
* @Foo
* (
* foo
* =
* "foo",
* bar
* =
* {
* "foo"
* :
* "foo",
* "bar"
* =
* "bar"
* }
* )
*/'],
['
/**
* @Foo(foo="foo", "bar"=@Bar\Baz({"foo" : true, "bar" = false}))
*/', '
/**
* @Foo ( foo = "foo", "bar" = @Bar\Baz({"foo":true, "bar"=false}) )
*/'],
['
/**
* @Foo(foo = "foo" ,bar="bar"
*/'],
['
/**
* Comment , with a comma.
*/'],
['
/**
* Description with a single " character.
*
* @Foo(foo="string "" with inner quote", bar="string "" with inner quote")
*
* @param mixed description with a single " character.
*/', '
/**
* Description with a single " character.
*
* @Foo( foo="string "" with inner quote" ,bar="string "" with inner quote" )
*
* @param mixed description with a single " character.
*/'],
['
/**
* // PHPDocumentor 1
* @abstract ( foo,bar = "baz" )
* @access ( foo,bar = "baz" )
* @code ( foo,bar = "baz" )
* @deprec ( foo,bar = "baz" )
* @encode ( foo,bar = "baz" )
* @exception ( foo,bar = "baz" )
* @final ( foo,bar = "baz" )
* @ingroup ( foo,bar = "baz" )
* @inheritdoc ( foo,bar = "baz" )
* @inheritDoc ( foo,bar = "baz" )
* @magic ( foo,bar = "baz" )
* @name ( foo,bar = "baz" )
* @toc ( foo,bar = "baz" )
* @tutorial ( foo,bar = "baz" )
* @private ( foo,bar = "baz" )
* @static ( foo,bar = "baz" )
* @staticvar ( foo,bar = "baz" )
* @staticVar ( foo,bar = "baz" )
* @throw ( foo,bar = "baz" )
*
* // PHPDocumentor 2
* @api ( foo,bar = "baz" )
* @author ( foo,bar = "baz" )
* @category ( foo,bar = "baz" )
* @copyright ( foo,bar = "baz" )
* @deprecated ( foo,bar = "baz" )
* @example ( foo,bar = "baz" )
* @filesource ( foo,bar = "baz" )
* @global ( foo,bar = "baz" )
* @ignore ( foo,bar = "baz" )
* @internal ( foo,bar = "baz" )
* @license ( foo,bar = "baz" )
* @link ( foo,bar = "baz" )
* @method ( foo,bar = "baz" )
* @package ( foo,bar = "baz" )
* @param ( foo,bar = "baz" )
* @property ( foo,bar = "baz" )
* @property-read ( foo,bar = "baz" )
* @property-write ( foo,bar = "baz" )
* @return ( foo,bar = "baz" )
* @see ( foo,bar = "baz" )
* @since ( foo,bar = "baz" )
* @source ( foo,bar = "baz" )
* @subpackage ( foo,bar = "baz" )
* @throws ( foo,bar = "baz" )
* @todo ( foo,bar = "baz" )
* @TODO ( foo,bar = "baz" )
* @usedBy ( foo,bar = "baz" )
* @uses ( foo,bar = "baz" )
* @var ( foo,bar = "baz" )
* @version ( foo,bar = "baz" )
*
* // PHPUnit
* @after ( foo,bar = "baz" )
* @afterClass ( foo,bar = "baz" )
* @backupGlobals ( foo,bar = "baz" )
* @backupStaticAttributes ( foo,bar = "baz" )
* @before ( foo,bar = "baz" )
* @beforeClass ( foo,bar = "baz" )
* @codeCoverageIgnore ( foo,bar = "baz" )
* @codeCoverageIgnoreStart ( foo,bar = "baz" )
* @codeCoverageIgnoreEnd ( foo,bar = "baz" )
* @covers ( foo,bar = "baz" )
* @coversDefaultClass ( foo,bar = "baz" )
* @coversNothing ( foo,bar = "baz" )
* @dataProvider ( foo,bar = "baz" )
* @depends ( foo,bar = "baz" )
* @expectedException ( foo,bar = "baz" )
* @expectedExceptionCode ( foo,bar = "baz" )
* @expectedExceptionMessage ( foo,bar = "baz" )
* @expectedExceptionMessageRegExp ( foo,bar = "baz" )
* @group ( foo,bar = "baz" )
* @large ( foo,bar = "baz" )
* @medium ( foo,bar = "baz" )
* @preserveGlobalState ( foo,bar = "baz" )
* @requires ( foo,bar = "baz" )
* @runTestsInSeparateProcesses ( foo,bar = "baz" )
* @runInSeparateProcess ( foo,bar = "baz" )
* @small ( foo,bar = "baz" )
* @test ( foo,bar = "baz" )
* @testdox ( foo,bar = "baz" )
* @ticket ( foo,bar = "baz" )
* @uses ( foo,bar = "baz" )
*
* // PHPCheckStyle
* @SuppressWarnings ( foo,bar = "baz" )
*
* // PHPStorm
* @noinspection ( foo,bar = "baz" )
*
* // PEAR
* @package_version ( foo,bar = "baz" )
*
* // PlantUML
* @enduml ( foo,bar = "baz" )
* @startuml ( foo,bar = "baz" )
*
* // other
* @fix ( foo,bar = "baz" )
* @FIXME ( foo,bar = "baz" )
* @fixme ( foo,bar = "baz" )
* @override
*/'],
['
/**
* @Transform /^(\d+)$/
*/'],
],
[
[],
[
'around_parentheses' => true,
'around_commas' => true,
'before_argument_assignments' => false,
'after_argument_assignments' => false,
'before_array_assignments_equals' => true,
'after_array_assignments_equals' => true,
'before_array_assignments_colon' => true,
'after_array_assignments_colon' => true,
],
],
);
yield [
'<?php
/**
* @see \User getId()
*/
',
];
yield from self::createMultipleTestCase(
[
['
/**
* @Foo
*/'],
['
/**
* Foo.
*
* @author John Doe
*
* @Foo()
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Foo ( )
*/'],
['
/**
* @Foo("bar")
*/', '
/**
* @Foo( "bar" )
*/'],
['
/**
* @Foo("bar", "baz")
*/', '
/**
* @Foo( "bar", "baz" )
*/'],
[
'/** @Foo("bar", "baz") */',
'/** @Foo( "bar", "baz" ) */',
],
['
/**
* @Foo("bar", "baz")
*/', '
/**
* @Foo( "bar", "baz" )
*/'],
['
/**
* @Foo("bar", "baz")
*/', '
/**
* @Foo ( "bar", "baz" )
*/'],
['
/**
* @Foo(
* "bar",
* "baz"
* )
*/', '
/**
* @Foo
* (
* "bar",
* "baz"
* )
*/'],
['
/**
* @Foo(
* @Bar("baz")
* )
*/', '
/**
* @Foo
* (
* @Bar ( "baz" )
* )
*/'],
['
/**
* @Foo ( @Bar ( "bar" )
*/'],
['
/**
* Foo ( Bar Baz )
*/'],
['
/**
* Description with a single " character.
*
* @Foo("string "" with inner quote")
*
* @param mixed description with a single " character.
*/', '
/**
* Description with a single " character.
*
* @Foo ( "string "" with inner quote" )
*
* @param mixed description with a single " character.
*/'],
['
/**
* // PHPDocumentor 1
* @abstract ( foo,bar = "baz" )
* @access ( foo,bar = "baz" )
* @code ( foo,bar = "baz" )
* @deprec ( foo,bar = "baz" )
* @encode ( foo,bar = "baz" )
* @exception ( foo,bar = "baz" )
* @final ( foo,bar = "baz" )
* @ingroup ( foo,bar = "baz" )
* @inheritdoc ( foo,bar = "baz" )
* @inheritDoc ( foo,bar = "baz" )
* @magic ( foo,bar = "baz" )
* @name ( foo,bar = "baz" )
* @toc ( foo,bar = "baz" )
* @tutorial ( foo,bar = "baz" )
* @private ( foo,bar = "baz" )
* @static ( foo,bar = "baz" )
* @staticvar ( foo,bar = "baz" )
* @staticVar ( foo,bar = "baz" )
* @throw ( foo,bar = "baz" )
*
* // PHPDocumentor 2
* @api ( foo,bar = "baz" )
* @author ( foo,bar = "baz" )
* @category ( foo,bar = "baz" )
* @copyright ( foo,bar = "baz" )
* @deprecated ( foo,bar = "baz" )
* @example ( foo,bar = "baz" )
* @filesource ( foo,bar = "baz" )
* @global ( foo,bar = "baz" )
* @ignore ( foo,bar = "baz" )
* @internal ( foo,bar = "baz" )
* @license ( foo,bar = "baz" )
* @link ( foo,bar = "baz" )
* @method ( foo,bar = "baz" )
* @package ( foo,bar = "baz" )
* @param ( foo,bar = "baz" )
* @property ( foo,bar = "baz" )
* @property-read ( foo,bar = "baz" )
* @property-write ( foo,bar = "baz" )
* @return ( foo,bar = "baz" )
* @see ( foo,bar = "baz" )
* @since ( foo,bar = "baz" )
* @source ( foo,bar = "baz" )
* @subpackage ( foo,bar = "baz" )
* @throws ( foo,bar = "baz" )
* @todo ( foo,bar = "baz" )
* @TODO ( foo,bar = "baz" )
* @usedBy ( foo,bar = "baz" )
* @uses ( foo,bar = "baz" )
* @var ( foo,bar = "baz" )
* @version ( foo,bar = "baz" )
*
* // PHPUnit
* @after ( foo,bar = "baz" )
* @afterClass ( foo,bar = "baz" )
* @backupGlobals ( foo,bar = "baz" )
* @backupStaticAttributes ( foo,bar = "baz" )
* @before ( foo,bar = "baz" )
* @beforeClass ( foo,bar = "baz" )
* @codeCoverageIgnore ( foo,bar = "baz" )
* @codeCoverageIgnoreStart ( foo,bar = "baz" )
* @codeCoverageIgnoreEnd ( foo,bar = "baz" )
* @covers ( foo,bar = "baz" )
* @coversDefaultClass ( foo,bar = "baz" )
* @coversNothing ( foo,bar = "baz" )
* @dataProvider ( foo,bar = "baz" )
* @depends ( foo,bar = "baz" )
* @expectedException ( foo,bar = "baz" )
* @expectedExceptionCode ( foo,bar = "baz" )
* @expectedExceptionMessage ( foo,bar = "baz" )
* @expectedExceptionMessageRegExp ( foo,bar = "baz" )
* @group ( foo,bar = "baz" )
* @large ( foo,bar = "baz" )
* @medium ( foo,bar = "baz" )
* @preserveGlobalState ( foo,bar = "baz" )
* @requires ( foo,bar = "baz" )
* @runTestsInSeparateProcesses ( foo,bar = "baz" )
* @runInSeparateProcess ( foo,bar = "baz" )
* @small ( foo,bar = "baz" )
* @test ( foo,bar = "baz" )
* @testdox ( foo,bar = "baz" )
* @ticket ( foo,bar = "baz" )
* @uses ( foo,bar = "baz" )
*
* // PHPCheckStyle
* @SuppressWarnings ( foo,bar = "baz" )
*
* // PHPStorm
* @noinspection ( foo,bar = "baz" )
*
* // PEAR
* @package_version ( foo,bar = "baz" )
*
* // PlantUML
* @enduml ( foo,bar = "baz" )
* @startuml ( foo,bar = "baz" )
*
* // other
* @fix ( foo,bar = "baz" )
* @FIXME ( foo,bar = "baz" )
* @fixme ( foo,bar = "baz" )
* @override
*/'],
],
[
[
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
[
'around_parentheses' => true,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo
*/'],
['
/**
* @Foo()
*/'],
['
/**
* @Foo ()
*/'],
['
/**
* @Foo( "bar" )
*/'],
['
/**
* Foo.
*
* @author John Doe
*
* @Foo( "bar", "baz")
*/', '
/**
* Foo.
*
* @author John Doe
*
* @Foo( "bar" ,"baz")
*/'],
[
'/** @Foo( "bar", "baz") */',
'/** @Foo( "bar" ,"baz") */',
],
['
/**
* @Foo( "bar", "baz")
*/', '
/**
* @Foo( "bar" , "baz")
*/'],
['
/**
* @Foo(
* "bar",
* "baz"
* )
*/', '
/**
* @Foo(
* "bar" ,
* "baz"
* )
*/'],
['
/**
* @Foo(
* "bar",
* "baz"
* )
*/', '
/**
* @Foo(
* "bar"
* ,
* "baz"
* )
*/'],
['
/**
* @Foo("bar ,", "baz,")
*/'],
['
/**
* @Foo(
* @Bar ( "foo", "bar"),
* @Baz
* )
*/', '
/**
* @Foo(
* @Bar ( "foo" ,"bar") ,
* @Baz
* )
*/'],
['
/**
* @Foo({"bar", "baz"})
*/', '
/**
* @Foo({"bar" ,"baz"})
*/'],
['
/**
* @Foo(foo="foo", bar="bar")
*/', '
/**
* @Foo(foo="foo" ,bar="bar")
*/'],
['
/**
* @Foo(foo="foo" ,bar="bar"
*/'],
['
/**
* Comment , with a comma.
*/'],
['
/**
* Description with a single " character.
*
* @Foo(foo="string "" with inner quote", bar="string "" with inner quote")
*
* @param mixed description with a single " character.
*/', '
/**
* Description with a single " character.
*
* @Foo(foo="string "" with inner quote" ,bar="string "" with inner quote")
*
* @param mixed description with a single " character.
*/'],
['
/**
* // PHPDocumentor 1
* @abstract ( foo,bar = "baz" )
* @access ( foo,bar = "baz" )
* @code ( foo,bar = "baz" )
* @deprec ( foo,bar = "baz" )
* @encode ( foo,bar = "baz" )
* @exception ( foo,bar = "baz" )
* @final ( foo,bar = "baz" )
* @ingroup ( foo,bar = "baz" )
* @inheritdoc ( foo,bar = "baz" )
* @inheritDoc ( foo,bar = "baz" )
* @magic ( foo,bar = "baz" )
* @name ( foo,bar = "baz" )
* @toc ( foo,bar = "baz" )
* @tutorial ( foo,bar = "baz" )
* @private ( foo,bar = "baz" )
* @static ( foo,bar = "baz" )
* @staticvar ( foo,bar = "baz" )
* @staticVar ( foo,bar = "baz" )
* @throw ( foo,bar = "baz" )
*
* // PHPDocumentor 2
* @api ( foo,bar = "baz" )
* @author ( foo,bar = "baz" )
* @category ( foo,bar = "baz" )
* @copyright ( foo,bar = "baz" )
* @deprecated ( foo,bar = "baz" )
* @example ( foo,bar = "baz" )
* @filesource ( foo,bar = "baz" )
* @global ( foo,bar = "baz" )
* @ignore ( foo,bar = "baz" )
* @internal ( foo,bar = "baz" )
* @license ( foo,bar = "baz" )
* @link ( foo,bar = "baz" )
* @method ( foo,bar = "baz" )
* @package ( foo,bar = "baz" )
* @param ( foo,bar = "baz" )
* @property ( foo,bar = "baz" )
* @property-read ( foo,bar = "baz" )
* @property-write ( foo,bar = "baz" )
* @return ( foo,bar = "baz" )
* @see ( foo,bar = "baz" )
* @since ( foo,bar = "baz" )
* @source ( foo,bar = "baz" )
* @subpackage ( foo,bar = "baz" )
* @throws ( foo,bar = "baz" )
* @todo ( foo,bar = "baz" )
* @TODO ( foo,bar = "baz" )
* @usedBy ( foo,bar = "baz" )
* @uses ( foo,bar = "baz" )
* @var ( foo,bar = "baz" )
* @version ( foo,bar = "baz" )
*
* // PHPUnit
* @after ( foo,bar = "baz" )
* @afterClass ( foo,bar = "baz" )
* @backupGlobals ( foo,bar = "baz" )
* @backupStaticAttributes ( foo,bar = "baz" )
* @before ( foo,bar = "baz" )
* @beforeClass ( foo,bar = "baz" )
* @codeCoverageIgnore ( foo,bar = "baz" )
* @codeCoverageIgnoreStart ( foo,bar = "baz" )
* @codeCoverageIgnoreEnd ( foo,bar = "baz" )
* @covers ( foo,bar = "baz" )
* @coversDefaultClass ( foo,bar = "baz" )
* @coversNothing ( foo,bar = "baz" )
* @dataProvider ( foo,bar = "baz" )
* @depends ( foo,bar = "baz" )
* @expectedException ( foo,bar = "baz" )
* @expectedExceptionCode ( foo,bar = "baz" )
* @expectedExceptionMessage ( foo,bar = "baz" )
* @expectedExceptionMessageRegExp ( foo,bar = "baz" )
* @group ( foo,bar = "baz" )
* @large ( foo,bar = "baz" )
* @medium ( foo,bar = "baz" )
* @preserveGlobalState ( foo,bar = "baz" )
* @requires ( foo,bar = "baz" )
* @runTestsInSeparateProcesses ( foo,bar = "baz" )
* @runInSeparateProcess ( foo,bar = "baz" )
* @small ( foo,bar = "baz" )
* @test ( foo,bar = "baz" )
* @testdox ( foo,bar = "baz" )
* @ticket ( foo,bar = "baz" )
* @uses ( foo,bar = "baz" )
*
* // PHPCheckStyle
* @SuppressWarnings ( foo,bar = "baz" )
*
* // PHPStorm
* @noinspection ( foo,bar = "baz" )
*
* // PEAR
* @package_version ( foo,bar = "baz" )
*
* // PlantUML
* @enduml ( foo,bar = "baz" )
* @startuml ( foo,bar = "baz" )
*
* // other
* @fix ( foo,bar = "baz" )
* @FIXME ( foo,bar = "baz" )
* @fixme ( foo,bar = "baz" )
* @override
*/'],
],
[
[
'around_parentheses' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
[
'around_parentheses' => false,
'around_commas' => true,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo ="foo", bar ={"foo":"foo", "bar"="bar"})
*/', '
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => true,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo= "foo", bar= {"foo" : "foo", "bar" = "bar"})
*/', '
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => false,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo= "foo", bar= {"foo":"foo", "bar"="bar"})
*/', '
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => true,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo ="foo", bar ={"foo" : "foo", "bar" = "bar"})
*/', '
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => false,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar" ="bar"})
*/', '
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => true,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar"= "bar"})
*/', '
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => false,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"= "bar"})
*/', '
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => true,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" ="bar"})
*/', '
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => false,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo" :"foo", "bar"="bar"})
*/', '
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => true,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo": "foo", "bar" = "bar"})
*/', '
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => false,
'after_array_assignments_colon' => null,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo": "foo", "bar"="bar"})
*/', '
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => true,
],
],
);
yield from self::createMultipleTestCase(
[
['
/**
* @Foo(foo="foo", bar={"foo":"foo", "bar"="bar"})
*/'],
['
/**
* @Foo(foo = "foo", bar = {"foo" :"foo", "bar" = "bar"})
*/', '
/**
* @Foo(foo = "foo", bar = {"foo" : "foo", "bar" = "bar"})
*/'],
],
[
[
'around_parentheses' => false,
'around_commas' => false,
'before_argument_assignments' => null,
'after_argument_assignments' => null,
'before_array_assignments_equals' => null,
'after_array_assignments_equals' => null,
'before_array_assignments_colon' => null,
'after_array_assignments_colon' => false,
],
],
);
$elements = [
'private $foo;',
'private string $foo;',
'private Foo\Bar $foo;',
'private ?Foo\Bar $foo;',
];
foreach ($elements as $element) {
yield [
\sprintf('<?php
class Foo
{
/**
* @Foo(foo="foo")
*/
%s
}
', $element),
\sprintf('<?php
class Foo
{
/**
* @Foo(foo = "foo")
*/
%s
}
', $element),
];
}
}
/**
* @param list<array{0: string, 1?: string}> $commentCases
* @param list<_AutogeneratedInputConfiguration> $configurations
*
* @return iterable<array{string, null|string, _AutogeneratedInputConfiguration}>
*/
private static function createMultipleTestCase(array $commentCases, array $configurations): iterable
{
foreach ($configurations as $configuration) {
foreach (self::createTestCases($commentCases, $configuration) as $testCase) {
yield $testCase;
$testCase[0] = str_replace("\n", "\r\n", $testCase[0]);
if (null !== $testCase[1]) {
$testCase[1] = str_replace("\n", "\r\n", $testCase[1]);
}
yield $testCase;
}
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Naming/NoHomoglyphNamesFixerTest.php | tests/Fixer/Naming/NoHomoglyphNamesFixerTest.php | <?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\Naming;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Naming\NoHomoglyphNamesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Naming\NoHomoglyphNamesFixer>
*
* @author Fred Cox <mcfedr@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoHomoglyphNamesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php $øøøøa = 1;'];
yield ['<?php $name = "This should not be changed";'];
yield ['<?php $name = "Это не меняется";'];
yield ['<?php $name = \'Это не меняется\';'];
yield ['<?php // This should not be chаnged'];
yield ['<?php /* This should not be chаnged */'];
yield [
'<?php $name = \'wrong\';',
'<?php $nаmе = \'wrong\';', // 'а' in name is a cyrillic letter
];
yield [
'<?php $a->name = \'wrong\';',
'<?php $a->nаmе = \'wrong\';',
];
yield [
'<?php class A { private $name; }',
'<?php class A { private $nаmе; }',
];
yield [
'<?php class Broken {}',
'<?php class Вroken {}', // 'В' in Broken is a cyrillic letter
];
yield [
'<?php interface Broken {}',
'<?php interface Вroken {}',
];
yield [
'<?php trait Broken {}',
'<?php trait Вroken {}',
];
yield [
'<?php $a = new Broken();',
'<?php $a = new Вroken();',
];
yield [
'<?php class A extends Broken {}',
'<?php class A extends Вroken {}',
];
yield [
'<?php class A implements Broken {}',
'<?php class A implements Вroken {}',
];
yield [
'<?php class A { use Broken; }',
'<?php class A { use Вroken; }',
];
yield [
'<?php echo Broken::class;',
'<?php echo Вroken::class;',
];
yield [
'<?php function name() {}',
'<?php function nаmе() {}',
];
yield [
'<?php name();',
'<?php nаmе();',
];
yield [
'<?php $first_name = "a";',
'<?php $first_name = "a";', // Weird underscore symbol
];
yield [
'<?php class A { private string $name; }',
'<?php class A { private string $nаmе; }',
];
yield [
'<?php class A { private ? Foo\Bar $name; }',
'<?php class A { private ? Foo\Bar $nаmе; }',
];
yield [
'<?php class A { private array $name; }',
'<?php class A { private array $nаmе; }',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/TernaryToElvisOperatorFixerTest.php | tests/Fixer/Operator/TernaryToElvisOperatorFixerTest.php | <?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\Operator;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Operator\TernaryToElvisOperatorFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\TernaryToElvisOperatorFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class TernaryToElvisOperatorFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
$operators = ['+=', '-=', '*=', '**=', '/=', '.=', '%=', '&=', '|=', '^=', '<<=', '>>='];
foreach ($operators as $operator) {
yield \sprintf('Test with operator "%s".', $operator) => [
\sprintf('<?php $z = $a %s $b ? : $c;', $operator),
\sprintf('<?php $z = $a %s $b ? $b : $c;', $operator),
];
}
yield 'multiple fixes' => [
'<?php $a ? : 1; $a ? : 1; $a ? : 1; $a ? : 1; $a ? : 1; $a ? : 1; $a ? : 1;',
'<?php $a ? $a : 1; $a ? $a : 1; $a ? $a : 1; $a ? $a : 1; $a ? $a : 1; $a ? $a : 1; $a ? $a : 1;',
];
yield [
'<?php $z = $z ? : "a";',
'<?php $z = $z ? $z : "a";',
];
yield [
'<?php ${"foo"} ? : "a";',
'<?php ${"foo"} ? ${"foo"} : "a";',
];
yield [
'<?php $z::$a()[1] ? : 1;',
'<?php $z::$a()[1] ? $z::$a()[1] : 1;',
];
yield [
'<?php $z->$a ? : 1;',
'<?php $z->$a ? $z->$a : 1;',
];
yield [
'<?php $z = $z ? : 1;',
'<?php $z = $z ? $z : 1;',
];
yield [
'<?php $z = $z ? : 1.1;',
'<?php $z = $z ? $z : 1.1;',
];
yield [
'<?php $z = $a ? : foo();',
'<?php $z = $a ? $a : foo();',
];
yield [
'<?php $z = $a ? : \foo();',
'<?php $z = $a ? $a : \foo();',
];
yield [
'<?php $z = 1 ? : $z;',
'<?php $z = 1 ? 1 : $z;',
];
yield [
'<?php $z = 1.1 ? : $z;',
'<?php $z = 1.1 ? 1.1 : $z;',
];
yield [
'<?php $z = "a" ? : "b";',
'<?php $z = "a" ? "a" : "b";',
];
yield [
'<?php $z = foo() ? : $a;',
'<?php $z = foo() ? foo() : $a;',
];
yield [
'<?php $z = \foo() ? : $a;',
'<?php $z = \foo() ? \foo() : $a;',
];
yield [
'<?php 1 ? : $z->$a;',
'<?php 1 ? 1 : $z->$a;',
];
yield [
'<?php 1 ? : $z::$a()[1];',
'<?php 1 ? 1 : $z::$a()[1];',
];
yield [
'<?php $a ? : ${"foo"};',
'<?php $a ? $a : ${"foo"};',
];
yield [
'<?php {$b ? : 1;}',
'<?php {$b ? $b : 1;}',
];
yield [
'<?php {echo 1;} $c = $c ? : 1;',
'<?php {echo 1;} $c = $c ? $c : 1;',
];
yield [
'<?php $d ? : 1;',
'<?php $d ? ($d) : 1;',
];
yield [
'<?php $d ? : 1;',
'<?php $d ? (($d)) : 1;',
];
yield [
'<?php ($d) ? : 1;',
'<?php ($d) ? $d : 1;',
];
yield [
'<?php ($d) ? : 1;',
'<?php ($d) ? (($d)) : 1;',
];
yield [
'<?php
a($d) ? $d : 1;
$d ? a($d) : 1;
',
];
yield [
'<?php ; $e ? : 1;',
'<?php ; $e ? $e : 1;',
];
yield [
'<?php $foo8 = $bar[0] ? : $foo;',
'<?php $foo8 = $bar[0] ? $bar[0] : $foo;',
];
yield [
'<?php $foo7 = $_GET[$a] ? : $foo;',
'<?php $foo7 = $_GET[$a] ? $_GET[$a] : $foo;',
];
yield [
'<?php $foo6 = $bar[$a][0][$a ? 1 : 2][2] ? /* 1 *//* 2 *//* 3 */ /* 4 */ : $foo;',
'<?php $foo6 = $bar[$a][0][$a ? 1 : 2][2] ? $bar/* 1 */[$a]/* 2 */[0]/* 3 */[$a ? 1 : 2]/* 4 */[2] : $foo;',
];
yield [
'<?php ; 2 ? : 1;',
'<?php ; 2 ? 2 : 1;',
];
yield [
'<?php
$bar1[0][1] = $bar[0][1] ? $bar[0][1] + 1 : $bar[0][1];
$bar2[0] = $bar[0] ? $bar[0] + 1 : $bar[0];
$bar3[0][1] = $bar[0][1] ? ++$bar[0][1] : $bar[0][1];
$bar4[0] = $bar[0] ? --$bar[0] : $bar[0];
',
];
yield [
'<?php
$foo77 = $foo ? "$foo" : $foo;
$foo77 = $foo ? \'$foo\' : $foo;
',
];
yield 'comments 1' => [
'<?php $a /* a */ = /* b */ $a /* c */ ? /* d */ /* e */ : /* f */ 1;',
'<?php $a /* a */ = /* b */ $a /* c */ ? /* d */ $a /* e */ : /* f */ 1;',
];
yield 'comments 2' => [
'<?php $foo = $bar/* a */?/* b *//* c */:/* d */$baz;',
'<?php $foo = $bar/* a */?/* b */$bar/* c */:/* d */$baz;',
];
yield 'minimal' => [
'<?php $b?:$c;',
'<?php $b?$b:$c;',
];
yield 'minimal 2x' => [
'<?php $b?:$c;$f=$b?:$c;',
'<?php $b?$b:$c;$f=$b?$b:$c;',
];
yield [
'<?php
$foo = $bar
? '.'
: $foo;
',
'<?php
$foo = $bar
? $bar
: $foo;
',
];
yield [
'<?php
$foo = $bar # 1
? # 2
: $foo; # 3
',
'<?php
$foo = $bar # 1
? $bar # 2
: $foo; # 3
',
];
yield [
'<?php foo($a ? : $b, $c ? : $d);',
'<?php foo($a ? $a : $b, $c ? $c : $d);',
];
yield [
'<?php $j[$b ? : $c];',
'<?php $j[$b ? $b : $c];',
];
yield [
'<?php foo($a[0] ? : $b[0], $c[0] ? : $d[0]);',
'<?php foo($a[0] ? $a[0] : $b[0], $c[0] ? $c[0] : $d[0]);',
];
yield [
'<?php $a + 1 ? : $b;',
'<?php $a + 1 ? $a + 1 : $b;',
];
yield [
'<?php
$a ? : <<<EOT
EOT;
$a ? : <<<\'EOT\'
EOT;
<<<EOT
EOT
? '.'
: $a
;
<<<\'EOT\'
EOT
? '.'
: $a
;
',
'<?php
$a ? $a : <<<EOT
EOT;
$a ? $a : <<<\'EOT\'
EOT;
<<<EOT
EOT
? <<<EOT
EOT
: $a
;
<<<\'EOT\'
EOT
? <<<\'EOT\'
EOT
: $a
;
',
];
yield [
'<?php @foo() ? : 1;',
'<?php @foo() ? @foo() : 1;',
];
yield [
'<?php
$f = !foo() ? : 1;
$f = !$a ? : 1;
$f = $a[1][!$a][@foo()] ? : 1;
$f = !foo() ? : 1;
',
'<?php
$f = !foo() ? !foo() : 1;
$f = !$a ? !$a : 1;
$f = $a[1][!$a][@foo()] ? $a[1][!$a][@foo()] : 1;
$f = !foo() ? !foo() : 1;
',
];
yield [
'<?php $foo = $foo ? $bar : $foo;',
];
yield [
'<?php $foo1 = $bar[$a][0][1][2] ? 123 : $foo;',
];
yield [
'<?php $foo2 = $bar[$a] ? $bar[$b] : $foo;',
];
yield [
'<?php $foo2a = $bar[$a] ? $bar[$a][1] : $foo;',
];
yield [
'<?php $foo2b = $bar[$a][1] ? $bar[$a] : $foo;',
];
yield [
'<?php $foo3 = $bar[$a][1] ? $bar[$a][2] : $foo;',
];
yield [
'<?php $foo4 = 1 + $bar[0] ? $bar[0] : $foo;',
];
yield [
'<?php $foo = $bar ? $$bar : 1;',
];
yield 'complex case 1 left out by design' => [
'<?php $foo = !empty($bar) ? $bar : $baz;',
];
yield 'complex case 2 left out by design' => [
'<?php $foo = !!$bar ? $bar : $baz;',
];
yield [
'<?php $f = 1 + $f ? $f : 1;',
];
yield [
'<?php $g = $g ? $g - 1 : 1;',
];
yield [
'<?php
$c = ++$a ? ++$a : $b;
$c = (++$a) ? (++$a) : $b;
$c = ($a++) ? ($a++) : $b;
$c = fooBar(++$a) ? fooBar(++$a) : $b;
$c = [++$a] ? [++$a] : $b;
',
];
yield [
'<?php
$c = --$a ? --$a : $b;
$c = (--$a) ? (--$a) : $b;
$c = ($a--) ? ($a--) : $b;
$c = fooBar(--$a) ? fooBar(--$a) : $b;
$c = [--$a] ? [--$a] : $b;
',
];
yield [
'<?= $a ? : $b ?>',
'<?= $a ? $a : $b ?>',
];
yield [
'<?php new class() extends Foo {} ? new class{} : $a;',
];
yield [
'<?php $a ? : new class{};',
'<?php $a ? $a : new class{};',
];
yield [
'<?php $a ? : new class($a) extends Foo {};',
'<?php $a ? $a : new class($a) extends Foo {};',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php $foo = $a->{$b} ? $bar{0} : $foo;',
];
yield [
'<?php $l[$b[0] ? : $c[0]];',
'<?php $l[$b[0] ? $b{0} : $c[0]];',
];
yield [
'<?php $l{$b{0} ? : $c{0}};',
'<?php $l{$b{0} ? $b{0} : $c{0}};',
];
yield [
'<?php $z = $a[1][2] ? : 1;',
'<?php $z = $a[1][2] ? $a[1][2] : 1;',
];
yield [
'<?php $i = $bar{0}[1]{2}[3] ? : $foo;',
'<?php $i = $bar{0}[1]{2}[3] ? $bar{0}[1]{2}[3] : $foo;',
];
yield [
'<?php $fooX = $bar{0}[1]{2}[3] ? : $foo;',
'<?php $fooX = $bar{0}[1]{2}[3] ? $bar{0}[1]{2}[3] : $foo;',
];
yield [
'<?php $k = $bar{0} ? : $foo;',
'<?php $k = $bar{0} ? $bar{0} : $foo;',
];
yield 'ignore different type of index braces' => [
'<?php $z = $a[1] ? : 1;',
'<?php $z = $a[1] ? $a{1} : 1;',
];
yield [
'<?php __FILE__.$a.$b{2}.$c->$a[0] ? : 1;',
'<?php __FILE__.$a.$b{2}.$c->$a[0] ? __FILE__.$a.$b{2}.$c->$a[0] : 1;',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected): void
{
$this->doTest($expected);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield ['<?php
function test(#[TestAttribute] ?User $user) {}
'];
yield ['<?php
function test(#[TestAttribute] ?User $user = null) {}
'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Operator/NoUselessNullsafeOperatorFixerTest.php | tests/Fixer/Operator/NoUselessNullsafeOperatorFixerTest.php | <?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\Operator;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @requires PHP 8.0
*
* @covers \PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUselessNullsafeOperatorFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple case + comment' => [
'<?php $a = new class extends foo {
public function bar() {
$this->g();
// $this?->g();
}
};',
'<?php $a = new class extends foo {
public function bar() {
$this?->g();
// $this?->g();
}
};',
];
yield 'multiple casing cases + comment + no candidate' => [
'<?php $a = new class extends foo {
public function bar() {
return $THIS /*1*/ -> g().$THis->g().$this->do()?->o();
}
};',
'<?php $a = new class extends foo {
public function bar() {
return $THIS /*1*/ ?-> g().$THis?->g().$this->do()?->o();
}
};',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.