Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Add Event #18

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Events/LaravelCartDecreaseQuantityEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartDecreaseQuantityEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
36 changes: 36 additions & 0 deletions src/Events/LaravelCartEmptyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartEmptyEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
36 changes: 36 additions & 0 deletions src/Events/LaravelCartIncreaseQuantityEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartIncreaseQuantityEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
36 changes: 36 additions & 0 deletions src/Events/LaravelCartRemoveItemEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartRemoveItemEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
36 changes: 36 additions & 0 deletions src/Events/LaravelCartStoreItemEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Binafy\LaravelCart\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LaravelCartStoreItemEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
20 changes: 20 additions & 0 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Binafy\LaravelCart\Models;

use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
use Binafy\LaravelCart\Cartable;
use Binafy\LaravelCart\Events\LaravelCartDecreaseQuantityEvent;
use Binafy\LaravelCart\Events\LaravelCartEmptyEvent;
use Binafy\LaravelCart\Events\LaravelCartIncreaseQuantityEvent;
use Binafy\LaravelCart\Events\LaravelCartRemoveItemEvent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -122,6 +127,9 @@ public function storeItem(Model|array $item): static
]);
}

// Dispatch Event
LaravelCartStoreItemEvent::dispatch();

return $this;
}

Expand All @@ -136,6 +144,9 @@ public function removeItem(Model $item): static
$itemToDelete->delete();
}

// Dispatch Event
LaravelCartRemoveItemEvent::dispatch();

return $this;
}

Expand All @@ -146,6 +157,9 @@ public function emptyCart(): static
{
$this->items()->delete();

// Dispatch Event
LaravelCartEmptyEvent::dispatch();

return $this;
}

Expand All @@ -161,6 +175,9 @@ public function increaseQuantity(Model $item, int $quantity = 1): static

$item->increment('quantity', $quantity);

// Dispatch Event
LaravelCartIncreaseQuantityEvent::dispatch($item);

return $this;
}

Expand All @@ -176,6 +193,9 @@ public function decreaseQuantity(Model $item, int $quantity = 1): static

$item->decrement('quantity', $quantity);

// Dispatch Event
LaravelCartDecreaseQuantityEvent::dispatch($item);

return $this;
}
}
18 changes: 17 additions & 1 deletion tests/Feature/Models/CartDeleteTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use Binafy\LaravelCart\Events\LaravelCartEmptyEvent;
use Binafy\LaravelCart\Events\LaravelCartRemoveItemEvent;
use Binafy\LaravelCart\Events\LaravelCartStoreItemEvent;
use Binafy\LaravelCart\Models\Cart;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\SetUp\Models\Product;
use Tests\SetUp\Models\User;

Expand All @@ -12,7 +16,9 @@
*/
uses(RefreshDatabase::class);

test('can remove an item from the cart', function () {
test('can remove an item from the cart', closure: function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product1 = Product::query()->create(['title' => 'Product 1']);
$product2 = Product::query()->create(['title' => 'Product 2']);
Expand Down Expand Up @@ -54,9 +60,15 @@

$cart->removeItem($product1);
assertDatabaseCount('cart_items', 2);

// Event Assertions
Event::assertDispatched(LaravelCartStoreItemEvent::class);
Event::assertDispatched(LaravelCartRemoveItemEvent::class);
});

test('can empty the cart', function () {
Event::fake();

$user = User::query()->create(['name' => 'Milwad', 'email' => 'milwad.dev@gmail.comd']);
$product1 = Product::query()->create(['title' => 'Product 1']);
$product2 = Product::query()->create(['title' => 'Product 2']);
Expand Down Expand Up @@ -91,4 +103,8 @@
// Remove all items from cart
$cart->emptyCart();
assertDatabaseCount('cart_items', 0);

// Event Assertions
Event::assertDispatched(LaravelCartStoreItemEvent::class);
Event::assertDispatched(LaravelCartEmptyEvent::class);
});