@@ -516,6 +516,62 @@ public function test_trace_structure_diff_output_with_multiple_root_spans(): voi
516516 }
517517 }
518518
519+ /**
520+ * Test that the diff output is generated correctly when an expected root span is missing.
521+ */
522+ public function test_trace_structure_diff_output_with_missing_root_span (): void
523+ {
524+ $ tracer = $ this ->tracerProvider ->getTracer ('test-tracer ' );
525+
526+ // Create only one root span
527+ $ rootSpan = $ tracer ->spanBuilder ('root-span-1 ' )
528+ ->setSpanKind (SpanKind::KIND_SERVER )
529+ ->startSpan ();
530+ $ rootSpan ->setAttribute ('attribute.one ' , 'value1 ' );
531+ $ rootSpan ->end ();
532+
533+ // Define an expected structure with two root spans
534+ $ expectedStructure = [
535+ [
536+ 'name ' => 'root-span-1 ' ,
537+ 'kind ' => SpanKind::KIND_SERVER ,
538+ 'attributes ' => [
539+ 'attribute.one ' => 'value1 ' ,
540+ ],
541+ ],
542+ [
543+ 'name ' => 'root-span-2 ' , // This span doesn't exist
544+ 'kind ' => SpanKind::KIND_CLIENT ,
545+ 'attributes ' => [
546+ 'attribute.two ' => 42 ,
547+ ],
548+ ],
549+ ];
550+
551+ try {
552+ // This should fail
553+ $ this ->assertTraceStructure ($ this ->storage , $ expectedStructure );
554+ $ this ->fail ('Expected assertion to fail but it passed ' );
555+ } catch (\PHPUnit \Framework \AssertionFailedError $ e ) {
556+ // Verify that the error message contains the diff
557+ $ errorMessage = $ e ->getMessage ();
558+
559+ // Check for diff markers
560+ $ this ->assertStringContainsString ('--- Expected Trace Structure ' , $ errorMessage );
561+ $ this ->assertStringContainsString ('+++ Actual Trace Structure ' , $ errorMessage );
562+
563+ // Check for specific content in the diff
564+ $ this ->assertStringContainsString ('root-span-1 ' , $ errorMessage );
565+
566+ // Check for the missing root span indicator in the diff
567+ $ this ->assertStringContainsString ('[1] => Array ' , $ errorMessage );
568+
569+ // Check that the error message indicates the missing root span count
570+ $ this ->assertStringContainsString ('Expected 2 root spans ' , $ errorMessage );
571+ $ this ->assertStringContainsString ('found 1 ' , $ errorMessage );
572+ }
573+ }
574+
519575 /**
520576 * Test asserting a trace structure using PHPUnit matchers.
521577 */
0 commit comments