Skip to content

Commit

Permalink
[Performance][Testing] Avoid double call FileSystem::read() on Abstra…
Browse files Browse the repository at this point in the history
…ctRectorTestCase (#4908)

* [Performance][Testing] Avoid double call FileSystem::read() on AbstractRectorTestCase

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 4, 2023
1 parent 5595f80 commit 4a3627e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
Expand All @@ -41,6 +42,8 @@ abstract class AbstractRectorTestCase extends AbstractLazyTestCase implements Re
*/
private static array $cacheByRuleAndConfig = [];

private CurrentFileProvider $currentFileProvider;

/**
* Restore default parameters
*/
Expand Down Expand Up @@ -112,6 +115,8 @@ protected function setUp(): void
/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
$bootstrapFilesIncluder = $this->make(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();

$this->currentFileProvider = $this->make(CurrentFileProvider::class);
}

protected function tearDown(): void
Expand Down Expand Up @@ -225,16 +230,16 @@ private function doTestFileMatchesExpectedContent(
): void {
SimpleParameterProvider::setParameter(Option::SOURCE, [$originalFilePath]);

$originalContents = FileSystem::read($originalFilePath);
$changedContent = $this->processFilePath($originalFilePath);
$originalFileContent = $this->currentFileProvider->getFile()->getOriginalFileContent();

$fixtureFilename = basename($fixtureFilePath);
$failureMessage = sprintf('Failed on fixture file "%s"', $fixtureFilename);

try {
$this->assertSame($expectedFileContents, $changedContent, $failureMessage);
} catch (ExpectationFailedException) {
FixtureFileUpdater::updateFixtureContent($originalContents, $changedContent, $fixtureFilePath);
FixtureFileUpdater::updateFixtureContent($originalFileContent, $changedContent, $fixtureFilePath);

// if not exact match, check the regex version (useful for generated hashes/uuids in the code)
$this->assertStringMatchesFormat($expectedFileContents, $changedContent, $failureMessage);
Expand All @@ -256,7 +261,7 @@ private function processFilePath(string $filePath): string

$this->applicationFileProcessor->processFiles([$filePath], $configuration);

return FileSystem::read($filePath);
return $this->currentFileProvider->getFile()->getFileContent();
}

private function createInputFilePath(string $fixtureFilePath): string
Expand Down

0 comments on commit 4a3627e

Please sign in to comment.