Skip to content

Commit e944f99

Browse files
committed
Fix skipped cart item destroy test and add regression tests
- Fix the "can destroy item" test by calling $cart->fresh() to reload the cart from the database before asserting empty line items - Add test for add→remove→add scenario ensuring quantity is correct - Add test for variant product add→remove→add scenario These tests verify that when a product is added to cart, then removed, then added again, the quantity correctly shows 1 (not 2). This addresses the issue reported in discussion #1262. https://claude.ai/code/session_01MS6QXNFBhaFMcz6TdZsi4r
1 parent 1a8760e commit e944f99

File tree

1 file changed

+154
-1
lines changed

1 file changed

+154
-1
lines changed

tests/Http/Controllers/CartItemControllerTest.php

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,5 +2146,158 @@
21462146
'cart',
21472147
]);
21482148

2149+
$cart = $cart->fresh();
2150+
21492151
expect($cart->lineItems()->toArray())->toBeEmpty();
2150-
})->skip();
2152+
});
2153+
2154+
test('can add item after destroying it and quantity should be correct', function () {
2155+
$product = Product::make()
2156+
->price(1000)
2157+
->data([
2158+
'title' => 'Food',
2159+
]);
2160+
2161+
$product->save();
2162+
2163+
// Step 1: Add product with quantity 1
2164+
$response = $this
2165+
->from('/products/food')
2166+
->post(route('statamic.simple-commerce.cart-items.store'), [
2167+
'product' => $product->id,
2168+
'quantity' => 1,
2169+
]);
2170+
2171+
$response->assertRedirect('/products/food');
2172+
$response->assertSessionHas('simple-commerce-cart');
2173+
2174+
$cartId = session()->get('simple-commerce-cart');
2175+
$cart = Order::find($cartId);
2176+
2177+
expect($cart->lineItems()->count())->toBe(1);
2178+
expect($cart->lineItems()->first()->quantity())->toBe(1);
2179+
2180+
$lineItemId = $cart->lineItems()->first()->id();
2181+
2182+
// Step 2: Remove the product
2183+
$response = $this
2184+
->from('/cart')
2185+
->withSession(['simple-commerce-cart' => $cartId])
2186+
->deleteJson(route('statamic.simple-commerce.cart-items.destroy', [
2187+
'item' => $lineItemId,
2188+
]));
2189+
2190+
$response->assertJsonStructure([
2191+
'status',
2192+
'message',
2193+
'cart',
2194+
]);
2195+
2196+
$cart = Order::find($cartId);
2197+
expect($cart->lineItems()->count())->toBe(0);
2198+
2199+
// Step 3: Add the same product again with quantity 1
2200+
$response = $this
2201+
->from('/products/food')
2202+
->withSession(['simple-commerce-cart' => $cartId])
2203+
->post(route('statamic.simple-commerce.cart-items.store'), [
2204+
'product' => $product->id,
2205+
'quantity' => 1,
2206+
]);
2207+
2208+
$response->assertRedirect('/products/food');
2209+
2210+
$cart = Order::find($cartId);
2211+
2212+
// The quantity should be 1, not 2
2213+
expect($cart->lineItems()->count())->toBe(1);
2214+
expect($cart->lineItems()->first()->quantity())->toBe(1);
2215+
});
2216+
2217+
test('can add variant item after destroying it and quantity should be correct', function () {
2218+
$product = Product::make()
2219+
->data([
2220+
'title' => 'Dog Food',
2221+
'slug' => 'dog-food',
2222+
])
2223+
->productVariants([
2224+
'variants' => [
2225+
[
2226+
'name' => 'Sizes',
2227+
'values' => [
2228+
'Small',
2229+
'Large',
2230+
],
2231+
],
2232+
],
2233+
'options' => [
2234+
[
2235+
'key' => 'Small',
2236+
'variant' => 'Small',
2237+
'price' => 1000,
2238+
],
2239+
[
2240+
'key' => 'Large',
2241+
'variant' => 'Large',
2242+
'price' => 2000,
2243+
],
2244+
],
2245+
]);
2246+
2247+
$product->save();
2248+
2249+
// Step 1: Add product with variant
2250+
$response = $this
2251+
->from('/products/dog-food')
2252+
->post(route('statamic.simple-commerce.cart-items.store'), [
2253+
'product' => $product->id,
2254+
'variant' => 'Small',
2255+
'quantity' => 1,
2256+
]);
2257+
2258+
$response->assertRedirect('/products/dog-food');
2259+
$response->assertSessionHas('simple-commerce-cart');
2260+
2261+
$cartId = session()->get('simple-commerce-cart');
2262+
$cart = Order::find($cartId);
2263+
2264+
expect($cart->lineItems()->count())->toBe(1);
2265+
expect($cart->lineItems()->first()->quantity())->toBe(1);
2266+
2267+
$lineItemId = $cart->lineItems()->first()->id();
2268+
2269+
// Step 2: Remove the product
2270+
$response = $this
2271+
->from('/cart')
2272+
->withSession(['simple-commerce-cart' => $cartId])
2273+
->deleteJson(route('statamic.simple-commerce.cart-items.destroy', [
2274+
'item' => $lineItemId,
2275+
]));
2276+
2277+
$response->assertJsonStructure([
2278+
'status',
2279+
'message',
2280+
'cart',
2281+
]);
2282+
2283+
$cart = Order::find($cartId);
2284+
expect($cart->lineItems()->count())->toBe(0);
2285+
2286+
// Step 3: Add the same product/variant again with quantity 1
2287+
$response = $this
2288+
->from('/products/dog-food')
2289+
->withSession(['simple-commerce-cart' => $cartId])
2290+
->post(route('statamic.simple-commerce.cart-items.store'), [
2291+
'product' => $product->id,
2292+
'variant' => 'Small',
2293+
'quantity' => 1,
2294+
]);
2295+
2296+
$response->assertRedirect('/products/dog-food');
2297+
2298+
$cart = Order::find($cartId);
2299+
2300+
// The quantity should be 1, not 2
2301+
expect($cart->lineItems()->count())->toBe(1);
2302+
expect($cart->lineItems()->first()->quantity())->toBe(1);
2303+
});

0 commit comments

Comments
 (0)