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
79 changes: 79 additions & 0 deletions config/sets/composer-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
use Rector\PHPUnit\PHPUnit110\Rector\ClassMethod\MockObjectArgCreateStubToCreateMockRector;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsForDataProviderRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector;
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
use Rector\PHPUnit\PHPUnit120\Rector\Property\MockObjectVarToStubRector;
use Rector\PHPUnit\ValueObject\AnnotationWithValueToAttribute;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;

/**
* Rules and configuration bound to the PHPUnit version installed in the analysed project,
Expand Down Expand Up @@ -61,6 +67,14 @@
// deprecated in PHPUnit 11.5
AssertContainsOnlyMethodCallRector::class,
AssertIsTypeMethodCallRector::class,

// the TestCase::__construct() is final since PHPUnit 12.0.3
RemoveOverrideFinalConstructTestCaseRector::class,

// the AllowMockObjectsWithoutExpectations attribute exists since PHPUnit 12.5.2
AllowMockObjectsWhereParentClassRector::class,
AllowMockObjectsForDataProviderRector::class,
AllowMockObjectsWithoutExpectationsAttributeRector::class,
]);

// both attributes were added in PHPUnit 10.0
Expand All @@ -87,4 +101,69 @@
'PHPUnit\Framework\Attributes\RunClassInSeparateProcess'
),
], 'phpunit/phpunit', '>=10.0 <13.0');

// the MockBuilder::onlyMethods() method was added in PHPUnit 8.3
// @see https://github.com/sebastianbergmann/phpunit/pull/3687
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
new MethodCallRename('PHPUnit\Framework\MockObject\MockBuilder', 'setMethods', 'onlyMethods'),
], 'phpunit/phpunit', '>=8.3');

// the expectExceptionMessageMatches() method was added in PHPUnit 8.4
// @see https://github.com/sebastianbergmann/phpunit/issues/3957
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
new MethodCallRename(
'PHPUnit\Framework\TestCase',
'expectExceptionMessageRegExp',
'expectExceptionMessageMatches'
),
], 'phpunit/phpunit', '>=8.4');

// all these assert methods were added in PHPUnit 9.1
// @see https://github.com/sebastianbergmann/phpunit/blob/9.1.0/ChangeLog-9.1.md
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
new MethodCallRename('PHPUnit\Framework\Assert', 'assertRegExp', 'assertMatchesRegularExpression'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertNotRegExp', 'assertDoesNotMatchRegularExpression'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertFileNotExists', 'assertFileDoesNotExist'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertFileNotIsReadable', 'assertFileIsNotReadable'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertDirectoryNotExists', 'assertDirectoryDoesNotExist'),
new MethodCallRename(
'PHPUnit\Framework\Assert',
'assertDirectoryNotIsReadable',
'assertDirectoryIsNotReadable'
),
new MethodCallRename(
'PHPUnit\Framework\Assert',
'assertDirectoryNotIsWritable',
'assertDirectoryIsNotWritable'
),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertNotIsReadable', 'assertIsNotReadable'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertNotIsWritable', 'assertIsNotWritable'),
], 'phpunit/phpunit', '>=9.1');

// the numberOfInvocations() method was added in PHPUnit 10.0
// @see https://github.com/sebastianbergmann/phpunit/commit/2ba8b7fded44a1a75cf5712a3b7310a8de0b6bb8
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
new MethodCallRename(
'PHPUnit\Framework\MockObject\Rule\InvocationOrder',
'getInvocationCount',
'numberOfInvocations'
),
], 'phpunit/phpunit', '>=10.0');

// the assertObjectHasProperty() and assertObjectNotHasProperty() methods were added in PHPUnit 10.1
// @see https://github.com/sebastianbergmann/phpunit/issues/5220
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
new MethodCallRename('PHPUnit\Framework\Assert', 'assertObjectHasAttribute', 'assertObjectHasProperty'),
new MethodCallRename('PHPUnit\Framework\Assert', 'assertObjectNotHasAttribute', 'assertObjectNotHasProperty'),
], 'phpunit/phpunit', '>=10.1');

// the expectExceptionMessageIsOrContains() method was added in PHPUnit 13.2
// @see https://github.com/sebastianbergmann/phpunit/issues/6560
$rectorConfig->ruleWithConfigurationComposerVersionBound(RenameMethodRector::class, [
new MethodCallRename(
'PHPUnit\Framework\TestCase',
'expectExceptionMessage',
'expectExceptionMessageIsOrContains'
),
], 'phpunit/phpunit', '>=13.2');
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
use Rector\PHPUnit\Enum\PHPUnitClassName;
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;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ChangeMockObjectReturnUnionToIntersectionRector\ChangeMockObjectReturnUnionToIntersectionRectorTest
*/
final class ChangeMockObjectReturnUnionToIntersectionRector extends AbstractRector
final class ChangeMockObjectReturnUnionToIntersectionRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
Expand All @@ -32,6 +34,11 @@ public function __construct(
) {
}

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

public function getNodeTypes(): array
{
return [ClassMethod::class];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\InlineStubPropertyToCreateStubMethodCallRectorTest
*/
final class InlineStubPropertyToCreateStubMethodCallRector extends AbstractRector
final class InlineStubPropertyToCreateStubMethodCallRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
Expand All @@ -44,6 +46,11 @@ public function __construct(
) {
}

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

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@
use PHPUnit\Framework\MockObject\MockObject;
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;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector\SingleMockPropertyTypeRectorTest
*/
final class SingleMockPropertyTypeRector extends AbstractRector
final class SingleMockPropertyTypeRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

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

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* The AllowMockObjectsWithoutExpectations attribute was added in PHPUnit 12.5.2
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsForDataProviderRector\AllowMockObjectsForDataProviderRectorTest
*
* @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
*/
final class AllowMockObjectsForDataProviderRector extends AbstractRector implements MinPhpVersionInterface
final class AllowMockObjectsForDataProviderRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
Expand All @@ -33,6 +39,11 @@ public function __construct(
) {
}

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

public function getNodeTypes(): array
{
return [Class_::class];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* The AllowMockObjectsWithoutExpectations attribute was added in PHPUnit 12.5.2
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector\AllowMockObjectsWhereParentClassRectorTest
*
* @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
*/
final class AllowMockObjectsWhereParentClassRector extends AbstractRector implements MinPhpVersionInterface
final class AllowMockObjectsWhereParentClassRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
/**
* @var string[]
Expand All @@ -41,6 +45,11 @@ public function __construct(
) {
}

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

public function getNodeTypes(): array
{
return [Class_::class];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* The AllowMockObjectsWithoutExpectations attribute was added in PHPUnit 12.5.2
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\AllowMockObjectsWithoutExpectationsAttributeRectorTest
*
* @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
*/
final class AllowMockObjectsWithoutExpectationsAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class AllowMockObjectsWithoutExpectationsAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
Expand All @@ -44,6 +48,11 @@ public function __construct(
) {
}

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

public function getNodeTypes(): array
{
return [Class_::class];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\ValueObject\ComposerPackageConstraint;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* The TestCase::__construct() was declared final in PHPUnit 12.0.3, overriding it is a fatal error since then
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector\RemoveOverrideFinalConstructTestCaseRectorTest
*
* @see https://github.com/sebastianbergmann/phpunit/commit/3263f4c62d4af44777ff1c81d093ee88eafccdad
*/
final class RemoveOverrideFinalConstructTestCaseRector extends AbstractRector
final class RemoveOverrideFinalConstructTestCaseRector extends AbstractRector implements ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

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

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use PHPUnit\Framework\TestCase;

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

?>
-----
<?php

use PHPUnit\Framework\TestCase;

final class RemoveOverrideFinalConstructTest extends TestCase
{
}

?>
29 changes: 29 additions & 0 deletions tests/ComposerBased/Fixture/version_bound_method_renames.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use PHPUnit\Framework\TestCase;

final class VersionBoundMethodRenamesTest extends TestCase
{
public function testSomething()
{
$this->assertObjectHasAttribute('someProperty', new stdClass());
$this->expectExceptionMessage('some message');
}
}

?>
-----
<?php

use PHPUnit\Framework\TestCase;

final class VersionBoundMethodRenamesTest extends TestCase
{
public function testSomething()
{
$this->assertObjectHasProperty('someProperty', new stdClass());
$this->expectExceptionMessageIsOrContains('some message');
}
}

?>
Loading