Skip to content

Commit e52cf46

Browse files
authored
Merge pull request #7 from Falseclock/master
Raplaced child doesn't have parent
2 parents 3ce7adf + d9cc1a2 commit e52cf46

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/ASN1/ASN1Object.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function getChildren(): array
272272
/**
273273
* @return null|ASN1Object
274274
*/
275-
public function getParent()
275+
public function getParent(): ASN1ObjectInterface
276276
{
277277
return $this->parent;
278278
}
@@ -412,6 +412,8 @@ public function replaceChild(ASN1ObjectInterface $childToReplace, ASN1ObjectInte
412412
foreach ($this->getChildren() as $index => $child) {
413413
if ($child === $childToReplace) {
414414
$this->children[$index] = $replacement;
415+
// New replacement must have old child's parent.
416+
$replacement->parent = $this;
415417
$this->rebuildTree();
416418

417419
return $this;

src/ASN1/ExplicitlyTaggedObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function getEncodedValue()
3131
return $this->getBinaryContent();
3232
}
3333

34-
public static function create(int $tagNumber, ASN1Object $object, $class = Identifier::CLASS_CONTEXT_SPECIFIC)
34+
public static function create(int $tagNumber, ASN1ObjectInterface $object, $class = Identifier::CLASS_CONTEXT_SPECIFIC)
3535
{
3636
$hasIndefiniteLength = $object->getContentLength()->getLengthForm() === ContentLength::INDEFINITE_FORM;
3737

tests/ASN1/Universal/ChildrenTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)