Skip to content

Commit

Permalink
Fix class not found from Easy-Testing package (#2864)
Browse files Browse the repository at this point in the history
* Fix class not found from Easy-Testing package

* port Fixture updater

* port Fixture updater

* [ci-review] Rector Rectify

* add missing methods on FixtureFileFinder

* phpstan

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 31, 2022
1 parent 6db4c0e commit 8ce5353
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 19 deletions.
68 changes: 68 additions & 0 deletions packages/Testing/Fixture/FixtureFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Rector\Testing\Fixture;

use Iterator;
use Nette\Utils\Strings;
use Rector\Core\Exception\ShouldNotHappenException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\Exception\FileNotFoundException;

final class FixtureFileFinder
{
Expand Down Expand Up @@ -38,4 +41,69 @@ private static function findFilesInDirectory(string $directory, string $suffix):
$fileInfos = iterator_to_array($finder);
return array_values($fileInfos);
}

/**
* @return Iterator<array<int, SmartFileInfo>>
*/
public static function yieldDirectoryExclusively(string $directory, string $suffix = '*.php.inc'): Iterator
{
$fileInfos = self::findFilesInDirectoryExclusively($directory, $suffix);

return self::yieldFileInfos($fileInfos);
}

private static function ensureNoOtherFileName(string $directory, string $suffix): void
{
$finder = Finder::create()->in($directory)
->files()
->notName($suffix);

/** @var SplFileInfo[] $fileInfos */
$fileInfos = iterator_to_array($finder->getIterator());

$relativeFilePaths = [];
foreach ($fileInfos as $fileInfo) {
$relativeFilePaths[] = Strings::substring($fileInfo->getRealPath(), strlen(getcwd()) + 1);
}

if ($relativeFilePaths === []) {
return;
}

throw new ShouldNotHappenException(sprintf(
'Files "%s" have invalid suffix, use "%s" suffix instead',
implode('", ', $relativeFilePaths),
$suffix
));
}

/**
* @return SplFileInfo[]
*/
private static function findFilesInDirectoryExclusively(string $directory, string $suffix): array
{
self::ensureNoOtherFileName($directory, $suffix);

$finder = Finder::create()->in($directory)
->files()
->name($suffix);

$fileInfos = iterator_to_array($finder->getIterator());
return array_values($fileInfos);
}

/**
* @param SplFileInfo[] $fileInfos
* @return Iterator<array<int, SmartFileInfo>>
*/
private static function yieldFileInfos(array $fileInfos): Iterator
{
foreach ($fileInfos as $fileInfo) {
try {
$smartFileInfo = new SmartFileInfo($fileInfo->getRealPath());
yield [$smartFileInfo];
} catch (FileNotFoundException) {
}
}
}
}
63 changes: 63 additions & 0 deletions packages/Testing/Fixture/FixtureFileUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Rector\Testing\Fixture;

use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

/**
* @api
*/
final class FixtureFileUpdater
{
public static function updateFixtureContent(
SmartFileInfo|string $originalFileInfo,
string $changedContent,
SmartFileInfo $fixtureFileInfo
): void {
if (! getenv('UPDATE_TESTS') && ! getenv('UT')) {
return;
}

$newOriginalContent = self::resolveNewFixtureContent($originalFileInfo, $changedContent);

self::getSmartFileSystem()
->dumpFile($fixtureFileInfo->getRealPath(), $newOriginalContent);
}

public static function updateExpectedFixtureContent(
string $newOriginalContent,
SmartFileInfo $expectedFixtureFileInfo
): void {
if (! getenv('UPDATE_TESTS') && ! getenv('UT')) {
return;
}

self::getSmartFileSystem()
->dumpFile($expectedFixtureFileInfo->getRealPath(), $newOriginalContent);
}

private static function getSmartFileSystem(): SmartFileSystem
{
return new SmartFileSystem();
}

private static function resolveNewFixtureContent(
SmartFileInfo|string $originalFileInfo,
string $changedContent
): string {
if ($originalFileInfo instanceof SmartFileInfo) {
$originalContent = $originalFileInfo->getContents();
} else {
$originalContent = $originalFileInfo;
}

if ($originalContent === $changedContent) {
return $originalContent;
}

return $originalContent . '-----' . PHP_EOL . $changedContent;
}
}
5 changes: 3 additions & 2 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Testing\Contract\RectorTestInterface;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\Fixture\FixtureFileUpdater;
use Rector\Testing\Fixture\FixtureSplitter;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use Rector\Testing\PHPUnit\Behavior\MovingFilesTrait;
use SplFileInfo;
use Symplify\EasyTesting\DataProvider\StaticFixtureUpdater;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand Down Expand Up @@ -180,7 +180,8 @@ private function doTestFileMatchesExpectedContent(
throw $expectationFailedException;
}

StaticFixtureUpdater::updateFixtureContent($originalFileInfo, $changedContent, $fixtureFileInfo);
FixtureFileUpdater::updateFixtureContent($originalFileInfo, $changedContent, $fixtureFileInfo);

$contents = $expectedFileInfo->getContents();

// make sure we don't get a diff in which every line is different (because of differences in EOL)
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,7 @@ parameters:

# used in abstract test case
- '#Class constant "SPLIT_LINE_REGEX" is never used#'

-
message: '#Only booleans are allowed in a negated boolean, string\|false given#'
path: packages/Testing/Fixture/FixtureFileUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Tests\Composer\Rector\AddPackageToRequireComposerRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class AddPackageToRequireComposerRectorTest extends AbstractRectorTestCase
Expand All @@ -21,7 +21,7 @@ public function test(SmartFileInfo $fileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Tests\Composer\Rector\AddPackageToRequireDevComposerRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class AddPackageToRequireDevComposerRectorTest extends AbstractRectorTestCase
Expand All @@ -21,7 +21,7 @@ public function test(SmartFileInfo $fileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Tests\Composer\Rector\ChangePackageVersionComposerRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ChangePackageVersionComposerRectorTest extends AbstractRectorTestCase
Expand All @@ -21,7 +21,7 @@ public function test(SmartFileInfo $fileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Tests\Composer\Rector\CombinationComposerRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class CombinationComposerRectorTest extends AbstractRectorTestCase
Expand All @@ -21,7 +21,7 @@ public function test(SmartFileInfo $fileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Tests\Composer\Rector\RemovePackageComposerRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RemovePackageComposerRectorTest extends AbstractRectorTestCase
Expand All @@ -21,7 +21,7 @@ public function test(SmartFileInfo $fileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Tests\Composer\Rector\ReplacePackageAndVersionComposerRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ReplacePackageAndVersionComposerRectorTest extends AbstractRectorTestCase
Expand All @@ -21,7 +21,7 @@ public function test(SmartFileInfo $fileInfo): void

public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use PhpParser\Node\Stmt\Use_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\TestingParser\TestingParser;
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\FirstClass;
use Rector\Tests\Naming\Naming\UseImportsResolver\Source\SecondClass;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class UseImportsResolverTest extends AbstractTestCase
Expand Down Expand Up @@ -61,6 +61,6 @@ public function testUsesFromProperty(SmartFileInfo $file): void
*/
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectoryExclusively(__DIR__ . '/Fixture');
return FixtureFileFinder::yieldDirectoryExclusively(__DIR__ . '/Fixture');
}
}
2 changes: 1 addition & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ final public function enterNode(Node $node)
$this->connectParentNodes($refactoredNode);
}

/** @var MutatingScope $currentScope */
/** @var MutatingScope|null $currentScope */
$currentScope = $originalNode->getAttribute(AttributeKey::SCOPE);
$this->changedNodeScopeRefresher->refresh($refactoredNode, $currentScope, $this->file->getSmartFileInfo());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\Core\Tests\NonPhpFile\Rector\RenameClassNonPhpRector;

use Iterator;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RenameClassNonPhpRectorTest extends AbstractRectorTestCase
Expand All @@ -24,7 +24,7 @@ public function test(SmartFileInfo $fixtureFileInfo): void
*/
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*');
return FixtureFileFinder::yieldDirectory(__DIR__ . '/Fixture', '*');
}

public function provideConfigFilePath(): string
Expand Down

0 comments on commit 8ce5353

Please sign in to comment.