Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Rules/PHPUnit/ShouldCallParentMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,31 +16,33 @@
use function strtolower;

/**
* @implements Rule<InClassMethodNode>
* @implements Rule<MethodReturnStatementsNode>
*/
class ShouldCallParentMethodsRule implements Rule
{

public function getNodeType(): string
{
return InClassMethodNode::class;
return MethodReturnStatementsNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
$methodName = $node->getOriginalNode()->name->name;
$methodName = $node->getMethodName();
if (!in_array(strtolower($methodName), ['setup', 'teardown'], true)) {
return [];
}
if ($scope->getClassReflection() === null) {

if ($node->getStatementResult()->isAlwaysTerminating()) {
return [];
}

if (!$scope->getClassReflection()->is(TestCase::class)) {
$classReflection = $node->getClassReflection();
if (!$classReflection->is(TestCase::class)) {
return [];
}

$parentClass = $scope->getClassReflection()->getParentClass();
$parentClass = $classReflection->getParentClass();

if ($parentClass === null) {
return [];
Expand All @@ -54,13 +56,13 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$hasParentCall = $this->hasParentClassCall($node->getOriginalNode()->getStmts(), strtolower($methodName));
$hasParentCall = $this->hasParentClassCall($node->getStatements(), strtolower($methodName));

if (!$hasParentCall) {
return [
RuleErrorBuilder::message(
sprintf('Missing call to parent::%s() method.', $methodName),
)->identifier('phpunit.callParent')->build(),
)->line($node->getStartLine())->identifier('phpunit.callParent')->build(),
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function testRule(): void
'Missing call to parent::setUp() method.',
155,
],
[
'Missing call to parent::setUp() method.',
218,
],
]);
}

Expand Down
75 changes: 75 additions & 0 deletions tests/Rules/PHPUnit/data/missing-parent-method-calls.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,78 @@ public function setUp(): void
};
}
}

class TerminatingSetUpSelfMarkSkippedTest extends BaseTestCase
{
public function setUp(): void
{
self::markTestSkipped('Skipped');
}
}

class TerminatingSetUpThisMarkSkippedTest extends BaseTestCase
{
public function setUp(): void
{
$this->markTestSkipped('Skipped');
}
}

class TerminatingSetUpMarkIncompleteTest extends BaseTestCase
{
public function setUp(): void
{
$this->markTestIncomplete('Incomplete');
}
}

class TerminatingSetUpFailTest extends BaseTestCase
{
public function setUp(): void
{
$this->fail('Failed');
}
}

class TerminatingSetUpThrowTest extends BaseTestCase
{
public function setUp(): void
{
throw new \RuntimeException('Error');
}
}

class TerminatingTearDownTest extends BaseTestCase
{
public function tearDown(): void
{
$this->fail('Failed');
}
}

class NonTerminatingConditionalSkipMissingParentTest extends BaseTestCase
{
private bool $shouldSkip = false;

public function setUp(): void
{
if ($this->shouldSkip) {
self::markTestSkipped('Skipped');
}
}
}

class NonTerminatingConditionalWithMissingParentTest extends BaseTestCase
{
private bool $shouldSkip = false;

public function setUp(): void
{
if ($this->shouldSkip) {
self::markTestSkipped('Skipped');
}
parent::setUp();
}
}