Skip to content

Commit

Permalink
Add Cart and Order test
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Jul 10, 2023
1 parent a681d7a commit 9f58877
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
19 changes: 14 additions & 5 deletions app/Http/Controllers/User/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 25 additions & 0 deletions app/Http/Requests/User/OrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Requests\User;

use Illuminate\Foundation\Http\FormRequest;

/**
* @property int cart_id
* @property int quantity
*/
class OrderRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

public function rules(): array
{
return [
'cart_id' => 'required|integer',
'quantity' => 'required|integer',
];
}
}
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
51 changes: 51 additions & 0 deletions tests/Feature/Http/Controllers/User/OrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Feature\Http\Controllers\User;

use App\Models\Cart;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class OrderTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function happyPath()
{
Artisan::call('db:seed');
Auth::loginUsingId(3);

/** @var Product $randomProduct */
$randomProduct = (new Product())->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);
}
}

0 comments on commit 9f58877

Please sign in to comment.