@@ -572,6 +572,81 @@ public function test_trace_structure_diff_output_with_missing_root_span(): void
572572 }
573573 }
574574
575+ /**
576+ * Test that the diff output is generated correctly when a nested child span is missing.
577+ */
578+ public function test_trace_structure_diff_output_with_missing_nested_span (): void
579+ {
580+ $ tracer = $ this ->tracerProvider ->getTracer ('test-tracer ' );
581+
582+ // Create a root span
583+ $ rootSpan = $ tracer ->spanBuilder ('root-span ' )
584+ ->setSpanKind (SpanKind::KIND_SERVER )
585+ ->startSpan ();
586+
587+ // Activate the root span
588+ $ rootScope = $ rootSpan ->activate ();
589+
590+ try {
591+ // Create only one child span
592+ $ childSpan = $ tracer ->spanBuilder ('child-span-1 ' )
593+ ->setSpanKind (SpanKind::KIND_INTERNAL )
594+ ->startSpan ();
595+ $ childSpan ->setAttribute ('attribute.one ' , 'value1 ' );
596+ $ childSpan ->end ();
597+ } finally {
598+ $ rootSpan ->end ();
599+ $ rootScope ->detach ();
600+ }
601+
602+ // Define an expected structure with two child spans
603+ $ expectedStructure = [
604+ [
605+ 'name ' => 'root-span ' ,
606+ 'kind ' => SpanKind::KIND_SERVER ,
607+ 'children ' => [
608+ [
609+ 'name ' => 'child-span-1 ' ,
610+ 'kind ' => SpanKind::KIND_INTERNAL ,
611+ 'attributes ' => [
612+ 'attribute.one ' => 'value1 ' ,
613+ ],
614+ ],
615+ [
616+ 'name ' => 'child-span-2 ' , // This span doesn't exist
617+ 'kind ' => SpanKind::KIND_CLIENT ,
618+ 'attributes ' => [
619+ 'attribute.two ' => 42 ,
620+ ],
621+ ],
622+ ],
623+ ],
624+ ];
625+
626+ try {
627+ // This should fail
628+ $ this ->assertTraceStructure ($ this ->storage , $ expectedStructure );
629+ $ this ->fail ('Expected assertion to fail but it passed ' );
630+ } catch (\PHPUnit \Framework \AssertionFailedError $ e ) {
631+ // Verify that the error message contains the diff
632+ $ errorMessage = $ e ->getMessage ();
633+
634+ // Check for diff markers
635+ $ this ->assertStringContainsString ('--- Expected Trace Structure ' , $ errorMessage );
636+ $ this ->assertStringContainsString ('+++ Actual Trace Structure ' , $ errorMessage );
637+
638+ // Check for specific content in the diff
639+ $ this ->assertStringContainsString ('root-span ' , $ errorMessage );
640+ $ this ->assertStringContainsString ('child-span-1 ' , $ errorMessage );
641+
642+ // Check for the missing child span indicator in the diff
643+ $ this ->assertStringContainsString ('[1] => Array ' , $ errorMessage );
644+
645+ // Check that the error message indicates a missing span
646+ $ this ->assertStringContainsString ('No matching span found ' , $ errorMessage );
647+ }
648+ }
649+
575650 /**
576651 * Test asserting a trace structure using PHPUnit matchers.
577652 */
0 commit comments