forked from nishangupta/Mart
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |