-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement Classes\PHPUnit\Framework\TestCaseWithSuffixRule
- Loading branch information
1 parent
cebf79a
commit bdccf23
Showing
14 changed files
with
320 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018-2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/phpstan-rules | ||
*/ | ||
|
||
namespace Ergebnis\PHPStan\Rules\Classes\PHPUnit\Framework; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser; | ||
use PHPStan\Broker; | ||
use PHPStan\Rules; | ||
use PHPStan\ShouldNotHappenException; | ||
|
||
final class TestCaseWithSuffixRule implements Rules\Rule | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private static $phpunitTestCaseClassNames = [ | ||
'PHPUnit\Framework\TestCase', | ||
]; | ||
|
||
/** | ||
* @var Broker\Broker | ||
*/ | ||
private $broker; | ||
|
||
public function __construct(Broker\Broker $broker) | ||
{ | ||
$this->broker = $broker; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Class_::class; | ||
} | ||
|
||
public function processNode(Node $node, Analyser\Scope $scope): array | ||
{ | ||
if (!$node instanceof Node\Stmt\Class_) { | ||
throw new ShouldNotHappenException(\sprintf( | ||
'Expected node to be instance of "%s", but got instance of "%s" instead.', | ||
Node\Stmt\Class_::class, | ||
\get_class($node) | ||
)); | ||
} | ||
|
||
if ($node->isAbstract()) { | ||
return []; | ||
} | ||
|
||
if (!$node->extends instanceof Node\Name) { | ||
return []; | ||
} | ||
|
||
if (!isset($node->namespacedName)) { | ||
return []; | ||
} | ||
|
||
/** @var string $fullyQualifiedClassName */ | ||
$fullyQualifiedClassName = $node->namespacedName->toString(); | ||
|
||
$classReflection = $this->broker->getClass($fullyQualifiedClassName); | ||
|
||
$extendedPhpunitTestCaseClassName = ''; | ||
|
||
foreach (self::$phpunitTestCaseClassNames as $phpunitTestCaseClassName) { | ||
if ($classReflection->isSubclassOf($phpunitTestCaseClassName)) { | ||
$extendedPhpunitTestCaseClassName = $phpunitTestCaseClassName; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if ('' === $extendedPhpunitTestCaseClassName) { | ||
return []; | ||
} | ||
|
||
if (1 === \preg_match('/Test$/', $fullyQualifiedClassName)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
\sprintf( | ||
'Class %s extends %s, is concrete, but does not have a Test suffix.', | ||
$fullyQualifiedClassName, | ||
$extendedPhpunitTestCaseClassName | ||
), | ||
]; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/Fixture/Classes/PHPUnit/Framework/TestCaseWithSuffixRule/Failure/AbstractTestCase.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Classes\PHPUnit\Framework\TestCaseWithSuffixRule\Failure; | ||
|
||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
abstract class AbstractTestCase extends Framework\TestCase | ||
{ | ||
} |
14 changes: 14 additions & 0 deletions
14
...CaseWithSuffixRule/Failure/ConcreteTestCaseExtendingAbstractTestCaseWithoutTestSuffix.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Classes\PHPUnit\Framework\TestCaseWithSuffixRule\Failure; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
final class ConcreteTestCaseExtendingAbstractTestCaseWithoutTestSuffix extends AbstractTestCase | ||
{ | ||
} |
20 changes: 20 additions & 0 deletions
20
...es/PHPUnit/Framework/TestCaseWithSuffixRule/Failure/ConcreteTestCaseWithoutTestSuffix.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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Classes\PHPUnit\Framework\TestCaseWithSuffixRule\Failure; | ||
|
||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
final class ConcreteTestCaseWithoutTestSuffix extends Framework\TestCase | ||
{ | ||
public function testFooIsNotBar(): void | ||
{ | ||
self::assertNotSame('bar', 'foo'); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...asses/PHPUnit/Framework/TestCaseWithSuffixRule/Success/ConcreteTestCaseWithSuffixTest.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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Classes\PHPUnit\Framework\TestCaseWithSuffixRule\Success; | ||
|
||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
final class ConcreteTestCaseWithSuffixTest extends Framework\TestCase | ||
{ | ||
public function testFooIsNotBar(): void | ||
{ | ||
self::assertNotSame('bar', 'foo'); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...e/Classes/PHPUnit/Framework/TestCaseWithSuffixRule/Success/ExplicitlyAbstractTestCase.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Classes\PHPUnit\Framework\TestCaseWithSuffixRule\Success; | ||
|
||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
abstract class ExplicitlyAbstractTestCase extends Framework\TestCase | ||
{ | ||
} |
17 changes: 17 additions & 0 deletions
17
...e/Classes/PHPUnit/Framework/TestCaseWithSuffixRule/Success/ImplicitlyAbstractTestCase.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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Classes\PHPUnit\Framework\TestCaseWithSuffixRule\Success; | ||
|
||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @coversNothing | ||
*/ | ||
class ImplicitlyAbstractTestCase extends Framework\TestCase | ||
{ | ||
abstract protected function foo(); | ||
} |
Oops, something went wrong.