Skip to content

Commit

Permalink
Merge pull request #255 from fabio-ivona/exclude-pest-extends
Browse files Browse the repository at this point in the history
Exclude Pest expectation pipes code from failure snippets
  • Loading branch information
nunomaduro committed Feb 19, 2023
2 parents dc683d4 + d137151 commit d7ff6ad
Showing 1 changed file with 67 additions and 28 deletions.
95 changes: 67 additions & 28 deletions src/Adapters/Phpunit/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function Termwind\renderUsing;
use Termwind\Terminal;
use function Termwind\terminal;
use Whoops\Exception\Frame;
use Whoops\Exception\Inspector;

/**
Expand All @@ -42,7 +43,7 @@ final class Style
/**
* @var string[]
*/
private const TYPES = [TestResult::DEPRECATED, TestResult::FAIL, TestResult::WARN, TestResult::RISKY, TestResult::INCOMPLETE, TestResult::TODO, TestResult::SKIPPED, TestResult::PASS];
private const TYPES = [TestResult::DEPRECATED, TestResult::FAIL, TestResult::WARN, TestResult::RISKY, TestResult::INCOMPLETE, TestResult::TODO, TestResult::SKIPPED, TestResult::PASS];

/**
* Style constructor.
Expand Down Expand Up @@ -167,7 +168,8 @@ public function writeErrorsSummary(State $state, bool $onFailure): void
<div class="mx-2 text-red">
<hr/>
</div>
HTML);
HTML
);

$testCaseName = $testResult->testCaseName;
$description = $testResult->description;
Expand Down Expand Up @@ -336,32 +338,8 @@ public function writeError(Throwable $throwable): void
'/vendor\/sulu\/sulu\/src\/Sulu\/Bundle\/TestBundle\/Testing/',
'/vendor\/webmozart\/assert/',

//Ignores frames in PestPHP custom expectations
function ($frame) {
if (class_exists(Expectation::class)) {
$reflection = new ReflectionClass(Expectation::class);

/** @var array<int, Closure> $extends */
$extends = $reflection->getStaticPropertyValue('extends', []);

foreach ($extends as $extendClosure) {
$reflection = new ReflectionFunction($extendClosure);

$sanitizedPath = (string) str_replace('\\', '/', $frame->getFile());

/** @phpstan-ignore-next-line */
$sanitizedClosurePath = (string) str_replace('\\', '/', $reflection->getFileName());

if ($sanitizedPath === $sanitizedClosurePath) {
if ($reflection->getStartLine() <= $frame->getLine() && $frame->getLine() <= $reflection->getEndLine()) {
return true;
}
}
}
}

return false;
},
$this->ignorePestPipes(...),
$this->ignorePestExtends(...),
]);

/** @var \Throwable $throwable */
Expand Down Expand Up @@ -459,4 +437,65 @@ private function writeDescriptionLine(TestResult $result): void
</div>
HTML, $seconds === '' ? '' : 'flex space-x-1 justify-between', $truncateClasses, $result->color, $result->icon, $description, $warning, $seconds));
}

/**
* @param Frame $frame
*/
private function ignorePestPipes($frame): bool
{
if (class_exists(Expectation::class)) {
$reflection = new ReflectionClass(Expectation::class);
$expectationPipes = $reflection->getStaticPropertyValue('pipes', []);

foreach ($expectationPipes as $pipes) {
foreach ($pipes as $pipeClosure) {
if ($this->isFrameInClosure($frame, $pipeClosure)) {
return true;
}
}
}
}

return false;
}

/**
* @param Frame $frame
*/
private function ignorePestExtends($frame): bool
{
if (class_exists(Expectation::class)) {
$reflection = new ReflectionClass(Expectation::class);
$extends = $reflection->getStaticPropertyValue('extends', []);

foreach ($extends as $extendClosure) {
if ($this->isFrameInClosure($frame, $extendClosure)) {
return true;
}
}
}

return false;
}

/**
* @param Frame $frame
*/
private function isFrameInClosure($frame, Closure $closure): bool
{
$reflection = new ReflectionFunction($closure);

$sanitizedPath = (string) str_replace('\\', '/', $frame->getFile());

/** @phpstan-ignore-next-line */
$sanitizedClosurePath = (string) str_replace('\\', '/', $reflection->getFileName());

if ($sanitizedPath === $sanitizedClosurePath) {
if ($reflection->getStartLine() <= $frame->getLine() && $frame->getLine() <= $reflection->getEndLine()) {
return true;
}
}

return false;
}
}

0 comments on commit d7ff6ad

Please sign in to comment.