Skip to content

Commit

Permalink
Add access to query builder with friends() and friendships() method i…
Browse files Browse the repository at this point in the history
…n friendable trait
  • Loading branch information
stephane-monnot committed Mar 7, 2016
1 parent 87f31bc commit 855c81f
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/Contracts/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ public function getFriendsCount();
public function canBefriend($recipient);

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
* Get friends for the user
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function friends();

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function friendships();
}
45 changes: 45 additions & 0 deletions src/Models/Friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;

/**
* Class Friendship
* @package Hootlex\Friendships\Models
*
* @method static \Illuminate\Database\Eloquent\Builder|Friendship pending() Get pending friendships
* @method static \Illuminate\Database\Eloquent\Builder|Friendship accepted() Get accepted friendships
* @method static \Illuminate\Database\Eloquent\Builder|Friendship blocked() Get blocked friendships
* @method static \Illuminate\Database\Eloquent\Builder|Friendship denied() Get denied friendships
*/
class Friendship extends Model
{
/**
Expand Down Expand Up @@ -67,6 +76,42 @@ public function scopeWhereSender($query, $model)
->where('sender_type', get_class($model));
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopePending($query)
{
return $query->whereStatus(Status::PENDING);
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopeAccepted($query)
{
return $query->whereStatus(Status::ACCEPTED);
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopeBlocked($query)
{
return $query->whereStatus(Status::BLOCKED);
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder|Friendship
*/
public function scopeDenied($query)
{
return $query->whereStatus(Status::DENIED);
}

/**
* @param $query
* @param Model $sender
Expand Down
24 changes: 13 additions & 11 deletions src/Traits/Friendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function befriend(Model $recipient)
'status' => Status::PENDING,
]);

$this->friends()->save($friendship);
$this->friendships()->save($friendship);

return $friendship;

Expand Down Expand Up @@ -98,7 +98,7 @@ public function blockFriend(Model $recipient)
'status' => Status::BLOCKED,
]);

return $this->friends()->save($friendship);
return $this->friendships()->save($friendship);
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ public function getBlockedFriendships()
*/
public function hasBlocked(Model $recipient)
{
return $this->friends()->whereRecipient($recipient)->whereStatus(Status::BLOCKED)->exists();
return $this->friendships()->whereRecipient($recipient)->whereStatus(Status::BLOCKED)->exists();
}

/**
Expand Down Expand Up @@ -204,22 +204,22 @@ public function getFriendRequests()
public function getFriends($perPage = 0)
{
if ($perPage == 0) {
return $this->getFriendsQueryBuilder()->get();
return $this->friends()->get();
} else {
return $this->getFriendsQueryBuilder()->paginate($perPage);
return $this->friends()->paginate($perPage);
}
}

/**
* Get the query builder of the 'friend' model
* Get friends for the user
*
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getFriendsQueryBuilder()
public function friends()
{
$friendships = $this->findFriendships(Status::ACCEPTED)->get(['sender_id', 'recipient_id']);
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();
$recipients = $friendships->lists('recipient_id')->all();
$senders = $friendships->lists('sender_id')->all();

return $this->where('id', '!=', $this->getKey())->whereIn('id', array_merge($recipients, $senders));
}
Expand Down Expand Up @@ -277,9 +277,11 @@ private function findFriendships($status = '%')
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
* Get Friendships for the user
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany|Friendship
*/
public function friends()
public function friendships()
{
return $this->morphMany(Friendship::class, 'sender');
}
Expand Down
78 changes: 78 additions & 0 deletions tests/FriedshipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ public function it_returns_accepted_user_friendships(){
$this->assertCount(2, $sender->getAcceptedFriendships());
}

/** @test */
public function it_returns_accepted_user_friendships_with_scope(){
$sender = createUser();
$recipients = createUser([], 3);

foreach ($recipients as $recipient) {
$sender->befriend($recipient);
}

$recipients[0]->acceptFriendRequest($sender);
$recipients[1]->acceptFriendRequest($sender);
$recipients[2]->denyFriendRequest($sender);
$this->assertCount(2, $sender->friendships()->accepted()->get());
}

/** @test */
public function it_returns_only_accepted_user_friendships(){
$sender = createUser();
Expand All @@ -204,6 +219,26 @@ public function it_returns_only_accepted_user_friendships(){
$this->assertCount(0, $recipients[3]->getAcceptedFriendships());
}

/** @test */
public function it_returns_only_accepted_user_friendships_with_scope(){
$sender = createUser();
$recipients = createUser([], 4);

foreach ($recipients as $recipient) {
$sender->befriend($recipient);
}

$recipients[0]->acceptFriendRequest($sender);
$recipients[1]->acceptFriendRequest($sender);
$recipients[2]->denyFriendRequest($sender);
$this->assertCount(2, $sender->friendships()->accepted()->get());

$this->assertCount(1, $recipients[0]->friendships()->accepted()->get());
$this->assertCount(1, $recipients[1]->friendships()->accepted()->get());
$this->assertCount(0, $recipients[2]->friendships()->accepted()->get());
$this->assertCount(0, $recipients[3]->friendships()->accepted()->get());
}

/** @test */
public function it_returns_pending_user_friendships(){
$sender = createUser();
Expand All @@ -217,6 +252,19 @@ public function it_returns_pending_user_friendships(){
$this->assertCount(2, $sender->getPendingFriendships());
}

/** @test */
public function it_returns_pending_user_friendships_with_scope(){
$sender = createUser();
$recipients = createUser([], 3);

foreach ($recipients as $recipient) {
$sender->befriend($recipient);
}

$recipients[0]->acceptFriendRequest($sender);
$this->assertCount(2, $sender->friendships()->pending()->get());
}

/** @test */
public function it_returns_denied_user_friendships(){
$sender = createUser();
Expand All @@ -232,6 +280,21 @@ public function it_returns_denied_user_friendships(){
$this->assertCount(1, $sender->getDeniedFriendships());
}

/** @test */
public function it_returns_denied_user_friendships_with_scope(){
$sender = createUser();
$recipients = createUser([], 3);

foreach ($recipients as $recipient) {
$sender->befriend($recipient);
}

$recipients[0]->acceptFriendRequest($sender);
$recipients[1]->acceptFriendRequest($sender);
$recipients[2]->denyFriendRequest($sender);
$this->assertCount(1, $sender->friendships()->denied()->get());
}

/** @test */
public function it_returns_blocked_user_friendships(){
$sender = createUser();
Expand All @@ -247,6 +310,21 @@ public function it_returns_blocked_user_friendships(){
$this->assertCount(1, $sender->getBlockedFriendships());
}

/** @test */
public function it_returns_blocked_user_friendships_with_scope(){
$sender = createUser();
$recipients = createUser([], 3);

foreach ($recipients as $recipient) {
$sender->befriend($recipient);
}

$recipients[0]->acceptFriendRequest($sender);
$recipients[1]->acceptFriendRequest($sender);
$recipients[2]->blockFriend($sender);
$this->assertCount(1, $sender->friendships()->blocked()->get());
}

/** @test */
public function it_return_user_friends(){
$sender = createUser();
Expand Down

0 comments on commit 855c81f

Please sign in to comment.