Skip to content

Commit c6c7a66

Browse files
committed
fix: matching will be strict
1 parent c37dbac commit c6c7a66

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/Utils/Test/src/TraceStructureAssertionTrait.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -584,17 +584,12 @@ private function isConstraint($value): bool
584584
private function compareAttributes(array $expectedAttributes, array $actualAttributes, bool $strict, string $spanName): void
585585
{
586586
foreach ($expectedAttributes as $key => $expectedValue) {
587-
// In strict mode, all attributes must be present and match
588-
if ($strict) {
589-
Assert::assertArrayHasKey(
590-
$key,
591-
$actualAttributes,
592-
sprintf('Attribute "%s" not found in span "%s"', $key, $spanName)
593-
);
594-
} elseif (!isset($actualAttributes[$key])) {
595-
// In non-strict mode, if the attribute is not present, skip it
596-
continue;
597-
}
587+
// Both in strict and non-strict mode, all expected attributes must be present
588+
Assert::assertArrayHasKey(
589+
$key,
590+
$actualAttributes,
591+
sprintf('Attribute "%s" not found in span "%s"', $key, $spanName)
592+
);
598593

599594
// Get the actual value
600595
$actualValue = $actualAttributes[$key];

src/Utils/Test/tests/Unit/TraceStructureAssertionTraitTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,41 @@ public function test_assert_trace_structure_with_strict_matching(): void
204204
$this->assertTraceStructure($this->storage, $expectedStructureStrict, true);
205205
}
206206

207+
/**
208+
* Test that assertTraceStructure fails when expected attributes don't exist in the actual span,
209+
* even in non-strict mode.
210+
*/
211+
public function test_assert_fails_with_nonexistent_attributes_in_nonstrict_mode(): void
212+
{
213+
$tracer = $this->tracerProvider->getTracer('test-tracer');
214+
215+
// Create a span with specific attributes
216+
$span = $tracer->spanBuilder('test-span')
217+
->startSpan();
218+
219+
$span->setAttribute('attribute.one', 'value1');
220+
$span->setAttribute('attribute.two', 42);
221+
222+
$span->end();
223+
224+
// Define expected structure with an attribute that doesn't exist in the actual span
225+
$expectedStructure = [
226+
[
227+
'name' => 'test-span',
228+
'attributes' => [
229+
'attribute.one' => 'value1',
230+
'nonexistent.attribute' => 'this-does-not-exist', // This attribute doesn't exist
231+
],
232+
],
233+
];
234+
235+
// Expect assertion to fail even in non-strict mode
236+
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
237+
$this->expectExceptionMessage('No matching span found for expected span "test-span"');
238+
239+
$this->assertTraceStructure($this->storage, $expectedStructure, false);
240+
}
241+
207242
/**
208243
* Test asserting a trace structure with multiple root spans.
209244
*/

0 commit comments

Comments
 (0)