mirror of
https://github.com/sp-tarkov/forge.git
synced 2025-02-12 12:10:41 -05:00
add test
This commit is contained in:
parent
aabf5a1b44
commit
4ba181900e
@ -76,13 +76,23 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
public function follow(User|int $user): void
|
||||
{
|
||||
$userId = $user instanceof User ? $user->id : $user;
|
||||
|
||||
if ($this->id === $userId) {
|
||||
// don't allow following yourself
|
||||
return;
|
||||
}
|
||||
|
||||
$this->following()->syncWithoutDetaching($userId);
|
||||
}
|
||||
|
||||
public function unfollow(User|int $user): void
|
||||
{
|
||||
$userId = $user instanceof User ? $user->id : $user;
|
||||
$this->following()->detach($userId);
|
||||
|
||||
// make sure the user is being followed before trying to detach
|
||||
if ($this->isFollowing($userId)) {
|
||||
$this->following()->detach($userId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
100
tests/Feature/User/FollowTest.php
Normal file
100
tests/Feature/User/FollowTest.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
test('confirm a user cannot follow themself', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->follow($user);
|
||||
|
||||
$this->assertEmpty($user->follwers);
|
||||
$this->assertEmpty($user->following);
|
||||
});
|
||||
|
||||
test('confirm a user can follow and unfollow another user', function () {
|
||||
$user1 = User::factory()->create();
|
||||
$user2 = User::factory()->create();
|
||||
|
||||
$user1->follow($user2);
|
||||
|
||||
$this->assertTrue($user1->isFollowing($user2));
|
||||
|
||||
$user1->unfollow($user2);
|
||||
|
||||
$this->assertFalse($user1->isFollowing($user2));
|
||||
});
|
||||
|
||||
test('confirm following a user cannot be done twice', function () {
|
||||
$user1 = User::factory()->create();
|
||||
$user2 = User::factory()->create();
|
||||
|
||||
$user1->follow($user2);
|
||||
$user1->follow($user2);
|
||||
|
||||
|
||||
$this->assertCount(1, $user1->following);
|
||||
$this->assertCount(1, $user2->followers);
|
||||
});
|
||||
|
||||
test('confirm unfollowing a user that isnt being followed doesnt throw', function () {
|
||||
$user1 = User::factory()->create();
|
||||
$user2 = User::factory()->create();
|
||||
|
||||
$user1->unfollow($user2);
|
||||
|
||||
$this->assertEmpty($user1->following);
|
||||
$this->assertEmpty($user2->followers);
|
||||
});
|
||||
|
||||
test('confirm unfollowing random number doesnt perform detach all', function () {
|
||||
$user1 = User::factory()->create();
|
||||
$user2 = User::factory()->create();
|
||||
$user3 = User::factory()->create();
|
||||
|
||||
$user1->follow($user2);
|
||||
$user1->follow($user3);
|
||||
|
||||
$this->assertTrue($user1->isFollowing($user2));
|
||||
$this->assertTrue($user1->isFollowing($user3));
|
||||
|
||||
$this->assertCount(2, $user1->following);
|
||||
$this->assertCount(1, $user2->followers);
|
||||
$this->assertCount(1, $user3->followers);
|
||||
|
||||
$user1->unfollow(111112222233333);
|
||||
|
||||
$this->assertTrue($user1->isFollowing($user2));
|
||||
$this->assertTrue($user1->isFollowing($user3));
|
||||
});
|
||||
|
||||
test('confirm null follow input fails', function () {
|
||||
$this->expectException(TypeError::class);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->follow(null);
|
||||
});
|
||||
|
||||
test('confirm empty follow input fails', function () {
|
||||
$this->expectException(ArgumentCountError::class);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->follow();
|
||||
});
|
||||
|
||||
test('confirm null unfollow input fails', function () {
|
||||
$this->expectException(TypeError::class);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->unfollow(null);
|
||||
});
|
||||
|
||||
test('confirm empty unfollow input fails', function () {
|
||||
$this->expectException(ArgumentCountError::class);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$user->unfollow();
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user