5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Model \ResourceModel \Product ;
7
7
8
+ use Magento \Catalog \Model \Indexer ;
9
+ use Magento \Catalog \Model \Product as ProductModel ;
10
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
8
11
use Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitationFactory ;
9
12
use Magento \Framework \DB \Select ;
13
+ use Magento \Eav \Model \Entity \AbstractEntity ;
14
+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
15
+ use Magento \Eav \Model \EntityFactory ;
16
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
17
+ use Magento \Framework \App \ResourceConnection ;
18
+ use Magento \Framework \Data \Collection ;
19
+ use Magento \Framework \Data \Collection \Db \FetchStrategyInterface ;
20
+ use Magento \Framework \DB ;
21
+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
22
+ use Magento \Framework \EntityManager \MetadataPool ;
23
+ use Magento \Framework \Event ;
10
24
use Magento \Framework \Model \ResourceModel \ResourceModelPoolInterface ;
25
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
26
+ use Magento \Framework \TestFramework ;
27
+ use Magento \Store \Api \Data \StoreInterface ;
28
+ use Magento \Store \Model \StoreManagerInterface ;
29
+ use PHPUnit \Framework \TestCase ;
30
+ use Psr \Log \LoggerInterface ;
11
31
12
32
/**
13
33
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
34
*/
15
- class CollectionTest extends \ PHPUnit \ Framework \ TestCase
35
+ class CollectionTest extends TestCase
16
36
{
17
37
/**
18
38
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
@@ -25,12 +45,12 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
25
45
protected $ selectMock ;
26
46
27
47
/**
28
- * @var \PHPUnit_Framework_MockObject_MockObject
48
+ * @var \PHPUnit_Framework_MockObject_MockObject|DB\Adapter\AdapterInterface
29
49
*/
30
50
protected $ connectionMock ;
31
51
32
52
/**
33
- * @var \Magento\Catalog\Model\ResourceModel\Product \Collection
53
+ * @var ProductResource \Collection
34
54
*/
35
55
protected $ collection ;
36
56
@@ -64,130 +84,51 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
64
84
*/
65
85
protected function setUp ()
66
86
{
67
- $ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
68
- $ entityFactory = $ this ->createMock (\Magento \Framework \Data \Collection \EntityFactory::class);
69
- $ logger = $ this ->getMockBuilder (\Psr \Log \LoggerInterface::class)
70
- ->disableOriginalConstructor ()
71
- ->getMockForAbstractClass ();
72
- $ fetchStrategy = $ this ->getMockBuilder (\Magento \Framework \Data \Collection \Db \FetchStrategyInterface::class)
73
- ->disableOriginalConstructor ()
74
- ->getMockForAbstractClass ();
75
- $ eventManager = $ this ->getMockBuilder (\Magento \Framework \Event \ManagerInterface::class)
76
- ->disableOriginalConstructor ()
77
- ->getMockForAbstractClass ();
78
- $ eavConfig = $ this ->getMockBuilder (\Magento \Eav \Model \Config::class)
79
- ->disableOriginalConstructor ()
80
- ->getMock ();
81
- $ resource = $ this ->getMockBuilder (\Magento \Framework \App \ResourceConnection::class)
82
- ->disableOriginalConstructor ()
83
- ->getMock ();
84
- $ eavEntityFactory = $ this ->getMockBuilder (\Magento \Eav \Model \EntityFactory::class)
85
- ->disableOriginalConstructor ()
86
- ->getMock ();
87
- $ resourceHelper = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Helper::class)
88
- ->disableOriginalConstructor ()
89
- ->getMock ();
90
- $ resourceModelPool = $ this ->getMockBuilder (ResourceModelPoolInterface::class)
91
- ->disableOriginalConstructor ()
92
- ->getMock ();
93
- $ this ->storeManager = $ this ->getMockBuilder (\Magento \Store \Model \StoreManagerInterface::class)
94
- ->disableOriginalConstructor ()
95
- ->setMethods (['getStore ' , 'getId ' , 'getWebsiteId ' ])
96
- ->getMockForAbstractClass ();
97
- $ moduleManager = $ this ->getMockBuilder (\Magento \Framework \Module \Manager::class)
98
- ->disableOriginalConstructor ()
99
- ->getMock ();
100
- $ catalogProductFlatState = $ this ->getMockBuilder (\Magento \Catalog \Model \Indexer \Product \Flat \State::class)
101
- ->disableOriginalConstructor ()
102
- ->getMock ();
103
- $ scopeConfig = $ this ->getMockBuilder (\Magento \Framework \App \Config \ScopeConfigInterface::class)
104
- ->disableOriginalConstructor ()
105
- ->getMockForAbstractClass ();
106
- $ productOptionFactory = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \OptionFactory::class)
107
- ->disableOriginalConstructor ()
108
- ->getMock ();
109
- $ catalogUrl = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Url::class)
110
- ->disableOriginalConstructor ()
111
- ->getMock ();
112
- $ localeDate = $ this ->getMockBuilder (\Magento \Framework \Stdlib \DateTime \TimezoneInterface::class)
113
- ->disableOriginalConstructor ()
114
- ->getMockForAbstractClass ();
115
- $ customerSession = $ this ->getMockBuilder (\Magento \Customer \Model \Session::class)
116
- ->disableOriginalConstructor ()
117
- ->getMock ();
118
- $ dateTime = $ this ->getMockBuilder (\Magento \Framework \Stdlib \DateTime::class)
119
- ->disableOriginalConstructor ()
120
- ->getMock ();
121
- $ groupManagement = $ this ->getMockBuilder (\Magento \Customer \Api \GroupManagementInterface::class)
122
- ->disableOriginalConstructor ()
123
- ->getMockForAbstractClass ();
124
-
125
- $ this ->connectionMock = $ this ->getMockBuilder (\Magento \Framework \DB \Adapter \AdapterInterface::class)
126
- ->setMethods (['getId ' ])
127
- ->disableOriginalConstructor ()
128
- ->getMockForAbstractClass ();
129
-
130
- $ this ->selectMock = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
131
- ->disableOriginalConstructor ()
132
- ->getMock ();
133
-
134
- $ this ->entityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \AbstractEntity::class)
135
- ->disableOriginalConstructor ()
136
- ->getMock ();
137
-
138
- $ this ->galleryResourceMock = $ this ->getMockBuilder (
139
- \Magento \Catalog \Model \ResourceModel \Product \Gallery::class
140
- )->disableOriginalConstructor ()->getMock ();
141
-
142
- $ this ->metadataPoolMock = $ this ->getMockBuilder (
143
- \Magento \Framework \EntityManager \MetadataPool::class
144
- )->disableOriginalConstructor ()->getMock ();
145
-
146
- $ this ->galleryReadHandlerMock = $ this ->getMockBuilder (
147
- \Magento \Catalog \Model \Product \Gallery \ReadHandler::class
148
- )->disableOriginalConstructor ()->getMock ();
149
-
150
- $ this ->storeManager ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
151
- $ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturnSelf ();
152
- $ resourceModelPool ->expects ($ this ->exactly (1 ))->method ('get ' )->willReturnOnConsecutiveCalls (
153
- $ this ->entityMock
154
- );
87
+ $ this ->objectManager = new TestFramework \Unit \Helper \ObjectManager ($ this );
88
+ $ this ->selectMock = $ this ->createMock (DB \Select::class);
89
+ $ this ->connectionMock = $ this ->createMock (DB \Adapter \AdapterInterface::class);
90
+ $ this ->connectionMock ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ this ->selectMock );
91
+ $ this ->entityMock = $ this ->createMock (AbstractEntity::class);
155
92
$ this ->entityMock ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ this ->connectionMock );
156
93
$ this ->entityMock ->expects ($ this ->once ())->method ('getDefaultAttributes ' )->willReturn ([]);
157
- $ this ->entityMock ->expects ($ this ->any ())->method ('getTable ' )->willReturnArgument (0 );
158
- $ this ->connectionMock ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ this ->selectMock );
94
+ $ this ->entityMock ->method ('getTable ' )->willReturnArgument (0 );
95
+ $ this ->galleryResourceMock = $ this ->createMock (ProductResource \Gallery::class);
96
+ $ this ->metadataPoolMock = $ this ->createMock (MetadataPool::class);
97
+ $ this ->galleryReadHandlerMock = $ this ->createMock (ProductModel \Gallery \ReadHandler::class);
159
98
160
- $ productLimitationMock = $ this ->createMock (
161
- \Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation::class
162
- );
163
- $ productLimitationFactoryMock = $ this ->getMockBuilder (
164
- ProductLimitationFactory::class
165
- )->disableOriginalConstructor ()->setMethods (['create ' ])->getMock ();
99
+ $ storeStub = $ this ->createMock (StoreInterface::class);
100
+ $ storeStub ->method ('getId ' )->willReturn (1 );
101
+ $ storeStub ->method ('getWebsiteId ' )->willReturn (1 );
102
+ $ this ->storeManager = $ this ->createMock (StoreManagerInterface::class);
103
+ $ this ->storeManager ->method ('getStore ' )->willReturn ($ storeStub );
104
+ $ resourceModelPool = $ this ->createMock (ResourceModelPoolInterface::class);
105
+ $ resourceModelPool ->expects ($ this ->exactly (1 ))->method ('get ' )->willReturn ($ this ->entityMock );
166
106
107
+ $ productLimitationFactoryMock = $ this ->createPartialMock (ProductLimitationFactory::class, ['create ' ]);
167
108
$ productLimitationFactoryMock ->method ('create ' )
168
- ->willReturn ($ productLimitationMock );
109
+ ->willReturn ($ this -> createMock ( ProductResource \ Collection \ProductLimitation::class) );
169
110
$ this ->collection = $ this ->objectManager ->getObject (
170
- \ Magento \ Catalog \ Model \ ResourceModel \ Product \Collection::class,
111
+ ProductResource \Collection::class,
171
112
[
172
- 'entityFactory ' => $ entityFactory ,
173
- 'logger ' => $ logger ,
174
- 'fetchStrategy ' => $ fetchStrategy ,
175
- 'eventManager ' => $ eventManager ,
176
- 'eavConfig ' => $ eavConfig ,
177
- 'resource ' => $ resource ,
178
- 'eavEntityFactory ' => $ eavEntityFactory ,
179
- 'resourceHelper ' => $ resourceHelper ,
113
+ 'entityFactory ' => $ this -> createMock ( Collection \EntityFactory::class) ,
114
+ 'logger ' => $ this -> createMock (LoggerInterface::class) ,
115
+ 'fetchStrategy ' => $ this -> createMock (FetchStrategyInterface::class) ,
116
+ 'eventManager ' => $ this -> createMock ( Event \ManagerInterface::class) ,
117
+ 'eavConfig ' => $ this -> createMock (\ Magento \ Eav \ Model \Config::class) ,
118
+ 'resource ' => $ this -> createMock (ResourceConnection::class) ,
119
+ 'eavEntityFactory ' => $ this -> createMock (EntityFactory::class) ,
120
+ 'resourceHelper ' => $ this -> createMock (\ Magento \ Catalog \ Model \ ResourceModel \Helper::class) ,
180
121
'resourceModelPool ' => $ resourceModelPool ,
181
122
'storeManager ' => $ this ->storeManager ,
182
- 'moduleManager ' => $ moduleManager ,
183
- 'catalogProductFlatState ' => $ catalogProductFlatState ,
184
- 'scopeConfig ' => $ scopeConfig ,
185
- 'productOptionFactory ' => $ productOptionFactory ,
186
- 'catalogUrl ' => $ catalogUrl ,
187
- 'localeDate ' => $ localeDate ,
188
- 'customerSession ' => $ customerSession ,
189
- 'dateTime ' => $ dateTime ,
190
- 'groupManagement ' => $ groupManagement ,
123
+ 'moduleManager ' => $ this -> createMock (\ Magento \ Framework \ Module \Manager::class) ,
124
+ 'catalogProductFlatState ' => $ this -> createMock ( Indexer \ Product \ Flat \State::class) ,
125
+ 'scopeConfig ' => $ this -> createMock (ScopeConfigInterface::class) ,
126
+ 'productOptionFactory ' => $ this -> createMock ( ProductModel \OptionFactory::class) ,
127
+ 'catalogUrl ' => $ this -> createMock (\ Magento \ Catalog \ Model \ ResourceModel \Url::class) ,
128
+ 'localeDate ' => $ this -> createMock (TimezoneInterface::class) ,
129
+ 'customerSession ' => $ this -> createMock (\ Magento \ Customer \ Model \Session::class) ,
130
+ 'dateTime ' => $ this -> createMock (\ Magento \ Framework \ Stdlib \DateTime::class) ,
131
+ 'groupManagement ' => $ this -> createMock (\ Magento \ Customer \ Api \GroupManagementInterface::class) ,
191
132
'connection ' => $ this ->connectionMock ,
192
133
'productLimitationFactory ' => $ productLimitationFactoryMock ,
193
134
'metadataPool ' => $ this ->metadataPoolMock ,
@@ -212,9 +153,8 @@ public function testAddProductCategoriesFilter()
212
153
$ condition = ['in ' => [1 , 2 ]];
213
154
$ values = [1 , 2 ];
214
155
$ conditionType = 'nin ' ;
215
- $ preparedSql = "category_id IN(1,2) " ;
216
- $ tableName = "catalog_category_product " ;
217
- $ this ->connectionMock ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
156
+ $ preparedSql = 'category_id IN(1,2) ' ;
157
+ $ tableName = 'catalog_category_product ' ;
218
158
$ this ->connectionMock ->expects ($ this ->exactly (2 ))->method ('prepareSqlCondition ' )->withConsecutive (
219
159
['cat.category_id ' , $ condition ],
220
160
['e.entity_id ' , [$ conditionType => $ this ->selectMock ]]
@@ -239,19 +179,14 @@ public function testAddMediaGalleryData()
239
179
$ rowId = 4 ;
240
180
$ linkField = 'row_id ' ;
241
181
$ mediaGalleriesMock = [[$ linkField => $ rowId ]];
242
- $ itemMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
182
+ /** @var ProductModel|\PHPUnit_Framework_MockObject_MockObject $itemMock */
183
+ $ itemMock = $ this ->getMockBuilder (ProductModel::class)
243
184
->disableOriginalConstructor ()
244
185
->setMethods (['getOrigData ' ])
245
186
->getMock ();
246
- $ attributeMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class)
247
- ->disableOriginalConstructor ()
248
- ->getMock ();
249
- $ selectMock = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
250
- ->disableOriginalConstructor ()
251
- ->getMock ();
252
- $ metadataMock = $ this ->getMockBuilder (\Magento \Framework \EntityManager \EntityMetadataInterface::class)
253
- ->disableOriginalConstructor ()
254
- ->getMock ();
187
+ $ attributeMock = $ this ->createMock (AbstractAttribute::class);
188
+ $ selectMock = $ this ->createMock (DB \Select::class);
189
+ $ metadataMock = $ this ->createMock (EntityMetadataInterface::class);
255
190
$ this ->collection ->addItem ($ itemMock );
256
191
$ this ->galleryResourceMock ->expects ($ this ->once ())->method ('createBatchBaseSelect ' )->willReturn ($ selectMock );
257
192
$ attributeMock ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
@@ -281,25 +216,15 @@ public function testAddMediaGalleryData()
281
216
public function testAddTierPriceDataByGroupId ()
282
217
{
283
218
$ customerGroupId = 2 ;
284
- $ itemMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
285
- ->disableOriginalConstructor ()
286
- ->setMethods (['getData ' ])
287
- ->getMock ();
288
- $ attributeMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class)
219
+ /** @var ProductModel|\PHPUnit_Framework_MockObject_MockObject $itemMock */
220
+ $ itemMock = $ this ->createMock (ProductModel::class);
221
+ $ attributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
289
222
->disableOriginalConstructor ()
290
223
->setMethods (['isScopeGlobal ' , 'getBackend ' ])
291
224
->getMock ();
292
- $ backend = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Attribute \Backend \Tierprice::class)
293
- ->disableOriginalConstructor ()
294
- ->getMock ();
295
- $ resource = $ this ->getMockBuilder (
296
- \Magento \Catalog \Model \ResourceModel \Product \Attribute \Backend \GroupPrice \AbstractGroupPrice::class
297
- )
298
- ->disableOriginalConstructor ()
299
- ->getMock ();
300
- $ select = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
301
- ->disableOriginalConstructor ()
302
- ->getMock ();
225
+ $ backend = $ this ->createMock (ProductModel \Attribute \Backend \Tierprice::class);
226
+ $ resource = $ this ->createMock (ProductResource \Attribute \Backend \GroupPrice \AbstractGroupPrice::class);
227
+ $ select = $ this ->createMock (DB \Select::class);
303
228
$ this ->connectionMock ->expects ($ this ->once ())->method ('getAutoIncrementField ' )->willReturn ('entity_id ' );
304
229
$ this ->collection ->addItem ($ itemMock );
305
230
$ itemMock ->expects ($ this ->atLeastOnce ())->method ('getData ' )->with ('entity_id ' )->willReturn (1 );
@@ -309,7 +234,6 @@ public function testAddTierPriceDataByGroupId()
309
234
->willReturn ($ attributeMock );
310
235
$ attributeMock ->expects ($ this ->atLeastOnce ())->method ('getBackend ' )->willReturn ($ backend );
311
236
$ attributeMock ->expects ($ this ->once ())->method ('isScopeGlobal ' )->willReturn (false );
312
- $ this ->storeManager ->expects ($ this ->once ())->method ('getWebsiteId ' )->willReturn (1 );
313
237
$ backend ->expects ($ this ->once ())->method ('getResource ' )->willReturn ($ resource );
314
238
$ resource ->expects ($ this ->once ())->method ('getSelect ' )->willReturn ($ select );
315
239
$ select ->expects ($ this ->once ())->method ('columns ' )->with (['product_id ' => 'entity_id ' ])->willReturnSelf ();
@@ -336,25 +260,22 @@ public function testAddTierPriceDataByGroupId()
336
260
*/
337
261
public function testAddTierPriceData ()
338
262
{
339
- $ itemMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
263
+ /** @var ProductModel|\PHPUnit_Framework_MockObject_MockObject $itemMock */
264
+ $ itemMock = $ this ->getMockBuilder (ProductModel::class)
340
265
->disableOriginalConstructor ()
341
266
->setMethods (['getData ' ])
342
267
->getMock ();
343
- $ attributeMock = $ this ->getMockBuilder (\ Magento \ Eav \ Model \ Entity \ Attribute \ AbstractAttribute::class)
268
+ $ attributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
344
269
->disableOriginalConstructor ()
345
270
->setMethods (['isScopeGlobal ' , 'getBackend ' ])
346
271
->getMock ();
347
- $ backend = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Attribute \Backend \Tierprice::class)
348
- ->disableOriginalConstructor ()
349
- ->getMock ();
272
+ $ backend = $ this ->createMock (ProductModel \Attribute \Backend \Tierprice::class);
350
273
$ resource = $ this ->getMockBuilder (
351
- \ Magento \ Catalog \ Model \ ResourceModel \ Product \Attribute \Backend \GroupPrice \AbstractGroupPrice::class
274
+ ProductResource \Attribute \Backend \GroupPrice \AbstractGroupPrice::class
352
275
)
353
276
->disableOriginalConstructor ()
354
277
->getMock ();
355
- $ select = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
356
- ->disableOriginalConstructor ()
357
- ->getMock ();
278
+ $ select = $ this ->createMock (DB \Select::class);
358
279
$ this ->connectionMock ->expects ($ this ->once ())->method ('getAutoIncrementField ' )->willReturn ('entity_id ' );
359
280
$ this ->collection ->addItem ($ itemMock );
360
281
$ itemMock ->expects ($ this ->atLeastOnce ())->method ('getData ' )->with ('entity_id ' )->willReturn (1 );
@@ -364,7 +285,6 @@ public function testAddTierPriceData()
364
285
->willReturn ($ attributeMock );
365
286
$ attributeMock ->expects ($ this ->atLeastOnce ())->method ('getBackend ' )->willReturn ($ backend );
366
287
$ attributeMock ->expects ($ this ->once ())->method ('isScopeGlobal ' )->willReturn (false );
367
- $ this ->storeManager ->expects ($ this ->once ())->method ('getWebsiteId ' )->willReturn (1 );
368
288
$ backend ->expects ($ this ->once ())->method ('getResource ' )->willReturn ($ resource );
369
289
$ resource ->expects ($ this ->once ())->method ('getSelect ' )->willReturn ($ select );
370
290
$ select ->expects ($ this ->once ())->method ('columns ' )->with (['product_id ' => 'entity_id ' ])->willReturnSelf ();
0 commit comments