1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace Tests\Controllers;
9
10use App\Models\User;
11use Tests\TestCase;
12
13class FriendsControllerTest extends TestCase
14{
15 public function testStore(): void
16 {
17 $user = User::factory()->create();
18 $target = User::factory()->create();
19
20 $this->expectCountChange(fn () => $user->friends()->count(), 1);
21
22 $this
23 ->actingAsVerified($user)
24 ->post(route('friends.store'), [
25 'target' => $target->getKey(),
26 ])->assertSuccessful();
27 }
28
29 public function testStoreAlreadyFriend(): void
30 {
31 $user = User::factory()->create();
32 $target = User::factory()->create();
33 $user->relations()->create([
34 'foe' => false,
35 'friend' => true,
36 'zebra_id' => $target->getKey(),
37 ]);
38
39 $this->expectCountChange(fn () => $user->friends()->count(), 0);
40
41 $this
42 ->actingAsVerified($user)
43 ->post(route('friends.store'), [
44 'target' => $target->getKey(),
45 ])->assertSuccessful();
46 }
47
48 public function testStoreBlocked(): void
49 {
50 $user = User::factory()->create();
51 $target = User::factory()->create();
52 $user->relations()->create([
53 'foe' => true,
54 'friend' => false,
55 'zebra_id' => $target->getKey(),
56 ]);
57
58 $this->expectCountChange(fn () => $user->friends()->count(), 1);
59 $this->expectCountChange(fn () => $user->blocks()->count(), -1);
60
61 $this
62 ->actingAsVerified($user)
63 ->post(route('friends.store'), [
64 'target' => $target->getKey(),
65 ])->assertSuccessful();
66 }
67
68 public function testStoreNonExistentTarget(): void
69 {
70 $user = User::factory()->create();
71 $targetId = User::max('user_id') + 1;
72
73 $this->expectCountChange(fn () => $user->friends()->count(), 0);
74
75 $this
76 ->actingAsVerified($user)
77 ->post(route('friends.store'), [
78 'target' => $targetId,
79 ])->assertStatus(404);
80 }
81}