mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-30 16:00:52 +00:00
feature: add rule to refactor exception methods
see: https://github.com/sebastianbergmann/phpunit/issues/3775
This commit is contained in:
parent
650279493e
commit
5ef6073711
|
@ -1,2 +1,3 @@
|
|||
services:
|
||||
Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector: null
|
||||
Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector: null
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# All 446 Rectors Overview
|
||||
# All 447 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
|
@ -4815,6 +4815,31 @@ Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.
|
|||
|
||||
<br>
|
||||
|
||||
### `ExplicitPhpErrorApiRector`
|
||||
|
||||
- class: `Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector`
|
||||
|
||||
Use explicit API for expecting PHP errors, warnings, and notices
|
||||
|
||||
```diff
|
||||
final class SomeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
- $this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class);
|
||||
- $this->expectException(\PHPUnit\Framework\TestCase\Error::class);
|
||||
- $this->expectException(\PHPUnit\Framework\TestCase\Notice::class);
|
||||
- $this->expectException(\PHPUnit\Framework\TestCase\Warning::class);
|
||||
+ $this->expectDeprecation();
|
||||
+ $this->expectError();
|
||||
+ $this->expectNotice();
|
||||
+ $this->expectWarning();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `FixDataProviderAnnotationTypoRector`
|
||||
|
||||
- class: `Rector\PHPUnit\Rector\ClassMethod\FixDataProviderAnnotationTypoRector`
|
||||
|
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\PHPUnit\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use Rector\Rector\AbstractPHPUnitRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-9.0.md
|
||||
* @see https://github.com/sebastianbergmann/phpunit/commit/1ba2e3e1bb091acda3139f8a9259fa8161f3242d
|
||||
* @see \Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\ExplicitPhpErrorApiRectorTest
|
||||
*/
|
||||
final class ExplicitPhpErrorApiRector extends AbstractPHPUnitRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition(
|
||||
'Use explicit API for expecting PHP errors, warnings, and notices',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
final class SomeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class);
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Error::class);
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Notice::class);
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Warning::class);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
final class SomeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectDeprecation();
|
||||
$this->expectError();
|
||||
$this->expectNotice();
|
||||
$this->expectWarning();
|
||||
}
|
||||
}
|
||||
PHP
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [MethodCall::class, StaticCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isPHPUnitMethodNames($node, ['expectException'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$replacements = [
|
||||
'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
|
||||
'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
|
||||
'PHPUnit\Framework\TestCase\Error' => 'expectError',
|
||||
'PHPUnit\Framework\TestCase\Warning' => 'expectWarning',
|
||||
];
|
||||
|
||||
foreach ($replacements as $class => $method) {
|
||||
$this->replaceExceptionWith($node, $class, $method);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
* @param string $exceptionClass
|
||||
* @param string $explicitMethod
|
||||
*/
|
||||
private function replaceExceptionWith(Node $node, $exceptionClass, $explicitMethod): void
|
||||
{
|
||||
if (isset($node->args[0]) && property_exists(
|
||||
$node->args[0]->value,
|
||||
'class'
|
||||
) && (string) $node->args[0]->value->class === $exceptionClass) {
|
||||
$this->replaceNode($node, $this->createPHPUnitCallWithName($node, $explicitMethod));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class ExplicitPhpErrorApiRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ExplicitPhpErrorApiRector::class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorDeprecated extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorDeprecated extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectDeprecation();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorError extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Error::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorError extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectError();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorNotice extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Notice::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorNotice extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectNotice();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorWarning extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectException(\PHPUnit\Framework\TestCase\Warning::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RefactorWarning extends TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$this->expectWarning();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user