@@ -50,11 +50,45 @@ public function testRemoveChild()
5050 $ i ++;
5151 }
5252
53- $ this ->expectException (Exception::class);
53+ // Check all children have correct parent
54+ foreach ($ object ->getChildren () as $ child ) {
55+ self ::assertEquals ($ child ->getParent (), $ object );
56+ }
5457
58+ $ this ->expectException (Exception::class);
5559 $ object ->removeChild ($ unknownChild );
5660 }
5761
62+ public function testReplacedChildParent () {
63+
64+ $ childToReplace = Integer::create (1 );
65+ $ replacement = Integer::create (0 );
66+
67+ $ object = Sequence::create ([
68+ NullObject::create (),
69+ $ childToReplace ,
70+ NullObject::create (),
71+ ]);
72+
73+ $ expectedObject = Sequence::create ([
74+ NullObject::create (),
75+ $ replacement ,
76+ NullObject::create (),
77+ ]);
78+
79+ $ expectedBinary = $ expectedObject ->getBinary ();
80+
81+ $ object ->replaceChild ($ childToReplace , $ replacement );
82+
83+ // Check binary data as expected after tree rebuild
84+ self ::assertEquals ($ expectedObject ->getBinary (), $ expectedBinary );
85+
86+ // Check all children have correct parent
87+ foreach ($ object ->getChildren () as $ child ) {
88+ self ::assertEquals ($ child ->getParent (), $ object );
89+ }
90+ }
91+
5892 public function testReplaceChild ()
5993 {
6094 $ unknownChild = Integer::create (0 );
@@ -72,7 +106,9 @@ public function testReplaceChild()
72106 $ object ->replaceChild ($ childToReplace , $ replacement );
73107
74108 // New child now NullObject instead of Sequence
75- self ::assertInstanceOf (NullObject::class, $ object ->getChildren ()[0 ]);
109+ foreach ($ object ->getChildren () as $ child ) {
110+ self ::assertInstanceOf (NullObject::class, $ child );
111+ }
76112
77113 // We still have 1 child
78114 self ::assertCount (3 , $ object ->getChildren ());
@@ -85,6 +121,11 @@ public function testReplaceChild()
85121 $ i ++;
86122 }
87123
124+ // Check all children have correct parent
125+ foreach ($ object ->getChildren () as $ child ) {
126+ self ::assertEquals ($ child ->getParent (), $ object );
127+ }
128+
88129 // Exception if we trying replace unknown child
89130 $ this ->expectException (Exception::class);
90131 $ object ->replaceChild ($ unknownChild , $ replacement );
@@ -128,5 +169,10 @@ public function testAppendChild()
128169 self ::assertEquals ($ i , $ index );
129170 $ i ++;
130171 }
172+
173+ // Check all children have correct parent
174+ foreach ($ object ->getChildren () as $ child ) {
175+ self ::assertEquals ($ child ->getParent (), $ object );
176+ }
131177 }
132178}
0 commit comments