10
10
11
11
/**
12
12
* Class CollectionTest
13
- *
14
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
13
*/
16
14
class CollectionTest extends \PHPUnit_Framework_TestCase
17
15
{
@@ -30,98 +28,143 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
30
28
*/
31
29
protected $ collection ;
32
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
+
33
51
/**
34
52
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
35
53
*/
36
54
protected function setUp ()
37
55
{
38
- $ entityFactory = $ this ->getMock (\ Magento \Framework \Data \Collection \EntityFactory::class , [], [], '' , false );
39
- $ logger = $ this ->getMockBuilder (\ Psr \Log \LoggerInterface::class )
56
+ $ entityFactory = $ this ->getMock (' Magento\Framework\Data\Collection\EntityFactory ' , [], [], '' , false );
57
+ $ logger = $ this ->getMockBuilder (' Psr\Log\LoggerInterface ' )
40
58
->disableOriginalConstructor ()
41
59
->getMockForAbstractClass ();
42
- $ fetchStrategy = $ this ->getMockBuilder (\ Magento \Framework \Data \Collection \Db \FetchStrategyInterface::class )
60
+ $ fetchStrategy = $ this ->getMockBuilder (' Magento\Framework\Data\Collection\Db\FetchStrategyInterface ' )
43
61
->disableOriginalConstructor ()
44
62
->getMockForAbstractClass ();
45
- $ eventManager = $ this ->getMockBuilder (\ Magento \Framework \Event \ManagerInterface::class )
63
+ $ eventManager = $ this ->getMockBuilder (' Magento\Framework\Event\ManagerInterface ' )
46
64
->disableOriginalConstructor ()
47
65
->getMockForAbstractClass ();
48
- $ eavConfig = $ this ->getMockBuilder (\ Magento \Eav \Model \Config::class )
66
+ $ eavConfig = $ this ->getMockBuilder (' Magento\Eav\Model\Config ' )
49
67
->disableOriginalConstructor ()
50
68
->getMock ();
51
- $ resource = $ this ->getMockBuilder (\ Magento \Framework \App \ResourceConnection::class )
69
+ $ resource = $ this ->getMockBuilder (' Magento\Framework\App\ResourceConnection ' )
52
70
->disableOriginalConstructor ()
53
71
->getMock ();
54
- $ eavEntityFactory = $ this ->getMockBuilder (\ Magento \Eav \Model \EntityFactory::class )
72
+ $ eavEntityFactory = $ this ->getMockBuilder (' Magento\Eav\Model\EntityFactory ' )
55
73
->disableOriginalConstructor ()
56
74
->getMock ();
57
- $ resourceHelper = $ this ->getMockBuilder (\ Magento \Catalog \Model \ResourceModel \Helper::class )
75
+ $ resourceHelper = $ this ->getMockBuilder (' Magento\Catalog\Model\ResourceModel\Helper ' )
58
76
->disableOriginalConstructor ()
59
77
->getMock ();
60
- $ universalFactory = $ this ->getMockBuilder (\ Magento \Framework \Validator \UniversalFactory::class )
78
+ $ universalFactory = $ this ->getMockBuilder (' Magento\Framework\Validator\UniversalFactory ' )
61
79
->disableOriginalConstructor ()
62
80
->getMock ();
63
- $ storeManager = $ this ->getMockBuilder (\ Magento \Store \Model \StoreManagerInterface::class )
81
+ $ storeManager = $ this ->getMockBuilder (' Magento\Store\Model\StoreManagerInterface ' )
64
82
->disableOriginalConstructor ()
65
83
->setMethods (['getStore ' , 'getId ' ])
66
84
->getMockForAbstractClass ();
67
- $ moduleManager = $ this ->getMockBuilder (\ Magento \Framework \Module \Manager::class )
85
+ $ moduleManager = $ this ->getMockBuilder (' Magento\Framework\Module\Manager ' )
68
86
->disableOriginalConstructor ()
69
87
->getMock ();
70
- $ catalogProductFlatState = $ this ->getMockBuilder (\ Magento \Catalog \Model \Indexer \Product \Flat \State::class )
88
+ $ catalogProductFlatState = $ this ->getMockBuilder (' Magento\Catalog\Model\Indexer\Product\Flat\State ' )
71
89
->disableOriginalConstructor ()
72
90
->getMock ();
73
- $ scopeConfig = $ this ->getMockBuilder (\ Magento \Framework \App \Config \ScopeConfigInterface::class )
91
+ $ scopeConfig = $ this ->getMockBuilder (' Magento\Framework\App\Config\ScopeConfigInterface ' )
74
92
->disableOriginalConstructor ()
75
93
->getMockForAbstractClass ();
76
- $ productOptionFactory = $ this ->getMockBuilder (\ Magento \Catalog \Model \Product \OptionFactory::class )
94
+ $ productOptionFactory = $ this ->getMockBuilder (' Magento\Catalog\Model\Product\OptionFactory ' )
77
95
->disableOriginalConstructor ()
78
96
->getMock ();
79
- $ catalogUrl = $ this ->getMockBuilder (\ Magento \Catalog \Model \ResourceModel \Url::class )
97
+ $ catalogUrl = $ this ->getMockBuilder (' Magento\Catalog\Model\ResourceModel\Url ' )
80
98
->disableOriginalConstructor ()
81
99
->getMock ();
82
- $ localeDate = $ this ->getMockBuilder (\ Magento \Framework \Stdlib \DateTime \TimezoneInterface::class )
100
+ $ localeDate = $ this ->getMockBuilder (' Magento\Framework\Stdlib\DateTime\TimezoneInterface ' )
83
101
->disableOriginalConstructor ()
84
102
->getMockForAbstractClass ();
85
- $ customerSession = $ this ->getMockBuilder (\ Magento \Customer \Model \Session::class )
103
+ $ customerSession = $ this ->getMockBuilder (' Magento\Customer\Model\Session ' )
86
104
->disableOriginalConstructor ()
87
105
->getMock ();
88
- $ dateTime = $ this ->getMockBuilder (\ Magento \Framework \Stdlib \DateTime::class )
106
+ $ dateTime = $ this ->getMockBuilder (' Magento\Framework\Stdlib\DateTime ' )
89
107
->disableOriginalConstructor ()
90
108
->getMock ();
91
- $ groupManagement = $ this ->getMockBuilder (\ Magento \Customer \Api \GroupManagementInterface::class )
109
+ $ groupManagement = $ this ->getMockBuilder (' Magento\Customer\Api\GroupManagementInterface ' )
92
110
->disableOriginalConstructor ()
93
111
->getMockForAbstractClass ();
94
112
95
- $ this ->connectionMock = $ this ->getMockBuilder (\ Magento \Framework \DB \Adapter \AdapterInterface::class )
113
+ $ this ->connectionMock = $ this ->getMockBuilder (' Magento\Framework\DB\Adapter\AdapterInterface ' )
96
114
->disableOriginalConstructor ()
97
115
->getMockForAbstractClass ();
98
116
99
- $ this ->selectMock = $ this ->getMockBuilder (\ Magento \Framework \DB \Select::class )
117
+ $ this ->selectMock = $ this ->getMockBuilder (' Magento\Framework\DB\Select ' )
100
118
->disableOriginalConstructor ()
101
119
->getMock ();
102
120
103
- $ entityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \AbstractEntity::class)
121
+ $ this -> entityMock = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \AbstractEntity::class)
104
122
->disableOriginalConstructor ()
105
123
->getMock ();
106
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
+
107
137
$ storeManager ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
108
138
$ storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturnSelf ();
109
139
$ universalFactory ->expects ($ this ->exactly (1 ))->method ('create ' )->willReturnOnConsecutiveCalls (
110
- $ entityMock
140
+ $ this -> entityMock
111
141
);
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 );
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 );
115
145
$ this ->connectionMock ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ this ->selectMock );
116
146
$ helper = new ObjectManager ($ this );
117
147
118
148
$ this ->prepareObjectManager ([
119
- [\Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation::class,
120
- $ this ->getMock (\Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation::class)
149
+ [
150
+ 'Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation ' ,
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
121
164
]
122
165
]);
123
166
$ this ->collection = $ helper ->getObject (
124
- \ Magento \Catalog \Model \ResourceModel \Product \Collection::class ,
167
+ ' Magento\Catalog\Model\ResourceModel\Product\Collection ' ,
125
168
[
126
169
'entityFactory ' => $ entityFactory ,
127
170
'logger ' => $ logger ,
@@ -150,8 +193,8 @@ protected function setUp()
150
193
151
194
public function testAddProductCategoriesFilter ()
152
195
{
153
- $ condition = ['in ' => [1 ,2 ]];
154
- $ values = [1 ,2 ];
196
+ $ condition = ['in ' => [1 , 2 ]];
197
+ $ values = [1 , 2 ];
155
198
$ conditionType = 'nin ' ;
156
199
$ preparedSql = "category_id IN(1,2) " ;
157
200
$ tableName = "catalog_category_product " ;
@@ -174,17 +217,58 @@ public function testAddProductCategoriesFilter()
174
217
$ this ->collection ->addCategoriesFilter ([$ conditionType => $ values ]);
175
218
}
176
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
+
177
261
/**
178
262
* @param $map
179
263
*/
180
264
private function prepareObjectManager ($ map )
181
265
{
182
- $ objectManagerMock = $ this ->getMock (\ Magento \Framework \ObjectManagerInterface::class );
266
+ $ objectManagerMock = $ this ->getMock (' Magento\Framework\ObjectManagerInterface ' );
183
267
$ objectManagerMock ->expects ($ this ->any ())->method ('getInstance ' )->willReturnSelf ();
184
268
$ objectManagerMock ->expects ($ this ->any ())
185
269
->method ('get ' )
186
270
->will ($ this ->returnValueMap ($ map ));
187
- $ reflectionClass = new \ReflectionClass (\ Magento \Framework \App \ObjectManager::class );
271
+ $ reflectionClass = new \ReflectionClass (' Magento\Framework\App\ObjectManager ' );
188
272
$ reflectionProperty = $ reflectionClass ->getProperty ('_instance ' );
189
273
$ reflectionProperty ->setAccessible (true );
190
274
$ reflectionProperty ->setValue ($ objectManagerMock );
0 commit comments