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
4 changes: 4 additions & 0 deletions config/sets/composer-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Rector\PHPUnit\PHPUnit110\Rector\Class_\NamedArgumentForDataProviderRector;
use Rector\PHPUnit\PHPUnit110\Rector\ClassMethod\ExpectsParamToMockObjectRector;
use Rector\PHPUnit\PHPUnit110\Rector\ClassMethod\MockObjectArgCreateStubToCreateMockRector;
use Rector\PHPUnit\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsForDataProviderRector;
Expand Down Expand Up @@ -106,6 +107,9 @@
// the TestCase::__construct() is final since PHPUnit 12.0.3
RemoveOverrideFinalConstructTestCaseRector::class,

// the any() matcher was deprecated in PHPUnit 12.5
AnyMatcherToNewAnyInvokedCountRector::class,

// the AllowMockObjectsWithoutExpectations attribute exists since PHPUnit 12.5.2
AllowMockObjectsWhereParentClassRector::class,
AllowMockObjectsForDataProviderRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector;

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

final class AnyMatcherToNewAnyInvokedCountRectorTest 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,31 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\Fixture;

use PHPUnit\Framework\TestCase;

final class Fixture extends TestCase
{
public function test(): void
{
$matcher = $this->any();
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\Fixture;

use PHPUnit\Framework\TestCase;

final class Fixture extends TestCase
{
public function test(): void
{
$matcher = new \PHPUnit\Framework\MockObject\Rule\AnyInvokedCount();
}
}

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

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\Fixture;

final class SkipNonTestCase
{
public function run(): void
{
$matcher = $this->any();
}

public function any(): string
{
return 'any';
}
}
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\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AnyMatcherToNewAnyInvokedCountRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit120\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* The TestCase::any() method was deprecated in PHPUnit 12.5
*
* @see https://github.com/sebastianbergmann/phpunit/issues/6461
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Assign\AnyMatcherToNewAnyInvokedCountRector\AnyMatcherToNewAnyInvokedCountRectorTest
*/
final class AnyMatcherToNewAnyInvokedCountRector extends AbstractRector implements ComposerPackageConstraintInterface
{
private const string ANY_INVOKED_COUNT_CLASS = 'PHPUnit\Framework\MockObject\Rule\AnyInvokedCount';

public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}

public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
return new ComposerPackageConstraint('phpunit/phpunit', '>=12.5');
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change deprecated `$this->any()` matcher assign to direct `new AnyInvokedCount()`',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test(): void
{
$matcher = $this->any();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
public function test(): void
{
$matcher = new AnyInvokedCount();
}
}
CODE_SAMPLE
),
]
);
}

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

/**
* @param Assign $node
*/
public function refactor(Node $node): ?Assign
{
if (! $node->expr instanceof MethodCall) {
return null;
}

$methodCall = $node->expr;
if ($methodCall->isFirstClassCallable()) {
return null;
}

if (! $this->isName($methodCall->name, 'any')) {
return null;
}

if ($methodCall->getArgs() !== []) {
return null;
}

if (! $this->testsNodeAnalyzer->isPHPUnitTestCaseCall($methodCall)) {
return null;
}

$node->expr = new New_(new FullyQualified(self::ANY_INVOKED_COUNT_CLASS));

return $node;
}
}
Loading