1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Friends. |
4
|
|
|
* |
5
|
|
|
* (c) Christopher Lass <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Arubacao\Friends\FriendshipStatus; |
12
|
|
|
|
13
|
|
|
class UserQueryTest extends AbstractTestCase |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function query_user_with_relationships() { |
20
|
|
|
$users = factory(User::class)->times(10)->create(); |
21
|
|
|
|
22
|
|
|
$users[0]->sendFriendRequestTo($users[1]); |
23
|
|
|
$users[1]->acceptFriendRequestFrom($users[0]); |
24
|
|
|
|
25
|
|
|
$users[0]->sendFriendRequestTo($users[2]); |
26
|
|
|
$users[2]->acceptFriendRequestFrom($users[0]); |
27
|
|
|
|
28
|
|
|
$users[0]->sendFriendRequestTo($users[3]); |
29
|
|
|
|
30
|
|
|
$users[4]->sendFriendRequestTo($users[0]); |
31
|
|
|
|
32
|
|
|
$results = User::includeRelationshipsWith($users[0])->get(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$users_with_relationships = collect([]); |
35
|
|
|
foreach ($results as $key => $result) { |
36
|
|
|
$users_with_relationships->put($key, $results->where('name', $users[$key]->name)->first()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// User 0 : No relations with self |
40
|
|
|
$this->assertCount(0, $users_with_relationships[0]->friends_i_am_recipient); |
41
|
|
|
$this->assertCount(0, $users_with_relationships[0]->friends_i_am_sender); |
42
|
|
|
|
43
|
|
|
// User 1 : Friends with User 0 - Is Recipient |
44
|
|
|
$this->assertCount(1, $users_with_relationships[1]->friends_i_am_recipient); |
45
|
|
|
$this->assertCount(0, $users_with_relationships[1]->friends_i_am_sender); |
46
|
|
|
$this->assertEquals($users[1]->id, $users_with_relationships[1]->friends_i_am_recipient->first()->pivot->recipient_id); |
47
|
|
|
$this->assertEquals($users[0]->id, $users_with_relationships[1]->friends_i_am_recipient->first()->pivot->sender_id); |
48
|
|
|
$this->assertEquals(FriendshipStatus::ACCEPTED, $users_with_relationships[1]->friends_i_am_recipient->first()->pivot->friendship_status); |
49
|
|
|
|
50
|
|
|
// User 2 : Friends with User 0 - Is Recipient |
51
|
|
|
$this->assertCount(1, $users_with_relationships[2]->friends_i_am_recipient); |
52
|
|
|
$this->assertCount(0, $users_with_relationships[2]->friends_i_am_sender); |
53
|
|
|
$this->assertEquals($users[2]->id, $users_with_relationships[2]->friends_i_am_recipient->first()->pivot->recipient_id); |
54
|
|
|
$this->assertEquals($users[0]->id, $users_with_relationships[2]->friends_i_am_recipient->first()->pivot->sender_id); |
55
|
|
|
$this->assertEquals(FriendshipStatus::ACCEPTED, $users_with_relationships[2]->friends_i_am_recipient->first()->pivot->friendship_status); |
56
|
|
|
|
57
|
|
|
// User 3 : Pending Request from User 0 - Is Recipient |
58
|
|
|
$this->assertCount(1, $users_with_relationships[3]->friends_i_am_recipient); |
59
|
|
|
$this->assertCount(0, $users_with_relationships[3]->friends_i_am_sender); |
60
|
|
|
$this->assertEquals($users[3]->id, $users_with_relationships[3]->friends_i_am_recipient->first()->pivot->recipient_id); |
61
|
|
|
$this->assertEquals($users[0]->id, $users_with_relationships[3]->friends_i_am_recipient->first()->pivot->sender_id); |
62
|
|
|
$this->assertEquals(FriendshipStatus::PENDING, $users_with_relationships[3]->friends_i_am_recipient->first()->pivot->friendship_status); |
63
|
|
|
|
64
|
|
|
// User 4 : Pending Request for User 0 - Is Sender |
65
|
|
|
$this->assertCount(0, $users_with_relationships[4]->friends_i_am_recipient); |
66
|
|
|
$this->assertCount(1, $users_with_relationships[4]->friends_i_am_sender); |
67
|
|
|
$this->assertEquals($users[4]->id, $users_with_relationships[4]->friends_i_am_sender->first()->pivot->sender_id); |
68
|
|
|
$this->assertEquals($users[0]->id, $users_with_relationships[4]->friends_i_am_sender->first()->pivot->recipient_id); |
69
|
|
|
$this->assertEquals(FriendshipStatus::PENDING, $users_with_relationships[4]->friends_i_am_sender->first()->pivot->friendship_status); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.