| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\TestCase;
-
- class CartTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->user = User::factory()->create();
- $this->store = $this->storeWithDesign($this->user);
- $this->actingAs($this->user);
- }
-
- private function storeWithDesign(User $user): Store
- {
- $store = Store::factory()->for($user)->create();
- $store->plan = SamplePlan::for($store);
- $store->save();
-
- return $store;
- }
-
- private function add(Product $product, int $quantity = 1, ?Store $store = null)
- {
- return $this->post(route('stores.preview.cart.add', ($store ?? $this->store)->slug), [
- 'product_id' => $product->id,
- 'quantity' => $quantity,
- ]);
- }
-
- private function assertCart(callable $assert, ?Store $store = null): void
- {
- $this->get(route('stores.preview.cart', ($store ?? $this->store)->slug))
- ->assertOk()
- ->assertInertia(fn (Assert $page) => $assert($page->where('view.type', 'cart')));
- }
-
- public function test_a_new_cart_is_empty()
- {
- $this->assertCart(fn (Assert $page) => $page
- ->where('cart.count', 0)
- ->where('cart.subtotal', 0)
- ->where('cart.adjusted', false)
- ->has('cart.lines', 0));
- }
-
- public function test_a_shopper_can_add_products_and_the_totals_are_exact()
- {
- $sambal = Product::factory()->for($this->store)->create(['name' => 'Sambal', 'price' => 1290, 'stock' => 10]);
- $kerepek = Product::factory()->for($this->store)->create(['name' => 'Kerepek', 'price' => 799, 'stock' => 10]);
-
- $this->add($sambal, 2)->assertRedirect();
- $this->add($kerepek, 3)->assertRedirect();
- // Adding the same product again increases its quantity.
- $this->add($sambal, 1)->assertRedirect();
-
- $this->assertCart(fn (Assert $page) => $page
- ->where('cart.count', 6)
- // 3 x 12.90 + 3 x 7.99 = 62.67
- ->where('cart.subtotal', 62.67)
- ->has('cart.lines', 2)
- ->where('cart.lines.0.product.name', 'Sambal')
- ->where('cart.lines.0.quantity', 3)
- ->where('cart.lines.0.max', 10)
- ->where('cart.lines.0.line_total', 38.7)
- ->where('cart.lines.1.quantity', 3)
- ->where('cart.lines.1.line_total', 23.97));
-
- // Every storefront page knows the cart, so the header can show the count.
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('cart.count', 6));
- }
-
- public function test_a_shopper_cannot_add_more_than_the_stock()
- {
- $product = Product::factory()->for($this->store)->create(['stock' => 3]);
-
- $this->add($product, 2);
- $this->add($product, 5);
-
- $this->assertCart(fn (Assert $page) => $page
- ->where('cart.lines.0.quantity', 3)
- ->where('cart.lines.0.max', 3));
- }
-
- public function test_quantity_can_be_changed_and_zero_removes_the_item()
- {
- $product = Product::factory()->for($this->store)->create(['stock' => 10]);
- $this->add($product, 2);
-
- $this->patch(route('stores.preview.cart.update', [$this->store->slug, $product->id]), ['quantity' => 7])->assertRedirect();
- $this->assertCart(fn (Assert $page) => $page->where('cart.lines.0.quantity', 7));
-
- $this->patch(route('stores.preview.cart.update', [$this->store->slug, $product->id]), ['quantity' => 50]);
- $this->assertCart(fn (Assert $page) => $page->where('cart.lines.0.quantity', 10));
-
- $this->patch(route('stores.preview.cart.update', [$this->store->slug, $product->id]), ['quantity' => 0]);
- $this->assertCart(fn (Assert $page) => $page->has('cart.lines', 0));
- }
-
- public function test_an_item_can_be_removed()
- {
- $keep = Product::factory()->for($this->store)->create();
- $drop = Product::factory()->for($this->store)->create();
- $this->add($keep);
- $this->add($drop);
-
- $this->delete(route('stores.preview.cart.remove', [$this->store->slug, $drop->id]))->assertRedirect();
-
- $this->assertCart(fn (Assert $page) => $page
- ->has('cart.lines', 1)
- ->where('cart.lines.0.product.id', $keep->id));
- }
-
- public function test_bad_quantities_are_refused()
- {
- $product = Product::factory()->for($this->store)->create(['stock' => 10]);
-
- $this->add($product, 0)->assertSessionHasErrors('quantity');
- $this->add($product, -3)->assertSessionHasErrors('quantity');
- $this->add($product, 100)->assertSessionHasErrors('quantity');
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => '1; drop'])
- ->assertSessionHasErrors('quantity');
-
- $this->assertCart(fn (Assert $page) => $page->has('cart.lines', 0));
- }
-
- public function test_only_products_shoppers_can_buy_may_be_added()
- {
- $draft = Product::factory()->for($this->store)->draft()->create();
- $soldOut = Product::factory()->for($this->store)->soldOut()->create();
- $elsewhere = Product::factory()->create();
-
- $this->add($draft)->assertNotFound();
- $this->add($elsewhere)->assertNotFound();
- $this->add($soldOut)->assertSessionHasErrors('quantity');
-
- $this->assertCart(fn (Assert $page) => $page->has('cart.lines', 0));
- }
-
- public function test_the_cart_corrects_itself_when_stock_or_status_changes()
- {
- $shrinks = Product::factory()->for($this->store)->create(['price' => 1000, 'stock' => 10]);
- $hidden = Product::factory()->for($this->store)->create(['stock' => 10]);
- $deleted = Product::factory()->for($this->store)->create(['stock' => 10]);
- $this->add($shrinks, 8);
- $this->add($hidden, 1);
- $this->add($deleted, 1);
-
- $shrinks->update(['stock' => 2, 'price' => 1500]);
- $hidden->update(['status' => 'draft']);
- $deleted->delete();
-
- $this->assertCart(fn (Assert $page) => $page
- ->where('cart.adjusted', true)
- ->has('cart.lines', 1)
- ->where('cart.lines.0.quantity', 2)
- // The price is always the current one, never the one from when it was added.
- ->where('cart.subtotal', 30));
-
- // The correction is saved, so the notice shows only once.
- $this->assertCart(fn (Assert $page) => $page->where('cart.adjusted', false)->has('cart.lines', 1));
- }
-
- public function test_each_store_has_its_own_cart()
- {
- $other = $this->storeWithDesign($this->user);
- $here = Product::factory()->for($this->store)->create();
- $there = Product::factory()->for($other)->create();
-
- $this->add($here, 2);
- $this->add($there, 1, $other);
-
- $this->assertCart(fn (Assert $page) => $page->where('cart.count', 2)->where('cart.lines.0.product.id', $here->id));
- $this->assertCart(fn (Assert $page) => $page->where('cart.count', 1)->where('cart.lines.0.product.id', $there->id), $other);
- }
-
- public function test_the_cart_of_someone_elses_store_cannot_be_opened_or_changed()
- {
- $theirs = $this->storeWithDesign(User::factory()->create());
- $product = Product::factory()->for($theirs)->create();
-
- $this->get(route('stores.preview.cart', $theirs->slug))->assertNotFound();
- $this->add($product, 1, $theirs)->assertNotFound();
- $this->patch(route('stores.preview.cart.update', [$theirs->slug, $product->id]), ['quantity' => 1])->assertNotFound();
- $this->delete(route('stores.preview.cart.remove', [$theirs->slug, $product->id]))->assertNotFound();
- }
- }
|