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
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
{
}
}

Loading