Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoresFixtureTest.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Model\CategoryFactory;
20 
21 class StoresFixtureTest extends \PHPUnit\Framework\TestCase
22 {
23 
27  private $fixtureModelMock;
28 
32  private $model;
33 
37  private $storeManagerMock;
38 
42  private $eventManagerMock;
43 
47  private $categoryFactoryMock;
48 
52  private $scopeConfigMock;
53 
57  private $localeConfigMock;
58 
62  public function testExecute()
63  {
64  $this->fixtureModelMock = $this->getMockBuilder(FixtureModel::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  $this->storeManagerMock = $this->getMockBuilder(StoreManager::class)
69  ->disableOriginalConstructor()
70  ->setMethods(
71  [
72  'getGroup',
73  'getGroups',
74  'getWebsite',
75  'getDefaultStoreView',
76  'getStore',
77  'getStores',
78  ]
79  )->getMock();
80 
81  $this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84 
85  $this->categoryFactoryMock = $this->getMockBuilder(CategoryFactory::class)
86  ->disableOriginalConstructor()
87  ->setMethods(['create'])
88  ->getMock();
89 
90  $categoryMock = $this->getMockBuilder(CategoryInterface::class)
91  ->disableOriginalConstructor()
92  ->setMethods(
93  [
94  'create',
95  'setName',
96  'setPath',
97  'setLevel',
98  'setAvailableSortBy',
99  'setDefaultSortBy',
100  'setIsActive',
101  'save'
102  ]
103  )
104  ->getMockForAbstractClass();
105 
106  $this->categoryFactoryMock->expects($this->exactly(5))
107  ->method('create')
108  ->willReturn($categoryMock);
109 
110  $categoryMock->expects($this->exactly(5))
111  ->method('setName')
112  ->willReturn($categoryMock);
113 
114  $categoryMock->expects($this->exactly(5))
115  ->method('setPath')
116  ->willReturn($categoryMock);
117 
118  $categoryMock->expects($this->exactly(5))
119  ->method('setLevel')
120  ->willReturn($categoryMock);
121 
122  $categoryMock->expects($this->exactly(5))
123  ->method('setAvailableSortBy')
124  ->willReturn($categoryMock);
125 
126  $categoryMock->expects($this->exactly(5))
127  ->method('setDefaultSortBy')
128  ->willReturn($categoryMock);
129 
130  $categoryMock->expects($this->exactly(5))
131  ->method('setIsActive')
132  ->willReturn($categoryMock);
133 
134  $categoryMock->expects($this->exactly(5))
135  ->method('getId')
136  ->willReturn($categoryMock);
137 
138  $categoryMock->expects($this->exactly(5))
139  ->method('save')
140  ->willReturn($categoryMock);
141 
142  $this->localeConfigMock = $this->getMockBuilder(Config::class)
143  ->disableOriginalConstructor()
144  ->setMethods(['getAllowedLocales'])
145  ->getMock();
146 
147  $this->localeConfigMock->expects($this->once())
148  ->method('getAllowedLocales')
149  ->willReturn(['en_US']);
150 
151  $this->scopeConfigMock = $this->getMockBuilder(Writer::class)
152  ->disableOriginalConstructor()
153  ->getMock();
154 
155  $storeMock = $this->getMockBuilder(StoreInterface::class)
156  ->disableOriginalConstructor()
157  ->setMethods(['getId', 'getRootCategoryId', 'addData', 'save'])
158  ->getMockForAbstractClass();
159 
160  $storeMock->expects($this->exactly(11))
161  ->method('getId')
162  ->willReturn(1);
163 
164  $storeMock->expects($this->exactly(11))
165  ->method('addData')
166  ->withConsecutive(
167  [
168  [
169  'store_id' => null,
170  'name' => 'Store view 2 - website_id_1 - group_id_1',
171  'website_id' => 1,
172  'group_id' => 1,
173  'code' => 'store_view_2',
174  ]
175  ],
176  [
177  [
178  'store_id' => null,
179  'name' => 'Store view 3 - website_id_1 - group_id_1',
180  'website_id' => 1,
181  'group_id' => 1,
182  'code' => 'store_view_3',
183  ]
184  ]
185  )
186  ->willReturn($storeMock);
187 
188  $storeGroupMock = $this->getMockBuilder(GroupInterface::class)
189  ->disableOriginalConstructor()
190  ->setMethods(['getId', 'addData', 'save'])
191  ->getMockForAbstractClass();
192 
193  $storeGroupMock->expects($this->exactly(11))
194  ->method('getId')
195  ->willReturn(1);
196 
197  $storeGroupMock->expects($this->exactly(5))
198  ->method('addData')
199  ->withConsecutive(
200  [
201  [
202  'group_id' => null,
203  'website_id' => 1,
204  'name' => 'Store Group 2 - website_id_1',
205  'code' => 'store_group_2',
206  'root_category_id' => $categoryMock,
207  ]
208  ],
209  [
210  [
211  'group_id' => null,
212  'website_id' => 1,
213  'name' => 'Store Group 3 - website_id_1',
214  'code' => 'store_group_3',
215  'root_category_id' => $categoryMock,
216  ]
217  ]
218  )
219  ->willReturn($storeGroupMock);
220 
221  $websiteMock = $this->getMockBuilder(WebsiteInterface::class)
222  ->disableOriginalConstructor()
223  ->setMethods(['getId', 'addData', 'save'])
224  ->getMockForAbstractClass();
225 
226  $websiteMock->expects($this->exactly(3))
227  ->method('getId')
228  ->willReturn(1);
229 
230  $websiteMock->expects($this->exactly(2))
231  ->method('addData')
232  ->withConsecutive(
233  [
234  [
235  'website_id' => null,
236  'code' => 'website_2',
237  'name' => 'Website 2',
238  'is_default' => false,
239  ]
240  ],
241  [
242  [
243  'website_id' => null,
244  'code' => 'website_3',
245  'name' => 'Website 3',
246  'is_default' => false,
247  ]
248  ]
249  )
250  ->willReturn($storeGroupMock);
251 
252  $this->storeManagerMock->expects($this->once())
253  ->method('getGroups')
254  ->willReturn([$storeGroupMock]);
255 
256  $this->storeManagerMock->expects($this->once())
257  ->method('getGroup')
258  ->willReturn($storeGroupMock);
259 
260  $this->storeManagerMock->expects($this->once())
261  ->method('getWebsite')
262  ->willReturn($websiteMock);
263 
264  $this->storeManagerMock->expects($this->once())
265  ->method('getStores')
266  ->willReturn([$storeMock]);
267 
268  $this->storeManagerMock->expects($this->once())
269  ->method('getDefaultStoreView')
270  ->willReturn($storeMock);
271 
272  $this->fixtureModelMock
273  ->expects($this->exactly(4))
274  ->method('getValue')
275  ->will($this->returnValueMap(
276  [
277  ['websites', 1, 3],
278  ['store_groups', 1, 6],
279  ['store_views', 1, 12],
280  ['assign_entities_to_all_websites', false]
281  ]
282  ));
283 
284  $this->model = new StoresFixture(
285  $this->fixtureModelMock,
286  $this->storeManagerMock,
287  $this->eventManagerMock,
288  $this->categoryFactoryMock,
289  $this->localeConfigMock,
290  $this->scopeConfigMock
291  );
292 
293  $this->model->execute();
294  }
295 }