Skip to content

Commit

Permalink
add `can store product in cart with firstOrCreateWithItems scope when…
Browse files Browse the repository at this point in the history
… user sign-in`
  • Loading branch information
milwad-dev committed Jun 3, 2024
1 parent 8854371 commit 3fae979
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/Feature/CartStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@
]);
});

test('can store product in cart with firstOrCreateWithItems scope when user sign-in', function () {
$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product = Product::query()->create(['title' => 'Product 1']);

auth()->login($user);

$cart = Cart::query()->firstOrCreateWithStoreItems($product, 1);

// DB Assertions
assertDatabaseCount('carts', 1);
assertDatabaseCount('cart_items', 1);
assertDatabaseHas('cart_items', [
'itemable_id' => $product->id,
'itemable_type' => $product::class,
'quantity' => 1,
]);
});

test('can store multiple products in cart', function () {
$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product1 = Product::query()->create(['title' => 'Product 1']);
Expand Down
5 changes: 3 additions & 2 deletions tests/SetUp/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Tests\SetUp\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
/**
* Fillable columns.
Expand Down

0 comments on commit 3fae979

Please sign in to comment.