-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHPUnit] Add AddProphecyTraitRector
- Loading branch information
1 parent
3be8fc5
commit e23321b
Showing
24 changed files
with
300 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
services: | ||
Rector\PHPUnit\Rector\Class_\AddProphecyTraitRector: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
rules/phpunit/src/Rector/Class_/AddProphecyTraitRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use Rector\Core\PhpParser\Node\Manipulator\ClassInsertManipulator; | ||
use Rector\Core\PhpParser\Node\Manipulator\ClassManipulator; | ||
use Rector\Core\Rector\AbstractPHPUnitRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/4142 | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/4141 | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/4149 | ||
* | ||
* @see \Rector\PHPUnit\Tests\Rector\Class_\AddProphecyTraitRector\AddProphecyTraitRectorTest | ||
*/ | ||
final class AddProphecyTraitRector extends AbstractPHPUnitRector | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const PROPHECY_TRAIT = 'Prophecy\PhpUnit\ProphecyTrait'; | ||
|
||
/** | ||
* @var ClassInsertManipulator | ||
*/ | ||
private $classInsertManipulator; | ||
|
||
/** | ||
* @var ClassManipulator | ||
*/ | ||
private $classManipulator; | ||
|
||
public function __construct(ClassManipulator $classManipulator, ClassInsertManipulator $classInsertManipulator) | ||
{ | ||
$this->classInsertManipulator = $classInsertManipulator; | ||
$this->classManipulator = $classManipulator; | ||
} | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Add Prophecy trait for method using $this->prophesize()', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
use PHPUnit\Framework\TestCase; | ||
final class ExampleTest extends TestCase | ||
{ | ||
public function testOne(): void | ||
{ | ||
$prophecy = $this->prophesize(\AnInterface::class); | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
final class ExampleTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
public function testOne(): void | ||
{ | ||
$prophecy = $this->prophesize(\AnInterface::class); | ||
} | ||
} | ||
PHP | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkipClass($node)) { | ||
return null; | ||
} | ||
|
||
$this->classInsertManipulator->addAsFirstTrait($node, self::PROPHECY_TRAIT); | ||
|
||
return $node; | ||
} | ||
|
||
private function hasProphesizeMethodCall(Class_ $node): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findFirst($node, function (Node $node) { | ||
return $this->isMethodCall($node, 'this', 'prophesize'); | ||
}); | ||
} | ||
|
||
private function shouldSkipClass(Class_ $class): bool | ||
{ | ||
if (! $this->isInTestClass($class)) { | ||
return true; | ||
} | ||
|
||
$hasProphesizeMethodCall = $this->hasProphesizeMethodCall($class); | ||
if ($hasProphesizeMethodCall === false) { | ||
return true; | ||
} | ||
|
||
return $this->classManipulator->hasTrait($class, self::PROPHECY_TRAIT); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
rules/phpunit/tests/Rector/Class_/AddProphecyTraitRector/AddProphecyTraitRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\Class_\AddProphecyTraitRector; | ||
|
||
use Iterator; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\PHPUnit\Rector\Class_\AddProphecyTraitRector; | ||
|
||
final class AddProphecyTraitRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $file): void | ||
{ | ||
$this->doTestFile($file); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return AddProphecyTraitRector::class; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
rules/phpunit/tests/Rector/Class_/AddProphecyTraitRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\Class_\AddProphecyTraitRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ExampleTest extends TestCase | ||
{ | ||
public function testOne(): void | ||
{ | ||
$prophecy = $this->prophesize(\AnInterface::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\Class_\AddProphecyTraitRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ExampleTest extends TestCase | ||
{ | ||
use \Prophecy\PhpUnit\ProphecyTrait; | ||
public function testOne(): void | ||
{ | ||
$prophecy = $this->prophesize(\AnInterface::class); | ||
} | ||
} | ||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
.../phpunit/tests/Rector/Class_/AddProphecyTraitRector/Fixture/skip_if_already_added.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\Class_\AddProphecyTraitRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
final class SkipIfAlreadyAdded extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testOne(): void | ||
{ | ||
$prophecy = $this->prophesize(\AnInterface::class); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
rules/phpunit/tests/Rector/Class_/AddProphecyTraitRector/Fixture/skip_non_prophesize.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\Class_\AddProphecyTraitRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class SkipNonProphesizeTest extends TestCase | ||
{ | ||
public function testOne(): void | ||
{ | ||
$that = new stdClass(); | ||
$prophecy = $that->prophesize(\AnInterface::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.