Skip to content

Commit

Permalink
add events
Browse files Browse the repository at this point in the history
  • Loading branch information
merodiro committed May 9, 2017
1 parent d6b90bc commit 37e4a4f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ $user->friendRequestFrom();
```php
$user->friendRequestTo();
```

## Events
This is the list of the events fired by default for each action

|Event name |Fired |
|----------------------|---------------------------------|
|friendrequest.sent |When a friend request is sent |
|friendrequest.accepted|When a friend request is accepted|
|friendship.deleted |When a friend request is denied |
|friendship.deleted |When a friendship is cancelled |
24 changes: 15 additions & 9 deletions src/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

namespace Merodiro\Friendships;

use Event;

trait Friendable
{
public function addFriend($user)
public function addFriend($recipient)
{
$friendshipStatus = $this->checkFriendship($user);
$friendshipStatus = $this->checkFriendship($recipient);

if ($friendshipStatus == 'not friends') {

Event::fire('friendrequest.sent', [$this, $recipient]);

return Friendship::create([
'requester' => $this->id,
'user_requested' => $user->id,
'user_requested' => $recipient->id,
]);
}

Expand All @@ -38,12 +43,14 @@ public function checkFriendship($user)
}
}

public function acceptFriend($user)
public function acceptFriend($sender)
{
$friendshipStatus = $this->checkFriendship($user);
$friendshipStatus = $this->checkFriendship($sender);

if ($friendshipStatus == 'pending') {
return $friendship = Friendship::betweenModels($this, $user)
Event::fire('friendrequest.accepted', [$this, $sender]);

return $friendship = Friendship::betweenModels($this, $sender)
->update([
'status' => 1,
]);
Expand All @@ -52,10 +59,9 @@ public function acceptFriend($user)

public function deleteFriend($user)
{
Friendship::betweenModels($this, $user)
Event::fire('friendship.deleted', [$this, $user]);
return Friendship::betweenModels($this, $user)
->delete();

return 1;
}

public function friends()
Expand Down
51 changes: 51 additions & 0 deletions tests/FriendshipsEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Tests;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Event;
use Mockery;
class FriendshipsEventsTest extends TestCase
{
use DatabaseMigrations;

public function setUp()
{
parent::setUp();

$this->sender = createUser();
$this->recipient = createUser();
}

public function tearDown()
{
Mockery::close();
}

/** @test */
public function friend_request_is_sent()
{
Event::shouldReceive('fire')->once()->withArgs(['friendrequest.sent', Mockery::any()]);

$this->sender->addfriend($this->recipient);
}

/** @test */
public function friend_request_is_accepted()
{
$this->sender->addfriend($this->recipient);
Event::shouldReceive('fire')->once()->withArgs(['friendrequest.accepted', Mockery::any()]);

$this->recipient->acceptFriend($this->sender);
}

/** @test */
public function friendship_is_cancelled()
{
$this->sender->addfriend($this->recipient);
$this->recipient->acceptFriend($this->sender);
Event::shouldReceive('fire')->once()->withArgs(['friendship.deleted', Mockery::any()]);

$this->recipient->deleteFriend($this->sender);
}
}

0 comments on commit 37e4a4f

Please sign in to comment.