Skip to content

Commit

Permalink
Follower cannot follow self. #170
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed May 17, 2022
1 parent 59089d5 commit 4052e33
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Add the Follower trait to your user model:

```php

use Overtrue\LaravelFavorite\Traits\Favoriter;
use Overtrue\LaravelFollow\Traits\Follower;

class User extends Authenticatable
{
Expand Down Expand Up @@ -158,7 +158,7 @@ foreach($users as $user) {

### Attach user follow status to followable collection

You can use `Follower::attachFollowStatus(Collection $followables)` to attach the user favorite status, it will set `has_followed` attribute to each model of `$followables`:
You can use `Follower::attachFollowStatus(Collection $followables)` to attach the user follow status, it will set `has_followed` attribute to each model of `$followables`:

#### For model

Expand Down
4 changes: 4 additions & 0 deletions src/Traits/Follower.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ trait Follower
#[ArrayShape(['pending' => "mixed"])]
public function follow(Model $followable): array
{
if ($followable->is($this)) {
throw new InvalidArgumentException('Cannot follow yourself.');
}

if (!in_array(Followable::class, class_uses($followable))) {
throw new InvalidArgumentException('The followable model must use the Followable trait.');
}
Expand Down
21 changes: 15 additions & 6 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ function ($event) use ($user1, $user2) {
Unfollowed::class,
function ($event) use ($user1, $user2) {
return $event->followable_type === $user2->getMorphClass()
&& $event->followable_id === $user2->id
&& $event->user_id === $user1->id;
&& $event->followable_id == $user2->id
&& $event->follower_id == $user1->id
&& $event->user_id == $user1->id;
}
);
}
Expand All @@ -61,8 +62,8 @@ public function test_user_can_follow_channels()
Followed::class,
function ($event) use ($user, $channel) {
return $event->followable_type === $channel->getMorphClass()
&& $event->followable_id === $channel->id
&& $event->user_id === $user->id;
&& $event->followable_id == $channel->id
&& $event->user_id == $user->id;
}
);

Expand All @@ -75,8 +76,8 @@ function ($event) use ($user, $channel) {
Unfollowed::class,
function ($event) use ($user, $channel) {
return $event->followable_type === $channel->getMorphClass()
&& $event->followable_id === $channel->id
&& $event->user_id === $user->id;
&& $event->followable_id == $channel->id
&& $event->user_id == $user->id;
}
);
}
Expand Down Expand Up @@ -329,4 +330,12 @@ public function test_repeat_actions()

$this->assertDatabaseCount('followables', 3);
}

public function test_follower_cannot_follow_self()
{
$user1 = User::create(['name' => 'user1']);

$this->expectException(\InvalidArgumentException::class);
$user1->follow($user1);
}
}

0 comments on commit 4052e33

Please sign in to comment.