8
8
namespace Magento \Wishlist \Test \Unit \Pricing \ConfiguredPrice ;
9
9
10
10
use 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 ;
11
15
use Magento \Framework \Pricing \Adjustment \CalculatorInterface ;
12
16
use Magento \Framework \Pricing \Price \PriceInterface ;
13
17
use Magento \Framework \Pricing \PriceCurrencyInterface ;
19
23
use PHPUnit \Framework \MockObject \MockObject ;
20
24
use PHPUnit \Framework \TestCase ;
21
25
26
+ /**
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
+ */
22
29
class ConfigurableProductTest extends TestCase
23
30
{
24
31
/**
@@ -72,9 +79,16 @@ protected function setUp(): void
72
79
);
73
80
}
74
81
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 )
76
89
{
77
90
$ priceValue = 10 ;
91
+ $ customPrice = 100 ;
78
92
79
93
$ priceMock = $ this ->getMockBuilder (PriceInterface::class)
80
94
->getMockForAbstractClass ();
@@ -100,14 +114,66 @@ public function testGetValue()
100
114
$ wishlistItemOptionMock = $ this ->getMockBuilder (Option::class)
101
115
->disableOriginalConstructor ()
102
116
->getMock ();
103
- $ wishlistItemOptionMock ->expects ($ this ->once ())
104
- ->method ('getProduct ' )
105
- ->willReturn ($ productMock );
117
+ $ wishlistItemOptionMock ->expects ($ this ->exactly (2 ))
118
+ ->method ('getProduct ' )->willReturn ($ productMock );
106
119
107
- $ this ->saleableItem ->expects ($ this ->once ())
120
+ $ this ->saleableItem ->expects ($ this ->any ())
108
121
->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 ;
111
177
112
178
$ this ->assertEquals ($ priceValue , $ this ->model ->getValue ());
113
179
}
@@ -122,10 +188,10 @@ public function testGetValueWithNoCustomOption()
122
188
->method ('getValue ' )
123
189
->willReturn ($ priceValue );
124
190
125
- $ this ->saleableItem ->expects ($ this ->once ())
191
+ $ this ->saleableItem ->expects ($ this ->any ())
126
192
->method ('getCustomOption ' )
127
- ->with ( 'simple_product ' )
128
- ->willReturn ( null );
193
+ ->withConsecutive ([ 'simple_product ' ], [ ' option_ids ' ] )
194
+ ->willReturnOnConsecutiveCalls ( null , null );
129
195
130
196
$ this ->saleableItem ->expects ($ this ->once ())
131
197
->method ('getPriceInfo ' )
@@ -138,4 +204,36 @@ public function testGetValueWithNoCustomOption()
138
204
139
205
$ this ->assertEquals (100 , $ this ->model ->getValue ());
140
206
}
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
+ }
141
239
}
0 commit comments