Skip to content

Commit

Permalink
[Testing] In case of failed test using multiple rules, show applied R…
Browse files Browse the repository at this point in the history
…ector rules to make easy to find them (#5222)
  • Loading branch information
TomasVotruba committed Nov 3, 2023
1 parent 2fac959 commit cc97bec
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
6 changes: 5 additions & 1 deletion packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public function enterNode(Node $node): ?Node
if (! $result instanceof Name && ! $node instanceof FullyQualified) {
$phpAttributeName = $node->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
if (is_string($phpAttributeName)) {
return $this->classRenamer->renameNode(new FullyQualified($phpAttributeName, $node->getAttributes()), $oldToNewClasses, $scope);
return $this->classRenamer->renameNode(
new FullyQualified($phpAttributeName, $node->getAttributes()),
$oldToNewClasses,
$scope
);
}
}

Expand Down
20 changes: 17 additions & 3 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\Fixture\FixtureFileUpdater;
use Rector\Testing\Fixture\FixtureSplitter;
use Rector\Testing\PHPUnit\ValueObject\RectorTestResult;

abstract class AbstractRectorTestCase extends AbstractLazyTestCase implements RectorTestInterface
{
Expand Down Expand Up @@ -228,11 +229,23 @@ private function doTestFileMatchesExpectedContent(
$originalFileContent = FileSystem::read($originalFilePath);

// the file is now changed (if any rule matches)
$changedContent = $this->processFilePath($originalFilePath);
$rectorTestResult = $this->processFilePath($originalFilePath);

$changedContent = $rectorTestResult->getChangedContents();

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

// give more context about used rules in case of set testing
if (count($rectorTestResult->getAppliedRectorClasses()) > 1) {
$failureMessage .= PHP_EOL . PHP_EOL;
$failureMessage .= 'Applied Rector rules:' . PHP_EOL;

foreach ($rectorTestResult->getAppliedRectorClasses() as $appliedRectorClass) {
$failureMessage .= ' * ' . $appliedRectorClass . PHP_EOL;
}
}

try {
$this->assertSame($expectedFileContents, $changedContent, $failureMessage);
} catch (ExpectationFailedException) {
Expand All @@ -243,7 +256,7 @@ private function doTestFileMatchesExpectedContent(
}
}

private function processFilePath(string $filePath): string
private function processFilePath(string $filePath): RectorTestResult
{
$this->dynamicSourceLocatorProvider->setFilePath($filePath);

Expand All @@ -265,7 +278,8 @@ private function processFilePath(string $filePath): string
}

// return changed file contents
return FileSystem::read($filePath);
$changedFileContents = FileSystem::read($filePath);
return new RectorTestResult($changedFileContents, $processResult);
}

private function createInputFilePath(string $fixtureFilePath): string
Expand Down
7 changes: 0 additions & 7 deletions packages/Testing/PHPUnit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

abstract class AbstractTestCase extends AbstractLazyTestCase
{
/**
* @deprecated only for BC
*/
protected function boot(): void
{
}

/**
* @param string[] $configFiles
*/
Expand Down
38 changes: 38 additions & 0 deletions packages/Testing/PHPUnit/ValueObject/RectorTestResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\Testing\PHPUnit\ValueObject;

use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\ValueObject\ProcessResult;

final class RectorTestResult
{
public function __construct(
private readonly string $changedContents,
private readonly ProcessResult $processResult
) {
}

public function getChangedContents(): string
{
return $this->changedContents;
}

/**
* @return array<class-string<RectorInterface>>
*/
public function getAppliedRectorClasses(): array
{
$rectorClasses = [];

foreach ($this->processResult->getFileDiffs() as $fileDiff) {
$rectorClasses = array_merge($rectorClasses, $fileDiff->getRectorClasses());
}

sort($rectorClasses);

return array_unique($rectorClasses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,3 @@ class KeepUnderscore
}
}
}

?>
-----
<?php

namespace Rector\Tests\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector\Fixture;

class KeepUnderscore
{
public function run()
{
$customer_names = [];
foreach ($customer_names as $customer_name) {
echo $customer_name;
}
}
}

?>

0 comments on commit cc97bec

Please sign in to comment.