From 4052e33558b9cba4a9e43dd44c5f090dff33127f Mon Sep 17 00:00:00 2001 From: overtrue Date: Tue, 17 May 2022 11:22:55 +0800 Subject: [PATCH] Follower cannot follow self. #170 --- README.md | 4 ++-- src/Traits/Follower.php | 4 ++++ tests/FeatureTest.php | 21 +++++++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 507d2f1..37a0b38 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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 diff --git a/src/Traits/Follower.php b/src/Traits/Follower.php index b48bb4f..aaf1489 100644 --- a/src/Traits/Follower.php +++ b/src/Traits/Follower.php @@ -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.'); } diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index 6727017..693469b 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -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; } ); } @@ -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; } ); @@ -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; } ); } @@ -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); + } }