Keranjang belanja adalah fitur yang paling sering disalahpahami pemula: banyak yang langsung membuat tabel carts padahal keranjang punya sifat yang bertolak belakang dari data database. Keranjang bersifat sementara, boleh hilang saat pengguna menutup browser, dan tidak pernah menjadi laporan. Karena itu kita menaruhnya di tempat yang memang dirancang untuk data seperti ini: session
. Bentuknya sederhana — array asosiatif product_id => qty di session — dan seluruh operasinya kita kumpulkan dalam satu service class agar controller tetap ramping.
CartService sebagai Satu-satunya Pemilik Keranjang #
app/Services/CartService.php menyuntik kontrak session lewat constructor sehingga mudah diuji dan tidak terikat pada facade:
use Illuminate\Contracts\Session\Session;
class CartService
{
private const KEY = 'cart';
public function __construct(private readonly Session $session)
{
}
public function get(): array
{
return $this->session->get(self::KEY, []);
}
public function add(int $productId, int $qty): void
{
$cart = $this->get();
$cart[$productId] = ($cart[$productId] ?? 0) + $qty;
$this->session->put(self::KEY, $cart);
}
}Kunci cart menyimpan array product_id => qty. Method add() sengaja menjumlahkan, bukan menimpa: menambah kopi dua kali harus menghasilkan satu baris dengan jumlah gabungan, persis perilaku keranjang toko nyata. Saudara-saudaranya melengkapi siklus hidupnya — setQuantity() menimpa jumlah dan menghapus baris ketika jumlahnya nol, remove() menghapus satu produk, clear() mengosongkan seluruh keranjang. Method items() adalah jembatan menuju view: method tersebut membaca kunci cart, mengambil produk terkait dengan whereIn, lalu mengembalikan koleksi baris berisi model produk, jumlah, dan subtotal. Method total() dan count() hanya penjumlahan turunan dari data yang sama.
Perilaku service ini bisa kita amati langsung dengan runner kecil yang memakai data seeder:
Tambah Kopi Arabika Gayo 250g x2
Tambah Kopi Arabika Gayo 250g x1 (produk sama)
Tambah Teh Hijau Melati 100g x3
Isi session cart : [1 => 3, 2 => 3]
Total barang : 6
Total harga : Rp330.000
Ubah jumlah kopi menjadi 0 (berarti hapus)
Isi session cart : [2 => 3]
Total barang : 3
Total harga : Rp105.000Enam baris pertama memperlihatkan inti representasinya: dua kali add() untuk kopi menghasilkan 1 => 3, bukan dua baris. Baris terakhir memperlihatkan konsekuensi setQuantity() dengan nol — kopi lenyap dari session dan total dihitung ulang menjadi hanya teh. Representasi yang memilih array asosiatif ini bukan tanpa alasan: kunci array menjamin satu produk paling banyak satu entri, tepat mencerminkan composite unique ['order_id', 'product_id'] yang kita pasang pada order_items di Bagian 1. Keranjang dan tabel item pesanan punya bentuk pikir yang sama, sehingga perubahan keranjang menjadi pesanan tidak perlu meleburkan baris ganda.
Satu pertanyaan wajar muncul: bagaimana bila produk di dalam session tiba-tiba dihapus atau dinonaktifkan admin? Method items() mengambil hanya produk yang masih ada di database, sehingga baris yang menggantung tidak ikut ditampilkan maupun ikut dihitung totalnya. Session memang menyimpan data yang bisa basi, dan kita tidak berpura-pura sebaliknya — pemeriksaan terakhir tetap diserahkan ke momen checkout, tempat setiap produk dibaca ulang dari baris database yang sedang dikunci.
Gerbang Masuk Keranjang #
Form menambah produk divalidasi oleh app/Http/Requests/CartItemRequest.php sebelum menyentuh session:
public function rules(): array
{
return [
'product_id' => [
'required',
'integer',
Rule::exists('products', 'id')->where('is_active', true),
],
'qty' => ['required', 'integer', 'min:1', 'max:99'],
];
}Aturan Rule::exists(...)->where('is_active', true) adalah gerbang ganda yang elegan: produk harus ada dan sedang aktif. Produk nonaktif yang dipaksa masuk lewat request palsu akan ditolak validasi dengan pesan error pada kolom produk, bukan diam-diam tersimpan lalu mengejutkan kita saat checkout.
CartController kemudian tinggal mendelegasikan: store() memanggil add(), update() memanggil setQuantity(), destroy() memanggil remove(). Seluruh route keranjang sengaja berada di luar middleware auth — tamu boleh berbelanja, dan keranjangnya menunggangi session tamu tersebut. Halaman resources/views/cart/index.blade.php menampilkan tabel isi keranjang dengan form jumlah per baris, tombol hapus, serta total di baris ringkasan.
Keranjang kini hidup: bisa diisi, diubah jumlahnya, dan dihitung totalnya. Tetapi seluruh isi itu masih rawan hilang karena memang hidup di session. Bagian berikutnya mengubah keranjang menjadi pesanan permanen — di dalam satu transaksi database yang menolak setengah jadi.
Berkas Sumber
Berkas pendukung halaman ini dapat diunduh dan dijalankan langsung sesuai penjelasan di atas.
app/Exceptions/EmptyCartException.php
Unduh berkas<?php
namespace App\Exceptions;
use RuntimeException;
class EmptyCartException extends RuntimeException
{
public function __construct()
{
parent::__construct('Keranjang belanja kosong.');
}
}
app/Exceptions/InsufficientStockException.php
Unduh berkas<?php
namespace App\Exceptions;
use App\Models\Product;
use RuntimeException;
class InsufficientStockException extends RuntimeException
{
public function __construct(public readonly Product $product, public readonly int $requested)
{
parent::__construct(sprintf(
'Stok %s tidak cukup: tersedia %d, diminta %d.',
$product->name,
$product->stock,
$requested,
));
}
}
app/Exceptions/InvalidStatusTransitionException.php
Unduh berkas<?php
namespace App\Exceptions;
use RuntimeException;
class InvalidStatusTransitionException extends RuntimeException
{
public function __construct(public readonly string $from, public readonly string $to)
{
parent::__construct(sprintf(
'Perubahan status dari "%s" ke "%s" tidak diizinkan.',
$from,
$to,
));
}
}
app/Exceptions/ProductUnavailableException.php
Unduh berkas<?php
namespace App\Exceptions;
use RuntimeException;
class ProductUnavailableException extends RuntimeException
{
public function __construct()
{
parent::__construct(
'Ada produk di keranjang yang sudah tidak tersedia lagi. Hapus produk tersebut lalu coba kembali.',
);
}
}
app/Http/Controllers/AuthController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function showLogin(): View
{
return view('auth.login');
}
public function login(Request $request): RedirectResponse
{
$credentials = $request->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended(route('catalog.index'));
}
return back()
->withErrors(['email' => 'Email atau password salah.'])
->onlyInput('email');
}
public function logout(Request $request): RedirectResponse
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('catalog.index');
}
}
app/Http/Controllers/CartController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Http\Requests\CartItemRequest;
use App\Http\Requests\CartQuantityRequest;
use App\Models\Product;
use App\Services\CartService;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
class CartController extends Controller
{
public function __construct(private readonly CartService $cart)
{
}
public function index(): View
{
return view('cart.index', [
'items' => $this->cart->items(),
'total' => $this->cart->total(),
'count' => $this->cart->count(),
]);
}
/**
* Tambah produk ke keranjang. Berlaku juga untuk tamu:
* keranjang hidup di session, bukan di tabel database.
*/
public function store(CartItemRequest $request): RedirectResponse
{
$this->cart->add((int) $request->integer('product_id'), (int) $request->integer('qty'));
$product = Product::findOrFail($request->integer('product_id'));
return back()
->with('status', "{$product->name} ({$request->integer('qty')} pcs) masuk keranjang.");
}
public function update(CartQuantityRequest $request, Product $product): RedirectResponse
{
$this->cart->setQuantity($product->id, (int) $request->integer('qty'));
return redirect()
->route('cart.index')
->with('status', "Jumlah {$product->name} diperbarui.");
}
public function destroy(Product $product): RedirectResponse
{
$this->cart->remove($product->id);
return redirect()
->route('cart.index')
->with('status', "{$product->name} dihapus dari keranjang.");
}
}
app/Http/Controllers/CatalogController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Contracts\View\View;
class CatalogController extends Controller
{
/**
* Katalog publik: hanya produk aktif, tamu boleh melihat.
*/
public function index(): View
{
$products = Product::query()
->active()
->orderBy('name')
->paginate(8)
->withQueryString();
return view('catalog.index', ['products' => $products]);
}
}
app/Http/Controllers/CheckoutController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Exceptions\EmptyCartException;
use App\Exceptions\InsufficientStockException;
use App\Exceptions\ProductUnavailableException;
use App\Services\CartService;
use App\Services\CheckoutService;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class CheckoutController extends Controller
{
public function __construct(
private readonly CartService $cart,
private readonly CheckoutService $checkout,
) {
}
/**
* Halaman tinjauan sebelum pesanan dibuat.
*/
public function review(): View
{
return view('checkout.review', [
'items' => $this->cart->items(),
'total' => $this->cart->total(),
]);
}
/**
* Jalankan checkout: keranjang berubah menjadi pesanan.
*/
public function store(Request $request): RedirectResponse
{
try {
$order = $this->checkout->checkout($request->user());
} catch (InsufficientStockException $exception) {
return back()->withErrors(['cart' => $exception->getMessage()]);
} catch (EmptyCartException|ProductUnavailableException $exception) {
return back()->withErrors(['cart' => $exception->getMessage()]);
}
return redirect()
->route('orders.show', $order)
->with('status', "Pesanan #{$order->id} berhasil dibuat.");
}
}
app/Http/Controllers/Controller.php
Unduh berkas<?php
namespace App\Http\Controllers;
abstract class Controller
{
//
}
app/Http/Controllers/OrderController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Exceptions\InvalidStatusTransitionException;
use App\Http\Requests\OrderStatusRequest;
use App\Models\Order;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class OrderController extends Controller
{
/**
* Daftar pesanan milik pengguna yang sedang login.
*/
public function index(Request $request): View
{
$orders = $request->user()
->orders()
->latest('id')
->paginate(10);
return view('orders.index', ['orders' => $orders]);
}
/**
* Detail pesanan: item dengan harga snapshot + riwayat status.
*/
public function show(Request $request, Order $order): View
{
abort_unless($order->user_id === $request->user()->id, 403);
$order->load('items.product');
return view('orders.show', [
'order' => $order,
'histories' => $order->histories()->with('user')->oldest('id')->get(),
]);
}
/**
* Pindahkan status pesanan dan catat riwayatnya.
*/
public function updateStatus(OrderStatusRequest $request, Order $order): RedirectResponse
{
abort_unless($order->user_id === $request->user()->id, 403);
try {
$order->transitionTo(
$request->string('status')->toString(),
$request->user(),
$request->string('note')->toString() ?: null,
);
} catch (InvalidStatusTransitionException $exception) {
return back()->withErrors(['status' => $exception->getMessage()]);
}
return redirect()
->route('orders.show', $order)
->with('status', 'Status pesanan menjadi ' . $order->status . '.');
}
}
app/Http/Controllers/ProductController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProductRequest;
use App\Models\Product;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Daftar produk untuk admin: termasuk yang nonaktif.
*/
public function index(Request $request): View
{
$products = Product::query()
->orderBy('name')
->paginate(10);
return view('products.index', ['products' => $products]);
}
public function create(): View
{
return view('products.create', ['product' => new Product()]);
}
public function store(ProductRequest $request): RedirectResponse
{
$product = Product::create($request->validated());
return redirect()
->route('products.show', $product)
->with('status', "Produk {$product->name} berhasil disimpan.");
}
public function show(Product $product): View
{
return view('products.show', ['product' => $product]);
}
public function edit(Product $product): View
{
return view('products.edit', ['product' => $product]);
}
public function update(ProductRequest $request, Product $product): RedirectResponse
{
$product->update($request->validated());
return redirect()
->route('products.show', $product)
->with('status', "Produk {$product->name} berhasil diperbarui.");
}
public function destroy(Product $product): RedirectResponse
{
$name = $product->name;
$product->delete();
return redirect()
->route('products.index')
->with('status', "Produk {$name} berhasil dihapus.");
}
}
app/Http/Requests/CartItemRequest.php
Unduh berkas<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class CartItemRequest extends FormRequest
{
public function rules(): array
{
return [
'product_id' => [
'required',
'integer',
// Hanya produk aktif yang boleh masuk keranjang.
Rule::exists('products', 'id')->where('is_active', true),
],
'qty' => ['required', 'integer', 'min:1', 'max:99'],
];
}
public function attributes(): array
{
return [
'product_id' => 'produk',
'qty' => 'jumlah',
];
}
}
app/Http/Requests/CartQuantityRequest.php
Unduh berkas<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CartQuantityRequest extends FormRequest
{
public function rules(): array
{
return [
// Nol diizinkan: artinya hapus produk dari keranjang.
'qty' => ['required', 'integer', 'min:0', 'max:99'],
];
}
public function attributes(): array
{
return [
'qty' => 'jumlah',
];
}
}
app/Http/Requests/OrderStatusRequest.php
Unduh berkas<?php
namespace App\Http\Requests;
use App\Models\Order;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class OrderStatusRequest extends FormRequest
{
public function rules(): array
{
return [
'status' => ['required', 'string', Rule::in(Order::STATUSES)],
'note' => ['nullable', 'string', 'max:255'],
];
}
public function attributes(): array
{
return [
'status' => 'status',
'note' => 'catatan',
];
}
}
app/Http/Requests/ProductRequest.php
Unduh berkas<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProductRequest extends FormRequest
{
/**
* Checkbox yang tidak dicentang tidak mengirim nilai apa pun,
* jadi lengkapi dulu dengan boolean() sebelum divalidasi.
*/
protected function prepareForValidation(): void
{
$this->merge(['is_active' => $this->boolean('is_active')]);
}
/**
* Aturan yang sama dipakai untuk menyimpan dan mengubah produk.
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'description' => ['nullable', 'string', 'max:1000'],
'price' => ['required', 'integer', 'min:0'],
'stock' => ['required', 'integer', 'min:0'],
'is_active' => ['required', 'boolean'],
];
}
public function attributes(): array
{
return [
'name' => 'nama produk',
'description' => 'deskripsi',
'price' => 'harga',
'stock' => 'stok',
'is_active' => 'status aktif',
];
}
}
app/Models/Order.php
Unduh berkas<?php
namespace App\Models;
use App\Exceptions\InvalidStatusTransitionException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\DB;
class Order extends Model
{
public const STATUS_PENDING = 'pending';
public const STATUS_PAID = 'paid';
public const STATUS_SHIPPED = 'shipped';
public const STATUS_COMPLETED = 'completed';
public const STATUS_CANCELLED = 'cancelled';
public const STATUSES = [
self::STATUS_PENDING,
self::STATUS_PAID,
self::STATUS_SHIPPED,
self::STATUS_COMPLETED,
self::STATUS_CANCELLED,
];
/**
* Peta transisi status yang sah. Kuncinya adalah status saat ini,
* nilainya daftar status yang boleh dituju.
*/
public const TRANSITIONS = [
self::STATUS_PENDING => [self::STATUS_PAID, self::STATUS_CANCELLED],
self::STATUS_PAID => [self::STATUS_SHIPPED, self::STATUS_CANCELLED],
self::STATUS_SHIPPED => [self::STATUS_COMPLETED],
self::STATUS_COMPLETED => [],
self::STATUS_CANCELLED => [],
];
protected $fillable = [
'user_id',
'total',
'status',
];
protected function casts(): array
{
return [
'total' => 'integer',
'status' => 'string',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function items(): HasMany
{
return $this->hasMany(OrderItem::class);
}
public function histories(): HasMany
{
return $this->hasMany(OrderStatusHistory::class);
}
/**
* Daftar status yang boleh dituju dari status saat ini.
*
* @return list<string>
*/
public function allowedTransitions(): array
{
return self::TRANSITIONS[$this->status] ?? [];
}
public function canTransitionTo(string $status): bool
{
return in_array($status, $this->allowedTransitions(), true);
}
/**
* Pindahkan status pesanan dan catat riwayatnya secara atomik.
*/
public function transitionTo(string $status, ?User $user = null, ?string $note = null): void
{
DB::transaction(function () use ($status, $user, $note): void {
// Baca ulang dan kunci baris pesanan agar dua request tidak
// memindahkan status yang sama secara bersamaan.
$locked = Order::query()
->whereKey($this->id)
->lockForUpdate()
->firstOrFail();
if (! $locked->canTransitionTo($status)) {
throw new InvalidStatusTransitionException($locked->status, $status);
}
$from = $locked->status;
$locked->status = $status;
$locked->save();
$locked->histories()->create([
'from' => $from,
'to' => $status,
'user_id' => $user?->id,
'note' => $note,
]);
});
$this->refresh();
}
}
app/Models/OrderItem.php
Unduh berkas<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class OrderItem extends Model
{
protected $fillable = [
'order_id',
'product_id',
'qty',
'price',
];
protected function casts(): array
{
return [
'qty' => 'integer',
'price' => 'integer',
];
}
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
/**
* Subtotal satu baris: harga snapshot dikali jumlah.
*/
public function getSubtotalAttribute(): int
{
return $this->price * $this->qty;
}
}
app/Models/OrderStatusHistory.php
Unduh berkas<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class OrderStatusHistory extends Model
{
protected $fillable = [
'order_id',
'from',
'to',
'user_id',
'note',
];
protected function casts(): array
{
return [
'from' => 'string',
'to' => 'string',
];
}
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
app/Models/Product.php
Unduh berkas<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Product extends Model
{
protected $fillable = [
'name',
'description',
'price',
'stock',
'is_active',
];
protected function casts(): array
{
return [
'price' => 'integer',
'stock' => 'integer',
'is_active' => 'boolean',
];
}
public function orderItems(): HasMany
{
return $this->hasMany(OrderItem::class);
}
/**
* Produk aktif yang boleh tampil di katalog dan dimasukkan
* ke keranjang.
*/
public function scopeActive(Builder $query): Builder
{
return $query->where('is_active', true);
}
}
app/Models/User.php
Unduh berkas<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
#[Fillable(['name', 'email', 'password'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
}
app/Services/CartService.php
Unduh berkas<?php
namespace App\Services;
use App\Models\Product;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Collection;
/**
* Keranjang belanja berbasis session.
*
* Session menyimpan pasangan product_id => qty, tanpa tabel khusus:
* keranjang bersifat sementara, per pengguna, dan boleh hilang saat
* session berakhir. Hanya pesanan hasil checkout yang permanen.
*/
class CartService
{
private const KEY = 'cart';
public function __construct(private readonly Session $session)
{
}
/**
* Isi keranjang: [product_id => qty].
*
* @return array<int, int>
*/
public function get(): array
{
return $this->session->get(self::KEY, []);
}
public function isEmpty(): bool
{
return $this->get() === [];
}
/**
* Tambah jumlah produk; produk yang sama digabung.
*/
public function add(int $productId, int $qty): void
{
$cart = $this->get();
$cart[$productId] = ($cart[$productId] ?? 0) + $qty;
$this->session->put(self::KEY, $cart);
}
/**
* Set jumlah produk; jumlah nol berarti hapus dari keranjang.
*/
public function setQuantity(int $productId, int $qty): void
{
$cart = $this->get();
if ($qty <= 0) {
unset($cart[$productId]);
} else {
$cart[$productId] = $qty;
}
$this->session->put(self::KEY, $cart);
}
public function remove(int $productId): void
{
$cart = $this->get();
unset($cart[$productId]);
$this->session->put(self::KEY, $cart);
}
public function clear(): void
{
$this->session->forget(self::KEY);
}
/**
* Baris keranjang yang siap ditampilkan, selalu urut product_id.
*
* @return Collection<int, array{product: Product, qty: int, subtotal: int}>
*/
public function items(): Collection
{
$cart = $this->get();
if ($cart === []) {
return collect();
}
$products = Product::query()
->whereIn('id', array_keys($cart))
->orderBy('id')
->get()
->keyBy('id');
return $products
->map(fn (Product $product): array => [
'product' => $product,
'qty' => $cart[$product->id],
'subtotal' => $product->price * $cart[$product->id],
])
->values();
}
/**
* Total harga seluruh keranjang berdasarkan harga saat ini.
*/
public function total(): int
{
return $this->items()->sum('subtotal');
}
/**
* Total jumlah barang (bukan jumlah jenis produk).
*/
public function count(): int
{
return array_sum($this->get());
}
}
app/Services/CheckoutService.php
Unduh berkas<?php
namespace App\Services;
use App\Exceptions\EmptyCartException;
use App\Exceptions\InsufficientStockException;
use App\Exceptions\ProductUnavailableException;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\User;
use Illuminate\Support\Facades\DB;
/**
* Mengubah keranjang session menjadi pesanan permanen.
*
* Seluruh pekerjaan berat berjalan di dalam DB::transaction():
* kunci baris produk, periksa ulang stok, kurangi stok, buat
* pesanan beserta itemnya (harga snapshot), lalu tulis riwayat
* status pertama. Keranjang hanya dibersihkan setelah commit.
*/
class CheckoutService
{
public function __construct(private readonly CartService $cart)
{
}
public function checkout(User $user): Order
{
if ($this->cart->isEmpty()) {
throw new EmptyCartException();
}
$cart = $this->cart->get();
$order = DB::transaction(function () use ($user, $cart): Order {
// Kunci semua baris produk sekaligus dengan urutan id yang
// tetap, agar dua checkout bersamaan tidak saling mengunci
// dalam urutan berbeda (mencegah deadlock).
$products = Product::query()
->whereIn('id', array_keys($cart))
->orderBy('id')
->lockForUpdate()
->get()
->keyBy('id');
// Periksa ulang ketersediaan dan stok dari baris yang dikunci.
foreach ($cart as $productId => $qty) {
$product = $products->get($productId);
if ($product === null || $product->is_active === false) {
throw new ProductUnavailableException();
}
if ($product->stock < $qty) {
throw new InsufficientStockException($product, $qty);
}
}
// Hitung total dan siapkan item dengan harga snapshot:
// harga yang dibaca saat checkout, bukan saat masuk keranjang.
$total = 0;
$items = [];
foreach ($cart as $productId => $qty) {
$product = $products->get($productId);
$total += $product->price * $qty;
$items[] = [
'product_id' => $product->id,
'qty' => $qty,
'price' => $product->price,
];
}
$order = $user->orders()->create([
'total' => $total,
'status' => Order::STATUS_PENDING,
]);
foreach ($items as $item) {
$order->items()->create($item);
}
foreach ($products as $product) {
$product->stock -= $cart[$product->id];
$product->save();
}
$order->histories()->create([
'from' => null,
'to' => Order::STATUS_PENDING,
'user_id' => $user->id,
'note' => 'Pesanan dibuat dari keranjang.',
]);
return $order;
});
// Keranjang hanya dibersihkan setelah transaksi berhasil di-commit.
$this->cart->clear();
return $order;
}
}
database/migrations/2026_10_13_234020_create_products_table.php
Unduh berkas<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->text('description')->nullable();
$table->unsignedInteger('price');
$table->unsignedInteger('stock')->default(0);
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('products');
}
};
database/migrations/2026_10_13_234021_create_orders_table.php
Unduh berkas<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained()
->cascadeOnDelete();
$table->unsignedBigInteger('total')->default(0);
$table->enum('status', ['pending', 'paid', 'shipped', 'completed', 'cancelled'])
->default('pending');
$table->timestamps();
$table->index(['user_id', 'id']);
});
}
public function down(): void
{
Schema::dropIfExists('orders');
}
};
database/migrations/2026_10_13_234022_create_order_items_table.php
Unduh berkas<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('order_items', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')
->constrained()
->cascadeOnDelete();
$table->foreignId('product_id')
->constrained()
->restrictOnDelete();
$table->unsignedInteger('qty');
$table->unsignedInteger('price');
$table->timestamps();
// Satu pesanan paling banyak satu baris per produk,
// karena keranjang disimpan sebagai pasangan produk => jumlah.
$table->unique(['order_id', 'product_id']);
});
}
public function down(): void
{
Schema::dropIfExists('order_items');
}
};
database/migrations/2026_10_13_234023_create_order_status_histories_table.php
Unduh berkas<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('order_status_histories', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id')
->constrained()
->cascadeOnDelete();
// NULL berarti pesanan baru dibuat (transisi pertama menuju pending).
$table->enum('from', ['pending', 'paid', 'shipped', 'completed', 'cancelled'])
->nullable();
$table->enum('to', ['pending', 'paid', 'shipped', 'completed', 'cancelled']);
$table->foreignId('user_id')
->nullable()
->constrained()
->nullOnDelete();
$table->string('note')->nullable();
$table->timestamps();
$table->index(['order_id', 'id']);
});
}
public function down(): void
{
Schema::dropIfExists('order_status_histories');
}
};
database/seeders/DatabaseSeeder.php
Unduh berkas<?php
namespace Database\Seeders;
use App\Models\Product;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder
{
public function run(): void
{
User::create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
'password' => Hash::make('password'),
]);
User::create([
'name' => 'Siti Rahayu',
'email' => '[email protected]',
'password' => Hash::make('password'),
]);
$products = [
[
'name' => 'Kopi Arabika Gayo 250g',
'description' => 'Biji kopi single origin dari Gayo, roasting medium.',
'price' => 75000,
'stock' => 25,
'is_active' => true,
],
[
'name' => 'Teh Hijau Melati 100g',
'description' => 'Teh hijau wangi melati dalam kemasan tin.',
'price' => 35000,
'stock' => 40,
'is_active' => true,
],
[
'name' => 'Gula Aren Cair 500ml',
'description' => 'Pemanis alami dari aren, cocok untuk kopi dan teh.',
'price' => 28000,
'stock' => 15,
'is_active' => true,
],
[
'name' => 'Madu Hutan 250ml',
'description' => 'Madu multiflora tanpa campuran gula.',
'price' => 95000,
'stock' => 10,
'is_active' => true,
],
[
'name' => 'Beras Merah 1kg',
'description' => null,
'price' => 22000,
'stock' => 30,
'is_active' => true,
],
[
'name' => 'Keripik Singkong Balado',
'description' => 'Sedang tidak dijual: menunggu kemasan baru.',
'price' => 18000,
'stock' => 50,
'is_active' => false,
],
];
foreach ($products as $product) {
Product::create($product);
}
}
}
routes/web.php
Unduh berkas<?php
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CatalogController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route;
Route::get('/', [CatalogController::class, 'index'])->name('catalog.index');
Route::middleware('guest')->group(function () {
Route::get('login', [AuthController::class, 'showLogin'])->name('login');
Route::post('login', [AuthController::class, 'login'])->name('login.attempt');
});
Route::post('logout', [AuthController::class, 'logout'])
->middleware('auth')
->name('logout');
// Keranjang hidup di session, jadi tamu pun boleh menggunakannya.
Route::get('cart', [CartController::class, 'index'])->name('cart.index');
Route::post('cart', [CartController::class, 'store'])->name('cart.store');
Route::put('cart/{product}', [CartController::class, 'update'])->name('cart.update');
Route::delete('cart/{product}', [CartController::class, 'destroy'])->name('cart.destroy');
Route::middleware('auth')->group(function () {
Route::get('checkout', [CheckoutController::class, 'review'])->name('checkout.review');
Route::post('checkout', [CheckoutController::class, 'store'])->name('checkout.store');
Route::get('orders', [OrderController::class, 'index'])->name('orders.index');
Route::get('orders/{order}', [OrderController::class, 'show'])->name('orders.show');
Route::post('orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.status');
Route::resource('products', ProductController::class);
});
tests/Feature/AuthTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthTest extends TestCase
{
use RefreshDatabase;
public function test_tamu_diarahkan_ke_halaman_login(): void
{
$this->get(route('products.index'))
->assertRedirect(route('login'));
}
public function test_login_berhasil_dan_gagal(): void
{
User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
'password' => 'password',
]);
$this->from(route('login'))
->post(route('login.attempt'), [
'email' => '[email protected]',
'password' => 'salah',
])
->assertSessionHasErrors('email');
$this->from(route('login'))
->post(route('login.attempt'), [
'email' => '[email protected]',
'password' => 'password',
])
->assertRedirect(route('catalog.index'));
$this->assertAuthenticated();
}
public function test_logout_mengakhiri_sesi(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
$this->actingAs($user)
->post(route('logout'))
->assertRedirect(route('catalog.index'));
$this->assertGuest();
}
}
tests/Feature/CartTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\Product;
use App\Models\User;
use App\Services\CartService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CartTest extends TestCase
{
use RefreshDatabase;
private function user(): User
{
return User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
}
private function product(array $overrides = []): Product
{
return Product::create([
'name' => 'Kopi Arabika Gayo 250g',
'description' => 'Biji kopi single origin dari Gayo, roasting medium.',
'price' => 75000,
'stock' => 25,
'is_active' => true,
...$overrides,
]);
}
public function test_tamu_bisa_menambah_produk_ke_keranjang(): void
{
$product = $this->product();
$this->from(route('catalog.index'))
->post(route('cart.store'), [
'product_id' => $product->id,
'qty' => 2,
])
->assertRedirect(route('catalog.index'));
$this->assertSame([$product->id => 2], app(CartService::class)->get());
}
public function test_menambah_produk_sama_menggabungkan_jumlah(): void
{
$product = $this->product();
$cart = app(CartService::class);
$cart->add($product->id, 2);
$cart->add($product->id, 3);
$this->assertSame([$product->id => 5], $cart->get());
$this->assertSame(5, $cart->count());
}
public function test_jumlah_produk_bisa_diperbarui(): void
{
$user = $this->user();
$product = $this->product();
app(CartService::class)->add($product->id, 2);
$this->actingAs($user)
->from(route('cart.index'))
->put(route('cart.update', $product), ['qty' => 4])
->assertRedirect(route('cart.index'));
$this->assertSame([$product->id => 4], app(CartService::class)->get());
}
public function test_update_jumlah_nol_menghapus_produk(): void
{
$user = $this->user();
$product = $this->product();
app(CartService::class)->add($product->id, 2);
$this->actingAs($user)
->put(route('cart.update', $product), ['qty' => 0]);
$this->assertSame([], app(CartService::class)->get());
}
public function test_produk_bisa_dihapus_dari_keranjang(): void
{
$user = $this->user();
$product = $this->product();
app(CartService::class)->add($product->id, 2);
$this->actingAs($user)
->delete(route('cart.destroy', $product))
->assertRedirect(route('cart.index'));
$this->assertSame([], app(CartService::class)->get());
}
public function test_produk_nonaktif_ditolak_masuk_keranjang(): void
{
$product = $this->product(['is_active' => false]);
$this->from(route('catalog.index'))
->post(route('cart.store'), [
'product_id' => $product->id,
'qty' => 1,
])
->assertSessionHasErrors('product_id');
$this->assertSame([], app(CartService::class)->get());
}
public function test_validasi_form_keranjang_ditolak(): void
{
$product = $this->product();
$this->from(route('catalog.index'))
->post(route('cart.store'), [
'product_id' => $product->id,
'qty' => 0,
])
->assertSessionHasErrors('qty');
$this->from(route('catalog.index'))
->post(route('cart.store'), [
'product_id' => 99999,
'qty' => 1,
])
->assertSessionHasErrors('product_id');
$this->assertSame([], app(CartService::class)->get());
}
public function test_halaman_keranjang_menampilkan_isi_dan_total(): void
{
$user = $this->user();
$kopi = $this->product();
$teh = $this->product([
'name' => 'Teh Hijau Melati 100g',
'price' => 35000,
]);
app(CartService::class)->add($kopi->id, 2); // 150000
app(CartService::class)->add($teh->id, 1); // 35000
$this->actingAs($user)
->get(route('cart.index'))
->assertOk()
->assertSee('Kopi Arabika Gayo 250g')
->assertSee('Teh Hijau Melati 100g')
->assertSee('Rp185.000')
->assertSee('3 barang');
}
}
tests/Feature/CheckoutTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Exceptions\InsufficientStockException;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\OrderStatusHistory;
use App\Models\Product;
use App\Models\User;
use App\Services\CartService;
use App\Services\CheckoutService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use RuntimeException;
use Tests\TestCase;
class CheckoutTest extends TestCase
{
use RefreshDatabase;
private function user(): User
{
return User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
}
private function product(array $overrides = []): Product
{
return Product::create([
'name' => 'Kopi Arabika Gayo 250g',
'description' => 'Biji kopi single origin dari Gayo, roasting medium.',
'price' => 75000,
'stock' => 25,
'is_active' => true,
...$overrides,
]);
}
public function test_checkout_membuat_pesanan_dengan_item_dan_snapshot(): void
{
$user = $this->user();
$kopi = $this->product();
$teh = $this->product([
'name' => 'Teh Hijau Melati 100g',
'price' => 35000,
'stock' => 40,
]);
$cart = app(CartService::class);
$cart->add($kopi->id, 2); // 150000
$cart->add($teh->id, 1); // 35000
$order = app(CheckoutService::class)->checkout($user);
$this->assertSame(185000, $order->total);
$this->assertSame(Order::STATUS_PENDING, $order->status);
$this->assertCount(2, $order->items);
// Stok berkurang tepat sesuai jumlah yang dipesan.
$this->assertSame(23, $kopi->fresh()->stock);
$this->assertSame(39, $teh->fresh()->stock);
// Riwayat pertama tercatat: dari NULL menuju pending.
$this->assertDatabaseHas('order_status_histories', [
'order_id' => $order->id,
'from' => null,
'to' => 'pending',
'user_id' => $user->id,
]);
// Keranjang dibersihkan setelah commit.
$this->assertSame([], $cart->get());
}
public function test_harga_snapshot_dibaca_saat_checkout_bukan_saat_masuk_keranjang(): void
{
$user = $this->user();
$kopi = $this->product();
app(CartService::class)->add($kopi->id, 2);
// Admin menaikkan harga setelah produk masuk keranjang.
$kopi->forceFill(['price' => 80000])->save();
$order = app(CheckoutService::class)->checkout($user);
$item = $order->items->first();
$this->assertSame(80000, $item->price);
$this->assertSame(160000, $order->total);
}
public function test_checkout_via_http_mengalihkan_ke_detail_pesanan(): void
{
$user = $this->user();
$kopi = $this->product();
app(CartService::class)->add($kopi->id, 2);
$this->actingAs($user)
->from(route('checkout.review'))
->post(route('checkout.store'))
->assertRedirect(route('orders.show', 1));
$this->assertDatabaseHas('orders', [
'user_id' => $user->id,
'total' => 150000,
'status' => 'pending',
]);
$this->assertDatabaseHas('order_items', [
'product_id' => $kopi->id,
'qty' => 2,
'price' => 75000,
]);
$this->assertSame(23, $kopi->fresh()->stock);
}
public function test_checkout_keranjang_kosong_ditolak(): void
{
$user = $this->user();
$this->actingAs($user)
->from(route('checkout.review'))
->post(route('checkout.store'))
->assertSessionHasErrors('cart');
$this->assertDatabaseCount('orders', 0);
}
public function test_stok_tidak_cukup_ditolak_dan_pesanan_tidak_terbentuk(): void
{
$user = $this->user();
$kopi = $this->product(['stock' => 3]);
app(CartService::class)->add($kopi->id, 5);
$this->actingAs($user)
->from(route('checkout.review'))
->post(route('checkout.store'))
->assertSessionHasErrors('cart');
// Tidak ada pesanan, stok utuh, keranjang tetap tersimpan.
$this->assertDatabaseCount('orders', 0);
$this->assertSame(3, $kopi->fresh()->stock);
$this->assertSame([$kopi->id => 5], app(CartService::class)->get());
}
public function test_produk_dinonaktifkan_setelah_masuk_keranjang_ditolak(): void
{
$user = $this->user();
$kopi = $this->product();
app(CartService::class)->add($kopi->id, 1);
$kopi->forceFill(['is_active' => false])->save();
$this->actingAs($user)
->from(route('checkout.review'))
->post(route('checkout.store'))
->assertSessionHasErrors('cart');
$this->assertDatabaseCount('orders', 0);
$this->assertSame(25, $kopi->fresh()->stock);
}
public function test_kegagalan_di_tengah_transaksi_dibatalkan_seluruhnya(): void
{
$user = $this->user();
$kopi = $this->product();
app(CartService::class)->add($kopi->id, 4);
// Simulasikan gangguan tepat saat baris order_items di-insert.
OrderItem::creating(function (): void {
throw new RuntimeException('simulasi gangguan saat menyimpan item');
});
try {
app(CheckoutService::class)->checkout($user);
$this->fail('Pengecualian simulasi seharusnya terlempar.');
} catch (RuntimeException) {
// dibiarkan: inilah skenario yang diuji
} finally {
OrderItem::flushEventListeners();
}
// Atomicity: tidak ada pesanan, stok utuh, keranjang masih ada.
$this->assertDatabaseCount('orders', 0);
$this->assertDatabaseCount('order_items', 0);
$this->assertDatabaseCount('order_status_histories', 0);
$this->assertSame(25, $kopi->fresh()->stock);
$this->assertSame([$kopi->id => 4], app(CartService::class)->get());
}
public function test_tamu_diarahkan_ke_halaman_login_sebelum_checkout(): void
{
$this->get(route('checkout.review'))
->assertRedirect(route('login'));
}
}
tests/Feature/ExampleTest.php
Unduh berkas<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
/**
* Halaman depan adalah katalog publik: seluruh produk aktif
* tampil tanpa perlu login.
*/
public function test_halaman_depan_menampilkan_katalog(): void
{
$this->get('/')
->assertOk()
->assertSee('Belum ada produk aktif.');
}
}
tests/Feature/OrderStatusTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Exceptions\InvalidStatusTransitionException;
use App\Models\Order;
use App\Models\Product;
use App\Models\User;
use App\Services\CartService;
use App\Services\CheckoutService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class OrderStatusTest extends TestCase
{
use RefreshDatabase;
private function user(): User
{
return User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
}
private function product(): Product
{
return Product::create([
'name' => 'Kopi Arabika Gayo 250g',
'description' => 'Biji kopi single origin dari Gayo, roasting medium.',
'price' => 75000,
'stock' => 25,
'is_active' => true,
]);
}
private function order(?User $user = null): Order
{
$user ??= $this->user();
app(CartService::class)->add($this->product()->id, 2);
return app(CheckoutService::class)->checkout($user);
}
public function test_pesanan_baru_berstatus_pending_dengan_riwayat_awal(): void
{
$order = $this->order();
$this->assertSame('pending', $order->status);
$this->assertSame(1, $order->histories()->count());
$history = $order->histories()->first();
$this->assertNull($history->from);
$this->assertSame('pending', $history->to);
}
public function test_transaksi_status_sah_tercatat_berurutan(): void
{
$user = $this->user();
$order = $this->order($user);
$order->transitionTo(Order::STATUS_PAID, $user, 'Pembayaran diterima via transfer.');
$order->transitionTo(Order::STATUS_SHIPPED, $user, 'Dikirim dengan kurir reguler.');
$order->transitionTo(Order::STATUS_COMPLETED, $user);
$this->assertSame('completed', $order->status);
$this->assertDatabaseHas('order_status_histories', [
'order_id' => $order->id,
'from' => 'pending',
'to' => 'paid',
'user_id' => $user->id,
'note' => 'Pembayaran diterima via transfer.',
]);
$this->assertDatabaseHas('order_status_histories', [
'order_id' => $order->id,
'from' => 'paid',
'to' => 'shipped',
]);
// Riwayat bertambah: 1 awal + 3 transisi.
$this->assertSame(4, $order->histories()->count());
}
public function test_transaksi_tidak_sah_ditolak(): void
{
$order = $this->order();
// Pesanan belum dibayar tidak boleh langsung dikirim.
try {
$order->transitionTo(Order::STATUS_SHIPPED);
$this->fail('Transisi pending ke seharusnya melempar pengecualian.');
} catch (InvalidStatusTransitionException $exception) {
$this->assertSame('pending', $exception->from);
$this->assertSame('shipped', $exception->to);
}
$this->assertSame('pending', $order->fresh()->status);
$this->assertSame(1, $order->histories()->count());
}
public function test_status_final_tidak_bisa_berubah(): void
{
$order = $this->order();
$order->transitionTo(Order::STATUS_CANCELLED);
$this->assertSame([], $order->fresh()->allowedTransitions());
$this->expectException(InvalidStatusTransitionException::class);
$order->transitionTo(Order::STATUS_PAID);
}
public function test_form_transaksi_tidak_sah_ditolak(): void
{
$user = $this->user();
$order = $this->order($user);
$this->actingAs($user)
->from(route('orders.show', $order))
->post(route('orders.status', $order), [
'status' => 'shipped',
'note' => 'Isi tidak sah dari pengguna nakal.',
])
->assertSessionHasErrors('status');
$this->assertSame('pending', $order->fresh()->status);
$this->assertSame(1, $order->histories()->count());
}
public function test_form_transaksi_sah_menyimpan_dan_mengalihkan(): void
{
$user = $this->user();
$order = $this->order($user);
$this->actingAs($user)
->from(route('orders.show', $order))
->post(route('orders.status', $order), [
'status' => 'paid',
'note' => 'Pembayaran diterima via transfer.',
])
->assertRedirect(route('orders.show', $order));
$this->assertSame('paid', $order->fresh()->status);
$this->assertSame(2, $order->histories()->count());
}
public function test_status_tidak_dikenal_ditolak_validasi(): void
{
$user = $this->user();
$order = $this->order($user);
$this->actingAs($user)
->post(route('orders.status', $order), [
'status' => 'dikirim',
])
->assertSessionHasErrors('status');
$this->assertSame('pending', $order->fresh()->status);
}
public function test_pesanan_orang_lain_tidak_bisa_dilihat(): void
{
$owner = $this->user();
$order = $this->order($owner);
$intruder = User::factory()->create([
'name' => 'Siti Rahayu',
'email' => '[email protected]',
]);
$this->actingAs($intruder)
->get(route('orders.show', $order))
->assertForbidden();
}
public function test_halaman_detail_menampilkan_item_dan_riwayat(): void
{
$user = $this->user();
$order = $this->order($user);
$order->transitionTo(Order::STATUS_PAID, $user, 'Pembayaran diterima via transfer.');
$this->actingAs($user)
->get(route('orders.show', $order))
->assertOk()
->assertSee('Kopi Arabika Gayo 250g')
->assertSee('Rp75.000')
->assertSee('Pembayaran diterima via transfer.')
->assertSee('pending')
->assertSee('paid');
}
}
tests/Feature/ProductTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\Product;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProductTest extends TestCase
{
use RefreshDatabase;
private function user(): User
{
return User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
}
private function product(array $overrides = []): Product
{
return Product::create([
'name' => 'Kopi Arabika Gayo 250g',
'description' => 'Biji kopi single origin dari Gayo, roasting medium.',
'price' => 75000,
'stock' => 25,
'is_active' => true,
...$overrides,
]);
}
public function test_katalog_hanya_menampilkan_produk_aktif(): void
{
$this->product();
$this->product([
'name' => 'Keripik Singkong Balado',
'is_active' => false,
]);
$this->get(route('catalog.index'))
->assertOk()
->assertSee('Kopi Arabika Gayo 250g')
->assertDontSee('Keripik Singkong Balado');
}
public function test_admin_menyimpan_produk_baru(): void
{
$user = $this->user();
$this->actingAs($user)
->from(route('products.create'))
->post(route('products.store'), [
'name' => 'Madu Hutan 250ml',
'description' => 'Madu multiflora tanpa campuran gula.',
'price' => 95000,
'stock' => 10,
'is_active' => '1',
])
->assertRedirect(route('products.show', 1));
$this->assertDatabaseHas('products', [
'name' => 'Madu Hutan 250ml',
'price' => 95000,
'stock' => 10,
'is_active' => true,
]);
}
public function test_checkbox_tidak_dicentang_menyimpan_produk_nonaktif(): void
{
$user = $this->user();
$this->actingAs($user)
->post(route('products.store'), [
'name' => 'Keripik Singkong Balado',
'description' => null,
'price' => 18000,
'stock' => 50,
]);
$this->assertDatabaseHas('products', [
'name' => 'Keripik Singkong Balado',
'is_active' => false,
]);
}
public function test_validasi_harga_dan_stok_ditolak(): void
{
$user = $this->user();
$this->actingAs($user)
->from(route('products.create'))
->post(route('products.store'), [
'name' => 'Gula Aren Cair 500ml',
'description' => null,
'price' => -1000,
'stock' => -5,
])
->assertSessionHasErrors(['price', 'stock']);
$this->assertDatabaseCount('products', 0);
}
public function test_admin_memperbarui_dan_menghapus_produk(): void
{
$user = $this->user();
$product = $this->product();
$this->actingAs($user)
->from(route('products.edit', $product))
->put(route('products.update', $product), [
'name' => 'Kopi Arabika Toraja 250g',
'description' => 'Biji kopi single origin dari Toraja.',
'price' => 78000,
'stock' => 20,
'is_active' => '1',
])
->assertRedirect(route('products.show', $product));
$this->assertSame('Kopi Arabika Toraja 250g', $product->fresh()->name);
$this->actingAs($user)
->delete(route('products.destroy', $product))
->assertRedirect(route('products.index'));
$this->assertDatabaseCount('products', 0);
}
public function test_daftar_produk_admin_terpaginasi_sepuluh_per_halaman(): void
{
$user = $this->user();
foreach (range(1, 11) as $i) {
$this->product(['name' => "Produk Contoh {$i}"]);
}
$response = $this->actingAs($user)->get(route('products.index'));
$response->assertOk();
$this->assertCount(10, $response->viewData('products')->items());
}
}