AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CartTest.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Product;
  4. use App\Models\Store;
  5. use App\Models\User;
  6. use App\StorePlan\SamplePlan;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Inertia\Testing\AssertableInertia as Assert;
  9. use Tests\TestCase;
  10. class CartTest extends TestCase
  11. {
  12. use RefreshDatabase;
  13. private User $user;
  14. private Store $store;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->user = User::factory()->create();
  19. $this->store = $this->storeWithDesign($this->user);
  20. $this->actingAs($this->user);
  21. }
  22. private function storeWithDesign(User $user): Store
  23. {
  24. $store = Store::factory()->for($user)->create();
  25. $store->plan = SamplePlan::for($store);
  26. $store->save();
  27. return $store;
  28. }
  29. private function add(Product $product, int $quantity = 1, ?Store $store = null)
  30. {
  31. return $this->post(route('stores.preview.cart.add', ($store ?? $this->store)->slug), [
  32. 'product_id' => $product->id,
  33. 'quantity' => $quantity,
  34. ]);
  35. }
  36. private function assertCart(callable $assert, ?Store $store = null): void
  37. {
  38. $this->get(route('stores.preview.cart', ($store ?? $this->store)->slug))
  39. ->assertOk()
  40. ->assertInertia(fn (Assert $page) => $assert($page->where('view.type', 'cart')));
  41. }
  42. public function test_a_new_cart_is_empty()
  43. {
  44. $this->assertCart(fn (Assert $page) => $page
  45. ->where('cart.count', 0)
  46. ->where('cart.subtotal', 0)
  47. ->where('cart.adjusted', false)
  48. ->has('cart.lines', 0));
  49. }
  50. public function test_a_shopper_can_add_products_and_the_totals_are_exact()
  51. {
  52. $sambal = Product::factory()->for($this->store)->create(['name' => 'Sambal', 'price' => 1290, 'stock' => 10]);
  53. $kerepek = Product::factory()->for($this->store)->create(['name' => 'Kerepek', 'price' => 799, 'stock' => 10]);
  54. $this->add($sambal, 2)->assertRedirect();
  55. $this->add($kerepek, 3)->assertRedirect();
  56. // Adding the same product again increases its quantity.
  57. $this->add($sambal, 1)->assertRedirect();
  58. $this->assertCart(fn (Assert $page) => $page
  59. ->where('cart.count', 6)
  60. // 3 x 12.90 + 3 x 7.99 = 62.67
  61. ->where('cart.subtotal', 62.67)
  62. ->has('cart.lines', 2)
  63. ->where('cart.lines.0.product.name', 'Sambal')
  64. ->where('cart.lines.0.quantity', 3)
  65. ->where('cart.lines.0.max', 10)
  66. ->where('cart.lines.0.line_total', 38.7)
  67. ->where('cart.lines.1.quantity', 3)
  68. ->where('cart.lines.1.line_total', 23.97));
  69. // Every storefront page knows the cart, so the header can show the count.
  70. $this->get(route('stores.preview', $this->store->slug))
  71. ->assertInertia(fn (Assert $page) => $page->where('cart.count', 6));
  72. }
  73. public function test_a_shopper_cannot_add_more_than_the_stock()
  74. {
  75. $product = Product::factory()->for($this->store)->create(['stock' => 3]);
  76. $this->add($product, 2);
  77. $this->add($product, 5);
  78. $this->assertCart(fn (Assert $page) => $page
  79. ->where('cart.lines.0.quantity', 3)
  80. ->where('cart.lines.0.max', 3));
  81. }
  82. public function test_quantity_can_be_changed_and_zero_removes_the_item()
  83. {
  84. $product = Product::factory()->for($this->store)->create(['stock' => 10]);
  85. $this->add($product, 2);
  86. $this->patch(route('stores.preview.cart.update', [$this->store->slug, $product->id]), ['quantity' => 7])->assertRedirect();
  87. $this->assertCart(fn (Assert $page) => $page->where('cart.lines.0.quantity', 7));
  88. $this->patch(route('stores.preview.cart.update', [$this->store->slug, $product->id]), ['quantity' => 50]);
  89. $this->assertCart(fn (Assert $page) => $page->where('cart.lines.0.quantity', 10));
  90. $this->patch(route('stores.preview.cart.update', [$this->store->slug, $product->id]), ['quantity' => 0]);
  91. $this->assertCart(fn (Assert $page) => $page->has('cart.lines', 0));
  92. }
  93. public function test_an_item_can_be_removed()
  94. {
  95. $keep = Product::factory()->for($this->store)->create();
  96. $drop = Product::factory()->for($this->store)->create();
  97. $this->add($keep);
  98. $this->add($drop);
  99. $this->delete(route('stores.preview.cart.remove', [$this->store->slug, $drop->id]))->assertRedirect();
  100. $this->assertCart(fn (Assert $page) => $page
  101. ->has('cart.lines', 1)
  102. ->where('cart.lines.0.product.id', $keep->id));
  103. }
  104. public function test_bad_quantities_are_refused()
  105. {
  106. $product = Product::factory()->for($this->store)->create(['stock' => 10]);
  107. $this->add($product, 0)->assertSessionHasErrors('quantity');
  108. $this->add($product, -3)->assertSessionHasErrors('quantity');
  109. $this->add($product, 100)->assertSessionHasErrors('quantity');
  110. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => '1; drop'])
  111. ->assertSessionHasErrors('quantity');
  112. $this->assertCart(fn (Assert $page) => $page->has('cart.lines', 0));
  113. }
  114. public function test_only_products_shoppers_can_buy_may_be_added()
  115. {
  116. $draft = Product::factory()->for($this->store)->draft()->create();
  117. $soldOut = Product::factory()->for($this->store)->soldOut()->create();
  118. $elsewhere = Product::factory()->create();
  119. $this->add($draft)->assertNotFound();
  120. $this->add($elsewhere)->assertNotFound();
  121. $this->add($soldOut)->assertSessionHasErrors('quantity');
  122. $this->assertCart(fn (Assert $page) => $page->has('cart.lines', 0));
  123. }
  124. public function test_the_cart_corrects_itself_when_stock_or_status_changes()
  125. {
  126. $shrinks = Product::factory()->for($this->store)->create(['price' => 1000, 'stock' => 10]);
  127. $hidden = Product::factory()->for($this->store)->create(['stock' => 10]);
  128. $deleted = Product::factory()->for($this->store)->create(['stock' => 10]);
  129. $this->add($shrinks, 8);
  130. $this->add($hidden, 1);
  131. $this->add($deleted, 1);
  132. $shrinks->update(['stock' => 2, 'price' => 1500]);
  133. $hidden->update(['status' => 'draft']);
  134. $deleted->delete();
  135. $this->assertCart(fn (Assert $page) => $page
  136. ->where('cart.adjusted', true)
  137. ->has('cart.lines', 1)
  138. ->where('cart.lines.0.quantity', 2)
  139. // The price is always the current one, never the one from when it was added.
  140. ->where('cart.subtotal', 30));
  141. // The correction is saved, so the notice shows only once.
  142. $this->assertCart(fn (Assert $page) => $page->where('cart.adjusted', false)->has('cart.lines', 1));
  143. }
  144. public function test_each_store_has_its_own_cart()
  145. {
  146. $other = $this->storeWithDesign($this->user);
  147. $here = Product::factory()->for($this->store)->create(['stock' => 10]);
  148. $there = Product::factory()->for($other)->create(['stock' => 10]);
  149. $this->add($here, 2);
  150. $this->add($there, 1, $other);
  151. $this->assertCart(fn (Assert $page) => $page->where('cart.count', 2)->where('cart.lines.0.product.id', $here->id));
  152. $this->assertCart(fn (Assert $page) => $page->where('cart.count', 1)->where('cart.lines.0.product.id', $there->id), $other);
  153. }
  154. public function test_the_cart_of_someone_elses_store_cannot_be_opened_or_changed()
  155. {
  156. $theirs = $this->storeWithDesign(User::factory()->create());
  157. $product = Product::factory()->for($theirs)->create();
  158. $this->get(route('stores.preview.cart', $theirs->slug))->assertNotFound();
  159. $this->add($product, 1, $theirs)->assertNotFound();
  160. $this->patch(route('stores.preview.cart.update', [$theirs->slug, $product->id]), ['quantity' => 1])->assertNotFound();
  161. $this->delete(route('stores.preview.cart.remove', [$theirs->slug, $product->id]))->assertNotFound();
  162. }
  163. }