Skip to content

Commit

Permalink
Fix tests fixture updater (#4892)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 1, 2023
1 parent 7078b51 commit c5897e5
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 13 deletions.
9 changes: 4 additions & 5 deletions packages/Testing/Fixture/FixtureFileUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@ final class FixtureFileUpdater
* @api
*/
public static function updateFixtureContent(
string $originalFilePath,
string $originalContent,
string $changedContent,
string $fixtureFilePath
): void {
if (! getenv('UPDATE_TESTS') && ! getenv('UT')) {
return;
}

$newOriginalContent = self::resolveNewFixtureContent($originalFilePath, $changedContent);
$newOriginalContent = self::resolveNewFixtureContent($originalContent, $changedContent);

FileSystem::write($fixtureFilePath, $newOriginalContent);
}

private static function resolveNewFixtureContent(
string $originalFilePath,
string $originalContent,
string $changedContent
): string {
$originalContent = FileSystem::read($originalFilePath);

if ($originalContent === $changedContent) {
return $originalContent;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ private function doTestFileMatchesExpectedContent(
): void {
SimpleParameterProvider::setParameter(Option::SOURCE, [$originalFilePath]);

$originalContents = FileSystem::read($originalFilePath);
$changedContent = $this->processFilePath($originalFilePath);

$fixtureFilename = basename($fixtureFilePath);
Expand All @@ -233,7 +234,7 @@ private function doTestFileMatchesExpectedContent(
try {
$this->assertSame($expectedFileContents, $changedContent, $failureMessage);
} catch (ExpectationFailedException) {
FixtureFileUpdater::updateFixtureContent($originalFilePath, $changedContent, $fixtureFilePath);
FixtureFileUpdater::updateFixtureContent($originalContents, $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 @@ -254,6 +255,7 @@ private function processFilePath(string $filePath): string
$configuration = $configurationFactory->createForTests([$filePath]);

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

return FileSystem::read($filePath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class TryFinallyOnlyCommentStmt
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ class User
public $phonenumbers;
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ final class ClosureType
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class TypedNonFinalClassProtectedProperty
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ abstract class IntersectionInUnion
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ class NullableMockObject extends TestCase
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ function notInScope()
{
}

?>
?>

0 comments on commit c5897e5

Please sign in to comment.