Skip to content

Commit c161b1d

Browse files
committed
test(api-client): cover fractional-quantity normalization in ItemFactory
Reproduces the reported 0.3 qty / 30.00 subtotal scenario for both the cart and order code paths, confirmed to fail with quantity=0 on the pre-fix code.
1 parent 52e4398 commit c161b1d

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,66 @@ public function testFromCartDefault()
8888
$this->assertEquals(42, $item->unit_amount()->value());
8989
}
9090

91+
public function testFromCartFractionalQuantityIsNormalizedToOne()
92+
{
93+
$testee = new ItemFactory($this->currency);
94+
95+
$product = Mockery::mock(\WC_Product_Simple::class);
96+
$product
97+
->expects('get_name')
98+
->andReturn('name');
99+
$product
100+
->expects('get_description')
101+
->andReturn('description');
102+
$product
103+
->expects('get_sku')
104+
->andReturn('sku');
105+
$product
106+
->expects('is_virtual')
107+
->andReturn(false);
108+
$items = [
109+
[
110+
'data' => $product,
111+
'quantity' => 0.3,
112+
'line_subtotal' => 30.00,
113+
'line_total' => 30.00
114+
],
115+
];
116+
$cart = Mockery::mock(\WC_Cart::class);
117+
$cart
118+
->expects('get_cart_contents')
119+
->andReturn($items);
120+
121+
when('get_woocommerce_currency')->justReturn('USD');
122+
123+
expect('wp_strip_all_tags')->andReturnFirstArg();
124+
expect('strip_shortcodes')->andReturnFirstArg();
125+
126+
$woocommerce = Mockery::mock(\WooCommerce::class);
127+
$session = Mockery::mock(\WC_Session::class);
128+
when('WC')->justReturn($woocommerce);
129+
$woocommerce->session = $session;
130+
$session->shouldReceive('get')->andReturn([]);
131+
132+
when('wp_get_attachment_image_src')->justReturn('image_url');
133+
$product
134+
->expects('get_image_id')
135+
->andReturn(1);
136+
$product
137+
->expects('get_permalink')
138+
->andReturn('url');
139+
$product->shouldReceive('get_id')->andReturn(null);
140+
141+
$result = $testee->from_wc_cart($cart);
142+
143+
$item = current($result);
144+
/**
145+
* @var Item $item
146+
*/
147+
$this->assertEquals(1, $item->quantity());
148+
$this->assertEquals(30.00, $item->unit_amount()->value());
149+
}
150+
91151
public function testFromCartDigitalGood()
92152
{
93153
$testee = new ItemFactory($this->currency);
@@ -216,6 +276,71 @@ public function testFromWcOrderDefault()
216276
$this->assertEquals(1, $item->unit_amount()->value());
217277
}
218278

279+
public function testFromWcOrderFractionalQuantityIsNormalizedToOne()
280+
{
281+
$testee = new ItemFactory($this->currency);
282+
283+
$product = Mockery::mock(\WC_Product::class);
284+
$product
285+
->expects('get_description')
286+
->andReturn('description');
287+
$product
288+
->expects('get_sku')
289+
->andReturn('sku');
290+
$product
291+
->expects('is_virtual')
292+
->andReturn(false);
293+
294+
expect('wp_strip_all_tags')->andReturnFirstArg();
295+
expect('strip_shortcodes')->andReturnFirstArg();
296+
297+
when('wp_get_attachment_image_src')->justReturn('image_url');
298+
$product
299+
->expects('get_image_id')
300+
->andReturn(1);
301+
$product
302+
->expects('get_permalink')
303+
->andReturn('url');
304+
305+
$item = Mockery::mock(\WC_Order_Item_Product::class);
306+
$item
307+
->expects('get_product')
308+
->andReturn($product);
309+
$item
310+
->expects('get_name')
311+
->andReturn('name');
312+
$item
313+
->expects('get_quantity')
314+
->andReturn(0.3);
315+
316+
$order = Mockery::mock(\WC_Order::class);
317+
$order
318+
->shouldReceive('get_currency')
319+
->once()
320+
->andReturn($this->currency->get());
321+
$order
322+
->expects('get_items')
323+
->andReturn([$item]);
324+
$order
325+
->expects('get_fees')
326+
->andReturn([]);
327+
$order
328+
->shouldNotReceive('get_item_subtotal');
329+
330+
$product->shouldReceive('get_id')->andReturn(123);
331+
$item->shouldReceive('get_subtotal')->andReturn(30.00);
332+
$item->shouldReceive('get_total')->andReturn(30.00);
333+
$item->shouldReceive('get_total_tax')->andReturn(0.00);
334+
335+
$result = $testee->from_wc_order($order);
336+
$item = current($result);
337+
/**
338+
* @var Item $item
339+
*/
340+
$this->assertEquals(1, $item->quantity());
341+
$this->assertEquals(30.00, $item->unit_amount()->value());
342+
}
343+
219344
public function testFromWcOrderDigitalGood()
220345
{
221346
$testee = new ItemFactory($this->currency);

0 commit comments

Comments
 (0)