diff --git a/app/Actions/User/FollowUserAction.php b/app/Actions/User/FollowUserAction.php index 6800df1..04b4384 100644 --- a/app/Actions/User/FollowUserAction.php +++ b/app/Actions/User/FollowUserAction.php @@ -4,11 +4,16 @@ use App\Models\User; use App\Notifications\NewFollowerNotification; +use Symfony\Component\HttpKernel\Exception\HttpException; class FollowUserAction { public function execute(User $follower, User $following): void { + if ($follower->is($following)) { + throw new HttpException(400, 'No one follows himself!'); + } + $userIsFollowed = $follower ->following() ->where('following_id', $following->id) diff --git a/tests/Feature/UserFollowersControllerTest.php b/tests/Feature/UserFollowersControllerTest.php index f797c21..eee8f5e 100644 --- a/tests/Feature/UserFollowersControllerTest.php +++ b/tests/Feature/UserFollowersControllerTest.php @@ -87,4 +87,11 @@ public function test_user_unfollows_following_user() $this->assertDatabaseEmpty('followers'); } + + public function test_user_cannot_follow_himself() + { + $this + ->postJson('api/users/1/followers') + ->assertStatus(400); + } }