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 ;
12
11
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
13
12
use Magento \Catalog \Model \Product \Price \PricePersistence ;
14
13
use Magento \Catalog \Model \Product \Type ;
15
14
use Magento \Catalog \Model \ProductIdLocatorInterface ;
16
15
use Magento \Catalog \Model \ResourceModel \Attribute ;
16
+ use Magento \Catalog \Model \ResourceModel \Product \Price \BasePrice ;
17
+ use Magento \Catalog \Model \ResourceModel \Product \Price \BasePriceFactory ;
17
18
use Magento \Framework \DB \Adapter \AdapterInterface ;
18
19
use Magento \Framework \DB \Select ;
19
- use Magento \Framework \EntityManager \EntityMetadataInterface ;
20
20
use Magento \Framework \EntityManager \MetadataPool ;
21
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
22
21
use PHPUnit \Framework \MockObject \MockObject ;
23
22
use PHPUnit \Framework \TestCase ;
24
23
@@ -57,6 +56,11 @@ class PricePersistenceTest extends TestCase
57
56
*/
58
57
private $ metadataPool ;
59
58
59
+ /**
60
+ * @var BasePriceFactory|MockObject
61
+ */
62
+ private $ basePriceFactory ;
63
+
60
64
/**
61
65
* @var PricePersistence
62
66
*/
@@ -91,16 +95,18 @@ protected function setUp(): void
91
95
$ this ->productAttribute = $ this ->getMockBuilder (ProductAttributeInterface::class)
92
96
->disableOriginalConstructor ()
93
97
->getMockForAbstractClass ();
98
+ $ this ->basePriceFactory = $ this ->getMockBuilder (BasePriceFactory::class)
99
+ ->disableOriginalConstructor ()
100
+ ->getMock ();
94
101
95
- $ objectManager = new ObjectManager ($ this );
96
- $ this ->model = $ objectManager ->getObject (
97
- PricePersistence::class,
98
- [
99
- 'attributeResource ' => $ this ->attributeResource ,
100
- 'attributeRepository ' => $ this ->attributeRepository ,
101
- 'productIdLocator ' => $ this ->productIdLocator ,
102
- 'metadataPool ' => $ this ->metadataPool ,
103
- ]
102
+ $ this ->model = new PricePersistence (
103
+ $ this ->attributeResource ,
104
+ $ this ->attributeRepository ,
105
+ $ this ->productIdLocator ,
106
+ $ this ->metadataPool ,
107
+ 'price ' ,
108
+ null ,
109
+ $ this ->basePriceFactory
104
110
);
105
111
}
106
112
@@ -160,7 +166,7 @@ public function testGet()
160
166
*/
161
167
public function testUpdate ()
162
168
{
163
- $ attributeId = 77 ;
169
+ $ attributeId = 5 ;
164
170
$ prices = [
165
171
[
166
172
'store_id ' => 1 ,
@@ -173,38 +179,14 @@ public function testUpdate()
173
179
'value ' => 20
174
180
]
175
181
];
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 );
182
+ $ basePrice = $ this ->createMock (BasePrice::class);
183
+ $ basePrice ->expects ($ this ->once ())
184
+ ->method ('update ' );
188
185
$ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
189
186
$ this ->productAttribute ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
190
- $ this ->attributeResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ this ->connection );
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 );
202
- $ this ->attributeResource
203
- ->expects ($ this ->atLeastOnce ())
204
- ->method ('getTable ' )
205
- ->with ('catalog_product_entity_decimal ' )
206
- ->willReturn ('catalog_product_entity_decimal ' );
207
- $ this ->connection ->expects ($ this ->once ())->method ('commit ' )->willReturnSelf ();
187
+ $ this ->basePriceFactory ->expects ($ this ->once ())
188
+ ->method ('create ' )
189
+ ->willReturn ($ basePrice );
208
190
$ this ->model ->update ($ prices );
209
191
}
210
192
@@ -223,36 +205,15 @@ public function testUpdateWithException()
223
205
'value ' => 15
224
206
]
225
207
];
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 );
208
+ $ basePrice = $ this ->createMock (BasePrice::class);
209
+ $ basePrice ->expects ($ this ->once ())
210
+ ->method ('update ' )
211
+ ->willThrowException (new \Exception ());
237
212
$ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
238
213
$ this ->productAttribute ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
239
- $ this ->attributeResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ this ->connection );
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
- );
249
- $ this ->attributeResource
250
- ->expects ($ this ->exactly (2 ))
251
- ->method ('getTable ' )
252
- ->with ('catalog_product_entity_decimal ' )
253
- ->willReturn ('catalog_product_entity_decimal ' );
254
- $ this ->connection ->expects ($ this ->once ())->method ('commit ' )->willThrowException (new \Exception ());
255
- $ this ->connection ->expects ($ this ->once ())->method ('rollback ' )->willReturnSelf ();
214
+ $ this ->basePriceFactory ->expects ($ this ->once ())
215
+ ->method ('create ' )
216
+ ->willReturn ($ basePrice );
256
217
$ this ->model ->update ($ prices );
257
218
}
258
219
0 commit comments