Skip to content

Commit ffaee2a

Browse files
committed
chore: add test covering multiple root spans
1 parent eefa3ad commit ffaee2a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,71 @@ public function test_trace_structure_diff_output(): void
451451
}
452452
}
453453

454+
/**
455+
* Test that the diff output is generated correctly for multiple root spans.
456+
*/
457+
public function test_trace_structure_diff_output_with_multiple_root_spans(): void
458+
{
459+
$tracer = $this->tracerProvider->getTracer('test-tracer');
460+
461+
// Create first root span
462+
$rootSpan1 = $tracer->spanBuilder('root-span-1')
463+
->setSpanKind(SpanKind::KIND_SERVER)
464+
->startSpan();
465+
$rootSpan1->setAttribute('attribute.one', 'actual-value-1');
466+
$rootSpan1->end();
467+
468+
// Create second root span
469+
$rootSpan2 = $tracer->spanBuilder('root-span-2')
470+
->setSpanKind(SpanKind::KIND_CLIENT)
471+
->startSpan();
472+
$rootSpan2->setAttribute('attribute.two', 42);
473+
$rootSpan2->end();
474+
475+
// Define an expected structure that doesn't match the actual structure
476+
$expectedStructure = [
477+
[
478+
'name' => 'root-span-1',
479+
'kind' => SpanKind::KIND_SERVER,
480+
'attributes' => [
481+
'attribute.one' => 'expected-value-1', // Different value
482+
],
483+
],
484+
[
485+
'name' => 'root-span-2',
486+
'kind' => SpanKind::KIND_CLIENT,
487+
'attributes' => [
488+
'attribute.two' => 24, // Different value
489+
'attribute.three' => true, // Extra attribute
490+
],
491+
],
492+
];
493+
494+
try {
495+
// This should fail
496+
$this->assertTraceStructure($this->storage, $expectedStructure);
497+
$this->fail('Expected assertion to fail but it passed');
498+
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
499+
// Verify that the error message contains the diff
500+
$errorMessage = $e->getMessage();
501+
502+
// Check for diff markers
503+
$this->assertStringContainsString('--- Expected Trace Structure', $errorMessage);
504+
$this->assertStringContainsString('+++ Actual Trace Structure', $errorMessage);
505+
506+
// Check for specific content in the diff for the first root span
507+
$this->assertStringContainsString('root-span-1', $errorMessage);
508+
$this->assertStringContainsString('expected-value-1', $errorMessage);
509+
$this->assertStringContainsString('actual-value-1', $errorMessage);
510+
511+
// Check for specific content in the diff for the second root span
512+
$this->assertStringContainsString('root-span-2', $errorMessage);
513+
$this->assertStringContainsString('24', $errorMessage);
514+
$this->assertStringContainsString('42', $errorMessage);
515+
$this->assertStringContainsString('attribute.three', $errorMessage);
516+
}
517+
}
518+
454519
/**
455520
* Test asserting a trace structure using PHPUnit matchers.
456521
*/

0 commit comments

Comments
 (0)