Skip to content

Commit

Permalink
DX: PsrAutoloadingFixerTest - do not build mock in data provider (#7491)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Dec 2, 2023
1 parent 2ca2052 commit eb775b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
37 changes: 16 additions & 21 deletions tests/Fixer/Basic/PsrAutoloadingFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace PhpCsFixer\Tests\Fixer\Basic;

use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use Prophecy\Prophet;

/**
* @author Graham Campbell <hello@gjcampbell.co.uk>
Expand Down Expand Up @@ -163,11 +162,13 @@ public static function provideFixNewCases(): iterable
* @dataProvider provideIgnoredCases
* @dataProvider provideAnonymousClassCases
*/
public function testFix(string $expected, ?string $input = null, ?\SplFileInfo $file = null, ?string $dir = null): void
public function testFix(string $expected, ?string $input = null, ?string $filepath = null, ?string $dir = null): void
{
if (null === $file) {
$file = self::getTestFile(__FILE__);
if (null === $filepath) {
$filepath = __FILE__;
}
$file = self::getTestFile($filepath);

if (null !== $dir) {
$this->fixer->configure(['dir' => $dir]);
}
Expand All @@ -177,13 +178,7 @@ public function testFix(string $expected, ?string $input = null, ?\SplFileInfo $

public static function provideFixCases(): iterable
{
$prophet = new Prophet();
$fileProphecy = $prophet->prophesize(\SplFileInfo::class);
$fileProphecy->willBeConstructedWith(['']);
$fileProphecy->getBasename('.php')->willReturn('Bar');
$fileProphecy->getExtension()->willReturn('php');
$fileProphecy->getRealPath()->willReturn(__DIR__.\DIRECTORY_SEPARATOR.'Psr'.\DIRECTORY_SEPARATOR.'Foo'.\DIRECTORY_SEPARATOR.'Bar.php');
$file = $fileProphecy->reveal();
$filepath = __DIR__.\DIRECTORY_SEPARATOR.'Psr'.\DIRECTORY_SEPARATOR.'Foo'.\DIRECTORY_SEPARATOR.'Bar.php';

yield [ // namespace with wrong casing
'<?php
Expand All @@ -194,7 +189,7 @@ class Bar {}
namespace Psr\foo;
class bar {}
',
$file,
$filepath,
__DIR__,
];

Expand All @@ -205,7 +200,7 @@ class Psr_Foo_Bar {}
'<?php
class Psr_fOo_bAr {}
',
$file,
$filepath,
__DIR__,
];

Expand All @@ -218,7 +213,7 @@ class Bar {}
namespace Psr\foo;
class bar {}
',
$file,
$filepath,
__DIR__,
];

Expand Down Expand Up @@ -297,7 +292,7 @@ class /* hi there */ blah /* why hello */ {}
class PsrAutoloadingFixer {}
',
null,
self::getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
];

yield [ // namespace partially matching directory structure with comment
Expand All @@ -306,7 +301,7 @@ class PsrAutoloadingFixer {}
class /* hi there */ PsrAutoloadingFixer {}
',
null,
self::getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
];

yield [ // namespace not matching directory structure
Expand All @@ -315,7 +310,7 @@ class /* hi there */ PsrAutoloadingFixer {}
class PsrAutoloadingFixer {}
',
null,
self::getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
];

yield [ // namespace partially matching directory structure with configured directory
Expand All @@ -327,7 +322,7 @@ class PsrAutoloadingFixer {}
namespace Foo\Bar\Baz\FIXER\Basic;
class PsrAutoloadingFixer {}
',
self::getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
__DIR__.'/../../../src/',
];

Expand All @@ -340,7 +335,7 @@ class /* hi there */ PsrAutoloadingFixer {}
namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
class /* hi there */ PsrAutoloadingFixer {}
',
self::getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
__DIR__.'/../../../src/',
];

Expand All @@ -350,7 +345,7 @@ class /* hi there */ PsrAutoloadingFixer {}
class PsrAutoloadingFixer {}
',
null,
self::getTestFile(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
__DIR__.'/../../../src/Fixer/Basic',
];

Expand Down Expand Up @@ -396,7 +391,7 @@ public static function provideIgnoredCases(): iterable
namespace Aaa;
class Bar {}',
null,
self::getTestFile($case),
$case,
], $cases);
}

Expand Down
9 changes: 8 additions & 1 deletion tests/Test/AbstractFixerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,14 @@ final protected static function getTestFile(string $filename = __FILE__): \SplFi
{
static $files = [];

return $files[$filename] ?? $files[$filename] = new \SplFileInfo($filename);
return $files[$filename] ?? $files[$filename] = new class($filename) extends \SplFileInfo {
public function getRealPath(): string
{
return false !== parent::getRealPath()
? parent::getRealPath()
: $this->getPathname();
}
};
}

/**
Expand Down

0 comments on commit eb775b4

Please sign in to comment.