123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?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;
- /**
- * @author Vladimir Reznichenko <kalessil@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
- * @covers \PhpCsFixer\Fixer\LanguageConstruct\DirConstantFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\DirConstantFixer>
- */
- final class DirConstantFixerTest 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
- {
- $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
- '
- );
- }
- }
|