11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2016 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
88namespace Magento \Catalog \Test \Unit \Model \Product \Price ;
99
1010use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11+ use Magento \Catalog \Api \Data \ProductInterface ;
1112use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
1213use Magento \Catalog \Model \Product \Price \PricePersistence ;
1314use Magento \Catalog \Model \Product \Type ;
1415use Magento \Catalog \Model \ProductIdLocatorInterface ;
1516use Magento \Catalog \Model \ResourceModel \Attribute ;
1617use Magento \Framework \DB \Adapter \AdapterInterface ;
1718use Magento \Framework \DB \Select ;
19+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
1820use Magento \Framework \EntityManager \MetadataPool ;
1921use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2022use PHPUnit \Framework \MockObject \MockObject ;
2123use PHPUnit \Framework \TestCase ;
2224
25+ /**
26+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+ */
2328class PricePersistenceTest extends TestCase
2429{
2530 /**
@@ -155,39 +160,50 @@ public function testGet()
155160 */
156161 public function testUpdate ()
157162 {
158- $ attributeId = 5 ;
163+ $ attributeId = 77 ;
159164 $ prices = [
160165 [
161166 'store_id ' => 1 ,
162167 'row_id ' => 1 ,
163168 'value ' => 15
169+ ],
170+ [
171+ 'store_id ' => 0 ,
172+ 'row_id ' => 2 ,
173+ 'value ' => 20
164174 ]
165175 ];
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 );
166188 $ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
167189 $ this ->productAttribute ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
168190 $ this ->attributeResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ this ->connection );
169191 $ 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 );
170202 $ this ->attributeResource
171- ->expects ($ this ->once ())
203+ ->expects ($ this ->atLeastOnce ())
172204 ->method ('getTable ' )
173205 ->with ('catalog_product_entity_decimal ' )
174206 ->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 ();
191207 $ this ->connection ->expects ($ this ->once ())->method ('commit ' )->willReturnSelf ();
192208 $ this ->model ->update ($ prices );
193209 }
@@ -207,31 +223,34 @@ public function testUpdateWithException()
207223 'value ' => 15
208224 ]
209225 ];
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 );
210237 $ this ->attributeRepository ->expects ($ this ->once ())->method ('get ' )->willReturn ($ this ->productAttribute );
211238 $ this ->productAttribute ->expects ($ this ->once ())->method ('getAttributeId ' )->willReturn ($ attributeId );
212239 $ this ->attributeResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ this ->connection );
213240 $ 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+ );
214249 $ this ->attributeResource
215- ->expects ($ this ->once ( ))
250+ ->expects ($ this ->exactly ( 2 ))
216251 ->method ('getTable ' )
217252 ->with ('catalog_product_entity_decimal ' )
218253 ->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 ();
235254 $ this ->connection ->expects ($ this ->once ())->method ('commit ' )->willThrowException (new \Exception ());
236255 $ this ->connection ->expects ($ this ->once ())->method ('rollback ' )->willReturnSelf ();
237256 $ this ->model ->update ($ prices );
0 commit comments