diff --git a/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php b/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php index f272cc5..7254dad 100644 --- a/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php +++ b/src/Rules/PHPUnit/ShouldCallParentMethodsRule.php @@ -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; @@ -16,31 +16,33 @@ use function strtolower; /** - * @implements Rule + * @implements Rule */ 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 []; @@ -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(), ]; } diff --git a/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php b/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php index 76705d1..7d891f9 100644 --- a/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php +++ b/tests/Rules/PHPUnit/ShouldCallParentMethodsRuleTest.php @@ -44,6 +44,10 @@ public function testRule(): void 'Missing call to parent::setUp() method.', 155, ], + [ + 'Missing call to parent::setUp() method.', + 218, + ], ]); } diff --git a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php index 12d9ce0..3f429a2 100644 --- a/tests/Rules/PHPUnit/data/missing-parent-method-calls.php +++ b/tests/Rules/PHPUnit/data/missing-parent-method-calls.php @@ -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(); + } +} + +