Skip to content

Commit

Permalink
Merge branch 'z_TEMP_Fix_PHP_84' into php84
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKleyman committed Jan 13, 2025
2 parents af86418 + 3af4114 commit 449e12e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/ElasticApmTests/Util/AssertMessageStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
final class AssertMessageStack implements LoggableInterface
{
/** @var bool */
private static $isEnabled = true;
private static $isEnabled = false;

/** @var ?AssertMessageStack */
private static $singleton = null;
Expand Down
46 changes: 44 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 $isFunctionRegex = false;

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

Expand Down Expand Up @@ -101,9 +104,35 @@ 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__}');
$expectedFunc = $result->function->getValue();
TestCaseBase::assertNotNull($expectedFunc);
$expectedFuncRegex = self::convertFunctionNameToRegPattern($expectedFunc);
$expectedFuncRegex = str_replace('__CLASS__', '[a-zA-Z0-9\\\\]+', $expectedFuncRegex);
$expectedFuncRegex = str_replace('__METHOD__', '[a-zA-Z0-9]+', $expectedFuncRegex);
$expectedFuncRegex = str_replace('__LINE__', '[0-9]+', $expectedFuncRegex);
$result->function->setValue($expectedFuncRegex);
$result->isFunctionRegex = true;
}

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

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

/**
* @inheritDoc
*/
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
AssertMessageStack::newScope(/* out */ $dbgCtx, AssertMessageStack::funcArgs());

try {
$pregMatchRetVal = preg_match($pattern, $string);
$dbgCtx->add(compact('pregMatchRetVal'));
Assert::assertTrue($pregMatchRetVal > 0, $message);
} catch (AssertionFailedError $ex) {
self::addMessageStackToException($ex);
throw $ex;
}
}

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

0 comments on commit 449e12e

Please sign in to comment.