Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddCoversClassAttributeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddCoversClassAttributeRectorConfiguredTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/ConfiguredFixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static function provideData(): Iterator

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
return __DIR__ . '/config/rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Utils\Rector\Tests\Rector\AddCoversClassAttributeRector\ConfiguredFixture;

use PHPUnit\Framework\TestCase;

class SomeServiceFunctionalTest extends TestCase {}
class SomeService {}

?>
-----
<?php

namespace Utils\Rector\Tests\Rector\AddCoversClassAttributeRector\ConfiguredFixture;

use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\CoversClass(\Utils\Rector\Tests\Rector\AddCoversClassAttributeRector\ConfiguredFixture\SomeService::class)]
class SomeServiceFunctionalTest extends TestCase {}
class SomeService {}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Utils\Rector\Tests\Rector\AddCoversClassAttributeRector\ConfiguredFixture;

use PHPUnit\Framework\TestCase;

class UserRepositoryIntegrationTest extends TestCase {}
class UserRepository {}

?>
-----
<?php

namespace Utils\Rector\Tests\Rector\AddCoversClassAttributeRector\ConfiguredFixture;

use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\CoversClass(\Utils\Rector\Tests\Rector\AddCoversClassAttributeRector\ConfiguredFixture\UserRepository::class)]
class UserRepositoryIntegrationTest extends TestCase {}
class UserRepository {}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddCoversClassAttributeRector;
use Rector\PHPUnit\ValueObject\TestClassSuffixesConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddCoversClassAttributeRector::class);
$rectorConfig->ruleWithConfiguration(AddCoversClassAttributeRector::class, [
new TestClassSuffixesConfig(['Test', 'TestCase', 'FunctionalTest', 'IntegrationTest']),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddCoversClassAttributeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddCoversClassAttributeRector::class);
};
123 changes: 79 additions & 44 deletions rules/CodeQuality/Rector/Class_/AddCoversClassAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\ValueObject\TestClassSuffixesConfig;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
use function array_filter;
use function array_merge;
use function count;
Expand All @@ -24,48 +27,57 @@
use function strtolower;
use function trim;

final class AddCoversClassAttributeRector extends AbstractRector
final class AddCoversClassAttributeRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string[]
*/
private array $testClassSuffixes = ['Test', 'TestCase'];

public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Adds `#[CoversClass(...)]` attribute to test files guessing source class name.', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeService
{
}

use PHPUnit\Framework\TestCase;

class SomeServiceTest extends TestCase
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeService
{
}

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(SomeService::class)]
class SomeServiceTest extends TestCase
{
}
CODE_SAMPLE
,
),
]);
return new RuleDefinition(
'Adds `#[CoversClass(...)]` attribute to test files guessing source class name.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeService
{
}

use PHPUnit\Framework\TestCase;

class SomeServiceFunctionalTest extends TestCase
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeService
{
}

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(SomeService::class)]
class SomeServiceFunctionalTest extends TestCase
{
}
CODE_SAMPLE
,
[new TestClassSuffixesConfig(['Test', 'TestCase', 'FunctionalTest', 'IntegrationTest'])]
),
]
);
}

/**
Expand All @@ -91,11 +103,13 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->phpAttributeAnalyzer->hasPhpAttributes($node, [
'PHPUnit\\Framework\\Attributes\\CoversNothing',
'PHPUnit\\Framework\\Attributes\\CoversClass',
'PHPUnit\\Framework\\Attributes\\CoversFunction',
])) {
if (
$this->phpAttributeAnalyzer->hasPhpAttributes($node, [
'PHPUnit\\Framework\\Attributes\\CoversNothing',
'PHPUnit\\Framework\\Attributes\\CoversClass',
'PHPUnit\\Framework\\Attributes\\CoversFunction',
])
) {
return null;
}

Expand All @@ -113,24 +127,45 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::countBetween($configuration, 0, 1);

if (isset($configuration[0])) {
Assert::isInstanceOf($configuration[0], TestClassSuffixesConfig::class);
$this->testClassSuffixes = $configuration[0]->getSuffixes();
}
}

/**
* @return string[]
*/
private function resolveSourceClassNames(string $className): array
{
$classNameParts = explode('\\', $className);
$partCount = count($classNameParts);
$classNameParts[$partCount - 1] = preg_replace(['#TestCase$#', '#Test$#'], '', $classNameParts[$partCount - 1]);

// Sort suffixes by length (longest first) to ensure more specific patterns match first
$sortedSuffixes = $this->testClassSuffixes;
usort($sortedSuffixes, static fn (string $a, string $b): int => strlen($b) <=> strlen($a));

$patterns = [];
foreach ($sortedSuffixes as $sortedSuffix) {
$patterns[] = '#' . preg_quote($sortedSuffix, '#') . '$#';
}

$classNameParts[$partCount - 1] = preg_replace($patterns, '', $classNameParts[$partCount - 1]);

$possibleTestClassNames = [implode('\\', $classNameParts)];

$partsWithoutTests = array_filter(
$classNameParts,
static fn (string|null $part): bool => $part === null ? false : ! in_array(
strtolower($part),
['test', 'tests'],
true
),
static fn (string|null $part): bool => $part === null
? false
: ! in_array(strtolower($part), ['test', 'tests'], true)
);

$possibleTestClassNames[] = implode('\\', $partsWithoutTests);
Expand Down
24 changes: 24 additions & 0 deletions src/ValueObject/TestClassSuffixesConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\ValueObject;

final readonly class TestClassSuffixesConfig
{
/**
* @param string[] $suffixes
*/
public function __construct(
private array $suffixes = ['Test', 'TestCase']
) {
}

/**
* @return string[]
*/
public function getSuffixes(): array
{
return $this->suffixes;
}
}