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); }
/** * Check is possible to get all categories for all store starting from top level root category */ public function testGetTreeForAllScope() { $rootCategoryId = null; $depth = null; $category = null; $categoriesMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Category\\Collection', [], [], '', false); $categoryMock = $this->getMock('\\Magento\\Catalog\\Model\\Category', [], [], 'categoryMock', false); $categoriesMock->expects($this->once())->method('getFirstItem')->willReturn($categoryMock); $categoriesMock->expects($this->once())->method('addFilter')->with('level', ['eq' => 0])->willReturnSelf(); $this->categoriesFactoryMock->expects($this->once())->method('create')->willReturn($categoriesMock); $nodeMock = $this->getMock('\\Magento\\Framework\\Data\\Tree\\Node', [], [], '', false); $this->categoryTreeMock->expects($this->once())->method('getTree')->with($nodeMock, $depth); $this->categoryRepositoryMock->expects($this->never())->method('get'); $this->categoryTreeMock->expects($this->once())->method('getRootNode')->with($categoryMock)->willReturn($nodeMock); $this->scopeResolverMock->expects($this->once())->method('getScope')->willReturn($this->scopeMock); $this->scopeMock->expects($this->once())->method('getCode')->willReturn(\Magento\Store\Model\Store::ADMIN_CODE); $this->model->getTree(); }
/** * 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; }