AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BundleTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\ProductVariant;
  7. use App\Models\Store;
  8. use App\Models\User;
  9. use App\StorePlan\SamplePlan;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Facades\Mail;
  12. use Inertia\Testing\AssertableInertia as Assert;
  13. use Tests\Support\FakeGateway;
  14. use Tests\TestCase;
  15. class BundleTest extends TestCase
  16. {
  17. use RefreshDatabase;
  18. private User $user;
  19. private Store $store;
  20. private Product $sambal;
  21. private ProductVariant $small;
  22. private ProductVariant $big;
  23. private Product $kerepek;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. Mail::fake();
  28. $this->user = User::factory()->create();
  29. $this->store = Store::factory()->for($this->user)->create();
  30. $this->store->plan = SamplePlan::for($this->store);
  31. $this->store->save();
  32. $this->actingAs($this->user);
  33. // A sambal in two sizes, and a simple kerepek.
  34. $this->post(route('stores.products.store', $this->store->slug), [
  35. 'name' => 'Sambal', 'status' => 'active',
  36. 'options' => [['name' => 'Size', 'values' => '250g, 500g']],
  37. 'variants' => [
  38. ['option1' => '250g', 'price' => '12.90', 'stock' => 10],
  39. ['option1' => '500g', 'price' => '22.90', 'stock' => 4],
  40. ],
  41. ])->assertSessionHasNoErrors();
  42. $this->sambal = $this->store->products()->where('name', 'Sambal')->firstOrFail();
  43. $this->small = $this->sambal->variants()->where('option1', '250g')->firstOrFail();
  44. $this->big = $this->sambal->variants()->where('option1', '500g')->firstOrFail();
  45. $this->kerepek = $this->store->products()->create(['name' => 'Kerepek', 'price' => 800, 'stock' => 3, 'status' => 'active']);
  46. }
  47. /**
  48. * Two small sambal and one kerepek for RM 30.
  49. *
  50. * @return array<string, mixed>
  51. */
  52. private function form(array $overrides = []): array
  53. {
  54. return [
  55. 'name' => 'Combo Pedas',
  56. 'status' => 'active',
  57. 'price' => '30.00',
  58. 'is_bundle' => '1',
  59. 'bundle_items' => [
  60. ['item' => $this->sambal->id.'-'.$this->small->id, 'quantity' => 2],
  61. ['item' => (string) $this->kerepek->id, 'quantity' => 1],
  62. ],
  63. ...$overrides,
  64. ];
  65. }
  66. private function createBundle(array $overrides = []): Product
  67. {
  68. $this->post(route('stores.products.store', $this->store->slug), $this->form($overrides))
  69. ->assertSessionHasNoErrors();
  70. return $this->store->products()->where('is_bundle', true)->latest('id')->firstOrFail();
  71. }
  72. private function addToCart(Product $product, ?int $variantId, int $quantity = 1)
  73. {
  74. return $this->post(route('stores.preview.cart.add', $this->store->slug), [
  75. 'product_id' => $product->id,
  76. 'variant_id' => $variantId,
  77. 'quantity' => $quantity,
  78. ]);
  79. }
  80. private function checkout()
  81. {
  82. return $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  83. 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
  84. 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
  85. ]);
  86. }
  87. public function test_a_bundle_gets_its_stock_from_what_is_inside()
  88. {
  89. $bundle = $this->createBundle();
  90. $this->assertSame(3000, $bundle->price);
  91. $this->assertCount(2, $bundle->bundleItems);
  92. // 10 small sambal ÷ 2 = 5, 3 kerepek ÷ 1 = 3: three bundles can be sold.
  93. $this->assertSame(3, $bundle->stock);
  94. // Changing an item's stock changes the bundle's.
  95. $this->post(route('stores.products.update', [$this->store->slug, $this->kerepek->id]), [
  96. 'name' => 'Kerepek', 'status' => 'active', 'price' => '8.00', 'stock' => 20,
  97. ])->assertSessionHasNoErrors();
  98. $this->assertSame(5, $bundle->fresh()->stock);
  99. $this->get(route('stores.products.edit', [$this->store->slug, $bundle->id]))
  100. ->assertInertia(fn (Assert $page) => $page
  101. ->where('product.is_bundle', true)
  102. ->where('product.bundle_items.0.item', $this->sambal->id.'-'.$this->small->id)
  103. ->where('product.bundle_items.0.label', '2 × Sambal (250g)')
  104. // The bundle cannot hold itself or another bundle: only the 2 sizes and the kerepek.
  105. ->has('bundleChoices', 3));
  106. }
  107. public function test_bundle_items_must_be_other_products_of_the_store()
  108. {
  109. $other = Product::factory()->create(['name' => 'Elsewhere']);
  110. $bundle = $this->createBundle();
  111. $url = route('stores.products.store', $this->store->slug);
  112. $this->post($url, $this->form(['bundle_items' => []]))->assertSessionHasErrors('bundle_items');
  113. $this->post($url, $this->form(['bundle_items' => [['item' => (string) $other->id, 'quantity' => 1]]]))
  114. ->assertSessionHasErrors('bundle_items.0.item');
  115. // The sambal has sizes, so the bundle must say which one.
  116. $this->post($url, $this->form(['bundle_items' => [['item' => (string) $this->sambal->id, 'quantity' => 1]]]))
  117. ->assertSessionHasErrors('bundle_items.0.item');
  118. $this->post($url, $this->form(['bundle_items' => [['item' => (string) $bundle->id, 'quantity' => 1]]]))
  119. ->assertSessionHasErrors('bundle_items.0.item');
  120. $this->post($url, $this->form(['bundle_items' => [
  121. ['item' => (string) $this->kerepek->id, 'quantity' => 1],
  122. ['item' => (string) $this->kerepek->id, 'quantity' => 2],
  123. ]]))->assertSessionHasErrors('bundle_items.1.item');
  124. $this->post(route('stores.products.update', [$this->store->slug, $bundle->id]), $this->form([
  125. 'bundle_items' => [['item' => (string) $bundle->id, 'quantity' => 1]],
  126. ]))->assertSessionHasErrors('bundle_items.0.item');
  127. $this->assertSame(1, $this->store->products()->where('is_bundle', true)->count());
  128. }
  129. public function test_products_inside_a_bundle_are_protected()
  130. {
  131. $bundle = $this->createBundle();
  132. // Deleting the kerepek would quietly empty part of the bundle.
  133. $this->delete(route('stores.products.destroy', [$this->store->slug, $this->kerepek->id]));
  134. $this->assertNotNull($this->kerepek->fresh());
  135. // The 250g size is inside the bundle, so it cannot be removed.
  136. $this->post(route('stores.products.update', [$this->store->slug, $this->sambal->id]), [
  137. 'name' => 'Sambal', 'status' => 'active',
  138. 'options' => [['name' => 'Size', 'values' => '500g']],
  139. 'variants' => [['option1' => '500g', 'price' => '22.90', 'stock' => 4]],
  140. ])->assertSessionHasErrors('options');
  141. // Nor can the sambal lose its sizes, or become a bundle itself.
  142. $this->post(route('stores.products.update', [$this->store->slug, $this->sambal->id]), [
  143. 'name' => 'Sambal', 'status' => 'active', 'price' => '12.90', 'stock' => 10,
  144. ])->assertSessionHasErrors('options');
  145. $this->post(route('stores.products.update', [$this->store->slug, $this->kerepek->id]), [
  146. 'name' => 'Kerepek', 'status' => 'active', 'price' => '8.00',
  147. 'is_bundle' => '1', 'bundle_items' => [['item' => $this->sambal->id.'-'.$this->big->id, 'quantity' => 1]],
  148. ])->assertSessionHasErrors('is_bundle');
  149. $this->assertCount(2, $this->sambal->variants()->get());
  150. // Once the bundle is gone, the kerepek can be deleted.
  151. $this->delete(route('stores.products.destroy', [$this->store->slug, $bundle->id]));
  152. $this->delete(route('stores.products.destroy', [$this->store->slug, $this->kerepek->id]));
  153. $this->assertNull($this->kerepek->fresh());
  154. }
  155. public function test_the_store_shows_what_is_inside()
  156. {
  157. $bundle = $this->createBundle();
  158. $this->get(route('stores.preview.product', [$this->store->slug, $bundle->slug]))
  159. ->assertInertia(fn (Assert $page) => $page
  160. ->where('view.product.price', 30)
  161. ->where('view.product.stock_left', 3)
  162. ->where('view.product.bundle.0.name', 'Sambal (250g)')
  163. ->where('view.product.bundle.0.quantity', 2)
  164. ->where('view.product.bundle.1.slug', $this->kerepek->slug));
  165. }
  166. public function test_paying_for_a_bundle_lowers_the_stock_of_what_is_inside()
  167. {
  168. $gateway = new FakeGateway;
  169. $this->app->instance(PaymentGateway::class, $gateway);
  170. $bundle = $this->createBundle();
  171. $this->addToCart($bundle, null, 2)->assertSessionHasNoErrors();
  172. $this->checkout()->assertRedirect();
  173. $order = Order::with('items')->firstOrFail();
  174. $item = $order->items->first();
  175. $this->assertSame('Combo Pedas', $item->name);
  176. $this->assertSame(6000, $order->subtotal);
  177. $this->assertSame(['2 × Sambal (250g)', '1 × Kerepek'], array_column($item->bundle_contents, 'label'));
  178. $gateway->pay($order);
  179. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  180. // Two bundles: 4 small sambal and 2 kerepek.
  181. $this->assertSame(6, $this->small->fresh()->stock);
  182. $this->assertSame(4, $this->big->fresh()->stock);
  183. $this->assertSame(10, $this->sambal->fresh()->stock);
  184. $this->assertSame(1, $this->kerepek->fresh()->stock);
  185. $this->assertSame(1, $bundle->fresh()->stock);
  186. $this->get(route('stores.orders.show', [$this->store->slug, $order->id]))
  187. ->assertInertia(fn (Assert $page) => $page
  188. ->where('order.items.0.contents', ['2 × Sambal (250g)', '1 × Kerepek']));
  189. }
  190. public function test_a_bundle_and_its_items_in_one_cart_share_the_stock()
  191. {
  192. $this->app->instance(PaymentGateway::class, new FakeGateway);
  193. $bundle = $this->createBundle();
  194. // 3 kerepek in stock: 2 bundles need 2, plus 2 on their own makes 4.
  195. $this->addToCart($bundle, null, 2)->assertSessionHasNoErrors();
  196. $this->addToCart($this->kerepek, null, 2)->assertSessionHasNoErrors();
  197. $this->checkout()->assertSessionHasErrors('cart');
  198. $this->assertSame(0, Order::count());
  199. $this->patch(route('stores.preview.cart.update', [$this->store->slug, (string) $this->kerepek->id]), ['quantity' => 1]);
  200. $this->checkout()->assertSessionHasNoErrors()->assertRedirect();
  201. $this->assertSame(1, Order::count());
  202. }
  203. }