@@ -30,6 +30,26 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
30
30
*/
31
31
protected $ collection ;
32
32
33
+ /**
34
+ * @var \PHPUnit_Framework_MockObject_MockObject
35
+ */
36
+ private $ galleryResourceMock ;
37
+
38
+ /**
39
+ * @var \PHPUnit_Framework_MockObject_MockObject
40
+ */
41
+ private $ entityMock ;
42
+
43
+ /**
44
+ * @var \PHPUnit_Framework_MockObject_MockObject
45
+ */
46
+ private $ metadataPoolMock ;
47
+
48
+ /**
49
+ * @var \PHPUnit_Framework_MockObject_MockObject
50
+ */
51
+ private $ galleryReadHandlerMock ;
52
+
33
53
/**
34
54
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
35
55
*/
@@ -100,24 +120,49 @@ protected function setUp()
100
120
->disableOriginalConstructor ()
101
121
->getMock ();
102
122
103
- $ entityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \AbstractEntity::class)
123
+ $ this -> entityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \AbstractEntity::class)
104
124
->disableOriginalConstructor ()
105
125
->getMock ();
106
126
127
+ $ this ->galleryResourceMock = $ this ->getMockBuilder (
128
+ \Magento \Catalog \Model \ResourceModel \Product \Gallery::class
129
+ )->disableOriginalConstructor ()->getMock ();
130
+
131
+ $ this ->metadataPoolMock = $ this ->getMockBuilder (
132
+ \Magento \Framework \EntityManager \MetadataPool::class
133
+ )->disableOriginalConstructor ()->getMock ();
134
+
135
+ $ this ->galleryReadHandlerMock = $ this ->getMockBuilder (
136
+ \Magento \Catalog \Model \Product \Gallery \ReadHandler::class
137
+ )->disableOriginalConstructor ()->getMock ();
138
+
107
139
$ storeManager ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
108
140
$ storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturnSelf ();
109
141
$ universalFactory ->expects ($ this ->exactly (1 ))->method ('create ' )->willReturnOnConsecutiveCalls (
110
- $ entityMock
142
+ $ this -> entityMock
111
143
);
112
- $ entityMock ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ this ->connectionMock );
113
- $ entityMock ->expects ($ this ->once ())->method ('getDefaultAttributes ' )->willReturn ([]);
114
- $ entityMock ->expects ($ this ->any ())->method ('getTable ' )->willReturnArgument (0 );
144
+ $ this -> entityMock ->expects ($ this ->once ())->method ('getConnection ' )->willReturn ($ this ->connectionMock );
145
+ $ this -> entityMock ->expects ($ this ->once ())->method ('getDefaultAttributes ' )->willReturn ([]);
146
+ $ this -> entityMock ->expects ($ this ->any ())->method ('getTable ' )->willReturnArgument (0 );
115
147
$ this ->connectionMock ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ this ->selectMock );
116
148
$ helper = new ObjectManager ($ this );
117
149
118
150
$ this ->prepareObjectManager ([
119
- [\Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation::class,
151
+ [
152
+ \Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation::class,
120
153
$ this ->getMock (\Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation::class)
154
+ ],
155
+ [
156
+ \Magento \Catalog \Model \ResourceModel \Product \Gallery::class,
157
+ $ this ->galleryResourceMock
158
+ ],
159
+ [
160
+ \Magento \Framework \EntityManager \MetadataPool::class,
161
+ $ this ->metadataPoolMock
162
+ ],
163
+ [
164
+ \Magento \Catalog \Model \Product \Gallery \ReadHandler::class,
165
+ $ this ->galleryReadHandlerMock
121
166
]
122
167
]);
123
168
$ this ->collection = $ helper ->getObject (
@@ -150,8 +195,8 @@ protected function setUp()
150
195
151
196
public function testAddProductCategoriesFilter ()
152
197
{
153
- $ condition = ['in ' => [1 ,2 ]];
154
- $ values = [1 ,2 ];
198
+ $ condition = ['in ' => [1 , 2 ]];
199
+ $ values = [1 , 2 ];
155
200
$ conditionType = 'nin ' ;
156
201
$ preparedSql = "category_id IN(1,2) " ;
157
202
$ tableName = "catalog_category_product " ;
@@ -174,6 +219,47 @@ public function testAddProductCategoriesFilter()
174
219
$ this ->collection ->addCategoriesFilter ([$ conditionType => $ values ]);
175
220
}
176
221
222
+ public function testAddMediaGalleryData ()
223
+ {
224
+ $ attributeId = 42 ;
225
+ $ itemId = 4242 ;
226
+ $ linkField = 'entity_id ' ;
227
+ $ mediaGalleriesMock = [[$ linkField => $ itemId ]];
228
+ $ itemMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
229
+ ->disableOriginalConstructor ()
230
+ ->getMock ();
231
+ $ attributeMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute \AbstractAttribute::class)
232
+ ->disableOriginalConstructor ()
233
+ ->getMock ();
234
+ $ selectMock = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
235
+ ->disableOriginalConstructor ()
236
+ ->getMock ();
237
+ $ metadataMock = $ this ->getMockBuilder (\Magento \Framework \EntityManager \EntityMetadataInterface::class)
238
+ ->disableOriginalConstructor ()
239
+ ->getMock ();
240
+ $ this ->collection ->addItem ($ itemMock );
241
+ $ reflection = new \ReflectionClass (get_class ($ this ->collection ));
242
+ $ reflectionProperty = $ reflection ->getProperty ('_isCollectionLoaded ' );
243
+ $ reflectionProperty ->setAccessible (true );
244
+ $ reflectionProperty ->setValue ($ this ->collection , true );
245
+
246
+ $ this ->galleryResourceMock ->expects ($ this ->once ())->method ('createBatchBaseSelect ' )->willReturn ($ selectMock );
247
+ $ attributeMock ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
248
+ $ this ->entityMock ->expects ($ this ->once ())->method ('getAttribute ' )->willReturn ($ attributeMock );
249
+ $ itemMock ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn ($ itemId );
250
+ $ selectMock ->expects ($ this ->once ())->method ('where ' )->with ('entity. ' . $ linkField . ' IN (?) ' , [$ itemId ]);
251
+ $ this ->metadataPoolMock ->expects ($ this ->once ())->method ('getMetadata ' )->willReturn ($ metadataMock );
252
+ $ metadataMock ->expects ($ this ->once ())->method ('getLinkField ' )->willReturn ($ linkField );
253
+
254
+ $ this ->connectionMock ->expects ($ this ->once ())->method ('fetchAll ' )->with ($ selectMock )->willReturn (
255
+ [['entity_id ' => $ itemId ]]
256
+ );
257
+ $ this ->galleryReadHandlerMock ->expects ($ this ->once ())->method ('addMediaDataToProduct ' )
258
+ ->with ($ itemMock , $ mediaGalleriesMock );
259
+
260
+ $ this ->assertSame ($ this ->collection , $ this ->collection ->addMediaGalleryData ());
261
+ }
262
+
177
263
/**
178
264
* @param $map
179
265
*/
0 commit comments