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