Skip to content

Commit

Permalink
Fixed #166
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 6, 2022
1 parent 1a539df commit 7e126f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Followable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function follow($user): array
/** @var \Illuminate\Database\Eloquent\Model|\Overtrue\LaravelFollow\Followable $user */
$isPending = $user->needsToApproveFollowRequests() ?: false;

$this->followings()->attach($user, [
$this->followings()->syncWithPivotValues($user, [
'accepted_at' => $isPending ? null : now()
]);
], false);

return ['pending' => $isPending];
}
Expand Down
21 changes: 21 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Overtrue\LaravelFollow\Events\Followed;
use Overtrue\LaravelFollow\Events\Unfollowed;
Expand Down Expand Up @@ -290,4 +291,24 @@ public function test_order_by_followers()
$this->assertSame($user1->name, $mostPopularUser->name);
$this->assertEquals(3, $mostPopularUser->followers_count);
}

public function test_repeat_actions()
{
$user1 = User::create(['name' => 'user1']);
$user2 = User::create(['name' => 'user2']);
$user3 = User::create(['name' => 'user2']);
$user4 = User::create(['name' => 'user2']);

$user1->follow($user2);
$user1->follow($user2);
$user1->follow($user2);
$user1->follow($user3);
$user1->follow($user4);

$this->assertDatabaseHas('user_follower', ['follower_id' => $user1->id, 'following_id' => $user2->id]);
$this->assertDatabaseHas('user_follower', ['follower_id' => $user1->id, 'following_id' => $user3->id]);
$this->assertDatabaseHas('user_follower', ['follower_id' => $user1->id, 'following_id' => $user4->id]);

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

0 comments on commit 7e126f7

Please sign in to comment.