1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Data\Test\Unit;
/**
* Class for Collection test.
*/
class CollectionTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Data\Collection
*/
protected $_model;
/**
* Set up.
*/
protected function setUp()
{
$this->_model = new \Magento\Framework\Data\Collection(
$this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
);
}
/**
* Test for method removeAllItems.
*
* @return void
*/
public function testRemoveAllItems()
{
$this->_model->addItem(new \Magento\Framework\DataObject());
$this->_model->addItem(new \Magento\Framework\DataObject());
$this->assertCount(2, $this->_model->getItems());
$this->_model->removeAllItems();
$this->assertEmpty($this->_model->getItems());
}
/**
* Test loadWithFilter()
*
* @return void
*/
public function testLoadWithFilter()
{
$this->assertInstanceOf(\Magento\Framework\Data\Collection::class, $this->_model->loadWithFilter());
$this->assertEmpty($this->_model->getItems());
$this->_model->addItem(new \Magento\Framework\DataObject());
$this->_model->addItem(new \Magento\Framework\DataObject());
$this->assertCount(2, $this->_model->loadWithFilter()->getItems());
}
/**
* Test for method etItemObjectClass
*
* @dataProvider setItemObjectClassDataProvider
*/
public function testSetItemObjectClass($class)
{
$this->_model->setItemObjectClass($class);
$this->assertAttributeSame($class, '_itemObjectClass', $this->_model);
}
/**
* Data provider.
*
* @return array
*/
public function setItemObjectClassDataProvider()
{
return [[\Magento\Framework\Url::class], [\Magento\Framework\DataObject::class]];
}
/**
* Test for method setItemObjectClass with exception.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Incorrect_ClassName does not extend \Magento\Framework\DataObject
*/
public function testSetItemObjectClassException()
{
$this->_model->setItemObjectClass('Incorrect_ClassName');
}
/**
* Test for method addFilter.
*
* @return void
*/
public function testAddFilter()
{
$this->_model->addFilter('field1', 'value');
$this->assertEquals('field1', $this->_model->getFilter('field1')->getData('field'));
}
/**
* Test for method getFilters.
*
* @return void
*/
public function testGetFilters()
{
$this->_model->addFilter('field1', 'value');
$this->_model->addFilter('field2', 'value');
$this->assertEquals('field1', $this->_model->getFilter(['field1', 'field2'])[0]->getData('field'));
$this->assertEquals('field2', $this->_model->getFilter(['field1', 'field2'])[1]->getData('field'));
}
/**
* Test for method get non existion filters.
*
* @return void
*/
public function testGetNonExistingFilters()
{
$this->assertEmpty($this->_model->getFilter([]));
$this->assertEmpty($this->_model->getFilter('non_existing_filter'));
}
/**
* Test for lag.
*
* @return void
*/
public function testFlag()
{
$this->_model->setFlag('flag_name', 'flag_value');
$this->assertEquals('flag_value', $this->_model->getFlag('flag_name'));
$this->assertTrue($this->_model->hasFlag('flag_name'));
$this->assertNull($this->_model->getFlag('non_existing_flag'));
}
/**
* Test for method getCurPage.
*
* @return void
*/
public function testGetCurPage()
{
$this->_model->setCurPage(1);
$this->assertEquals(1, $this->_model->getCurPage());
}
/**
* Test for method possibleFlowWithItem.
*
* @return void
*/
public function testPossibleFlowWithItem()
{
$firstItemMock = $this->createPartialMock(
\Magento\Framework\DataObject::class,
['getId', 'getData', 'toArray']
);
$secondItemMock = $this->createPartialMock(
\Magento\Framework\DataObject::class,
['getId', 'getData', 'toArray']
);
$requiredFields = ['required_field_one', 'required_field_two'];
$arrItems = [
'totalRecords' => 1,
'items' => [
0 => 'value',
],
];
$items = [
'item_id' => $firstItemMock,
0 => $secondItemMock,
];
$firstItemMock->expects($this->exactly(2))->method('getId')->will($this->returnValue('item_id'));
$firstItemMock
->expects($this->atLeastOnce())
->method('getData')
->with('colName')
->will($this->returnValue('first_value'));
$secondItemMock
->expects($this->atLeastOnce())
->method('getData')
->with('colName')
->will($this->returnValue('second_value'));
$firstItemMock
->expects($this->once())
->method('toArray')
->with($requiredFields)
->will($this->returnValue('value'));
/** add items and set them values */
$this->_model->addItem($firstItemMock);
$this->assertEquals($arrItems, $this->_model->toArray($requiredFields));
$this->_model->addItem($secondItemMock);
$this->_model->setDataToAll('column', 'value');
/** get items by column name */
$this->assertEquals(['first_value', 'second_value'], $this->_model->getColumnValues('colName'));
$this->assertEquals([$secondItemMock], $this->_model->getItemsByColumnValue('colName', 'second_value'));
$this->assertEquals($firstItemMock, $this->_model->getItemByColumnValue('colName', 'second_value'));
$this->assertEquals([], $this->_model->getItemsByColumnValue('colName', 'non_existing_value'));
$this->assertEquals(null, $this->_model->getItemByColumnValue('colName', 'non_existing_value'));
/** get items */
$this->assertEquals(['item_id', 0], $this->_model->getAllIds());
$this->assertEquals($firstItemMock, $this->_model->getFirstItem());
$this->assertEquals($secondItemMock, $this->_model->getLastItem());
$this->assertEquals($items, $this->_model->getItems('item_id'));
/** remove existing items */
$this->assertNull($this->_model->getItemById('not_existing_item_id'));
$this->_model->removeItemByKey('item_id');
$this->assertEquals([$secondItemMock], $this->_model->getItems());
$this->_model->removeAllItems();
$this->assertEquals([], $this->_model->getItems());
}
/**
* Test for method eachCallsMethodOnEachItemWithNoArgs.
*
* @return void
*/
public function testEachCallsMethodOnEachItemWithNoArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testCallback']);
$item->expects($this->once())->method('testCallback')->with();
$this->_model->addItem($item);
}
$this->_model->each('testCallback');
}
/**
* Test for method eachCallsMethodOnEachItemWithArgs.
*
* @return void
*/
public function testEachCallsMethodOnEachItemWithArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testCallback']);
$item->expects($this->once())->method('testCallback')->with('a', 'b', 'c');
$this->_model->addItem($item);
}
$this->_model->each('testCallback', ['a', 'b', 'c']);
}
/**
* Test for method callsClosureWithEachItemAndNoArgs.
*
* @return void
*/
public function testCallsClosureWithEachItemAndNoArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testCallback']);
$item->expects($this->once())->method('testCallback')->with();
$this->_model->addItem($item);
}
$this->_model->each(function ($item) {
$item->testCallback();
});
}
/**
* Test for method callsClosureWithEachItemAndArgs.
*
* @return void
*/
public function testCallsClosureWithEachItemAndArgs()
{
for ($i = 0; $i < 3; $i++) {
$item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
$item->expects($this->once())->method('testItemCallback')->with('a', 'b', 'c');
$this->_model->addItem($item);
}
$this->_model->each(function ($item, ...$args) {
$item->testItemCallback(...$args);
}, ['a', 'b', 'c']);
}
/**
* Test for method callsCallableArrayWithEachItemNoArgs.
*
* @return void
*/
public function testCallsCallableArrayWithEachItemNoArgs()
{
$mockCallbackObject = $this->getMockBuilder('DummyEachCallbackInstance')
->setMethods(['testObjCallback'])
->getMock();
$mockCallbackObject->method('testObjCallback')->willReturnCallback(function ($item, ...$args) {
$item->testItemCallback(...$args);
});
for ($i = 0; $i < 3; $i++) {
$item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
$item->expects($this->once())->method('testItemCallback')->with();
$this->_model->addItem($item);
}
$this->_model->each([$mockCallbackObject, 'testObjCallback']);
}
/**
* Test for method callsCallableArrayWithEachItemAndArgs.
*
* @return void
*/
public function testCallsCallableArrayWithEachItemAndArgs()
{
$mockCallbackObject = $this->getMockBuilder('DummyEachCallbackInstance')
->setMethods(['testObjCallback'])
->getMock();
$mockCallbackObject->method('testObjCallback')->willReturnCallback(function ($item, ...$args) {
$item->testItemCallback(...$args);
});
for ($i = 0; $i < 3; $i++) {
$item = $this->createPartialMock(\Magento\Framework\DataObject::class, ['testItemCallback']);
$item->expects($this->once())->method('testItemCallback')->with('a', 'b', 'c');
$this->_model->addItem($item);
}
$callback = [$mockCallbackObject, 'testObjCallback'];
$this->_model->each($callback, ['a', 'b', 'c']);
}
}