Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ChildrenCategoriesProviderTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class ChildrenCategoriesProviderTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $category;
15 
17  protected $select;
18 
20  protected $connection;
21 
24 
25  protected function setUp()
26  {
27  $this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
28  ->disableOriginalConstructor()
29  ->setMethods(['getPath', 'getResourceCollection', 'getResource', 'getLevel', '__wakeup', 'isObjectNew'])
30  ->getMock();
31  $categoryCollection = $this->getMockBuilder(
32  \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection::class
33  )->disableOriginalConstructor()->setMethods(['addAttributeToSelect', 'addIdFilter'])->getMock();
34  $this->category->expects($this->any())->method('getPath')->willReturn('category-path');
35  $this->category->expects($this->any())->method('getResourceCollection')->willReturn($categoryCollection);
36  $categoryCollection->expects($this->any())->method('addAttributeToSelect')->willReturnSelf();
37  $categoryCollection->expects($this->any())->method('addIdFilter')->with(['id'])->willReturnSelf();
38  $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
39  ->disableOriginalConstructor()->setMethods(['from', 'where', 'deleteFromSelect'])->getMock();
40  $this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
41  $categoryResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
42  ->disableOriginalConstructor()->getMock();
43  $this->category->expects($this->any())->method('getResource')->willReturn($categoryResource);
44  $categoryResource->expects($this->any())->method('getConnection')->willReturn($this->connection);
45  $this->connection->expects($this->any())->method('select')->willReturn($this->select);
46  $this->connection->expects($this->any())->method('quoteIdentifier')->willReturnArgument(0);
47  $this->select->expects($this->any())->method('from')->willReturnSelf();
48 
49  $this->childrenCategoriesProvider = (new ObjectManager($this))->getObject(
50  \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider::class
51  );
52  }
53 
54  public function testGetChildrenRecursive()
55  {
56  $bind = ['c_path' => 'category-path/%'];
57  $this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
58  $this->select->expects($this->any())->method('where')->with('path LIKE :c_path')->willReturnSelf();
59  $this->connection->expects($this->any())->method('fetchCol')->with($this->select, $bind)->willReturn(['id']);
60  $this->childrenCategoriesProvider->getChildren($this->category, true);
61  }
62 
64  {
65  $this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
66  $this->assertEquals([], $this->childrenCategoriesProvider->getChildren($this->category));
67  }
68 
69  public function testGetChildren()
70  {
71  $categoryLevel = 3;
72  $this->select->expects($this->at(1))->method('where')->with('path LIKE :c_path')->willReturnSelf();
73  $this->select->expects($this->at(2))->method('where')->with('level <= :c_level')->willReturnSelf();
74  $this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
75  $this->category->expects($this->once())->method('getLevel')->willReturn($categoryLevel);
76  $bind = ['c_path' => 'category-path/%', 'c_level' => $categoryLevel + 1];
77  $this->connection->expects($this->any())->method('fetchCol')->with($this->select, $bind)->willReturn(['id']);
78 
79  $this->childrenCategoriesProvider->getChildren($this->category, false);
80  }
81 }