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
57 changes: 57 additions & 0 deletions config/sets/composer-based.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\RequiresAnnotationWithValueToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\TicketAnnotationToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DependsAnnotationWithValueToAttributeRector;
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector;
use Rector\PHPUnit\ValueObject\AnnotationWithValueToAttribute;

/**
* Annotation to attribute pairs bound to the PHPUnit version installed in the analysed project,
* as not every attribute exists in every PHPUnit version.
*/
return static function (RectorConfig $rectorConfig): void {
// each of these rules declares the "phpunit/phpunit" version its attributes were added in,
// @see ComposerPackageConstraintInterface
$rectorConfig->rules([
TicketAnnotationToAttributeRector::class,
TestWithAnnotationToAttributeRector::class,
DataProviderAnnotationToAttributeRector::class,
CoversAnnotationWithValueToAttributeRector::class,
RequiresAnnotationWithValueToAttributeRector::class,
DependsAnnotationWithValueToAttributeRector::class,
]);

// both attributes were added in PHPUnit 10.0
$rectorConfig->ruleWithConfigurationComposerVersionBound(AnnotationWithValueToAttributeRector::class, [
// the PHPUnit 10 spelling of the "backupStaticAttributes" annotation
new AnnotationWithValueToAttribute(
'backupStaticProperties',
'PHPUnit\Framework\Attributes\BackupStaticProperties',
[
'enabled' => true,
'disabled' => false,
]
),
new AnnotationWithValueToAttribute(
'excludeGlobalVariableFromBackup',
'PHPUnit\Framework\Attributes\ExcludeGlobalVariableFromBackup'
),
], 'phpunit/phpunit', '>=10.0');

// the RunClassInSeparateProcess attribute was added in PHPUnit 10.0 and removed in PHPUnit 13.0
$rectorConfig->ruleWithConfigurationComposerVersionBound(AnnotationToAttributeRector::class, [
new AnnotationToAttribute(
'runClassInSeparateProcess',
'PHPUnit\Framework\Attributes\RunClassInSeparateProcess'
),
], 'phpunit/phpunit', '>=10.0 <13.0');
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\PhpVersion;
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;

/**
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector\DataProviderAnnotationToAttributeRectorTest
*/
final class DataProviderAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class DataProviderAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
Expand Down Expand Up @@ -84,6 +86,11 @@ public function getNodeTypes(): array
return [ClassMethod::class];
}

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

public function provideMinPhpVersion(): int
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
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;

/**
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\DependsAnnotationWithValueToAttributeRector\DependsAnnotationWithValueToAttributeRectorTest
*/
final class DependsAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class DependsAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
private const string DEPENDS_ATTRIBUTE = 'PHPUnit\Framework\Attributes\Depends';

Expand Down Expand Up @@ -85,6 +87,11 @@ public function getNodeTypes(): array
return [Class_::class];
}

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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;

Expand All @@ -28,7 +30,7 @@
*
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector\TestWithAnnotationToAttributeRectorTest
*/
final class TestWithAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class TestWithAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
private const string TEST_WITH_ATTRIBUTE = 'PHPUnit\Framework\Attributes\TestWith';

Expand Down Expand Up @@ -148,6 +150,11 @@ public function refactor(Node $node): ?Node
return $node;
}

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
use Rector\PHPUnit\ValueObject\AnnotationWithValueToAttribute;
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\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector\AnnotationWithValueToAttributeRectorTest
*/
final class AnnotationWithValueToAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface
final class AnnotationWithValueToAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface, ComposerPackageConstraintInterface
{
/**
* @var AnnotationWithValueToAttribute[]
Expand Down Expand Up @@ -89,6 +91,11 @@ public function getNodeTypes(): array
return [Class_::class];
}

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
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;

/**
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\CoversAnnotationWithValueToAttributeRectorTest
*/
final class CoversAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class CoversAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
private const string COVERS_FUNCTION_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversFunction';

Expand Down Expand Up @@ -94,6 +96,11 @@ public function getNodeTypes(): array
return [Class_::class, ClassMethod::class];
}

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
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;

/**
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\RequiresAnnotationWithValueToAttributeRector\RequiresAnnotationWithValueToAttributeRectorTest
*/
final class RequiresAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class RequiresAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
public function __construct(
private readonly PhpDocTagRemover $phpDocTagRemover,
Expand Down Expand Up @@ -84,6 +86,11 @@ public function getNodeTypes(): array
return [Class_::class, ClassMethod::class];
}

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
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;

Expand All @@ -31,7 +33,7 @@
*
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\TicketAnnotationToAttributeRector\TicketAnnotationToAttributeRectorTest
*/
final class TicketAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface
final class TicketAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
{
private const string TICKET_CLASS = 'PHPUnit\Framework\Attributes\Ticket';

Expand Down Expand Up @@ -81,6 +83,11 @@ public function getNodeTypes(): array
return [Class_::class, ClassMethod::class];
}

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

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
2 changes: 2 additions & 0 deletions src/Set/PHPUnitSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ final class PHPUnitSetList
public const string PHPUNIT_NARROW_ASSERTS = __DIR__ . '/../../config/sets/phpunit-narrow-asserts.php';

public const string ANNOTATIONS_TO_ATTRIBUTES = __DIR__ . '/../../config/sets/annotations-to-attributes.php';

public const string COMPOSER_BASED = __DIR__ . '/../../config/sets/composer-based.php';
}
28 changes: 28 additions & 0 deletions tests/ComposerBased/ComposerBasedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\ComposerBased;

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

final class ComposerBasedTest 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/composer_based.php';
}
}
41 changes: 41 additions & 0 deletions tests/ComposerBased/Fixture/backup_static_properties.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use PHPUnit\Framework\TestCase;

class BackupStaticPropertiesTest extends TestCase
{
/**
* @backupStaticProperties enabled
*/
public function testSomething()
{
}

/**
* @excludeGlobalVariableFromBackup someGlobalVariable
*/
public function testSomethingElse()
{
}
}

?>
-----
<?php

use PHPUnit\Framework\TestCase;

class BackupStaticPropertiesTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\BackupStaticProperties(true)]
public function testSomething()
{
}

#[\PHPUnit\Framework\Attributes\ExcludeGlobalVariableFromBackup('someGlobalVariable')]
public function testSomethingElse()
{
}
}

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

use PHPUnit\Framework\TestCase;

/**
* @runClassInSeparateProcess
*/
class RunClassInSeparateProcessTest extends TestCase
{
public function testSomething()
{
}
}
10 changes: 10 additions & 0 deletions tests/ComposerBased/config/composer_based.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\Set\PHPUnitSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([PHPUnitSetList::COMPOSER_BASED]);
};
Loading