public function testApplyCallback() { $initialized = []; $callback = function (Node $node) use(&$initialized) { $initialized[] = $node; }; Utils::applyCallback($callback, $this->root); // no duplicate calls, 1 call per node self::assertTrue(count($initialized) === 3); self::assertTrue(in_array($this->root, $initialized, true)); self::assertTrue(in_array($this->attCh, $initialized, true)); self::assertTrue(in_array($this->attChL2, $initialized, true)); self::assertFalse(in_array($this->ch, $initialized, true)); // Check init of nodes added later $this->attChL2->addChild($this->ch); self::assertTrue(count($initialized) === 6); // change tree structure $this->attChL2->detach(); $this->attChL2->attachTo($this->root); // changing tree structure must not provoke duplicate init self::assertTrue(count($initialized) === 6); // assert that init works for already initialized nodes that was detached & attached again $this->ch->detach(); $newNode = new Node($this->chL2); $this->ch->attachTo($this->attChL2); self::assertTrue(count($initialized) === 7); self::assertTrue(in_array($newNode, $initialized, true)); $newNode->detach(); // 6 new nodes instead of 7 must be added $callback2 = function (Node $node) use(&$initialized) { $initialized[] = $node; }; Utils::applyCallback($callback2, $this->root); self::assertTrue(count($initialized) === 13); }
public function test() { $config = [1 => [2 => [], 3 => []], 4 => [5]]; $items = [1 => $i1 = new Node(), 2 => $i2 = new Node(), 3 => $i3 = new Node(), 4 => $i4 = new Node(), 5 => $i5 = new Node()]; $builder = new TreeBuilder(); $tree = $builder->build($config, $items); self::assertEquals([$i1, $i4], array_values($tree)); self::assertEquals(null, $i1->parent()); self::assertEquals(null, $i4->parent()); self::assertTrue($i1 === $i2->parent()); self::assertTrue($i1 === $i3->parent()); self::assertTrue($i4 === $i5->parent(), 'Test config items that requires normalization.'); }
public function test() { $config = ['i1' => ['i2' => [], 'i3' => []], 'i4' => ['i5' => []]]; $items = ['i1' => $i1 = new Node(), 'i2' => $i2 = new Node(), 'i3' => $i3 = new Node(), 'i4' => $i4 = new Node(), 'i5' => $i5 = new Node()]; $root = new Node(); $tree = new Tree($root, $config, $items); $tree->build(); // test root self::assertTrue($tree->getRoot() === $root); self::assertTrue($i1->parent() === $root, 'test tree level 1'); self::assertTrue($i5->parent() === $i4, 'test tree level N'); // test append $newNode = new Node(); $tree->append('i4', 'new', $newNode); $tree->build(); self::assertTrue($newNode->parent() === $i4); $items = $i4->children()->toArray(); self::assertTrue(array_pop($items) === $newNode); // test remove $tree->remove('new'); $tree->build(); self::assertTrue($newNode->parent() === null); // test adding same node again $tree->prepend('i4', 'new', $newNode); $tree->build(); self::assertTrue($newNode->parent() === $i4); self::assertTrue($i4->children()->first() === $newNode); // test addMany $tree->addMany('i5', ['a' => $a = new Node(), 'b' => $b = new Node()]); $tree->build(); self::assertTrue($a->parent() === $i5); self::assertTrue($b->parent() === $i5); // test move $tree->move('a', 'b'); $tree->build(); self::assertTrue($a->parent() === $b); $c = new Node(); $tree->replace('a', $c); $tree->build(); self::assertTrue($c->parent() === $b); self::assertTrue($a->parent() === null); }