Skip to content

Commit

Permalink
Adapted expected stack trace to PHP 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKleyman committed Jan 13, 2025
1 parent af86418 commit 092327f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
36 changes: 34 additions & 2 deletions tests/ElasticApmTests/Util/StackTraceFrameExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ final class StackTraceFrameExpectations extends ExpectationsBase
/** @var Optional<?string> */
public $function;

/** @var bool */
public $isFunctionNameRegex = false;

/** @var Optional<int> */
public $lineno;

Expand Down Expand Up @@ -101,9 +104,32 @@ public static function fromStandaloneFunction(string $fileName, int $lineNumber,
return $result;
}

private static function convertFunctionNameToRegPattern(string $text): string
{
$result = $text;
$result = str_replace('\\', '\\\\', $result);
$result = str_replace('{', '\\{', $result);
$result = str_replace('}', '\\}', $result);
$result = str_replace('(', '\\(', $result);
$result = str_replace(')', '\\)', $result);
return '/^' . $result . '$/';
}

public static function fromClosure(string $fileName, int $lineNumber, ?string $namespace, string $class, bool $isStatic): self
{
$result = self::fromClassMethodUnknownLocation($class, $isStatic, ($namespace === null ? '' : ($namespace . '\\')) . '{closure}');
// Before PHP 8.4: ElasticApmTests\\TestsSharedCode\\SpanStackTraceTestSharedCode::ElasticApmTests\\TestsSharedCode\\{closure}
// PHP 8.4: ElasticApmTests\\TestsSharedCode\\SpanStackTraceTestSharedCode::{closure:ElasticApmTests\\TestsSharedCode\\SpanStackTraceTestSharedCode::allSpanCreatingApis():207}
if (PHP_VERSION_ID < 80400) {
$result = self::fromClassMethodUnknownLocation($class, $isStatic, ($namespace === null ? '' : ($namespace . '\\')) . '{closure}');
} else {
$result = self::fromClassMethodUnknownLocation($class, $isStatic, '{closure:' . $class . '::__METHOD__():__LINE__}');
$regex = self::convertFunctionNameToRegPattern($result->function->getValue());
$regex = str_replace('__METHOD__', '[a-zA-Z0-9]+', $regex);
$regex = str_replace('__LINE__', '[0-9]+', $regex);
$result->function->setValue($regex);
$result->isFunctionNameRegex = true;
}

$result->filename->setValue($fileName);
$result->lineno->setValue($lineNumber);
return $result;
Expand Down Expand Up @@ -143,7 +169,13 @@ public function assertMatches(StackTraceFrame $actual): void
$dbgCtx->add(['this' => $this]);

TestCaseBase::assertSameExpectedOptional($this->filename, $actual->filename);
TestCaseBase::assertSameExpectedOptional($this->function, $actual->function);
if ($this->isFunctionNameRegex) {
if ($this->function->isValueSet()) {
TestCaseBase::assertMatchesRegularExpression($this->function->getValue(), $actual->function);
}
} else {
TestCaseBase::assertSameExpectedOptional($this->function, $actual->function);
}
TestCaseBase::assertSameExpectedOptional($this->lineno, $actual->lineno);
}
}
13 changes: 13 additions & 0 deletions tests/ElasticApmTests/Util/TestCaseBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,19 @@ public static function assertContainsEx($needle, iterable $haystack, string $mes
}
}

/**
* @inheritDoc
*/
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
try {
Assert::assertMatchesRegularExpression($pattern, $string, $message);
} catch (AssertionFailedError $ex) {
self::addMessageStackToException($ex);
throw $ex;
}
}

public static function assertDirectoryDoesNotExist(string $directory, string $message = ''): void
{
/**
Expand Down

0 comments on commit 092327f

Please sign in to comment.