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
5 changes: 4 additions & 1 deletion bridge/rector/FEATURE_PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

abstract class AnonymousSetBaseTestCase extends \PHPUnit\Framework\TestCase
{
}

final class AnonymousSetFactory
{
public function make(): object
{
return new class extends AnonymousSetBaseTestCase {
protected function setUp(): void
{
parent::setUp();
parent::markTestSkipped('later');
}
};
}
}
-----
<?php

abstract class AnonymousSetBaseTestCase
{
}

final class AnonymousSetFactory
{
public function make(): object
{
return new class extends AnonymousSetBaseTestCase {
#[\Testo\Lifecycle\BeforeTest]
protected function setUp(): void
{
throw new \Testo\Core\Exception\SkipTest('later');
}
};
}
}
29 changes: 20 additions & 9 deletions bridge/rector/src/Internal/PhpunitTestCaseClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Testo\Bridge\Rector\Internal;

use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeNameResolver\NodeNameResolver;

Expand Down Expand Up @@ -39,13 +40,13 @@
*/
public function phpunitBaseFromVendor(Class_ $class): ?array
{
$name = $class->namespacedName?->toString();
if ($name === null || $this->extendsDirectly($class) || !$this->reflectionProvider->hasClass($name)) {
$base = $this->extendsDirectly($class) ? null : $this->base($class);
if ($base === null) {

Check warning on line 44 in bridge/rector/src/Internal/PhpunitTestCaseClass.php

View check run for this annotation

Codecov / codecov/patch

bridge/rector/src/Internal/PhpunitTestCaseClass.php#L43-L44

Added lines #L43 - L44 were not covered by tests
return null;
}

$direct = true;
foreach ($this->reflectionProvider->getClass($name)->getParents() as $parent) {
foreach ([$base, ...$base->getParents()] as $parent) {

Check warning on line 49 in bridge/rector/src/Internal/PhpunitTestCaseClass.php

View check run for this annotation

Codecov / codecov/patch

bridge/rector/src/Internal/PhpunitTestCaseClass.php#L49

Added line #L49 was not covered by tests
if ($parent->getName() === self::TEST_CASE) {
return null;
}
Expand All @@ -70,13 +71,23 @@
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);

Check warning on line 82 in bridge/rector/src/Internal/PhpunitTestCaseClass.php

View check run for this annotation

Codecov / codecov/patch

bridge/rector/src/Internal/PhpunitTestCaseClass.php#L82

Added line #L82 was not covered by tests

return $this->reflectionProvider->getClass($name)->isSubclassOf(self::TEST_CASE);
return $name !== null && $this->reflectionProvider->hasClass($name)
? $this->reflectionProvider->getClass($name)
: null;

Check warning on line 86 in bridge/rector/src/Internal/PhpunitTestCaseClass.php

View check run for this annotation

Codecov / codecov/patch

bridge/rector/src/Internal/PhpunitTestCaseClass.php#L84-L86

Added lines #L84 - L86 were not covered by tests
}

private function inheritsTestCase(Class_ $class): bool
{
return $this->base($class)?->isSubclassOf(self::TEST_CASE) ?? false;

Check warning on line 91 in bridge/rector/src/Internal/PhpunitTestCaseClass.php

View check run for this annotation

Codecov / codecov/patch

bridge/rector/src/Internal/PhpunitTestCaseClass.php#L91

Added line #L91 was not covered by tests
}
}
69 changes: 42 additions & 27 deletions bridge/rector/src/PhpunitToTesto/ExtendsTestCaseToTestoRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

abstract class AnonymousProjectBaseTestCase extends \PHPUnit\Framework\TestCase
{
}

final class AnonymousOnProjectBaseFactory
{
public function make(): object
{
return new class extends AnonymousProjectBaseTestCase {
#[\Override]
protected function setUp(): void
{
parent::setUp();
parent::markTestSkipped('later');
}

public function testFoo(): void
{
}
};
}
}
-----
<?php

abstract class AnonymousProjectBaseTestCase
{
}

final class AnonymousOnProjectBaseFactory
{
public function make(): object
{
return new class extends AnonymousProjectBaseTestCase {
protected function setUp(): void
{
self::markTestSkipped('later');
}

#[\Testo\Test]
public function testFoo(): void
{
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

final class AnonymousOnVendorBaseFactory
{
public function make(): object
{
return new class extends \Tests\Bridge\Rector\Stub\VendorTestCase {
protected function setUp(): void
{
parent::setUp();
}

public function testFoo(): void
{
}
};
}
}
-----
<?php

final class AnonymousOnVendorBaseFactory
{
public function make(): object
{
return new class extends \Tests\Bridge\Rector\Stub\VendorTestCase {
protected function setUp(): void
{
parent::setUp();
}

#[\Testo\Test]
public function testFoo(): void
{
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

abstract class OverrideProjectBaseTestCase extends \PHPUnit\Framework\TestCase
{
protected function boot(): void
{
}

#[\Override]
protected function tearDown(): void
{
}
}

final class OverrideProjectBaseChildTest extends OverrideProjectBaseTestCase
{
#[\Override]
protected function setUp(): void
{
}

#[\Override]
protected function tearDown(): void
{
}

#[\Override]
protected function boot(): void
{
}

public function testFoo(): void
{
}
}
-----
<?php

abstract class OverrideProjectBaseTestCase
{
protected function boot(): void
{
}

protected function tearDown(): void
{
}
}

final class OverrideProjectBaseChildTest extends OverrideProjectBaseTestCase
{
protected function setUp(): void
{
}

#[\Override]
protected function tearDown(): void
{
}

#[\Override]
protected function boot(): void
{
}

#[\Testo\Test]
public function testFoo(): void
{
}
}
Loading
Loading