1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2016 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
8
8
namespace Magento \Catalog \Test \Unit \Model \Product \Price ;
9
9
10
10
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
11
12
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
12
13
use Magento \Catalog \Model \Product \Price \PricePersistence ;
13
14
use Magento \Catalog \Model \Product \Type ;
14
15
use Magento \Catalog \Model \ProductIdLocatorInterface ;
15
16
use Magento \Catalog \Model \ResourceModel \Attribute ;
16
17
use Magento \Framework \DB \Adapter \AdapterInterface ;
17
18
use Magento \Framework \DB \Select ;
19
+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
18
20
use Magento \Framework \EntityManager \MetadataPool ;
19
21
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
22
use PHPUnit \Framework \MockObject \MockObject ;
21
23
use PHPUnit \Framework \TestCase ;
22
24
25
+ /**
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
+ */
23
28
class PricePersistenceTest extends TestCase
24
29
{
25
30
/**
@@ -155,39 +160,50 @@ public function testGet()
155
160
*/
156
161
public function testUpdate ()
157
162
{
158
- $ attributeId = 5 ;
163
+ $ attributeId = 77 ;
159
164
$ prices = [
160
165
[
161
166
'store_id ' => 1 ,
162
167
'row_id ' => 1 ,
163
168
'value ' => 15
169
+ ],
170
+ [
171
+ 'store_id ' => 0 ,
172
+ 'row_id ' => 2 ,
173
+ 'value ' => 20
164
174
]
165
175
];
176
+
177
+ $ metadataEntity = $ this ->createMock (EntityMetadataInterface::class);
178
+ $ select = $ this ->createMock (Select::class);
179
+
180
+ $ metadataEntity ->expects ($ this ->atLeastOnce ())->method ('getLinkField ' )->willReturn ('row_id ' );
181
+ $ select ->expects ($ this ->once ())->method ('from ' )->with ('catalog_product_entity_decimal ' )->willReturnSelf ();
182
+ $ select ->expects ($ this ->atLeastOnce ())->method ('where ' )->willReturnSelf ();
183
+
184
+ $ this ->metadataPool ->expects ($ this ->atLeastOnce ())
185
+ ->method ('getMetadata ' )
186
+ ->with (ProductInterface::class)
187
+ ->willReturn ($ metadataEntity );
166
188
$ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
167
189
$ this ->productAttribute ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
168
190
$ this ->attributeResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ this ->connection );
169
191
$ this ->connection ->expects ($ this ->once ())->method ('beginTransaction ' )->willReturnSelf ();
192
+ $ this ->connection ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ select );
193
+ $ this ->connection ->expects ($ this ->atLeastOnce ())
194
+ ->method ('fetchAll ' )
195
+ ->willReturn (
196
+ [
197
+ ['value_id ' => 1 , 'row_id ' => 1 , 'attribute_id ' => 77 , 'store_id ' => 1 , 'value ' => 10 ]
198
+ ]
199
+ );
200
+ $ this ->connection ->expects ($ this ->once ())->method ('update ' )->willReturn (1 );
201
+ $ this ->connection ->expects ($ this ->once ())->method ('insertMultiple ' )->willReturn (1 );
170
202
$ this ->attributeResource
171
- ->expects ($ this ->once ())
203
+ ->expects ($ this ->atLeastOnce ())
172
204
->method ('getTable ' )
173
205
->with ('catalog_product_entity_decimal ' )
174
206
->willReturn ('catalog_product_entity_decimal ' );
175
- $ this ->connection
176
- ->expects ($ this ->once ())
177
- ->method ('insertOnDuplicate ' )
178
- ->with (
179
- 'catalog_product_entity_decimal ' ,
180
- [
181
- [
182
- 'store_id ' => 1 ,
183
- 'row_id ' => 1 ,
184
- 'value ' => 15 ,
185
- 'attribute_id ' => 5 ,
186
- ]
187
- ],
188
- ['value ' ]
189
- )
190
- ->willReturnSelf ();
191
207
$ this ->connection ->expects ($ this ->once ())->method ('commit ' )->willReturnSelf ();
192
208
$ this ->model ->update ($ prices );
193
209
}
@@ -207,31 +223,34 @@ public function testUpdateWithException()
207
223
'value ' => 15
208
224
]
209
225
];
226
+ $ metadataEntity = $ this ->createMock (EntityMetadataInterface::class);
227
+ $ select = $ this ->createMock (Select::class);
228
+
229
+ $ metadataEntity ->expects ($ this ->atLeastOnce ())->method ('getLinkField ' )->willReturn ('row_id ' );
230
+ $ select ->expects ($ this ->once ())->method ('from ' )->with ('catalog_product_entity_decimal ' )->willReturnSelf ();
231
+ $ select ->expects ($ this ->atLeastOnce ())->method ('where ' )->willReturnSelf ();
232
+
233
+ $ this ->metadataPool ->expects ($ this ->atLeastOnce ())
234
+ ->method ('getMetadata ' )
235
+ ->with (ProductInterface::class)
236
+ ->willReturn ($ metadataEntity );
210
237
$ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
211
238
$ this ->productAttribute ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
212
239
$ this ->attributeResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ this ->connection );
213
240
$ this ->connection ->expects ($ this ->once ())->method ('beginTransaction ' )->willReturnSelf ();
241
+ $ this ->connection ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ select );
242
+ $ this ->connection ->expects ($ this ->atLeastOnce ())
243
+ ->method ('fetchAll ' )
244
+ ->willReturn (
245
+ [
246
+ ['value_id ' => 1 , 'row_id ' => 1 , 'attribute_id ' => 77 , 'store_id ' => 1 , 'value ' => 10 ]
247
+ ]
248
+ );
214
249
$ this ->attributeResource
215
- ->expects ($ this ->once ( ))
250
+ ->expects ($ this ->exactly ( 2 ))
216
251
->method ('getTable ' )
217
252
->with ('catalog_product_entity_decimal ' )
218
253
->willReturn ('catalog_product_entity_decimal ' );
219
- $ this ->connection
220
- ->expects ($ this ->once ())
221
- ->method ('insertOnDuplicate ' )
222
- ->with (
223
- 'catalog_product_entity_decimal ' ,
224
- [
225
- [
226
- 'store_id ' => 1 ,
227
- 'row_id ' => 1 ,
228
- 'value ' => 15 ,
229
- 'attribute_id ' => 5 ,
230
- ]
231
- ],
232
- ['value ' ]
233
- )
234
- ->willReturnSelf ();
235
254
$ this ->connection ->expects ($ this ->once ())->method ('commit ' )->willThrowException (new \Exception ());
236
255
$ this ->connection ->expects ($ this ->once ())->method ('rollback ' )->willReturnSelf ();
237
256
$ this ->model ->update ($ prices );
0 commit comments