public function testGetTreeWithNullArguments() { $rootCategoryId = null; $depth = null; $category = null; $this->categoryRepositoryMock->expects($this->never())->method('get'); $this->categoryTreeMock->expects($this->once())->method('getRootNode')->with($category)->willReturn(null); $this->categoryTreeMock->expects($this->exactly(2))->method('getTree')->with($category, $depth); $this->assertEquals($this->model->getTree($rootCategoryId, $depth), $this->categoryTreeMock->getTree(null, null)); }
/** * {@inheritdoc} */ public function getTree($rootCategoryId = null, $depth = null) { $category = null; if ($rootCategoryId !== null) { /** @var \Magento\Catalog\Model\Category $category */ $category = $this->categoryRepository->get($rootCategoryId); } $result = $this->categoryTree->getTree($this->categoryTree->getRootNode($category), $depth); return $result; }
public function testGetTreeWhenChildrenAreNotExist() { $currentLevel = 1; $treeNodeMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\CategoryTreeInterface'); $this->treeFactoryMock->expects($this->any())->method('create')->will($this->returnValue($treeNodeMock)); $treeNodeMock->expects($this->once())->method('setId')->with($this->equalTo($currentLevel))->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setParentId')->with($this->equalTo($currentLevel - 1))->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setName')->with($this->equalTo('Name' . $currentLevel))->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setPosition')->with($this->equalTo($currentLevel))->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setLevel')->with($this->equalTo($currentLevel))->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setIsActive')->with($this->equalTo(true))->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setProductCount')->with(4)->will($this->returnSelf()); $treeNodeMock->expects($this->once())->method('setChildrenData')->will($this->returnSelf()); $node = $this->getMockBuilder('Magento\\Framework\\Data\\Tree\\Node')->disableOriginalConstructor()->setMethods(['hasChildren', 'getChildren', 'getId', 'getParentId', 'getName', 'getPosition', 'getLevel', 'getIsActive', 'getProductCount'])->getMock(); $node->expects($this->any())->method('hasChildren')->will($this->returnValue(false)); $node->expects($this->never())->method('getChildren'); $node->expects($this->once())->method('getId')->will($this->returnValue($currentLevel)); $node->expects($this->once())->method('getParentId')->will($this->returnValue($currentLevel - 1)); $node->expects($this->once())->method('getName')->will($this->returnValue('Name' . $currentLevel)); $node->expects($this->once())->method('getPosition')->will($this->returnValue($currentLevel)); $node->expects($this->once())->method('getLevel')->will($this->returnValue($currentLevel)); $node->expects($this->once())->method('getIsActive')->will($this->returnValue(true)); $node->expects($this->once())->method('getProductCount')->will($this->returnValue(4)); $this->tree->getTree($node); }
/** * Get product category ids from array * * @param array $categories * @return array */ protected function getCategoryIds($categories) { $ids = []; $tree = $this->categoryTree->getTree($this->categoryTree->getRootNode(null), null); foreach ($categories as $name) { foreach ($tree->getChildrenData() as $child) { if ($child->getName() == $name) { /** @var \Magento\Catalog\Api\Data\CategoryTreeInterface $child */ $tree = $child; $ids[] = $child->getId(); if (!$tree->getChildrenData()) { $tree = $this->categoryTree->getTree($this->categoryTree->getRootNode(null), null); } break; } } } return $ids; }