/** * @param int $number * @param \PHPUnit_Framework_MockObject_MockObject|null $returnValue * * @dataProvider testAddDataProvider */ public function testAdd($number, $returnValue) { $nodeMock = $this->getMock('Magento\\Framework\\Data\\Tree\\Node', [], [], '', false); $nodeMock->expects($this->once())->method('setParent')->with($this->containerMock); $this->containerMock->expects($this->exactly($number))->method('getTree')->will($this->returnValue($returnValue)); $nodeMock->expects($this->once())->method('getId')->will($this->returnValue('id_node')); $this->assertEquals($nodeMock, $this->collection->add($nodeMock)); $this->assertEquals(['id_node' => $nodeMock], $this->collection->getNodes()); $this->assertEquals($nodeMock, $this->collection->searchById('id_node')); $this->assertEquals(null, $this->collection->searchById('id_node_new')); }
public function testSearchById() { $tree = new \Magento\Framework\Data\Tree(); $node1 = new \Magento\Framework\Data\Tree\Node(['id' => 'node1'], 'id', $tree); $this->collection->add($node1); $node2 = new \Magento\Framework\Data\Tree\Node(['id' => 'node2'], 'id', $tree); $this->collection->add($node2); $this->assertSame($this->collection->lastNode(), $node2); $node3 = new \Magento\Framework\Data\Tree\Node(['id' => 'node3'], 'id', $tree); $this->collection->add($node3); $this->assertSame($this->collection->searchById('node2'), $node2); }
/** * Add node * * @param Node $node * @param Node $parent * @return Node */ public function addNode($node, $parent = null) { $this->_nodes->add($node); $node->setParent($parent); if (!is_null($parent) && $parent instanceof Node) { $parent->addChild($node); } return $node; }
/** * Add child node * * @param Node $node * @return Node */ public function addChild($node) { $this->_childNodes->add($node); return $this; }