Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
CollectTest.php
Go to the documentation of this file.
1 <?php
7 
13 
17 class CollectTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $model;
23 
27  protected $moduleListMock;
28 
32  protected $moduleManagerMock;
33 
38 
42  protected $moduleFactoryMock;
43 
49 
50  protected function setUp()
51  {
52  $this->moduleListMock = $this->getMockBuilder(\Magento\Framework\Module\ModuleListInterface::class)
53  ->setMethods(['getNames', 'has', 'getAll', 'getOne'])
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->fullModuleListMock = $this->getMockBuilder(\Magento\Framework\Module\FullModuleList::class)
58  ->setMethods(['getNames', 'getAll'])
59  ->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->moduleManagerMock = $this->getMockBuilder(\Magento\Framework\Module\Manager::class)
63  ->setMethods(['isOutputEnabled'])
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->moduleFactoryMock = $this->createPartialMock(
68  \Magento\NewRelicReporting\Model\ModuleFactory::class,
69  ['create']
70  );
71 
72  $this->moduleCollectionFactoryMock = $this->createPartialMock(
73  \Magento\NewRelicReporting\Model\ResourceModel\Module\CollectionFactory::class,
74  ['create']
75  );
76 
77  $this->model = new Collect(
78  $this->moduleListMock,
79  $this->fullModuleListMock,
80  $this->moduleManagerMock,
81  $this->moduleFactoryMock,
82  $this->moduleCollectionFactoryMock
83  );
84  }
85 
92  {
93  $moduleCollectionMock = $this->getMockBuilder(
94  \Magento\NewRelicReporting\Model\ResourceModel\Module\Collection::class
95  )
96  ->disableOriginalConstructor()
97  ->getMock();
98  $itemMock = $this->createMock(\Magento\NewRelicReporting\Model\Module::class);
99  $modulesMockArray = [
100  'Module_Name' => [
101  'name' => 'Name',
102  'setup_version' => '2.0.0',
103  'sequence' => []
104  ]
105  ];
106  $testChangesMockArray = [
107  ['entity' => '3',
108  'name' => 'Name',
109  'active' => 'true',
110  'state' => 'enabled',
111  'setup_version' => '2.0.0',
112  'updated_at' => '2015-09-02 18:38:17'],
113  ['entity' => '4',
114  'name' => 'Name',
115  'active' => 'true',
116  'state' => 'disabled',
117  'setup_version' => '2.0.0',
118  'updated_at' => '2015-09-02 18:38:17'],
119  ['entity' => '5',
120  'name' => 'Name',
121  'active' => 'true',
122  'state' => 'uninstalled',
123  'setup_version' => '2.0.0',
124  'updated_at' => '2015-09-02 18:38:17']
125  ];
126  $itemMockArray = [$itemMock];
127  $enabledModulesMockArray = [];
128 
129  $this->moduleCollectionFactoryMock->expects($this->any())
130  ->method('create')
131  ->willReturn($moduleCollectionMock);
132 
133  $this->moduleFactoryMock->expects($this->any())
134  ->method('create')
135  ->willReturn($itemMock);
136 
137  $itemMock->expects($this->any())
138  ->method('setData')
139  ->willReturnSelf();
140 
141  $itemMock->expects($this->any())
142  ->method('save')
143  ->willReturnSelf();
144 
145  $moduleCollectionMock->expects($this->any())
146  ->method('getItems')
147  ->willReturn($itemMockArray);
148 
149  $moduleCollectionMock->expects($this->any())
150  ->method('getData')
151  ->willReturn($testChangesMockArray);
152 
153  $this->fullModuleListMock->expects($this->once())
154  ->method('getAll')
155  ->willReturn($modulesMockArray);
156 
157  $this->fullModuleListMock->expects($this->once())
158  ->method('getNames')
159  ->willReturn($enabledModulesMockArray);
160 
161  $this->moduleListMock->expects($this->once())
162  ->method('getNames')
163  ->willReturn($enabledModulesMockArray);
164 
165  $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will(
166  $this->returnValue(false)
167  );
168 
169  $this->assertInternalType(
170  'array',
171  $this->model->getModuleData()
172  );
173  }
174 
181  public function testGetModuleDataRefresh($data)
182  {
183  $moduleCollectionMock = $this->getMockBuilder(
184  \Magento\NewRelicReporting\Model\ResourceModel\Module\Collection::class
185  )
186  ->disableOriginalConstructor()
187  ->getMock();
189  $itemMock = $this->createPartialMock(
190  \Magento\NewRelicReporting\Model\Module::class,
191  ['getName', 'getData', 'setData', 'getState', 'save']
192  );
193  $modulesMockArray = [
194  'Module_Name1' => [
195  'name' => 'Module_Name1',
196  'setup_version' => '2.0.0',
197  'sequence' => []
198  ]
199  ];
200  $itemMock->setData($data);
201  $testChangesMockArray = [
202  'entity_id' => '3',
203  'name' => 'Name',
204  'active' => 'true',
205  'state' => 'uninstalled',
206  'setup_version' => '2.0.0',
207  'some_param' => 'some_value',
208  'updated_at' => '2015-09-02 18:38:17'
209  ];
210  $itemMockArray = [$itemMock];
211 
212  $enabledModulesMockArray = ['Module_Name2'];
213  $allModulesMockArray = ['Module_Name1','Module_Name2'];
214 
215  $this->moduleCollectionFactoryMock->expects($this->any())
216  ->method('create')
217  ->willReturn($moduleCollectionMock);
218 
219  $this->moduleFactoryMock->expects($this->any())
220  ->method('create')
221  ->willReturn($itemMock);
222 
223  $itemMock->expects($this->any())
224  ->method('setData')
225  ->willReturnSelf();
226 
227  $itemMock->expects($this->any())
228  ->method('save')
229  ->willReturnSelf();
230 
231  $itemMock->expects($this->any())
232  ->method('getState')
233  ->willReturn($data['state']);
234 
235  $itemMock->expects($this->any())
236  ->method('getName')
237  ->willReturn($data['name']);
238 
239  $moduleCollectionMock->expects($this->any())
240  ->method('getItems')
241  ->willReturn($itemMockArray);
242 
243  $itemMock->expects($this->any())
244  ->method('getData')
245  ->willReturn($testChangesMockArray);
246 
247  $this->fullModuleListMock->expects($this->once())
248  ->method('getAll')
249  ->willReturn($modulesMockArray);
250 
251  $this->fullModuleListMock->expects($this->any())
252  ->method('getNames')
253  ->willReturn($allModulesMockArray);
254 
255  $this->moduleListMock->expects($this->any())
256  ->method('getNames')
257  ->willReturn($enabledModulesMockArray);
258 
259  $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will(
260  $this->returnValue(true)
261  );
262 
263  $this->assertInternalType(
264  'array',
265  $this->model->getModuleData()
266  );
267  }
268 
275  public function testGetModuleDataRefreshOrStatement($data)
276  {
277  $moduleCollectionMock = $this->getMockBuilder(
278  \Magento\NewRelicReporting\Model\ResourceModel\Module\Collection::class
279  )
280  ->disableOriginalConstructor()
281  ->getMock();
283  $itemMock = $this->createPartialMock(
284  \Magento\NewRelicReporting\Model\Module::class,
285  ['getName', 'getData', 'setData', 'getState', 'save']
286  );
287  $modulesMockArray = [
288  'Module_Name1' => [
289  'name' => 'Module_Name1',
290  'setup_version' => '2.0.0',
291  'sequence' => []
292  ]
293  ];
294  $itemMock->setData($data);
295  $testChangesMockArray = [
296  'entity_id' => '3',
297  'name' => 'Name',
298  'active' => 'false',
299  'state' => 'enabled',
300  'setup_version' => '2.0.0',
301  'some_param' => 'some_value',
302  'updated_at' => '2015-09-02 18:38:17'
303  ];
304  $itemMockArray = [$itemMock];
305 
306  $enabledModulesMockArray = ['Module_Name2'];
307  $allModulesMockArray = ['Module_Name1','Module_Name2'];
308 
309  $this->moduleCollectionFactoryMock->expects($this->any())
310  ->method('create')
311  ->willReturn($moduleCollectionMock);
312 
313  $this->moduleFactoryMock->expects($this->any())
314  ->method('create')
315  ->willReturn($itemMock);
316 
317  $itemMock->expects($this->any())
318  ->method('setData')
319  ->willReturnSelf();
320 
321  $itemMock->expects($this->any())
322  ->method('save')
323  ->willReturnSelf();
324 
325  $itemMock->expects($this->any())
326  ->method('getState')
327  ->willReturn($data['state']);
328 
329  $itemMock->expects($this->any())
330  ->method('getName')
331  ->willReturn($data['name']);
332 
333  $moduleCollectionMock->expects($this->any())
334  ->method('getItems')
335  ->willReturn($itemMockArray);
336 
337  $itemMock->expects($this->any())
338  ->method('getData')
339  ->willReturn($testChangesMockArray);
340 
341  $this->fullModuleListMock->expects($this->once())
342  ->method('getAll')
343  ->willReturn($modulesMockArray);
344 
345  $this->fullModuleListMock->expects($this->any())
346  ->method('getNames')
347  ->willReturn($allModulesMockArray);
348 
349  $this->moduleListMock->expects($this->any())
350  ->method('getNames')
351  ->willReturn($enabledModulesMockArray);
352 
353  $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will(
354  $this->returnValue(true)
355  );
356 
357  $this->assertInternalType(
358  'array',
359  $this->model->getModuleData()
360  );
361  }
362 
366  public function itemDataProvider()
367  {
368  return [
369  [
370  [
371  'entity_id' => '1',
372  'name' => 'Module_Name1',
373  'active' => 'true',
374  'state' => 'enabled',
375  'setup_version' => '2.0.0'
376  ]
377  ],
378  [
379  [
380  'entity_id' => '2',
381  'name' => 'Module_Name2',
382  'active' => 'true',
383  'state' => 'disabled',
384  'setup_version' => '2.0.0'
385  ]
386  ],
387  [
388  [
389  'entity_id' => '2',
390  'name' => 'Module_Name2',
391  'active' => 'true',
392  'state' => 'uninstalled',
393  'setup_version' => '2.0.0'
394  ]
395  ]
396  ];
397  }
398 }