user = User::factory()->create(); $this->store = Store::factory()->for($this->user)->create(); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); // A sambal in two sizes, and a simple kerepek. $this->post(route('stores.products.store', $this->store->slug), [ 'name' => 'Sambal', 'status' => 'active', 'options' => [['name' => 'Size', 'values' => '250g, 500g']], 'variants' => [ ['option1' => '250g', 'price' => '12.90', 'stock' => 10], ['option1' => '500g', 'price' => '22.90', 'stock' => 4], ], ])->assertSessionHasNoErrors(); $this->sambal = $this->store->products()->where('name', 'Sambal')->firstOrFail(); $this->small = $this->sambal->variants()->where('option1', '250g')->firstOrFail(); $this->big = $this->sambal->variants()->where('option1', '500g')->firstOrFail(); $this->kerepek = $this->store->products()->create(['name' => 'Kerepek', 'price' => 800, 'stock' => 3, 'status' => 'active']); } /** * Two small sambal and one kerepek for RM 30. * * @return array */ private function form(array $overrides = []): array { return [ 'name' => 'Combo Pedas', 'status' => 'active', 'price' => '30.00', 'is_bundle' => '1', 'bundle_items' => [ ['item' => $this->sambal->id.'-'.$this->small->id, 'quantity' => 2], ['item' => (string) $this->kerepek->id, 'quantity' => 1], ], ...$overrides, ]; } private function createBundle(array $overrides = []): Product { $this->post(route('stores.products.store', $this->store->slug), $this->form($overrides)) ->assertSessionHasNoErrors(); return $this->store->products()->where('is_bundle', true)->latest('id')->firstOrFail(); } private function addToCart(Product $product, ?int $variantId, int $quantity = 1) { return $this->post(route('stores.preview.cart.add', $this->store->slug), [ 'product_id' => $product->id, 'variant_id' => $variantId, 'quantity' => $quantity, ]); } private function checkout() { return $this->post(route('stores.preview.checkout.store', $this->store->slug), [ 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789', 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor', ]); } public function test_a_bundle_gets_its_stock_from_what_is_inside() { $bundle = $this->createBundle(); $this->assertSame(3000, $bundle->price); $this->assertCount(2, $bundle->bundleItems); // 10 small sambal ÷ 2 = 5, 3 kerepek ÷ 1 = 3: three bundles can be sold. $this->assertSame(3, $bundle->stock); // Changing an item's stock changes the bundle's. $this->post(route('stores.products.update', [$this->store->slug, $this->kerepek->id]), [ 'name' => 'Kerepek', 'status' => 'active', 'price' => '8.00', 'stock' => 20, ])->assertSessionHasNoErrors(); $this->assertSame(5, $bundle->fresh()->stock); $this->get(route('stores.products.edit', [$this->store->slug, $bundle->id])) ->assertInertia(fn (Assert $page) => $page ->where('product.is_bundle', true) ->where('product.bundle_items.0.item', $this->sambal->id.'-'.$this->small->id) ->where('product.bundle_items.0.label', '2 × Sambal (250g)') // The bundle cannot hold itself or another bundle: only the 2 sizes and the kerepek. ->has('bundleChoices', 3)); } public function test_bundle_items_must_be_other_products_of_the_store() { $other = Product::factory()->create(['name' => 'Elsewhere']); $bundle = $this->createBundle(); $url = route('stores.products.store', $this->store->slug); $this->post($url, $this->form(['bundle_items' => []]))->assertSessionHasErrors('bundle_items'); $this->post($url, $this->form(['bundle_items' => [['item' => (string) $other->id, 'quantity' => 1]]])) ->assertSessionHasErrors('bundle_items.0.item'); // The sambal has sizes, so the bundle must say which one. $this->post($url, $this->form(['bundle_items' => [['item' => (string) $this->sambal->id, 'quantity' => 1]]])) ->assertSessionHasErrors('bundle_items.0.item'); $this->post($url, $this->form(['bundle_items' => [['item' => (string) $bundle->id, 'quantity' => 1]]])) ->assertSessionHasErrors('bundle_items.0.item'); $this->post($url, $this->form(['bundle_items' => [ ['item' => (string) $this->kerepek->id, 'quantity' => 1], ['item' => (string) $this->kerepek->id, 'quantity' => 2], ]]))->assertSessionHasErrors('bundle_items.1.item'); $this->post(route('stores.products.update', [$this->store->slug, $bundle->id]), $this->form([ 'bundle_items' => [['item' => (string) $bundle->id, 'quantity' => 1]], ]))->assertSessionHasErrors('bundle_items.0.item'); $this->assertSame(1, $this->store->products()->where('is_bundle', true)->count()); } public function test_products_inside_a_bundle_are_protected() { $bundle = $this->createBundle(); // Deleting the kerepek would quietly empty part of the bundle. $this->delete(route('stores.products.destroy', [$this->store->slug, $this->kerepek->id])); $this->assertNotNull($this->kerepek->fresh()); // The 250g size is inside the bundle, so it cannot be removed. $this->post(route('stores.products.update', [$this->store->slug, $this->sambal->id]), [ 'name' => 'Sambal', 'status' => 'active', 'options' => [['name' => 'Size', 'values' => '500g']], 'variants' => [['option1' => '500g', 'price' => '22.90', 'stock' => 4]], ])->assertSessionHasErrors('options'); // Nor can the sambal lose its sizes, or become a bundle itself. $this->post(route('stores.products.update', [$this->store->slug, $this->sambal->id]), [ 'name' => 'Sambal', 'status' => 'active', 'price' => '12.90', 'stock' => 10, ])->assertSessionHasErrors('options'); $this->post(route('stores.products.update', [$this->store->slug, $this->kerepek->id]), [ 'name' => 'Kerepek', 'status' => 'active', 'price' => '8.00', 'is_bundle' => '1', 'bundle_items' => [['item' => $this->sambal->id.'-'.$this->big->id, 'quantity' => 1]], ])->assertSessionHasErrors('is_bundle'); $this->assertCount(2, $this->sambal->variants()->get()); // Once the bundle is gone, the kerepek can be deleted. $this->delete(route('stores.products.destroy', [$this->store->slug, $bundle->id])); $this->delete(route('stores.products.destroy', [$this->store->slug, $this->kerepek->id])); $this->assertNull($this->kerepek->fresh()); } public function test_the_store_shows_what_is_inside() { $bundle = $this->createBundle(); $this->get(route('stores.preview.product', [$this->store->slug, $bundle->slug])) ->assertInertia(fn (Assert $page) => $page ->where('view.product.price', 30) ->where('view.product.stock_left', 3) ->where('view.product.bundle.0.name', 'Sambal (250g)') ->where('view.product.bundle.0.quantity', 2) ->where('view.product.bundle.1.slug', $this->kerepek->slug)); } public function test_paying_for_a_bundle_lowers_the_stock_of_what_is_inside() { $gateway = new FakeGateway; $this->app->instance(PaymentGateway::class, $gateway); $bundle = $this->createBundle(); $this->addToCart($bundle, null, 2)->assertSessionHasNoErrors(); $this->checkout()->assertRedirect(); $order = Order::with('items')->firstOrFail(); $item = $order->items->first(); $this->assertSame('Combo Pedas', $item->name); $this->assertSame(6000, $order->subtotal); $this->assertSame(['2 × Sambal (250g)', '1 × Kerepek'], array_column($item->bundle_contents, 'label')); $gateway->pay($order); $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id])); // Two bundles: 4 small sambal and 2 kerepek. $this->assertSame(6, $this->small->fresh()->stock); $this->assertSame(4, $this->big->fresh()->stock); $this->assertSame(10, $this->sambal->fresh()->stock); $this->assertSame(1, $this->kerepek->fresh()->stock); $this->assertSame(1, $bundle->fresh()->stock); $this->get(route('stores.orders.show', [$this->store->slug, $order->id])) ->assertInertia(fn (Assert $page) => $page ->where('order.items.0.contents', ['2 × Sambal (250g)', '1 × Kerepek'])); } public function test_a_bundle_and_its_items_in_one_cart_share_the_stock() { $this->app->instance(PaymentGateway::class, new FakeGateway); $bundle = $this->createBundle(); // 3 kerepek in stock: 2 bundles need 2, plus 2 on their own makes 4. $this->addToCart($bundle, null, 2)->assertSessionHasNoErrors(); $this->addToCart($this->kerepek, null, 2)->assertSessionHasNoErrors(); $this->checkout()->assertSessionHasErrors('cart'); $this->assertSame(0, Order::count()); $this->patch(route('stores.preview.cart.update', [$this->store->slug, (string) $this->kerepek->id]), ['quantity' => 1]); $this->checkout()->assertSessionHasNoErrors()->assertRedirect(); $this->assertSame(1, Order::count()); } }