Skip to content

Commit

Permalink
feature: add rule to refactor exception methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredbez committed Jan 31, 2020
1 parent 94126cd commit 0856a21
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/set/phpunit/phpunit90.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
services:
Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector: null
Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector: null
27 changes: 26 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 444 Rectors Overview
# All 445 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -4783,6 +4783,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`
Expand Down
102 changes: 102 additions & 0 deletions packages/PHPUnit/src/Rector/MethodCall/ExplicitPhpErrorApiRector.php
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;

use PHPUnit\Framework\TestCase;

final class RefactorCombination extends 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

namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;

use PHPUnit\Framework\TestCase;

final class RefactorCombination extends TestCase
{
public function test()
{
$this->expectDeprecation();
$this->expectError();
$this->expectNotice();
$this->expectWarning();
}
}

?>
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>
Original file line number Diff line number Diff line change
@@ -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();
}
}

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

namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;

use PHPUnit\Framework\TestCase;

final class DoNotRefactorException extends TestCase
{
public function test()
{
$this->expectException(\Exception::class);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture;

use PHPUnit\Framework\TestCase;

final class DoNotRefactorException extends TestCase
{
public function test()
{
$this->expectException(\Exception::class);
}
}

?>
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>

0 comments on commit 0856a21

Please sign in to comment.