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
2 changes: 2 additions & 0 deletions config/sets/phpunit100.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\AddProphecyTraitRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\PublicDataProviderClassMethodRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\StaticDataProviderClassMethodRector;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
Expand All @@ -23,6 +24,7 @@
WithConsecutiveRector::class,
RemoveSetMethodsMethodCallRector::class,
PropertyExistsWithoutAssertRector::class,
ParentTestClassConstructorRector::class,
]);

$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MockingHelper extends TestCase
{
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MockingHelper extends TestCase
{
public function __construct()
{
parent::__construct(static::class);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;

use PHPUnit\Framework\TestCase;

abstract class SkipAbstractClasses extends TestCase
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipAlreadyAdded extends TestCase
{
public function __construct()
{
parent::__construct(static::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipInCaseOfTest extends TestCase
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipInCaseOfTestCase extends TestCase
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ParentTestClassConstructorRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ParentTestClassConstructorRector::class);
};
124 changes: 124 additions & 0 deletions rules/PHPUnit100/Rector/Class_/ParentTestClassConstructorRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit100\Rector\Class_;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/sebastianbergmann/phpunit/issues/3975
* @see https://github.com/sebastianbergmann/phpunit/commit/705874f1b867fd99865e43cb5eaea4e6d141582f
*
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\ParentTestClassConstructorRectorTest
*/
final class ParentTestClassConstructorRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('PHPUnit\Framework\TestCase requires a parent constructor call', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeHelper extends TestCase
{
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeHelper extends TestCase
{
public function __construct()
{
parent::__construct(static::class);
}
}
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if ($this->shouldSkipClass($node)) {
return null;
}

// it already has a constructor, skip as it might require specific tweaking
if ($node->getMethod(MethodName::CONSTRUCT)) {
return null;
}

$constructorClassMethod = new ClassMethod(MethodName::CONSTRUCT);
$constructorClassMethod->flags |= Modifiers::PUBLIC;
$constructorClassMethod->stmts[] = new Expression($this->createParentConstructorCall());

$node->stmts = array_merge([$constructorClassMethod], $node->stmts);

return $node;
}

private function createParentConstructorCall(): StaticCall
{
$staticClassConstFetch = new ClassConstFetch(new Name('static'), 'class');

return new StaticCall(new Name('parent'), MethodName::CONSTRUCT, [new Arg($staticClassConstFetch)]);
}

private function shouldSkipClass(Class_ $class): bool
{
if ($class->isAbstract()) {
return true;
}

if ($class->isAnonymous()) {
return true;
}

$className = $this->getName($class);

// loaded automatically by PHPUnit
if (str_ends_with((string) $className, 'Test')) {
return true;
}

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