-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFriendshipsTest.php
243 lines (186 loc) · 7.65 KB
/
FriendshipsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
use Merodiro\Friendships\Friendship;
class FriendshipTest extends TestCase
{
/** @test */
public function user_can_send_a_friend_request()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$this->assertCount(1, $recipient->friendRequestsReceived);
$this->assertCount(1, $sender->friendRequestsSent);
$this->assertNotTrue($sender->isFriendsWith($recipient));
$this->assertNotTrue($recipient->isFriendsWith($sender));
}
/** @test */
public function user_can_not_send_a_friend_request_if_frienship_is_pending()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$sender->addFriend($recipient);
$sender->addFriend($recipient);
$this->assertCount(1, $recipient->friendRequestsReceived);
}
/** @test */
public function user_can_send_a_friend_request_if_frienship_is_denied()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$recipient->deleteFriend($sender);
$this->assertCount(0, $recipient->friendRequestsReceived);
$sender->addFriend($recipient);
// reset relationship
$recipient->setRelations([]);
$this->assertCount(1, $recipient->friendRequestsReceived);
}
/** @test */
public function user_can_remove_a_friend_request()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$sender->deleteFriend($recipient);
$this->assertCount(0, $recipient->friendRequestsReceived);
$recipient->setRelations([]);
$sender->addFriend($recipient);
$this->assertCount(1, $recipient->friendRequestsReceived);
$recipient->acceptfriend($sender);
$this->assertEquals('friends', $recipient->checkFriendship($sender));
$sender->deleteFriend($recipient);
$this->assertEquals('not_friends', $recipient->checkFriendship($sender));
}
/** @test */
public function change_statue_to_pending_and_waiting()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$this->assertEquals('pending', $recipient->checkFriendship($sender));
$this->assertEquals('waiting', $sender->checkFriendship($recipient));
}
/** @test */
public function user_can_not_send_a_friend_request_to_himself()
{
$user = factory(User::class)->create();
$user->addFriend($user);
$this->assertEquals('same_user', $user->checkFriendship($user));
$this->assertCount(0, $user->friendRequestsReceived);
$this->assertCount(0, $user->friendRequestsSent);
}
/** @test */
public function user_is_friend_with_another_user_if_accepts_a_friend_request()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$recipient->acceptFriend($sender);
$this->assertEquals('friends', $recipient->checkFriendship($sender));
$this->assertEquals('friends', $sender->checkFriendship($recipient));
$this->assertTrue($sender->isFriendsWith($recipient));
$this->assertTrue($recipient->isFriendsWith($sender));
}
/** @test */
public function user_has_not_friend_request_from_if_he_accepted_the_friend_request()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$recipient->acceptFriend($sender);
$this->assertCount(0, $recipient->friendRequestsReceived);
$this->assertCount(0, $sender->friendRequestsSent);
}
/** @test */
public function user_cannot_accept_his_own_friend_request()
{
$sender = factory(User::class)->create();
$recipient = factory(User::class)->create();
$sender->addFriend($recipient);
$sender->acceptFriend($recipient);
$this->assertEquals('pending', $recipient->checkFriendship($sender));
}
/** @test */
public function it_returns_accepted_friendships()
{
$sender = factory(User::class)->create();
$recipients = factory(User::class, 3)->create();
foreach ($recipients as $recipient) {
$sender->addFriend($recipient);
}
$recipients[0]->acceptFriend($sender);
$recipients[1]->acceptFriend($sender);
$recipients[2]->deleteFriend($sender);
$this->assertCount(2, $sender->friends);
}
/** @test */
public function it_returns_only_accepted_user_friendships()
{
$sender = factory(User::class)->create();
$recipients = factory(User::class, 4)->create();
foreach ($recipients as $recipient) {
$sender->addFriend($recipient);
}
$recipients[0]->acceptFriend($sender);
$recipients[1]->acceptFriend($sender);
$recipients[2]->deleteFriend($sender);
$this->assertCount(2, $sender->friends);
$this->assertCount(1, $recipients[0]->friends);
$this->assertCount(1, $recipients[1]->friends);
$this->assertCount(0, $recipients[2]->friends);
$this->assertCount(0, $recipients[3]->friends);
$this->containsOnlyInstancesOf(\App\User::class, $sender->friends);
}
/** @test */
public function it_returns_friend_requests_from_user()
{
$sender = factory(User::class)->create();
$recipients = factory(User::class, 3)->create();
foreach ($recipients as $recipient) {
$sender->addFriend($recipient);
}
$recipients[0]->acceptFriend($sender);
$this->assertCount(2, $sender->friendRequestsSent);
$this->containsOnlyInstancesOf(\App\User::class, $sender->friendRequestsSent);
}
/** @test */
public function it_returns_friend_requests_to_user()
{
$recipient = factory(User::class)->create();
$senders = factory(User::class, 3)->create();
foreach ($senders as $sender) {
$sender->addFriend($recipient);
}
$recipient->acceptFriend($senders[0]);
$this->assertCount(2, $recipient->friendRequestsReceived);
$this->containsOnlyInstancesOf(\App\User::class, $recipient->friendRequestsReceived);
}
/** @test */
public function it_returns_mutual_friends()
{
$users = factory(User::class, 6)->create();
// create users
// user one add friends
$users[0]->addFriend($users[1]);
$users[1]->acceptFriend($users[0]);
$users[0]->addFriend($users[2]);
$users[2]->acceptFriend($users[0]);
$users[0]->addFriend($users[3]);
$users[3]->acceptFriend($users[0]);
$users[0]->addFriend($users[5]);
$users[5]->acceptFriend($users[0]);
// user two add friends
$users[1]->addFriend($users[0]);
$users[0]->acceptFriend($users[1]);
$users[1]->addFriend($users[2]);
$users[2]->acceptFriend($users[1]);
$users[1]->addFriend($users[4]);
$users[4]->acceptFriend($users[1]);
$users[1]->addFriend($users[5]);
$users[5]->acceptFriend($users[1]);
$this->assertEquals(2, $users[0]->mutualFriendsCount($users[1]));
$this->assertCount(2, $users[0]->mutualFriends($users[1]));
$this->assertEquals([$users[2]->toArray(), $users[5]->toArray()], $users[0]->mutualFriends($users[1])->toArray());
}
}