-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from worksome/disallow_phpunit
Adds a rule for preventing PHPUnit tests
- Loading branch information
Showing
9 changed files
with
119 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\CodingStyle\PHPStan; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @implements Rule<Class_> | ||
*/ | ||
final class DisallowPHPUnitRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Class_::class; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($node->isAbstract()) { | ||
return []; | ||
} | ||
|
||
if (! is_subclass_of($node->namespacedName->toString(), TestCase::class)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
"PHPUnit tests are not allowed. Please use Pest PHP instead. If this is a TestCase, make it abstract to pass validation." | ||
)->build() | ||
]; | ||
} | ||
|
||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\PHPStan\DeclareStrictTypesRule; | ||
|
||
use Worksome\CodingStyle\PHPStan\DeclareStrictTypesRule; | ||
use Worksome\CodingStyle\PHPStan\DisallowPHPUnitRule; | ||
|
||
it('checks for declaration of strict types', function (string $path, array ...$errors) { | ||
$this->rule = new DisallowPHPUnitRule(); | ||
|
||
expect($path)->toHaveRuleErrors($errors); | ||
})->with([ | ||
'PHPUnit test' => [ | ||
__DIR__ . '/Fixture/PHPUnitTest.php', | ||
[ | ||
'PHPUnit tests are not allowed. Please use Pest PHP instead. If this is a TestCase, make it abstract to pass validation.', | ||
5, | ||
], | ||
], | ||
'Pest file' => [ | ||
__DIR__ . '/Fixture/PestTest.php', | ||
], | ||
'Abstract PHPUnit test case' => [ | ||
__DIR__ . '/Fixture/TestCase.php', | ||
], | ||
'Generic class' => [ | ||
__DIR__ . '/Fixture/GenericClass.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,8 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\PHPStan\DisallowPHPUnitRule\Fixture; | ||
|
||
class GenericClass | ||
{ | ||
|
||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\PHPStan\DisallowPHPUnitRule\Fixture; | ||
|
||
class PHPUnitTest extends TestCase | ||
{ | ||
|
||
} |
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,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
it('works as expected', function () { | ||
expect(true)->toBeTrue(); | ||
}); |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\CodingStyle\Tests\PHPStan\DisallowPHPUnitRule\Fixture; | ||
|
||
abstract class TestCase extends \PHPUnit\Framework\TestCase | ||
{ | ||
|
||
} |