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.

AutomaticDiscountTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Commerce\Payments\PaymentGateway;
  4. use App\Models\AutomaticDiscount;
  5. use App\Models\DiscountCode;
  6. use App\Models\Order;
  7. use App\Models\Product;
  8. use App\Models\Store;
  9. use App\Models\User;
  10. use App\StorePlan\SamplePlan;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\Mail;
  13. use Inertia\Testing\AssertableInertia as Assert;
  14. use Tests\Support\FakeGateway;
  15. use Tests\Support\ScriptedDriver;
  16. use Tests\TestCase;
  17. class AutomaticDiscountTest extends TestCase
  18. {
  19. use RefreshDatabase;
  20. private User $user;
  21. private Store $store;
  22. private FakeGateway $gateway;
  23. private Product $sambal;
  24. private Product $kerepek;
  25. protected function setUp(): void
  26. {
  27. parent::setUp();
  28. Mail::fake();
  29. $this->gateway = new FakeGateway;
  30. $this->app->instance(PaymentGateway::class, $this->gateway);
  31. $this->user = User::factory()->create();
  32. $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
  33. $this->store->plan = SamplePlan::for($this->store);
  34. $this->store->save();
  35. $this->actingAs($this->user);
  36. $this->sambal = $this->store->products()->create(['name' => 'Sambal', 'price' => 2000, 'stock' => 50, 'status' => 'active']);
  37. $this->kerepek = $this->store->products()->create(['name' => 'Kerepek', 'price' => 1000, 'stock' => 50, 'status' => 'active']);
  38. }
  39. /**
  40. * "Raya Sale: buy 2 or more, get 10% off", on every product unless changed.
  41. *
  42. * @return array<string, mixed>
  43. */
  44. private function form(array $overrides = []): array
  45. {
  46. return [
  47. 'name' => 'Raya Sale',
  48. 'type' => 'percent',
  49. 'value' => '10',
  50. 'minimum_quantity' => 2,
  51. 'minimum_subtotal' => '',
  52. 'applies_to' => 'all',
  53. 'starts_on' => '',
  54. 'ends_on' => '',
  55. 'active' => '1',
  56. ...$overrides,
  57. ];
  58. }
  59. private function createOffer(array $overrides = []): AutomaticDiscount
  60. {
  61. $this->post(route('stores.automatic-discounts.store', $this->store->slug), $this->form($overrides))
  62. ->assertSessionHasNoErrors();
  63. return $this->store->automaticDiscounts()->latest('id')->firstOrFail();
  64. }
  65. private function add(Product $product, int $quantity = 1): void
  66. {
  67. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity])
  68. ->assertSessionHasNoErrors();
  69. }
  70. private function cart(callable $assert): void
  71. {
  72. $this->get(route('stores.preview.cart', $this->store->slug))->assertInertia(fn (Assert $page) => $assert($page));
  73. }
  74. private function placeOrder(): Order
  75. {
  76. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  77. 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
  78. 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
  79. ])->assertRedirect();
  80. return Order::latest('id')->firstOrFail();
  81. }
  82. public function test_the_merchant_can_create_edit_and_delete_one()
  83. {
  84. $offer = $this->createOffer();
  85. $this->assertSame(10, $offer->value);
  86. $this->assertSame(2, $offer->minimum_quantity);
  87. $this->assertNull($offer->product_ids);
  88. $this->get(route('stores.discounts.index', $this->store->slug))
  89. ->assertInertia(fn (Assert $page) => $page
  90. ->where('offers.0.name', 'Raya Sale')
  91. ->where('offers.0.description', 'Buy 2 or more, get 10% off')
  92. ->where('offers.0.status', 'active'));
  93. $this->put(route('stores.automatic-discounts.update', [$this->store->slug, $offer->id]), $this->form([
  94. 'type' => 'fixed', 'value' => '5.50', 'minimum_quantity' => '', 'minimum_subtotal' => '50',
  95. 'applies_to' => 'chosen', 'product_ids' => [$this->sambal->id],
  96. ]))->assertSessionHasNoErrors();
  97. $offer->refresh();
  98. $this->assertSame(550, $offer->value);
  99. $this->assertSame(5000, $offer->minimum_subtotal);
  100. $this->assertSame([$this->sambal->id], $offer->product_ids);
  101. $this->assertSame('Spend RM 50.00 or more, get RM 5.50 off', $offer->describe());
  102. $this->delete(route('stores.automatic-discounts.destroy', [$this->store->slug, $offer->id]));
  103. $this->assertSame(0, AutomaticDiscount::count());
  104. }
  105. public function test_the_form_checks_its_values()
  106. {
  107. $other = Product::factory()->create();
  108. $url = route('stores.automatic-discounts.store', $this->store->slug);
  109. $this->post($url, $this->form(['value' => '150']))->assertSessionHasErrors('value');
  110. $this->post($url, $this->form(['name' => '']))->assertSessionHasErrors('name');
  111. $this->post($url, $this->form(['applies_to' => 'chosen']))->assertSessionHasErrors('product_ids');
  112. $this->post($url, $this->form(['applies_to' => 'chosen', 'product_ids' => [$other->id]]))->assertSessionHasErrors('product_ids.0');
  113. $this->post($url, $this->form(['starts_on' => '2030-05-10', 'ends_on' => '2030-05-01']))->assertSessionHasErrors('ends_on');
  114. $this->assertSame(0, AutomaticDiscount::count());
  115. // Another merchant cannot open it.
  116. $offer = $this->createOffer();
  117. $this->actingAs(User::factory()->create())
  118. ->get(route('stores.automatic-discounts.edit', [$this->store->slug, $offer->id]))
  119. ->assertNotFound();
  120. }
  121. public function test_it_applies_by_itself_once_the_cart_qualifies()
  122. {
  123. $this->createOffer();
  124. $this->add($this->sambal);
  125. // One item: not yet, but the cart says how close the shopper is.
  126. $this->cart(fn (Assert $page) => $page
  127. ->where('cart.applied', null)
  128. ->where('cart.total', 20)
  129. ->where('cart.offer_hint', 'Add 1 more to get 10% off (Raya Sale).'));
  130. $this->add($this->kerepek);
  131. $this->cart(fn (Assert $page) => $page
  132. ->where('cart.applied.name', 'Raya Sale')
  133. ->where('cart.applied.amount', 3)
  134. ->where('cart.applied.automatic', true)
  135. ->where('cart.offer_hint', null)
  136. ->where('cart.total', 27));
  137. $order = $this->placeOrder();
  138. $this->assertNull($order->discount_code);
  139. $this->assertSame('Raya Sale', $order->discount_name);
  140. $this->assertSame(300, $order->discount_total);
  141. $this->assertSame(2700 + 800, $order->total);
  142. }
  143. public function test_only_chosen_products_count_and_get_cheaper()
  144. {
  145. $this->createOffer(['applies_to' => 'chosen', 'product_ids' => [$this->sambal->id]]);
  146. // A sambal and a kerepek make 2 items, but only the sambal counts.
  147. $this->add($this->sambal);
  148. $this->add($this->kerepek);
  149. $this->cart(fn (Assert $page) => $page->where('cart.applied', null));
  150. $this->add($this->sambal);
  151. // 10% of the two sambal (RM 40), not of the kerepek.
  152. $this->cart(fn (Assert $page) => $page->where('cart.applied.amount', 4)->where('cart.total', 46));
  153. }
  154. public function test_a_spend_minimum_works_and_the_cart_says_how_much_more()
  155. {
  156. $this->createOffer(['type' => 'fixed', 'value' => '5', 'minimum_quantity' => '', 'minimum_subtotal' => '50']);
  157. $this->add($this->sambal, 2);
  158. $this->cart(fn (Assert $page) => $page->where('cart.offer_hint', 'Spend RM 10.00 more to get RM 5.00 off (Raya Sale).'));
  159. $this->add($this->kerepek);
  160. $this->cart(fn (Assert $page) => $page->where('cart.applied.amount', 5)->where('cart.total', 45));
  161. }
  162. public function test_only_the_bigger_of_a_code_and_an_automatic_discount_applies()
  163. {
  164. $this->createOffer();
  165. $this->post(route('stores.discounts.store', $this->store->slug), ['code' => 'BIG20', 'type' => 'percent', 'value' => 20, 'active' => true]);
  166. $this->post(route('stores.discounts.store', $this->store->slug), ['code' => 'SMALL', 'type' => 'fixed', 'value' => '1', 'active' => true]);
  167. $this->add($this->sambal, 2);
  168. // RM 1 off is less than 10% (RM 4), so the sale wins and the code is not used.
  169. $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => 'SMALL']);
  170. $this->cart(fn (Assert $page) => $page
  171. ->where('cart.applied.name', 'Raya Sale')
  172. ->where('cart.applied.amount', 4)
  173. ->where('cart.discount.code', 'SMALL')
  174. ->where('cart.discount.message', 'Raya Sale already saves you more, so this code is not used. Only one discount applies per order.'));
  175. // 20% (RM 8) beats the sale.
  176. $this->delete(route('stores.preview.cart.discount.remove', $this->store->slug));
  177. $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => 'BIG20']);
  178. $this->cart(fn (Assert $page) => $page
  179. ->where('cart.applied.name', 'BIG20')
  180. ->where('cart.applied.amount', 8)
  181. ->where('cart.discount.message', null)
  182. ->where('cart.total', 32));
  183. $order = $this->placeOrder();
  184. $this->assertSame('BIG20', $order->discount_code);
  185. $this->assertNull($order->discount_name);
  186. $this->assertSame(800, $order->discount_total);
  187. }
  188. public function test_a_code_is_not_counted_as_used_when_the_sale_won()
  189. {
  190. $this->createOffer();
  191. $this->post(route('stores.discounts.store', $this->store->slug), ['code' => 'SMALL', 'type' => 'fixed', 'value' => '1', 'active' => true]);
  192. $this->add($this->sambal, 2);
  193. $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => 'SMALL']);
  194. $order = $this->placeOrder();
  195. $this->gateway->pay($order);
  196. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  197. ->assertInertia(fn (Assert $page) => $page->where('view.order.discount_name', 'Raya Sale'));
  198. $this->assertTrue($order->fresh()->isPaid());
  199. $this->assertSame(0, DiscountCode::where('code', 'SMALL')->firstOrFail()->times_used);
  200. }
  201. public function test_switched_off_scheduled_and_ended_ones_do_not_apply()
  202. {
  203. $this->createOffer(['active' => '0']);
  204. $this->createOffer(['name' => 'Later', 'starts_on' => now('Asia/Kuala_Lumpur')->addDays(3)->format('Y-m-d')]);
  205. $this->createOffer(['name' => 'Over', 'starts_on' => '2020-01-01', 'ends_on' => '2020-01-31']);
  206. $this->add($this->sambal, 3);
  207. $this->cart(fn (Assert $page) => $page->where('cart.applied', null)->where('cart.total', 60));
  208. $this->assertSame(['off', 'scheduled', 'expired'], AutomaticDiscount::orderBy('id')->get()->map->status()->all());
  209. }
  210. public function test_product_pages_mention_the_offers_that_apply()
  211. {
  212. $this->createOffer(['applies_to' => 'chosen', 'product_ids' => [$this->sambal->id]]);
  213. $this->get(route('stores.preview.product', [$this->store->slug, $this->sambal->slug]))
  214. ->assertInertia(fn (Assert $page) => $page
  215. ->where('shop.offers.0.name', 'Raya Sale')
  216. ->where('shop.offers.0.text', 'Buy 2 or more, get 10% off')
  217. ->where('shop.offers.0.product_ids', [$this->sambal->id]));
  218. }
  219. public function test_the_ai_can_propose_one_from_chat()
  220. {
  221. ScriptedDriver::use([(string) json_encode([
  222. 'reply' => 'Please confirm below.',
  223. 'actions' => [[
  224. 'type' => 'create_automatic_discount', 'name' => 'Sambal Week', 'discount_type' => 'percent', 'value' => 15,
  225. 'minimum_quantity' => 3, 'products' => [$this->sambal->slug], 'ends_on' => '2030-01-31',
  226. ]],
  227. ])]);
  228. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Buy 3 sambal get 15% off until end of January']);
  229. $edit = $this->store->generations()->firstOrFail();
  230. $this->assertSame(
  231. 'Start the automatic discount "Sambal Week": Buy 3 or more, get 15% off, only on Sambal, until 2030-01-31. No code needed',
  232. $edit->actions[0]['summary'],
  233. );
  234. $this->assertSame(0, AutomaticDiscount::count());
  235. $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertSessionHasNoErrors();
  236. $offer = AutomaticDiscount::firstOrFail();
  237. $this->assertSame([$this->sambal->id], $offer->product_ids);
  238. $this->assertSame(3, $offer->minimum_quantity);
  239. }
  240. }