@@ -46,4 +46,37 @@ public function testSerialize(): void
4646 $ encoded
4747 );
4848 }
49+
50+ public function testSerializeWithMaxNestingDepth (): void
51+ {
52+ $ value = m::mock (ContentInterface::class)
53+ ->shouldReceive ("serialize " )
54+ ->andReturn ("{CONTENT} " )
55+ ->shouldReceive ("getKey " )
56+ ->andReturn ("content_interface " )
57+ ->mock ();
58+
59+ // Create deeply nested array that would cause memory issues
60+ $ deepArray = array ('level1 ' => array ('level2 ' => array ('level3 ' => array ('level4 ' => 'deep_value ' ))));
61+
62+ // Test without depth limit - should serialize completely
63+ $ bodyNoLimit = new Body ($ value , array ('deep ' => $ deepArray ), null , -1 );
64+ $ resultNoLimit = $ bodyNoLimit ->serialize ();
65+
66+ // Test with depth limit - should truncate deep nesting
67+ $ bodyWithLimit = new Body ($ value , array ('deep ' => $ deepArray ), null , 2 );
68+ $ resultWithLimit = $ bodyWithLimit ->serialize ();
69+
70+ // Verify basic structure exists
71+ $ this ->assertArrayHasKey ('extra ' , $ resultNoLimit );
72+ $ this ->assertArrayHasKey ('extra ' , $ resultWithLimit );
73+
74+ // Without limit should have all nested levels
75+ $ this ->assertEquals ('deep_value ' , $ resultNoLimit ['extra ' ]['deep ' ]['level1 ' ]['level2 ' ]['level3 ' ]['level4 ' ]);
76+
77+ // With limit should truncate the 'deep' array due to depth constraint
78+ // At depth 2: root -> extra -> deep (gets truncated to empty array)
79+ $ this ->assertArrayHasKey ('deep ' , $ resultWithLimit ['extra ' ]);
80+ $ this ->assertEmpty ($ resultWithLimit ['extra ' ]['deep ' ]); // Truncated due to depth limit
81+ }
4982}
0 commit comments