AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Commerce\Payments\PaymentGateway;
  4. use App\Models\Order;
  5. use App\Models\Product;
  6. use App\Models\Store;
  7. use App\Models\User;
  8. use App\StorePlan\SamplePlan;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Facades\Mail;
  11. use Inertia\Testing\AssertableInertia as Assert;
  12. use Tests\Support\FakeGateway;
  13. use Tests\TestCase;
  14. class ProductVariantTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. private User $user;
  18. private Store $store;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. Mail::fake();
  23. $this->user = User::factory()->create();
  24. $this->store = Store::factory()->for($this->user)->create();
  25. $this->store->plan = SamplePlan::for($this->store);
  26. $this->store->save();
  27. $this->actingAs($this->user);
  28. }
  29. /**
  30. * A sambal in two sizes and two flavours: four combinations.
  31. *
  32. * @return array<string, mixed>
  33. */
  34. private function form(array $overrides = []): array
  35. {
  36. return [
  37. 'name' => 'Sambal',
  38. 'description' => '',
  39. 'status' => 'active',
  40. 'options' => [
  41. ['name' => 'Size', 'values' => '250g, 500g'],
  42. ['name' => 'Flavour', 'values' => 'Original, Pedas'],
  43. ],
  44. 'variants' => [
  45. ['option1' => '250g', 'option2' => 'Original', 'price' => '12.90', 'stock' => 10],
  46. ['option1' => '250g', 'option2' => 'Pedas', 'price' => '12.90', 'stock' => 0],
  47. ['option1' => '500g', 'option2' => 'Original', 'price' => '22.90', 'compare_at_price' => '25.00', 'stock' => 5],
  48. ['option1' => '500g', 'option2' => 'Pedas', 'price' => '22.90', 'stock' => 3],
  49. ],
  50. ...$overrides,
  51. ];
  52. }
  53. private function createProduct(array $overrides = []): Product
  54. {
  55. $this->post(route('stores.products.store', $this->store->slug), $this->form($overrides))
  56. ->assertSessionHasNoErrors();
  57. return $this->store->products()->latest('id')->firstOrFail();
  58. }
  59. private function variant(Product $product, string $first, ?string $second = null)
  60. {
  61. return $product->variants()->where('option1', $first)->where('option2', $second)->firstOrFail();
  62. }
  63. private function addToCart(Product $product, ?int $variantId, int $quantity = 1)
  64. {
  65. return $this->post(route('stores.preview.cart.add', $this->store->slug), [
  66. 'product_id' => $product->id,
  67. 'variant_id' => $variantId,
  68. 'quantity' => $quantity,
  69. ]);
  70. }
  71. public function test_a_product_can_have_options_with_a_price_and_stock_for_each_combination()
  72. {
  73. $product = $this->createProduct();
  74. $this->assertSame([
  75. ['name' => 'Size', 'values' => ['250g', '500g']],
  76. ['name' => 'Flavour', 'values' => ['Original', 'Pedas']],
  77. ], $product->options);
  78. $this->assertCount(4, $product->variants);
  79. $this->assertSame(['250g / Original', '250g / Pedas', '500g / Original', '500g / Pedas'], $product->variants->map->name()->all());
  80. $this->assertSame(2500, $this->variant($product, '500g', 'Original')->compare_at_price);
  81. // The product itself shows the lowest price and all the stock together.
  82. $this->assertSame(1290, $product->price);
  83. $this->assertSame(18, $product->stock);
  84. $this->get(route('stores.products.edit', [$this->store->slug, $product->id]))
  85. ->assertInertia(fn (Assert $page) => $page
  86. ->where('product.options.0.values', '250g, 500g')
  87. ->has('product.variants', 4)
  88. ->where('product.variants.2.price', '22.90'));
  89. }
  90. public function test_a_single_option_works_too()
  91. {
  92. $product = $this->createProduct([
  93. 'options' => [['name' => 'Size', 'values' => 'S, M, L']],
  94. 'variants' => [
  95. ['option1' => 'S', 'price' => '30', 'stock' => 1],
  96. ['option1' => 'M', 'price' => '30', 'stock' => 2],
  97. ['option1' => 'L', 'price' => '32', 'stock' => 3],
  98. ],
  99. ]);
  100. $this->assertSame(['S', 'M', 'L'], $product->variants->map->name()->all());
  101. $this->assertNull($this->variant($product, 'L')->option2);
  102. }
  103. public function test_every_combination_needs_a_price_and_stock()
  104. {
  105. $form = $this->form();
  106. array_pop($form['variants']);
  107. $this->post(route('stores.products.store', $this->store->slug), $form)->assertSessionHasErrors('variants');
  108. $this->post(route('stores.products.store', $this->store->slug), $this->form([
  109. 'options' => [['name' => 'Size', 'values' => ' , ']],
  110. ]))->assertSessionHasErrors('options.0.values');
  111. $this->post(route('stores.products.store', $this->store->slug), $this->form([
  112. 'options' => [['name' => 'Size', 'values' => 'S'], ['name' => 'size', 'values' => 'M']],
  113. ]))->assertSessionHasErrors('options');
  114. $this->assertSame(0, $this->store->products()->count());
  115. }
  116. public function test_editing_keeps_variants_whose_choices_did_not_change()
  117. {
  118. $product = $this->createProduct();
  119. $kept = $this->variant($product, '500g', 'Pedas');
  120. $form = $this->form([
  121. 'options' => [
  122. ['name' => 'Size', 'values' => '500g, 1kg'],
  123. ['name' => 'Flavour', 'values' => 'Original, Pedas'],
  124. ],
  125. 'variants' => [
  126. ['option1' => '500g', 'option2' => 'Original', 'price' => '22.90', 'stock' => 5],
  127. ['option1' => '500g', 'option2' => 'Pedas', 'price' => '21.00', 'stock' => 9],
  128. ['option1' => '1kg', 'option2' => 'Original', 'price' => '40.00', 'stock' => 2],
  129. ['option1' => '1kg', 'option2' => 'Pedas', 'price' => '40.00', 'stock' => 2],
  130. ],
  131. ]);
  132. $this->post(route('stores.products.update', [$this->store->slug, $product->id]), $form)->assertSessionHasNoErrors();
  133. $product->refresh();
  134. $this->assertCount(4, $product->variants);
  135. $this->assertSame($kept->id, $this->variant($product, '500g', 'Pedas')->id);
  136. $this->assertSame(2100, $kept->fresh()->price);
  137. $this->assertSame(0, $product->variants()->where('option1', '250g')->count());
  138. $this->assertSame(2100, $product->price);
  139. $this->assertSame(18, $product->stock);
  140. }
  141. public function test_turning_options_off_makes_it_a_simple_product_again()
  142. {
  143. $product = $this->createProduct();
  144. $this->post(route('stores.products.update', [$this->store->slug, $product->id]), [
  145. 'name' => 'Sambal', 'status' => 'active', 'price' => '15.00', 'stock' => 7,
  146. ])->assertSessionHasNoErrors();
  147. $product->refresh();
  148. $this->assertNull($product->options);
  149. $this->assertCount(0, $product->variants);
  150. $this->assertSame(1500, $product->price);
  151. $this->assertSame(7, $product->stock);
  152. }
  153. public function test_the_store_gets_the_options_and_a_from_price()
  154. {
  155. $product = $this->createProduct();
  156. $this->get(route('stores.preview.product', [$this->store->slug, $product->slug]))
  157. ->assertInertia(fn (Assert $page) => $page
  158. ->where('view.product.price_varies', true)
  159. ->where('view.product.options.0.name', 'Size')
  160. ->has('view.product.variants', 4)
  161. ->where('view.product.variants.1.name', '250g / Pedas')
  162. ->where('view.product.variants.1.in_stock', false)
  163. ->where('view.product.variants.2.price', 22.9)
  164. ->where('view.product.variants.2.compare_at_price', 25));
  165. }
  166. public function test_shoppers_must_choose_a_variant_and_each_one_has_its_own_cart_line()
  167. {
  168. $product = $this->createProduct();
  169. $small = $this->variant($product, '250g', 'Original');
  170. $big = $this->variant($product, '500g', 'Pedas');
  171. $soldOut = $this->variant($product, '250g', 'Pedas');
  172. $elsewhere = $this->store->products()->create(['name' => 'Other', 'price' => 100, 'stock' => 1, 'status' => 'active']);
  173. $this->addToCart($product, null)->assertSessionHasErrors('variant_id');
  174. $this->addToCart($product, $soldOut->id)->assertSessionHasErrors('quantity');
  175. $this->addToCart($elsewhere, $small->id)->assertNotFound();
  176. $this->addToCart($product, $small->id, 2)->assertSessionHasNoErrors();
  177. $this->addToCart($product, $big->id, 5)->assertSessionHasNoErrors();
  178. $this->get(route('stores.preview.cart', $this->store->slug))
  179. ->assertInertia(fn (Assert $page) => $page
  180. ->has('cart.lines', 2)
  181. ->where('cart.lines.0.key', $product->id.'-'.$small->id)
  182. ->where('cart.lines.0.variant.name', '250g / Original')
  183. ->where('cart.lines.0.unit_price', 12.9)
  184. ->where('cart.lines.0.line_total', 25.8)
  185. // Only 3 of the big spicy one are in stock.
  186. ->where('cart.lines.1.quantity', 3)
  187. ->where('cart.lines.1.max', 3)
  188. ->where('cart.subtotal', 94.5));
  189. $line = $product->id.'-'.$big->id;
  190. $this->patch(route('stores.preview.cart.update', [$this->store->slug, $line]), ['quantity' => 1])->assertRedirect();
  191. $this->delete(route('stores.preview.cart.remove', [$this->store->slug, $product->id.'-'.$small->id]))->assertRedirect();
  192. $this->get(route('stores.preview.cart', $this->store->slug))
  193. ->assertInertia(fn (Assert $page) => $page
  194. ->has('cart.lines', 1)
  195. ->where('cart.lines.0.quantity', 1)
  196. ->where('cart.subtotal', 22.9));
  197. }
  198. public function test_a_variant_removed_after_it_was_added_drops_out_of_the_cart()
  199. {
  200. $product = $this->createProduct();
  201. $big = $this->variant($product, '500g', 'Pedas');
  202. $this->addToCart($product, $big->id);
  203. $big->delete();
  204. $this->get(route('stores.preview.cart', $this->store->slug))
  205. ->assertInertia(fn (Assert $page) => $page->has('cart.lines', 0)->where('cart.adjusted', true));
  206. }
  207. public function test_paying_lowers_the_stock_of_the_chosen_variant_and_the_order_names_it()
  208. {
  209. $gateway = new FakeGateway;
  210. $this->app->instance(PaymentGateway::class, $gateway);
  211. $product = $this->createProduct();
  212. $big = $this->variant($product, '500g', 'Original');
  213. $this->addToCart($product, $big->id, 2);
  214. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  215. 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
  216. 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
  217. ])->assertRedirect();
  218. $order = Order::with('items')->firstOrFail();
  219. $item = $order->items->first();
  220. $this->assertSame('Sambal (500g / Original)', $item->name);
  221. $this->assertSame('500g / Original', $item->variant_name);
  222. $this->assertSame($big->id, $item->variant_id);
  223. $this->assertSame(2290, $item->unit_price);
  224. $this->assertSame(4580, $order->subtotal);
  225. $gateway->pay($order);
  226. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  227. $this->assertSame(3, $big->fresh()->stock);
  228. // The product's own stock is all its variants together.
  229. $this->assertSame(16, $product->fresh()->stock);
  230. // Deleting the variant later keeps the order's record.
  231. $big->delete();
  232. $this->assertNull($item->fresh()->variant_id);
  233. $this->assertSame('Sambal (500g / Original)', $item->fresh()->name);
  234. }
  235. public function test_simple_products_still_work_without_a_variant()
  236. {
  237. $product = $this->store->products()->create(['name' => 'Kerepek', 'price' => 500, 'stock' => 4, 'status' => 'active']);
  238. $this->addToCart($product, null, 2)->assertSessionHasNoErrors();
  239. $this->get(route('stores.preview.cart', $this->store->slug))
  240. ->assertInertia(fn (Assert $page) => $page
  241. ->where('cart.lines.0.key', (string) $product->id)
  242. ->where('cart.lines.0.variant', null)
  243. ->where('cart.lines.0.unit_price', 5));
  244. }
  245. }