@@ -839,6 +839,106 @@ public function test_assert_fails_with_extra_status_in_strict_mode(): void
839839 $ this ->assertTraceStructure ($ this ->storage , $ expectedStructure , true );
840840 }
841841
842+ /**
843+ * Test that strict mode fails when actual status has a non-default code but expected status doesn't specify a code.
844+ */
845+ public function test_assert_fails_when_actual_status_has_code_but_expected_doesnt_in_strict_mode (): void
846+ {
847+ $ tracer = $ this ->tracerProvider ->getTracer ('test-tracer ' );
848+
849+ // Create a span with a non-default status code
850+ $ span = $ tracer ->spanBuilder ('test-span ' )
851+ ->startSpan ();
852+
853+ $ span ->setStatus (StatusCode::STATUS_ERROR , '' );
854+
855+ $ span ->end ();
856+
857+ // Define expected structure with status but without code
858+ $ expectedStructure = [
859+ [
860+ 'name ' => 'test-span ' ,
861+ 'status ' => [
862+ 'description ' => '' ,
863+ ],
864+ ],
865+ ];
866+
867+ // Expect assertion to fail in strict mode
868+ $ this ->expectException (\PHPUnit \Framework \AssertionFailedError::class);
869+ $ this ->expectExceptionMessageMatches ('/No matching span found for expected span "test-span"/ ' );
870+
871+ $ this ->assertTraceStructure ($ this ->storage , $ expectedStructure , true );
872+ }
873+
874+ /**
875+ * Test that strict mode fails when actual status has a description but expected status doesn't specify a description.
876+ */
877+ public function test_assert_fails_when_actual_status_has_description_but_expected_doesnt_in_strict_mode (): void
878+ {
879+ $ tracer = $ this ->tracerProvider ->getTracer ('test-tracer ' );
880+
881+ // Create a span with a status description
882+ $ span = $ tracer ->spanBuilder ('test-span ' )
883+ ->startSpan ();
884+
885+ $ span ->setStatus (StatusCode::STATUS_ERROR , 'Something went wrong ' );
886+
887+ $ span ->end ();
888+
889+ // Define expected structure with status but without description
890+ $ expectedStructure = [
891+ [
892+ 'name ' => 'test-span ' ,
893+ 'status ' => [
894+ 'code ' => StatusCode::STATUS_ERROR ,
895+ ],
896+ ],
897+ ];
898+
899+ // Expect assertion to fail in strict mode
900+ $ this ->expectException (\PHPUnit \Framework \AssertionFailedError::class);
901+ $ this ->expectExceptionMessageMatches ('/No matching span found for expected span "test-span"/ ' );
902+
903+ $ this ->assertTraceStructure ($ this ->storage , $ expectedStructure , true );
904+ }
905+
906+ /**
907+ * Test that status code constraint checking fails with the correct error message.
908+ */
909+ public function test_assert_fails_with_status_code_constraint_error_message (): void
910+ {
911+ $ tracer = $ this ->tracerProvider ->getTracer ('test-tracer ' );
912+
913+ // Create a span with error status
914+ $ span = $ tracer ->spanBuilder ('test-span ' )
915+ ->startSpan ();
916+
917+ $ span ->setStatus (StatusCode::STATUS_ERROR , 'Something went wrong ' );
918+
919+ $ span ->end ();
920+
921+ // Create a constraint that will fail (expecting OK status)
922+ $ constraint = new \PHPUnit \Framework \Constraint \IsIdentical (StatusCode::STATUS_OK );
923+
924+ // Define expected structure with a constraint that will fail
925+ $ expectedStructure = [
926+ [
927+ 'name ' => 'test-span ' ,
928+ 'status ' => $ constraint ,
929+ ],
930+ ];
931+
932+ try {
933+ $ this ->assertTraceStructure ($ this ->storage , $ expectedStructure );
934+ $ this ->fail ('Expected assertion to fail but it passed ' );
935+ } catch (\PHPUnit \Framework \AssertionFailedError $ e ) {
936+ // Verify that the error message contains the expected text
937+ $ errorMessage = $ e ->getMessage ();
938+ $ this ->assertStringContainsString ('No matching span found for expected span "test-span" ' , $ errorMessage );
939+ }
940+ }
941+
842942 /**
843943 * Test that strict mode fails when actual span has children but expected doesn't.
844944 */
0 commit comments