Skip to content

Commit b196a1a

Browse files
committed
Use Node::clone() method in order to not alter the original tree when getting using Node::getValue().
1 parent 4589580 commit b196a1a

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

spec/drupol/phptree/Node/MerkleNodeSpec.php

+41-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
namespace spec\drupol\phptree\Node;
66

7-
use drupol\phpmerkle\Hasher\DoubleSha256;
8-
use drupol\phptree\Node\MerkleNode;
97
use drupol\phpmerkle\Hasher\DummyHasher;
8+
use drupol\phptree\Node\MerkleNode;
109
use PhpSpec\ObjectBehavior;
1110

1211
class MerkleNodeSpec extends ObjectBehavior
1312
{
1413
public function it_can_get_a_hash()
1514
{
1615
$this
17-
->beConstructedWith('root', 2, new DoubleSha256());
16+
->beConstructedWith('root');
1817

1918
$input = [
2019
null,
@@ -50,7 +49,7 @@ public function it_can_get_a_hash()
5049
];
5150

5251
foreach ($input as $word) {
53-
$nodes[] = new MerkleNode($word, 2, new DoubleSha256());
52+
$nodes[] = new MerkleNode($word);
5453
}
5554

5655
$this
@@ -77,9 +76,19 @@ public function it_can_get_the_value_of_a_tree_with_four_nodes()
7776
];
7877

7978
$this
80-
->add(...$nodes)
79+
->add(...$nodes);
80+
81+
$this
82+
->count()
83+
->shouldReturn(5);
84+
85+
$this
8186
->getValue()
8287
->shouldReturn('abcc');
88+
89+
$this
90+
->count()
91+
->shouldReturn(5);
8392
}
8493

8594
public function it_can_get_the_value_of_a_tree_with_three_nodes()
@@ -105,6 +114,33 @@ public function it_can_get_the_value_of_a_tree_with_two_nodes()
105114
->shouldReturn('aa');
106115
}
107116

117+
public function it_can_handle_null_values()
118+
{
119+
$nodes = [
120+
new MerkleNode(null, 2, new DummyHasher()),
121+
new MerkleNode(null, 2, new DummyHasher()),
122+
new MerkleNode('a', 2, new DummyHasher()),
123+
new MerkleNode('b', 2, new DummyHasher()),
124+
new MerkleNode(null, 2, new DummyHasher()),
125+
new MerkleNode('c', 2, new DummyHasher()),
126+
];
127+
128+
$this
129+
->add(...$nodes);
130+
131+
$this
132+
->count()
133+
->shouldReturn(6);
134+
135+
$this
136+
->getValue()
137+
->shouldReturn('abcc');
138+
139+
$this
140+
->count()
141+
->shouldReturn(6);
142+
}
143+
108144
public function it_is_initializable()
109145
{
110146
$this->shouldHaveType(MerkleNode::class);

src/Node/MerkleNode.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getValue()
4343
return parent::getValue();
4444
}
4545

46-
return $this->hash();
46+
return $this->clone()->hash();
4747
}
4848

4949
/**

0 commit comments

Comments
 (0)