Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoriesFixtureTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Model\CategoryFactory;
11 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
15 
16 class CategoriesFixtureTest extends \PHPUnit\Framework\TestCase
17 {
21  private $fixtureModelMock;
22 
26  private $model;
27 
31  private $collectionFactoryMock;
32 
36  private $collectionMock;
37 
41  private $categoryFactoryMock;
42 
43  public function setUp()
44  {
45  $this->fixtureModelMock = $this->createMock(FixtureModel::class);
46  $this->collectionFactoryMock = $this->createPartialMock(CollectionFactory::class, ['create']);
47  $this->collectionMock = $this->createMock(Collection::class);
48  $this->categoryFactoryMock = $this->createPartialMock(CategoryFactory::class, ['create']);
49 
50  $this->model = (new ObjectManager($this))->getObject(CategoriesFixture::class, [
51  'fixtureModel' => $this->fixtureModelMock,
52  'collectionFactory' => $this->collectionFactoryMock,
53  'rootCategoriesIds' => [2],
54  'categoryFactory' => $this->categoryFactoryMock,
55  'firstLevelCategoryIndex' => 1,
56  ]);
57  }
58 
60  {
61  $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
62  $this->collectionMock->expects($this->once())->method('getSize')->willReturn(32);
63  $this->fixtureModelMock
64  ->expects($this->once())
65  ->method('getValue')
66  ->willReturn(30);
67  $this->categoryFactoryMock->expects($this->never())->method('create');
68 
69  $this->model->execute();
70  }
71 
72  public function testExecute()
73  {
74  $valueMap = [
75  ['categories', 0, 1],
76  ['categories_nesting_level', 3, 3]
77  ];
78 
79  $this->fixtureModelMock
80  ->expects($this->exactly(2))
81  ->method('getValue')
82  ->will($this->returnValueMap($valueMap));
83 
84  $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->collectionMock);
85  $this->collectionMock->expects($this->once())->method('getSize')->willReturn(2);
86 
87  $parentCategoryMock = $this->createPartialMock(\Magento\Catalog\Model\Category::class, [
88  'getName',
89  'setId',
90  'getId',
91  'setUrlKey',
92  'setUrlPath',
93  'setName',
94  'setParentId',
95  'setPath',
96  'setLevel',
97  'getLevel',
98  'setAvailableSortBy',
99  'setDefaultSortBy',
100  'setIsActive',
101  'setIsAnchor',
102  'save',
103  'setStoreId',
104  'load',
105  ]);
106  $parentCategoryMock->expects($this->once())->method('getId')->willReturn(5);
107  $parentCategoryMock->expects($this->once())->method('getLevel')->willReturn(3);
108  $categoryMock = clone $parentCategoryMock;
109  $categoryMock->expects($this->once())
110  ->method('getName')
111  ->with('Category 1')
112  ->will($this->returnValue('category_name'));
113  $categoryMock->expects($this->once())
114  ->method('setId')
115  ->willReturnSelf();
116  $categoryMock->expects($this->once())
117  ->method('setUrlKey')
118  ->willReturnSelf();
119  $categoryMock->expects($this->once())
120  ->method('setUrlPath')
121  ->willReturnSelf();
122  $categoryMock->expects($this->once())
123  ->method('setName')
124  ->willReturnSelf();
125  $categoryMock->expects($this->once())
126  ->method('setParentId')
127  ->with(5)
128  ->willReturnSelf();
129  $categoryMock->expects($this->once())
130  ->method('setPath')
131  ->willReturnSelf();
132  $categoryMock->expects($this->once())
133  ->method('setIsAnchor')
134  ->with(true)
135  ->willReturnSelf();
136  $categoryMock->expects($this->once())
137  ->method('setLevel')
138  ->with(4)
139  ->willReturnSelf();
140  $categoryMock->expects($this->once())
141  ->method('setAvailableSortBy')
142  ->willReturnSelf();
143  $categoryMock->expects($this->once())
144  ->method('setDefaultSortBy')
145  ->willReturnSelf();
146  $categoryMock->expects($this->once())
147  ->method('setIsActive')
148  ->willReturnSelf();
149 
150  $this->categoryFactoryMock->expects($this->once())->method('create')->willReturn($categoryMock);
151 
152  $this->model->execute();
153  }
154 
155  public function testGetActionTitle()
156  {
157  $this->assertSame('Generating categories', $this->model->getActionTitle());
158  }
159 
160  public function testIntroduceParamLabels()
161  {
162  $this->assertSame([
163  'categories' => 'Categories'
164  ], $this->model->introduceParamLabels());
165  }
166 }