From 9f58877b89002689e5c3133d0d5f35994144b0cd Mon Sep 17 00:00:00 2001 From: MilesChou Date: Mon, 10 Jul 2023 10:54:41 +0800 Subject: [PATCH] Add Cart and Order test --- app/Http/Controllers/User/Order.php | 19 +++++-- app/Http/Requests/User/OrderRequest.php | 25 +++++++++ routes/web.php | 4 +- .../Http/Controllers/User/OrderTest.php | 51 +++++++++++++++++++ 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 app/Http/Requests/User/OrderRequest.php create mode 100644 tests/Feature/Http/Controllers/User/OrderTest.php diff --git a/app/Http/Controllers/User/Order.php b/app/Http/Controllers/User/Order.php index 5a6a8465..7989806a 100644 --- a/app/Http/Controllers/User/Order.php +++ b/app/Http/Controllers/User/Order.php @@ -2,22 +2,31 @@ namespace App\Http\Controllers\User; +use App\Http\Requests\User\OrderRequest; use App\Models\Cart; +use App\Models\Order as OrderModel; +use App\Models\User; use Carbon\Carbon; use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Redirect; use RealRashid\SweetAlert\Facades\Alert; class Order { - public function store(Request $request): RedirectResponse + public function __invoke(OrderRequest $request, Cart $cartModel): RedirectResponse { - $cart = Cart::findOrFail($request->cart_id)->with('product')->first(); + /** @var Cart $cart */ + $cart = $cartModel->newQuery() + ->findOrFail($request->cart_id) + ->with('product') + ->first(); - $order = new \App\Models\Order(); - $order->user_id = Auth::user()->getAuthIdentifier(); + /** @var User $user */ + $user = Auth::user(); + + $order = new OrderModel(); + $order->user_id = $user->getAuthIdentifier(); $order->product_id = $cart->product->id; $order->quantity = $request->quantity; diff --git a/app/Http/Requests/User/OrderRequest.php b/app/Http/Requests/User/OrderRequest.php new file mode 100644 index 00000000..46ed1f5c --- /dev/null +++ b/app/Http/Requests/User/OrderRequest.php @@ -0,0 +1,25 @@ + 'required|integer', + 'quantity' => 'required|integer', + ]; + } +} diff --git a/routes/web.php b/routes/web.php index 54a0e202..2c866e50 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,11 +2,11 @@ use App\Http\Controllers\AccountController; use App\Http\Controllers\Admin\MyCancellationController; -use App\Http\Controllers\Admin\OrderController; use App\Http\Controllers\CartController; use App\Http\Controllers\HomeController; use App\Http\Controllers\MyOrderController; use App\Http\Controllers\User\Catalog; +use App\Http\Controllers\User\Order; use App\Http\Controllers\User\Shop; use App\Http\Controllers\User\ShowProduct; use App\Http\Controllers\UserController; @@ -45,7 +45,7 @@ Route::get('/cart/api/all', [CartController::class, 'all']); Route::post('/cart/destroy/selected', [CartController::class, 'destroySelected']); - Route::post('/order', [OrderController::class, 'store']); + Route::post('/order', Order::class); Route::get('/my-order', [MyOrderController::class, 'index'])->name('myOrder.index'); Route::delete('/my-order/{id}', [MyOrderController::class, 'destroy'])->name('myOrder.destroy'); diff --git a/tests/Feature/Http/Controllers/User/OrderTest.php b/tests/Feature/Http/Controllers/User/OrderTest.php new file mode 100644 index 00000000..634ef638 --- /dev/null +++ b/tests/Feature/Http/Controllers/User/OrderTest.php @@ -0,0 +1,51 @@ +newQuery() + ->inRandomOrder() + ->limit(1) + ->first(); + + $this->post('/cart', ['product_id' => (string)$randomProduct->id, 'quantity' => 1]) + ->assertRedirect(); + + $this->assertDatabaseCount(Cart::class, 1); + + /** @var Cart $cart */ + $cart = (new Cart())->newQuery()->first(); + + $this->assertSame($randomProduct->id, $cart->product_id); + + $this->post('/order', ['cart_id' => (string)$cart->id, 'quantity' => 1]) + ->assertRedirect(); + + /** @var Order $order */ + $order = (new Order())->newQuery()->first(); + + $this->assertDatabaseCount(Order::class, 1); + $this->assertDatabaseCount(Cart::class, 0); + + $this->assertSame($randomProduct->id, $order->product_id); + } +}