| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Commerce\Payments\PaymentGateway;
- use App\Models\AutomaticDiscount;
- use App\Models\DiscountCode;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Mail;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\FakeGateway;
- use Tests\Support\ScriptedDriver;
- use Tests\TestCase;
-
- class AutomaticDiscountTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- private FakeGateway $gateway;
-
- private Product $sambal;
-
- private Product $kerepek;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- Mail::fake();
- $this->gateway = new FakeGateway;
- $this->app->instance(PaymentGateway::class, $this->gateway);
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->actingAs($this->user);
-
- $this->sambal = $this->store->products()->create(['name' => 'Sambal', 'price' => 2000, 'stock' => 50, 'status' => 'active']);
- $this->kerepek = $this->store->products()->create(['name' => 'Kerepek', 'price' => 1000, 'stock' => 50, 'status' => 'active']);
- }
-
- /**
- * "Raya Sale: buy 2 or more, get 10% off", on every product unless changed.
- *
- * @return array<string, mixed>
- */
- private function form(array $overrides = []): array
- {
- return [
- 'name' => 'Raya Sale',
- 'type' => 'percent',
- 'value' => '10',
- 'minimum_quantity' => 2,
- 'minimum_subtotal' => '',
- 'applies_to' => 'all',
- 'starts_on' => '',
- 'ends_on' => '',
- 'active' => '1',
- ...$overrides,
- ];
- }
-
- private function createOffer(array $overrides = []): AutomaticDiscount
- {
- $this->post(route('stores.automatic-discounts.store', $this->store->slug), $this->form($overrides))
- ->assertSessionHasNoErrors();
-
- return $this->store->automaticDiscounts()->latest('id')->firstOrFail();
- }
-
- private function add(Product $product, int $quantity = 1): void
- {
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity])
- ->assertSessionHasNoErrors();
- }
-
- private function cart(callable $assert): void
- {
- $this->get(route('stores.preview.cart', $this->store->slug))->assertInertia(fn (Assert $page) => $assert($page));
- }
-
- private function placeOrder(): Order
- {
- $this->post(route('stores.preview.checkout.store', $this->store->slug), [
- 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
- 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
- ])->assertRedirect();
-
- return Order::latest('id')->firstOrFail();
- }
-
- public function test_the_merchant_can_create_edit_and_delete_one()
- {
- $offer = $this->createOffer();
-
- $this->assertSame(10, $offer->value);
- $this->assertSame(2, $offer->minimum_quantity);
- $this->assertNull($offer->product_ids);
-
- $this->get(route('stores.discounts.index', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('offers.0.name', 'Raya Sale')
- ->where('offers.0.description', 'Buy 2 or more, get 10% off')
- ->where('offers.0.status', 'active'));
-
- $this->put(route('stores.automatic-discounts.update', [$this->store->slug, $offer->id]), $this->form([
- 'type' => 'fixed', 'value' => '5.50', 'minimum_quantity' => '', 'minimum_subtotal' => '50',
- 'applies_to' => 'chosen', 'product_ids' => [$this->sambal->id],
- ]))->assertSessionHasNoErrors();
-
- $offer->refresh();
- $this->assertSame(550, $offer->value);
- $this->assertSame(5000, $offer->minimum_subtotal);
- $this->assertSame([$this->sambal->id], $offer->product_ids);
- $this->assertSame('Spend RM 50.00 or more, get RM 5.50 off', $offer->describe());
-
- $this->delete(route('stores.automatic-discounts.destroy', [$this->store->slug, $offer->id]));
- $this->assertSame(0, AutomaticDiscount::count());
- }
-
- public function test_the_form_checks_its_values()
- {
- $other = Product::factory()->create();
- $url = route('stores.automatic-discounts.store', $this->store->slug);
-
- $this->post($url, $this->form(['value' => '150']))->assertSessionHasErrors('value');
- $this->post($url, $this->form(['name' => '']))->assertSessionHasErrors('name');
- $this->post($url, $this->form(['applies_to' => 'chosen']))->assertSessionHasErrors('product_ids');
- $this->post($url, $this->form(['applies_to' => 'chosen', 'product_ids' => [$other->id]]))->assertSessionHasErrors('product_ids.0');
- $this->post($url, $this->form(['starts_on' => '2030-05-10', 'ends_on' => '2030-05-01']))->assertSessionHasErrors('ends_on');
-
- $this->assertSame(0, AutomaticDiscount::count());
-
- // Another merchant cannot open it.
- $offer = $this->createOffer();
- $this->actingAs(User::factory()->create())
- ->get(route('stores.automatic-discounts.edit', [$this->store->slug, $offer->id]))
- ->assertNotFound();
- }
-
- public function test_it_applies_by_itself_once_the_cart_qualifies()
- {
- $this->createOffer();
-
- $this->add($this->sambal);
-
- // One item: not yet, but the cart says how close the shopper is.
- $this->cart(fn (Assert $page) => $page
- ->where('cart.applied', null)
- ->where('cart.total', 20)
- ->where('cart.offer_hint', 'Add 1 more to get 10% off (Raya Sale).'));
-
- $this->add($this->kerepek);
-
- $this->cart(fn (Assert $page) => $page
- ->where('cart.applied.name', 'Raya Sale')
- ->where('cart.applied.amount', 3)
- ->where('cart.applied.automatic', true)
- ->where('cart.offer_hint', null)
- ->where('cart.total', 27));
-
- $order = $this->placeOrder();
-
- $this->assertNull($order->discount_code);
- $this->assertSame('Raya Sale', $order->discount_name);
- $this->assertSame(300, $order->discount_total);
- $this->assertSame(2700 + 800, $order->total);
- }
-
- public function test_only_chosen_products_count_and_get_cheaper()
- {
- $this->createOffer(['applies_to' => 'chosen', 'product_ids' => [$this->sambal->id]]);
-
- // A sambal and a kerepek make 2 items, but only the sambal counts.
- $this->add($this->sambal);
- $this->add($this->kerepek);
- $this->cart(fn (Assert $page) => $page->where('cart.applied', null));
-
- $this->add($this->sambal);
-
- // 10% of the two sambal (RM 40), not of the kerepek.
- $this->cart(fn (Assert $page) => $page->where('cart.applied.amount', 4)->where('cart.total', 46));
- }
-
- public function test_a_spend_minimum_works_and_the_cart_says_how_much_more()
- {
- $this->createOffer(['type' => 'fixed', 'value' => '5', 'minimum_quantity' => '', 'minimum_subtotal' => '50']);
-
- $this->add($this->sambal, 2);
- $this->cart(fn (Assert $page) => $page->where('cart.offer_hint', 'Spend RM 10.00 more to get RM 5.00 off (Raya Sale).'));
-
- $this->add($this->kerepek);
- $this->cart(fn (Assert $page) => $page->where('cart.applied.amount', 5)->where('cart.total', 45));
- }
-
- public function test_only_the_bigger_of_a_code_and_an_automatic_discount_applies()
- {
- $this->createOffer();
- $this->post(route('stores.discounts.store', $this->store->slug), ['code' => 'BIG20', 'type' => 'percent', 'value' => 20, 'active' => true]);
- $this->post(route('stores.discounts.store', $this->store->slug), ['code' => 'SMALL', 'type' => 'fixed', 'value' => '1', 'active' => true]);
-
- $this->add($this->sambal, 2);
-
- // RM 1 off is less than 10% (RM 4), so the sale wins and the code is not used.
- $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => 'SMALL']);
- $this->cart(fn (Assert $page) => $page
- ->where('cart.applied.name', 'Raya Sale')
- ->where('cart.applied.amount', 4)
- ->where('cart.discount.code', 'SMALL')
- ->where('cart.discount.message', 'Raya Sale already saves you more, so this code is not used. Only one discount applies per order.'));
-
- // 20% (RM 8) beats the sale.
- $this->delete(route('stores.preview.cart.discount.remove', $this->store->slug));
- $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => 'BIG20']);
- $this->cart(fn (Assert $page) => $page
- ->where('cart.applied.name', 'BIG20')
- ->where('cart.applied.amount', 8)
- ->where('cart.discount.message', null)
- ->where('cart.total', 32));
-
- $order = $this->placeOrder();
- $this->assertSame('BIG20', $order->discount_code);
- $this->assertNull($order->discount_name);
- $this->assertSame(800, $order->discount_total);
- }
-
- public function test_a_code_is_not_counted_as_used_when_the_sale_won()
- {
- $this->createOffer();
- $this->post(route('stores.discounts.store', $this->store->slug), ['code' => 'SMALL', 'type' => 'fixed', 'value' => '1', 'active' => true]);
-
- $this->add($this->sambal, 2);
- $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => 'SMALL']);
-
- $order = $this->placeOrder();
- $this->gateway->pay($order);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page->where('view.order.discount_name', 'Raya Sale'));
-
- $this->assertTrue($order->fresh()->isPaid());
- $this->assertSame(0, DiscountCode::where('code', 'SMALL')->firstOrFail()->times_used);
- }
-
- public function test_switched_off_scheduled_and_ended_ones_do_not_apply()
- {
- $this->createOffer(['active' => '0']);
- $this->createOffer(['name' => 'Later', 'starts_on' => now('Asia/Kuala_Lumpur')->addDays(3)->format('Y-m-d')]);
- $this->createOffer(['name' => 'Over', 'starts_on' => '2020-01-01', 'ends_on' => '2020-01-31']);
-
- $this->add($this->sambal, 3);
-
- $this->cart(fn (Assert $page) => $page->where('cart.applied', null)->where('cart.total', 60));
- $this->assertSame(['off', 'scheduled', 'expired'], AutomaticDiscount::orderBy('id')->get()->map->status()->all());
- }
-
- public function test_product_pages_mention_the_offers_that_apply()
- {
- $this->createOffer(['applies_to' => 'chosen', 'product_ids' => [$this->sambal->id]]);
-
- $this->get(route('stores.preview.product', [$this->store->slug, $this->sambal->slug]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('shop.offers.0.name', 'Raya Sale')
- ->where('shop.offers.0.text', 'Buy 2 or more, get 10% off')
- ->where('shop.offers.0.product_ids', [$this->sambal->id]));
- }
-
- public function test_the_ai_can_propose_one_from_chat()
- {
- ScriptedDriver::use([(string) json_encode([
- 'reply' => 'Please confirm below.',
- 'actions' => [[
- 'type' => 'create_automatic_discount', 'name' => 'Sambal Week', 'discount_type' => 'percent', 'value' => 15,
- 'minimum_quantity' => 3, 'products' => [$this->sambal->slug], 'ends_on' => '2030-01-31',
- ]],
- ])]);
-
- $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Buy 3 sambal get 15% off until end of January']);
- $edit = $this->store->generations()->firstOrFail();
-
- $this->assertSame(
- 'Start the automatic discount "Sambal Week": Buy 3 or more, get 15% off, only on Sambal, until 2030-01-31. No code needed',
- $edit->actions[0]['summary'],
- );
- $this->assertSame(0, AutomaticDiscount::count());
-
- $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertSessionHasNoErrors();
-
- $offer = AutomaticDiscount::firstOrFail();
- $this->assertSame([$this->sambal->id], $offer->product_ids);
- $this->assertSame(3, $offer->minimum_quantity);
- }
- }
|