88namespace Magento \Wishlist \Test \Unit \Pricing \ConfiguredPrice ;
99
1010use Magento \Catalog \Model \Product ;
11+ use Magento \Catalog \Model \Product \Configuration \Item \ItemInterface ;
12+ use Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface ;
13+ use Magento \Catalog \Model \Product \Option \Type \DefaultType ;
14+ use Magento \Catalog \Model \Product \Option as ProductOption ;
1115use Magento \Framework \Pricing \Adjustment \CalculatorInterface ;
1216use Magento \Framework \Pricing \Price \PriceInterface ;
1317use Magento \Framework \Pricing \PriceCurrencyInterface ;
1923use PHPUnit \Framework \MockObject \MockObject ;
2024use PHPUnit \Framework \TestCase ;
2125
26+ /**
27+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+ */
2229class ConfigurableProductTest extends TestCase
2330{
2431 /**
@@ -72,9 +79,16 @@ protected function setUp(): void
7279 );
7380 }
7481
75- public function testGetValue ()
82+ /**
83+ * @param array $options
84+ *
85+ * @dataProvider setOptionsDataProvider
86+ * @throws \Magento\Framework\Exception\LocalizedException
87+ */
88+ public function testGetValue (array $ options , $ optionIds )
7689 {
7790 $ priceValue = 10 ;
91+ $ customPrice = 100 ;
7892
7993 $ priceMock = $ this ->getMockBuilder (PriceInterface::class)
8094 ->getMockForAbstractClass ();
@@ -100,14 +114,66 @@ public function testGetValue()
100114 $ wishlistItemOptionMock = $ this ->getMockBuilder (Option::class)
101115 ->disableOriginalConstructor ()
102116 ->getMock ();
103- $ wishlistItemOptionMock ->expects ($ this ->once ())
104- ->method ('getProduct ' )
105- ->willReturn ($ productMock );
117+ $ wishlistItemOptionMock ->expects ($ this ->exactly (2 ))
118+ ->method ('getProduct ' )->willReturn ($ productMock );
106119
107- $ this ->saleableItem ->expects ($ this ->once ())
120+ $ this ->saleableItem ->expects ($ this ->any ())
108121 ->method ('getCustomOption ' )
109- ->with ('simple_product ' )
110- ->willReturn ($ wishlistItemOptionMock );
122+ ->withConsecutive (['simple_product ' ], ['option_ids ' ])
123+ ->willReturnOnConsecutiveCalls ($ wishlistItemOptionMock , $ wishlistItemOptionMock );
124+
125+ $ wishlistItemOptionMock ->expects ($ this ->any ())
126+ ->method ('getValue ' )->willReturn ($ optionIds );
127+
128+ $ wishlistItemOptionMock ->expects ($ this ->exactly (2 ))
129+ ->method ('getProduct ' )->willReturn ($ productMock );
130+
131+ $ productOptionMock = $ this ->getMockBuilder (ProductOption::class)
132+ ->disableOriginalConstructor ()
133+ ->getMock ();
134+
135+ $ defaultTypeMock = $ this ->getMockBuilder (DefaultType::class)
136+ ->disableOriginalConstructor ()
137+ ->getMock ();
138+
139+ $ productOptionMock ->expects ($ this ->any ())
140+ ->method ('getId ' )
141+ ->willReturn ($ options ['option_id ' ]);
142+ $ productOptionMock ->expects ($ this ->any ())
143+ ->method ('getType ' )
144+ ->willReturn ($ options ['type ' ]);
145+
146+ $ productOptionMock ->expects ($ this ->any ())
147+ ->method ('groupFactory ' )
148+ ->with ($ options ['type ' ])
149+ ->willReturn ($ defaultTypeMock );
150+ $ productMock ->expects ($ this ->any ())
151+ ->method ('getOptionById ' )
152+ ->with ($ options ['option_id ' ])->willReturn ($ productOptionMock );
153+ $ defaultTypeMock ->expects ($ this ->any ())
154+ ->method ('setOption ' )
155+ ->with ($ productOptionMock )
156+ ->willReturnSelf ();
157+
158+ $ itemMock = $ this ->getMockForAbstractClass (ItemInterface::class);
159+ $ this ->model ->setItem ($ itemMock );
160+
161+ $ optionInterfaceMock = $ this ->getMockForAbstractClass (OptionInterface::class);
162+
163+ $ itemMock ->expects ($ this ->any ())
164+ ->method ('getOptionByCode ' )
165+ ->with ('option_ ' .$ options ['option_id ' ])
166+ ->willReturn ($ optionInterfaceMock );
167+
168+ $ optionInterfaceMock ->expects ($ this ->any ())
169+ ->method ('getValue ' )
170+ ->willReturn ($ productOptionMock );
171+
172+ $ defaultTypeMock ->expects ($ this ->any ())
173+ ->method ('getOptionPrice ' )
174+ ->with ($ productOptionMock , $ priceValue )
175+ ->willReturn ($ customPrice );
176+ $ priceValue += $ customPrice ;
111177
112178 $ this ->assertEquals ($ priceValue , $ this ->model ->getValue ());
113179 }
@@ -122,10 +188,10 @@ public function testGetValueWithNoCustomOption()
122188 ->method ('getValue ' )
123189 ->willReturn ($ priceValue );
124190
125- $ this ->saleableItem ->expects ($ this ->once ())
191+ $ this ->saleableItem ->expects ($ this ->any ())
126192 ->method ('getCustomOption ' )
127- ->with ( 'simple_product ' )
128- ->willReturn ( null );
193+ ->withConsecutive ([ 'simple_product ' ], [ ' option_ids ' ] )
194+ ->willReturnOnConsecutiveCalls ( null , null );
129195
130196 $ this ->saleableItem ->expects ($ this ->once ())
131197 ->method ('getPriceInfo ' )
@@ -138,4 +204,36 @@ public function testGetValueWithNoCustomOption()
138204
139205 $ this ->assertEquals (100 , $ this ->model ->getValue ());
140206 }
207+
208+ public function setOptionsDataProvider (): array
209+ {
210+ return ['options ' =>
211+ [
212+ [
213+ 'option_id ' => '1 ' ,
214+ 'product_id ' => '2091 ' ,
215+ 'type ' => 'checkbox ' ,
216+ 'is_require ' => '1 ' ,
217+ 'default_title ' => 'check ' ,
218+ 'title ' => 'check ' ,
219+ 'default_price ' => null ,
220+ 'default_price_type ' => null ,
221+ 'price ' => null ,
222+ 'price_type ' => null
223+ ], '1 ' ,
224+ [
225+ 'option_id ' => '2 ' ,
226+ 'product_id ' => '2091 ' ,
227+ 'type ' => 'field ' ,
228+ 'is_require ' => '1 ' ,
229+ 'default_title ' => 'field ' ,
230+ 'title ' => 'field ' ,
231+ 'default_price ' => '100.000000 ' ,
232+ 'default_price_type ' => 'fixed ' ,
233+ 'price ' => '100.000000 ' ,
234+ 'price_type ' => 'fixed '
235+ ], '2 '
236+ ],
237+ ];
238+ }
141239}
0 commit comments