Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

class KeepDeepCallsBeforeAssertHelper extends TestCase
{
public function testSomething(): void
{
// a deep, non-asserting call chain visited before the assert helper;
// it must not exhaust the nested-call budget and hide the assertion below
$this->prepare();

$this->checkResult(true);
}

private function prepare(): void
{
$this->stepTwo();
}

private function stepTwo(): void
{
$this->stepThree();
}

private function stepThree(): void
{
$this->stepFour();
}

private function stepFour(): void
{
$this->stepFive();
}

private function stepFive(): void
{
// no assertion here
}

private function checkResult(bool $bool): void
{
self::assertTrue($bool);
}
}
41 changes: 23 additions & 18 deletions src/NodeAnalyzer/AssertCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,34 @@ public function containsAssertCall(ClassMethod $classMethod): bool
{
++$this->classMethodNestingLevel;

// probably no assert method in the end
if ($this->classMethodNestingLevel > self::MAX_NESTED_METHOD_CALL_LEVEL) {
return false;
}
try {
// probably no assert method in the end
if ($this->classMethodNestingLevel > self::MAX_NESTED_METHOD_CALL_LEVEL) {
return false;
}

$cacheHash = md5($this->betterStandardPrinter->prettyPrint([$classMethod]));
$cacheHash = md5($this->betterStandardPrinter->prettyPrint([$classMethod]));

if (isset($this->containsAssertCallByClassMethod[$cacheHash])) {
return $this->containsAssertCallByClassMethod[$cacheHash];
}
if (isset($this->containsAssertCallByClassMethod[$cacheHash])) {
return $this->containsAssertCallByClassMethod[$cacheHash];
}

// A. try "->assert" shallow search first for performance
$hasDirectAssertOrMockCall = $this->hasDirectAssertOrMockCall($classMethod);
if ($hasDirectAssertOrMockCall) {
$this->containsAssertCallByClassMethod[$cacheHash] = $hasDirectAssertOrMockCall;
return true;
}
// A. try "->assert" shallow search first for performance
$hasDirectAssertOrMockCall = $this->hasDirectAssertOrMockCall($classMethod);
if ($hasDirectAssertOrMockCall) {
$this->containsAssertCallByClassMethod[$cacheHash] = $hasDirectAssertOrMockCall;
return true;
}

// B. look for nested calls
$hasNestedAssertOrMockCall = $this->hasNestedAssertCall($classMethod);
$this->containsAssertCallByClassMethod[$cacheHash] = $hasNestedAssertOrMockCall;
// B. look for nested calls
$hasNestedAssertOrMockCall = $this->hasNestedAssertCall($classMethod);
$this->containsAssertCallByClassMethod[$cacheHash] = $hasNestedAssertOrMockCall;

return $hasNestedAssertOrMockCall;
return $hasNestedAssertOrMockCall;
} finally {
// restore depth so sibling calls in the same DFS keep their full budget
--$this->classMethodNestingLevel;
}
}

public function isAssertMethodCall(MethodCall|StaticCall $call): bool
Expand Down
Loading