Skip to content

Commit

Permalink
Merge pull request #9 from worksome/disallow_phpunit
Browse files Browse the repository at this point in the history
Adds a rule for preventing PHPUnit tests
  • Loading branch information
lukeraymonddowning authored Feb 14, 2022
2 parents aa05d9b + f2106b1 commit 409cc38
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ This rule disallows the usage of the `Route::resource` method when combined with
partial route resources should be split into multiple routes.

#### DisallowPartialRouteFacadeResource
Similar to `DisallowPartialRouteFacadeResource`, but prevents partial resource usage when used in a route group.
Similar to `DisallowPartialRouteFacadeResource`, but prevents partial resource usage when used in a route group.

#### DisallowPHPUnit
This rule prevents PHPUnit tests in favour of Pest PHP. It will allow abstract `TestCase` classes.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
],
"extra": {
"class": "Worksome\\CodingStyle\\Plugin"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"pestphp/pest-plugin": true
}
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ parameters:
parametersSchema:
namespaceAndSuffix: arrayOf(string(), string())

rules:
- Worksome\CodingStyle\PHPStan\DisallowPHPUnitRule
services:
-
class: Worksome\CodingStyle\PHPStan\DeclareStrictTypesRule
Expand Down
45 changes: 45 additions & 0 deletions src/PHPStan/DisallowPHPUnitRule.php
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()
];
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/DisallowPHPUnitRule/DisallowPHPUnitRule.php
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',
],
]);
8 changes: 8 additions & 0 deletions tests/PHPStan/DisallowPHPUnitRule/Fixture/GenericClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Worksome\CodingStyle\Tests\PHPStan\DisallowPHPUnitRule\Fixture;

class GenericClass
{

}
8 changes: 8 additions & 0 deletions tests/PHPStan/DisallowPHPUnitRule/Fixture/PHPUnitTest.php
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
{

}
7 changes: 7 additions & 0 deletions tests/PHPStan/DisallowPHPUnitRule/Fixture/PestTest.php
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();
});
10 changes: 10 additions & 0 deletions tests/PHPStan/DisallowPHPUnitRule/Fixture/TestCase.php
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
{

}

0 comments on commit 409cc38

Please sign in to comment.