| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Commerce\Payments\PaymentGateway;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\ProductVariant;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Mail;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\FakeGateway;
- use Tests\TestCase;
-
- class BundleTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- private Product $sambal;
-
- private ProductVariant $small;
-
- private ProductVariant $big;
-
- private Product $kerepek;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- Mail::fake();
-
- $this->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<string, mixed>
- */
- 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());
- }
- }
|