Skip to content

Commit

Permalink
[AnnotationsToAttributes] Fix @Covers qualified::method conversion (#451
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andrewnicols authored Jan 29, 2025
1 parent 0ba734c commit e7e84da
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase;

/**
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::someMethod
*/
final class CoversClass extends TestCase
{
Expand All @@ -23,6 +24,7 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnot
use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class)]
#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::class, 'someMethod')]
final class CoversClass extends TestCase
{
public function test()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ final class CoversMethod extends TestCase
public function test()
{
}

/**
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::someMethod
*/
public function test_foo()
{
}
}

?>
Expand All @@ -23,11 +30,16 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnot
use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class)]
#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::class, 'someMethod')]
final class CoversMethod extends TestCase
{
public function test()
{
}

public function test_foo()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class CoversAnnotationWithValueToAttributeRector extends AbstractRector im
*/
private const COVERTS_CLASS_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversClass';

/**
* @var string
*/
private const COVERS_METHOD_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversMethod';

public function __construct(
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
Expand Down Expand Up @@ -139,13 +144,16 @@ private function createAttributeGroup(string $annotationValue): AttributeGroup
{
if (str_starts_with($annotationValue, '::')) {
$attributeClass = self::COVERS_FUNCTION_ATTRIBUTE;
$attributeValue = trim($annotationValue, ':()');
$attributeValue = [trim($annotationValue, ':()')];
} elseif (str_contains($annotationValue, '::')) {
$attributeClass = self::COVERS_METHOD_ATTRIBUTE;
$attributeValue = [$this->getClass($annotationValue) . '::class', $this->getMethod($annotationValue)];
} else {
$attributeClass = self::COVERTS_CLASS_ATTRIBUTE;
$attributeValue = trim($annotationValue) . '::class';
$attributeValue = [trim($annotationValue) . '::class'];
}

return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [$attributeValue]);
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, $attributeValue);
}

/**
Expand Down Expand Up @@ -235,7 +243,6 @@ private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCove

$covers = $desiredTagValueNode->value->value;
if (str_starts_with($covers, '\\')) {
$covers = $this->getClass($covers);
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
} elseif (! $hasCoversDefault && str_starts_with($covers, '::')) {
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
Expand Down Expand Up @@ -271,4 +278,9 @@ private function getClass(string $classWithMethod): string
{
return Strings::replace($classWithMethod, '/::.*$/');
}

private function getMethod(string $classWithMethod): string
{
return Strings::replace($classWithMethod, '/^.*::/');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private function isTestFilePath(FuncCall $funcCall): bool
if (str_ends_with($className, 'Test')) {
return true;
}

return str_ends_with($className, 'TestCase');
}

Expand Down
3 changes: 1 addition & 2 deletions tests/Issues/DoubleAnnotation/Fixture/some_test.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Rector\PHPUnit\Tests\Issues\DoubleAnnotation\Fixture;
use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector\Fixture\SomeClass;

#[\PHPUnit\Framework\Attributes\CoversClass(\SomeClass::class)]
#[\PHPUnit\Framework\Attributes\CoversMethod(\SomeClass::class, 'method')]
final class SomeTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('generatorInput')]
Expand All @@ -42,4 +42,3 @@ final class SomeTest extends TestCase
{
}
}

0 comments on commit e7e84da

Please sign in to comment.