Skip to content

Commit cbec65f

Browse files
committed
fix: compare status more flexibly
1 parent 6fda261 commit cbec65f

File tree

2 files changed

+339
-28
lines changed

2 files changed

+339
-28
lines changed

src/Utils/Test/src/TraceStructureAssertionTrait.php

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,12 @@ private function findMatchingSpan(array $expectedSpan, array $actualSpans, bool
451451
// If we get here, the span and its children match
452452
return;
453453
} catch (AssertionFailedError $e) {
454-
// This span didn't match, try the next one
454+
// If the error is about an unexpected field in the status, rethrow it
455+
if (strpos($e->getMessage(), 'Unexpected field') !== false) {
456+
throw $e;
457+
}
458+
459+
// Otherwise, this span didn't match, try the next one
455460
continue;
456461
}
457462
}
@@ -653,32 +658,43 @@ private function compareAttributes(array $expectedAttributes, array $actualAttri
653658
/**
654659
* Compares the status of an expected span with the status of an actual span.
655660
*
656-
* @param array $expectedStatus
657-
* @param array $actualStatus
658-
* @param bool $strict
659-
* @param string $spanName
661+
* @param mixed $expectedStatus The expected status (multiple formats supported)
662+
* @param array $actualStatus The actual status
663+
* @param bool $strict Whether to perform strict matching
664+
* @param string $spanName The name of the span being compared
660665
* @throws AssertionFailedError
661666
* @return void
662667
*/
663-
private function compareStatus(array $expectedStatus, array $actualStatus, bool $strict, string $spanName): void
668+
private function compareStatus($expectedStatus, array $actualStatus, bool $strict, string $spanName): void
664669
{
665-
// In strict mode, verify that all fields in expected status are also in actual status
666-
if ($strict) {
667-
// Check if code is specified in expected status
668-
if (!isset($expectedStatus['code']) && $actualStatus['code'] !== 0) {
669-
Assert::fail(sprintf('Actual status has non-default code but expected status does not specify code for span "%s"', $spanName));
670-
}
670+
// Case 1: Constraint directly on status code
671+
if ($this->isConstraint($expectedStatus)) {
672+
Assert::assertThat(
673+
$actualStatus['code'],
674+
$expectedStatus,
675+
sprintf('Status code does not match constraint for span "%s"', $spanName)
676+
);
671677

672-
// Check if description is specified in expected status
673-
if (!isset($expectedStatus['description']) && $actualStatus['description'] !== '') {
674-
Assert::fail(sprintf('Actual status has description but expected status does not specify description for span "%s"', $spanName));
675-
}
678+
return;
676679
}
677680

678-
// Compare status code if specified
679-
if (isset($expectedStatus['code'])) {
680-
$expectedCode = $expectedStatus['code'];
681+
// Case 2: Scalar value (direct status code comparison)
682+
if (is_scalar($expectedStatus)) {
683+
Assert::assertSame(
684+
$expectedStatus,
685+
$actualStatus['code'],
686+
sprintf('Status code does not match for span "%s"', $spanName)
687+
);
681688

689+
return;
690+
}
691+
692+
// Case 3: Simple indexed array [code, description]
693+
if (is_array($expectedStatus) && array_keys($expectedStatus) === [0, 1] && count($expectedStatus) === 2) {
694+
$expectedCode = $expectedStatus[0];
695+
$expectedDescription = $expectedStatus[1];
696+
697+
// Compare code
682698
if ($this->isConstraint($expectedCode)) {
683699
Assert::assertThat(
684700
$actualStatus['code'],
@@ -692,12 +708,8 @@ private function compareStatus(array $expectedStatus, array $actualStatus, bool
692708
sprintf('Status code does not match for span "%s"', $spanName)
693709
);
694710
}
695-
}
696-
697-
// Compare status description if specified
698-
if (isset($expectedStatus['description'])) {
699-
$expectedDescription = $expectedStatus['description'];
700711

712+
// Compare description
701713
if ($this->isConstraint($expectedDescription)) {
702714
Assert::assertThat(
703715
$actualStatus['description'],
@@ -711,6 +723,69 @@ private function compareStatus(array $expectedStatus, array $actualStatus, bool
711723
sprintf('Status description does not match for span "%s"', $spanName)
712724
);
713725
}
726+
727+
return;
728+
}
729+
730+
// Case 4: Traditional associative array with keys
731+
if (is_array($expectedStatus)) {
732+
// In strict mode, verify that the expected status doesn't have unexpected fields
733+
if ($strict) {
734+
// Check for unexpected fields in expected status
735+
foreach (array_keys($expectedStatus) as $key) {
736+
if (!in_array($key, ['code', 'description'])) {
737+
Assert::fail(sprintf('Unexpected field "%s" in expected status for span "%s"', $key, $spanName));
738+
}
739+
}
740+
741+
// Check if code is specified in expected status
742+
if (!isset($expectedStatus['code']) && $actualStatus['code'] !== 0) {
743+
Assert::fail(sprintf('Actual status has non-default code but expected status does not specify code for span "%s"', $spanName));
744+
}
745+
746+
// Check if description is specified in expected status
747+
if (!isset($expectedStatus['description']) && $actualStatus['description'] !== '') {
748+
Assert::fail(sprintf('Actual status has description but expected status does not specify description for span "%s"', $spanName));
749+
}
750+
}
751+
752+
// Compare status code if specified
753+
if (isset($expectedStatus['code'])) {
754+
$expectedCode = $expectedStatus['code'];
755+
756+
if ($this->isConstraint($expectedCode)) {
757+
Assert::assertThat(
758+
$actualStatus['code'],
759+
$expectedCode,
760+
sprintf('Status code does not match constraint for span "%s"', $spanName)
761+
);
762+
} else {
763+
Assert::assertSame(
764+
$expectedCode,
765+
$actualStatus['code'],
766+
sprintf('Status code does not match for span "%s"', $spanName)
767+
);
768+
}
769+
}
770+
771+
// Compare status description if specified
772+
if (isset($expectedStatus['description'])) {
773+
$expectedDescription = $expectedStatus['description'];
774+
775+
if ($this->isConstraint($expectedDescription)) {
776+
Assert::assertThat(
777+
$actualStatus['description'],
778+
$expectedDescription,
779+
sprintf('Status description does not match constraint for span "%s"', $spanName)
780+
);
781+
} else {
782+
Assert::assertSame(
783+
$expectedDescription,
784+
$actualStatus['description'],
785+
sprintf('Status description does not match for span "%s"', $spanName)
786+
);
787+
}
788+
}
714789
}
715790
}
716791

0 commit comments

Comments
 (0)