UserQueryTest::query_user_with_relationships()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 2
eloc 34
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method includeRelationshipsWith() does not exist on User. Did you maybe mean scopeIncludeRelationshipsWith()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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
}