88namespace Magento \Catalog \Test \Unit \Model \Product \Price ;
99
1010use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11- use Magento \Catalog \Api \Data \ProductInterface ;
1211use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
1312use Magento \Catalog \Model \Product \Price \PricePersistence ;
1413use Magento \Catalog \Model \Product \Type ;
1514use Magento \Catalog \Model \ProductIdLocatorInterface ;
1615use Magento \Catalog \Model \ResourceModel \Attribute ;
16+ use Magento \Catalog \Model \ResourceModel \Product \Price \BasePrice ;
17+ use Magento \Catalog \Model \ResourceModel \Product \Price \BasePriceFactory ;
1718use Magento \Framework \DB \Adapter \AdapterInterface ;
1819use Magento \Framework \DB \Select ;
19- use Magento \Framework \EntityManager \EntityMetadataInterface ;
2020use Magento \Framework \EntityManager \MetadataPool ;
21- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2221use PHPUnit \Framework \MockObject \MockObject ;
2322use PHPUnit \Framework \TestCase ;
2423
@@ -57,6 +56,11 @@ class PricePersistenceTest extends TestCase
5756 */
5857 private $ metadataPool ;
5958
59+ /**
60+ * @var BasePriceFactory|MockObject
61+ */
62+ private $ basePriceFactory ;
63+
6064 /**
6165 * @var PricePersistence
6266 */
@@ -91,16 +95,18 @@ protected function setUp(): void
9195 $ this ->productAttribute = $ this ->getMockBuilder (ProductAttributeInterface::class)
9296 ->disableOriginalConstructor ()
9397 ->getMockForAbstractClass ();
98+ $ this ->basePriceFactory = $ this ->getMockBuilder (BasePriceFactory::class)
99+ ->disableOriginalConstructor ()
100+ ->getMock ();
94101
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
104110 );
105111 }
106112
@@ -160,7 +166,7 @@ public function testGet()
160166 */
161167 public function testUpdate ()
162168 {
163- $ attributeId = 77 ;
169+ $ attributeId = 5 ;
164170 $ prices = [
165171 [
166172 'store_id ' => 1 ,
@@ -173,38 +179,14 @@ public function testUpdate()
173179 'value ' => 20
174180 ]
175181 ];
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 ' );
188185 $ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
189186 $ 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 );
208190 $ this ->model ->update ($ prices );
209191 }
210192
@@ -223,36 +205,15 @@ public function testUpdateWithException()
223205 'value ' => 15
224206 ]
225207 ];
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 ());
237212 $ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
238213 $ 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 );
256217 $ this ->model ->update ($ prices );
257218 }
258219
0 commit comments