diff --git a/bridge/rector/FEATURE_PARITY.md b/bridge/rector/FEATURE_PARITY.md index 4e1540f1..88afa5d4 100644 --- a/bridge/rector/FEATURE_PARITY.md +++ b/bridge/rector/FEATURE_PARITY.md @@ -107,7 +107,10 @@ like), or to the PHPUnit method its enclosing method overrides, is dropped; a ca assertion of PHPUnit's `Assert` becomes `\PHPUnit\Framework\Assert::assertX()`, which the assertion rules convert when Testo maps it and which stays a valid PHPUnit call otherwise; any other call, such as `parent::markTestSkipped()` or `parent::expectException()`, becomes `self::`/`$this->` and converts -like that spelling. An anonymous class extending `TestCase` directly is handled the same way. The mirror +like that spelling. An anonymous class extending `TestCase`, directly or through a project base, is +handled the same way. `#[\Override]` is dropped from a method that only PHPUnit declares once the +chain is detached (a `setUp()` no local base overrides); one the base or an interface declares keeps +it. The mirror `TestClassToTestCaseRector` (Testo → PHPUnit) adds `extends \PHPUnit\Framework\TestCase` and, for a class-level `#[\Testo\Test]`, removes it and adds a per-method `#[\PHPUnit\Framework\Attributes\Test]` to every public, non-static, `void`/`never` method (mirroring Testo's locator, so static/`iterable` diff --git a/bridge/rector/config/phpunit-to-testo/anonymous_class_on_project_base_converted.php.inc b/bridge/rector/config/phpunit-to-testo/anonymous_class_on_project_base_converted.php.inc new file mode 100644 index 00000000..799a6647 --- /dev/null +++ b/bridge/rector/config/phpunit-to-testo/anonymous_class_on_project_base_converted.php.inc @@ -0,0 +1,39 @@ +namespacedName?->toString(); - if ($name === null || $this->extendsDirectly($class) || !$this->reflectionProvider->hasClass($name)) { + $base = $this->extendsDirectly($class) ? null : $this->base($class); + if ($base === null) { return null; } $direct = true; - foreach ($this->reflectionProvider->getClass($name)->getParents() as $parent) { + foreach ([$base, ...$base->getParents()] as $parent) { if ($parent->getName() === self::TEST_CASE) { return null; } @@ -70,13 +71,23 @@ public function extendsDirectly(Class_ $class): bool return $class->extends !== null && $this->nodeNameResolver->isName($class->extends, self::TEST_CASE); } - private function inheritsTestCase(Class_ $class): bool + /** + * The class's direct base as loaded from source, reflected from the name it extends: an anonymous + * class has no name of its own to reflect. + * + * @return ClassReflection|null Null when the class extends nothing or its base cannot be resolved. + */ + public function base(Class_ $class): ?ClassReflection { - $name = $class->namespacedName?->toString(); - if ($name === null || !$this->reflectionProvider->hasClass($name)) { - return false; - } + $name = $class->extends === null ? null : $this->nodeNameResolver->getName($class->extends); - return $this->reflectionProvider->getClass($name)->isSubclassOf(self::TEST_CASE); + return $name !== null && $this->reflectionProvider->hasClass($name) + ? $this->reflectionProvider->getClass($name) + : null; + } + + private function inheritsTestCase(Class_ $class): bool + { + return $this->base($class)?->isSubclassOf(self::TEST_CASE) ?? false; } } diff --git a/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector.php b/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector.php index beb5802a..4016b0d2 100644 --- a/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector.php +++ b/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector.php @@ -52,7 +52,7 @@ * its own enclosing method overrides, is dropped, and any other call, such as * `parent::markTestSkipped()`, becomes `self::`/`$this->` for the skip and expectation rules to * convert. A call whose result is used is left for the manual pass. An anonymous class that - * extends `TestCase` directly is handled the same way; one on a project base is left untouched. + * extends `TestCase` directly is handled the same way. * * "Test method" mirrors PHPUnit's own discovery: a method carrying the PHPUnit * `#[\PHPUnit\Framework\Attributes\Test]` attribute, a method with a `@test` docblock @@ -62,16 +62,18 @@ * idempotent — a method that already carries `#[\Testo\Test]` is left as-is. * * A class that reaches `TestCase` through an intermediate base (`extends RuleTestCase`) keeps its - * `extends` and its `#[\Override]` attributes — the base is still there, converted on its own — - * and only gains `#[\Testo\Test]` on its test methods. Without them Testo would not discover the - * subclass's tests at all. Its `parent::` calls to a PHPUnit method the base does not override are - * rewritten the same way. A base that cannot be resolved, or does not lead to `TestCase`, leaves the - * class untouched. + * `extends` — the base is still there, converted on its own — and gains `#[\Testo\Test]` on its test + * methods. Without them Testo would not discover the subclass's tests at all. It keeps `#[\Override]` + * where the base or one of the class's interfaces declares the method; on a method only PHPUnit + * declares, such as a `setUp()` the base does not override, the attribute goes, as it does for a + * direct subclass. Its `parent::` calls to a PHPUnit method the base does not override are rewritten + * the same way. An anonymous class on such a base is handled alike. A base that cannot be resolved, + * or does not lead to `TestCase`, leaves the class untouched. * * A base from outside the processed paths, such as a framework's test case in vendor, stays a * PHPUnit class. Its subclasses are converted all the same, keep their `parent::` calls, and the * class extending that base gets `#[\Testo\Skip]` with a reason naming it, until the base is - * rewritten for Testo. + * rewritten for Testo. An anonymous class gets no skip: Testo never discovers it. * * A trait gets `#[\Testo\Test]` on the same test methods, since the classes that use it are out of * sight: a test method a PHPUnit class takes from a trait would otherwise go undiscovered. Abstract @@ -175,20 +177,27 @@ public function refactor(Node $node): ?Node # Indirect subclass: the base stays, only discovery needs the attribute. $vendorBase = $this->testCaseClass->phpunitBaseFromVendor($node); - $base = $vendorBase === null ? $this->base($node) : null; + $base = $vendorBase === null ? $this->testCaseClass->base($node) : null; + $interfaces = $this->nodeNameResolver->getNames($node->implements); $changed = false; foreach ($node->getMethods() as $method) { $method->isPublic() && $this->markTestMethod($method) and $changed = true; $base !== null && $this->rewritePhpunitParentCalls($base, $method) and $changed = true; + $base !== null + && $this->overridesPhpunitOnly($base, $method) + && !$this->isDeclaredByInterface($method, $interfaces) + && $this->removeOverrideAttribute($method) + and $changed = true; } - $vendorBase !== null && $vendorBase['direct'] && $this->skipOnVendorBase($node, $vendorBase['name']) + $vendorBase !== null && $vendorBase['direct'] && !$node->isAnonymous() + && $this->skipOnVendorBase($node, $vendorBase['name']) and $changed = true; return $changed ? $node : null; } - $base = $this->base($node); + $base = $this->testCaseClass->base($node); $node->extends = null; $interfaces = $this->nodeNameResolver->getNames($node->implements); @@ -232,21 +241,6 @@ private function skipOnVendorBase(Class_ $class, string $vendorBase): bool return true; } - /** - * The class's direct base as loaded from source, read from the name it extends before its - * `extends` is removed: an anonymous class has no name of its own to reflect. - * - * @return ClassReflection|null Null when the base cannot be resolved. - */ - private function base(Class_ $class): ?ClassReflection - { - $base = $class->extends === null ? null : $this->getName($class->extends); - - return $base !== null && $this->reflectionProvider->hasClass($base) - ? $this->reflectionProvider->getClass($base) - : null; - } - /** * Rewrites the `parent::method()` statements that resolve into PHPUnit, which the converted chain * no longer extends: without a parent PHP rejects `parent` at compile time, and on a converted base @@ -414,19 +408,40 @@ private function isDeclaredByInterface(ClassMethod $method, array $interfaces): return false; } - private function removeOverrideAttribute(ClassMethod $method): void + /** + * Whether the method overrides one that only PHPUnit declares: once the base is detached from + * `TestCase`, its `#[\Override]` has no parent method left and PHP rejects the class on load. + */ + private function overridesPhpunitOnly(ClassReflection $base, ClassMethod $method): bool + { + $native = $base->getNativeReflection(); + $name = $method->name->toString(); + + return $native->hasMethod($name) + && \str_starts_with($native->getMethod($name)->getDeclaringClass()->getName(), 'PHPUnit\\'); + } + + /** + * @return bool Whether an `#[\Override]` attribute was removed. + */ + private function removeOverrideAttribute(ClassMethod $method): bool { # Trim groups in place: a rebuilt group would lose its source position. + $removed = false; $keptGroups = []; foreach ($method->attrGroups as $attrGroup) { - $attrGroup->attrs = \array_values(\array_filter( + $kept = \array_values(\array_filter( $attrGroup->attrs, fn(Attribute $attr): bool => !$this->isName($attr->name, 'Override'), )); + \count($kept) === \count($attrGroup->attrs) or $removed = true; + $attrGroup->attrs = $kept; $attrGroup->attrs === [] or $keptGroups[] = $attrGroup; } $method->attrGroups = $keptGroups; DroppedAttributes::keepFormat($method, $this->originalTokens()); + + return $removed; } } diff --git a/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector/anonymous_class_on_project_base.php.inc b/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector/anonymous_class_on_project_base.php.inc new file mode 100644 index 00000000..bcbd4aab --- /dev/null +++ b/bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector/anonymous_class_on_project_base.php.inc @@ -0,0 +1,48 @@ + Rector detaches each class from `TestCase` and marks its test methods with `#[\Testo\Test]`, including -> subclasses of a project base class. Of its `parent::` statements that call into PHPUnit, a -> `parent::setUp();`-style hook call is dropped, as is a call inside an override of that same method -> (a wrapper around `expectException()` loses its delegation), and any other, such as +> subclasses of a project base class, named or anonymous. Of its `parent::` statements that call +> into PHPUnit, a `parent::setUp();`-style hook call is dropped, as is a call inside an override of +> that same method (a wrapper around `expectException()` loses its delegation), and any other, such as > `parent::markTestSkipped()`, becomes `self::`/`$this->` for the skip and expectation rules to -> convert. A class on a PHPUnit base from `vendor/` (a framework's `TestCase`) is converted as well, +> convert. `#[\Override]` goes from a method only PHPUnit declares, such as a `setUp()` no local base +> overrides. A class on a PHPUnit base from `vendor/` (a framework's `TestCase`) is converted as well, > but the class extending that base gets `#[\Testo\Skip]` naming it, since the base still runs on > PHPUnit. > Tests inherited from such a base are not converted. `scan-residuals.php` and the pitfalls in the