Skip to content

Commit 30b389a

Browse files
irosnersamsonasik
andauthored
fix phpunit covers default class with short covers methods (#588)
* fix phpunit covers default class with short covers methods * Fix phpstan * fix cs * unify logic --------- Co-authored-by: Abdul Malik Ikhsan <[email protected]>
1 parent 153312c commit 30b389a

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_class_default.php.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnot
3636
use PHPUnit\Framework\TestCase;
3737

3838
#[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class)]
39+
#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class, 'foo')]
3940
#[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\HelperClass::class)]
41+
#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class, 'bar')]
4042
final class CoversClassDefault extends TestCase
4143
{
4244
public function testFoo()

rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,20 @@ private function resolveClassAttributes(Class_ $class): array
186186
$coversGroups = [];
187187
$methodGroups = [];
188188
$hasCoversDefault = false;
189+
$coversDefaultClass = '';
189190
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
190191
if ($phpDocInfo instanceof PhpDocInfo) {
191192
$coversDefaultGroups = $this->handleCoversDefaultClass($phpDocInfo);
192193
// If there is a ::coversDefaultClass, @covers ::function will refer to class methods, otherwise it will refer to global functions.
193194
$hasCoversDefault = $coversDefaultGroups !== [];
195+
$coversDefaultClass = $hasCoversDefault ? $this->getName(
196+
$coversDefaultGroups[0]->attrs[0]->args[0]->value
197+
) : null;
194198
$coversGroups = $this->handleCovers($phpDocInfo, $hasCoversDefault);
195199
}
196200

197201
foreach ($class->getMethods() as $classMethod) {
198-
$methodGroups = [...$methodGroups, ...$this->resolveMethodAttributes($classMethod, $hasCoversDefault)];
202+
$methodGroups = [...$methodGroups, ...$this->resolveMethodAttributes($classMethod, $coversDefaultClass)];
199203
}
200204

201205
return [...$coversDefaultGroups, ...$coversGroups, ...$methodGroups];
@@ -260,8 +264,9 @@ private function handleCovers(PhpDocInfo $phpDocInfo, bool $hasCoversDefault): a
260264
/**
261265
* @return array<string, AttributeGroup>
262266
*/
263-
private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCoversDefault): array
267+
private function resolveMethodAttributes(ClassMethod $classMethod, ?string $coversDefaultClass): array
264268
{
269+
$hasCoversDefault = $coversDefaultClass !== null;
265270
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
266271
if (! $phpDocInfo instanceof PhpDocInfo) {
267272
return [];
@@ -275,16 +280,20 @@ private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCove
275280
}
276281

277282
$covers = $desiredTagValueNode->value->value;
278-
if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) {
279-
$attributeGroup = $this->createAttributeGroup($covers);
283+
if (! str_starts_with($covers, '\\') && ! str_starts_with($covers, '::')) {
284+
continue;
285+
}
280286

281-
// phpunit 10 may not fully support attribute
282-
if (! $attributeGroup instanceof AttributeGroup) {
283-
continue;
284-
}
287+
if ($hasCoversDefault && str_starts_with($covers, '::')) {
288+
$covers = $coversDefaultClass . $covers;
289+
}
285290

286-
$attributeGroups[$covers] = $attributeGroup;
291+
$attributeGroup = $this->createAttributeGroup($covers);
292+
if (! $attributeGroup instanceof AttributeGroup) {
293+
continue;
287294
}
295+
296+
$attributeGroups[$covers] = $attributeGroup;
288297
}
289298

290299
return $attributeGroups;

0 commit comments

Comments
 (0)