| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Commerce\Payments\PaymentGateway;
- use App\Models\Order;
- use App\Models\Product;
- 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 ProductVariantTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- 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 two flavours: four combinations.
- *
- * @return array<string, mixed>
- */
- private function form(array $overrides = []): array
- {
- return [
- 'name' => 'Sambal',
- 'description' => '',
- 'status' => 'active',
- 'options' => [
- ['name' => 'Size', 'values' => '250g, 500g'],
- ['name' => 'Flavour', 'values' => 'Original, Pedas'],
- ],
- 'variants' => [
- ['option1' => '250g', 'option2' => 'Original', 'price' => '12.90', 'stock' => 10],
- ['option1' => '250g', 'option2' => 'Pedas', 'price' => '12.90', 'stock' => 0],
- ['option1' => '500g', 'option2' => 'Original', 'price' => '22.90', 'compare_at_price' => '25.00', 'stock' => 5],
- ['option1' => '500g', 'option2' => 'Pedas', 'price' => '22.90', 'stock' => 3],
- ],
- ...$overrides,
- ];
- }
-
- private function createProduct(array $overrides = []): Product
- {
- $this->post(route('stores.products.store', $this->store->slug), $this->form($overrides))
- ->assertSessionHasNoErrors();
-
- return $this->store->products()->latest('id')->firstOrFail();
- }
-
- private function variant(Product $product, string $first, ?string $second = null)
- {
- return $product->variants()->where('option1', $first)->where('option2', $second)->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,
- ]);
- }
-
- public function test_a_product_can_have_options_with_a_price_and_stock_for_each_combination()
- {
- $product = $this->createProduct();
-
- $this->assertSame([
- ['name' => 'Size', 'values' => ['250g', '500g']],
- ['name' => 'Flavour', 'values' => ['Original', 'Pedas']],
- ], $product->options);
- $this->assertCount(4, $product->variants);
- $this->assertSame(['250g / Original', '250g / Pedas', '500g / Original', '500g / Pedas'], $product->variants->map->name()->all());
- $this->assertSame(2500, $this->variant($product, '500g', 'Original')->compare_at_price);
-
- // The product itself shows the lowest price and all the stock together.
- $this->assertSame(1290, $product->price);
- $this->assertSame(18, $product->stock);
-
- $this->get(route('stores.products.edit', [$this->store->slug, $product->id]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('product.options.0.values', '250g, 500g')
- ->has('product.variants', 4)
- ->where('product.variants.2.price', '22.90'));
- }
-
- public function test_a_single_option_works_too()
- {
- $product = $this->createProduct([
- 'options' => [['name' => 'Size', 'values' => 'S, M, L']],
- 'variants' => [
- ['option1' => 'S', 'price' => '30', 'stock' => 1],
- ['option1' => 'M', 'price' => '30', 'stock' => 2],
- ['option1' => 'L', 'price' => '32', 'stock' => 3],
- ],
- ]);
-
- $this->assertSame(['S', 'M', 'L'], $product->variants->map->name()->all());
- $this->assertNull($this->variant($product, 'L')->option2);
- }
-
- public function test_every_combination_needs_a_price_and_stock()
- {
- $form = $this->form();
- array_pop($form['variants']);
-
- $this->post(route('stores.products.store', $this->store->slug), $form)->assertSessionHasErrors('variants');
-
- $this->post(route('stores.products.store', $this->store->slug), $this->form([
- 'options' => [['name' => 'Size', 'values' => ' , ']],
- ]))->assertSessionHasErrors('options.0.values');
-
- $this->post(route('stores.products.store', $this->store->slug), $this->form([
- 'options' => [['name' => 'Size', 'values' => 'S'], ['name' => 'size', 'values' => 'M']],
- ]))->assertSessionHasErrors('options');
-
- $this->assertSame(0, $this->store->products()->count());
- }
-
- public function test_editing_keeps_variants_whose_choices_did_not_change()
- {
- $product = $this->createProduct();
- $kept = $this->variant($product, '500g', 'Pedas');
-
- $form = $this->form([
- 'options' => [
- ['name' => 'Size', 'values' => '500g, 1kg'],
- ['name' => 'Flavour', 'values' => 'Original, Pedas'],
- ],
- 'variants' => [
- ['option1' => '500g', 'option2' => 'Original', 'price' => '22.90', 'stock' => 5],
- ['option1' => '500g', 'option2' => 'Pedas', 'price' => '21.00', 'stock' => 9],
- ['option1' => '1kg', 'option2' => 'Original', 'price' => '40.00', 'stock' => 2],
- ['option1' => '1kg', 'option2' => 'Pedas', 'price' => '40.00', 'stock' => 2],
- ],
- ]);
-
- $this->post(route('stores.products.update', [$this->store->slug, $product->id]), $form)->assertSessionHasNoErrors();
-
- $product->refresh();
- $this->assertCount(4, $product->variants);
- $this->assertSame($kept->id, $this->variant($product, '500g', 'Pedas')->id);
- $this->assertSame(2100, $kept->fresh()->price);
- $this->assertSame(0, $product->variants()->where('option1', '250g')->count());
- $this->assertSame(2100, $product->price);
- $this->assertSame(18, $product->stock);
- }
-
- public function test_turning_options_off_makes_it_a_simple_product_again()
- {
- $product = $this->createProduct();
-
- $this->post(route('stores.products.update', [$this->store->slug, $product->id]), [
- 'name' => 'Sambal', 'status' => 'active', 'price' => '15.00', 'stock' => 7,
- ])->assertSessionHasNoErrors();
-
- $product->refresh();
- $this->assertNull($product->options);
- $this->assertCount(0, $product->variants);
- $this->assertSame(1500, $product->price);
- $this->assertSame(7, $product->stock);
- }
-
- public function test_the_store_gets_the_options_and_a_from_price()
- {
- $product = $this->createProduct();
-
- $this->get(route('stores.preview.product', [$this->store->slug, $product->slug]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.product.price_varies', true)
- ->where('view.product.options.0.name', 'Size')
- ->has('view.product.variants', 4)
- ->where('view.product.variants.1.name', '250g / Pedas')
- ->where('view.product.variants.1.in_stock', false)
- ->where('view.product.variants.2.price', 22.9)
- ->where('view.product.variants.2.compare_at_price', 25));
- }
-
- public function test_shoppers_must_choose_a_variant_and_each_one_has_its_own_cart_line()
- {
- $product = $this->createProduct();
- $small = $this->variant($product, '250g', 'Original');
- $big = $this->variant($product, '500g', 'Pedas');
- $soldOut = $this->variant($product, '250g', 'Pedas');
- $elsewhere = $this->store->products()->create(['name' => 'Other', 'price' => 100, 'stock' => 1, 'status' => 'active']);
-
- $this->addToCart($product, null)->assertSessionHasErrors('variant_id');
- $this->addToCart($product, $soldOut->id)->assertSessionHasErrors('quantity');
- $this->addToCart($elsewhere, $small->id)->assertNotFound();
-
- $this->addToCart($product, $small->id, 2)->assertSessionHasNoErrors();
- $this->addToCart($product, $big->id, 5)->assertSessionHasNoErrors();
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->has('cart.lines', 2)
- ->where('cart.lines.0.key', $product->id.'-'.$small->id)
- ->where('cart.lines.0.variant.name', '250g / Original')
- ->where('cart.lines.0.unit_price', 12.9)
- ->where('cart.lines.0.line_total', 25.8)
- // Only 3 of the big spicy one are in stock.
- ->where('cart.lines.1.quantity', 3)
- ->where('cart.lines.1.max', 3)
- ->where('cart.subtotal', 94.5));
-
- $line = $product->id.'-'.$big->id;
- $this->patch(route('stores.preview.cart.update', [$this->store->slug, $line]), ['quantity' => 1])->assertRedirect();
- $this->delete(route('stores.preview.cart.remove', [$this->store->slug, $product->id.'-'.$small->id]))->assertRedirect();
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->has('cart.lines', 1)
- ->where('cart.lines.0.quantity', 1)
- ->where('cart.subtotal', 22.9));
- }
-
- public function test_a_variant_removed_after_it_was_added_drops_out_of_the_cart()
- {
- $product = $this->createProduct();
- $big = $this->variant($product, '500g', 'Pedas');
- $this->addToCart($product, $big->id);
-
- $big->delete();
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->has('cart.lines', 0)->where('cart.adjusted', true));
- }
-
- public function test_paying_lowers_the_stock_of_the_chosen_variant_and_the_order_names_it()
- {
- $gateway = new FakeGateway;
- $this->app->instance(PaymentGateway::class, $gateway);
-
- $product = $this->createProduct();
- $big = $this->variant($product, '500g', 'Original');
- $this->addToCart($product, $big->id, 2);
-
- $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',
- ])->assertRedirect();
-
- $order = Order::with('items')->firstOrFail();
- $item = $order->items->first();
-
- $this->assertSame('Sambal (500g / Original)', $item->name);
- $this->assertSame('500g / Original', $item->variant_name);
- $this->assertSame($big->id, $item->variant_id);
- $this->assertSame(2290, $item->unit_price);
- $this->assertSame(4580, $order->subtotal);
-
- $gateway->pay($order);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
-
- $this->assertSame(3, $big->fresh()->stock);
- // The product's own stock is all its variants together.
- $this->assertSame(16, $product->fresh()->stock);
-
- // Deleting the variant later keeps the order's record.
- $big->delete();
- $this->assertNull($item->fresh()->variant_id);
- $this->assertSame('Sambal (500g / Original)', $item->fresh()->name);
- }
-
- public function test_simple_products_still_work_without_a_variant()
- {
- $product = $this->store->products()->create(['name' => 'Kerepek', 'price' => 500, 'stock' => 4, 'status' => 'active']);
-
- $this->addToCart($product, null, 2)->assertSessionHasNoErrors();
-
- $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('cart.lines.0.key', (string) $product->id)
- ->where('cart.lines.0.variant', null)
- ->where('cart.lines.0.unit_price', 5));
- }
- }
|