CompositeToCompositeRelationTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 55
c 1
b 0
f 0
dl 0
loc 159
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A validateImproperBelongsToRelation() 0 14 2
A validateUpdateBelongsToRRelation() 0 15 1
A validateEagerWithBelongsToRelation() 0 15 1
A validateQuickUpdateBelongsToRelation() 0 15 1
A validateMissingBelongsToRelation() 0 13 1
A validateAutomaticBelongsToRelation() 0 12 1
A validateEagerBelongsToRelation() 0 15 1
A validateBelongsToRelation() 0 15 1
A validateEagerLoadBelongsToRelation() 0 17 1
1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Tests;
4
5
use MaksimM\CompositePrimaryKeys\Exceptions\WrongRelationConfigurationException;
6
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestUser;
7
8
class CompositeToCompositeRelationTest extends CompositeKeyBaseUnit
9
{
10
    /** @test */
11
    public function validateMissingBelongsToRelation()
12
    {
13
        /**
14
         * @var TestUser
15
         */
16
        $user = TestUser::find([
17
            'user_id'         => 1,
18
            'organization_id' => 101,
19
        ]);
20
21
        $referrer_user = $user->referrer()->first();
22
23
        $this->assertNull($referrer_user);
24
    }
25
26
    /** @test */
27
    public function validateBelongsToRelation()
28
    {
29
        /**
30
         * @var TestUser
31
         */
32
        $user = TestUser::find([
33
            'user_id'         => 2,
34
            'organization_id' => 101,
35
        ]);
36
37
        $referrer_user = $user->referrer()->first();
38
39
        $this->assertNotNull($referrer_user);
40
41
        $this->assertInstanceOf(TestUser::class, $referrer_user);
42
    }
43
44
    /** @test */
45
    public function validateImproperBelongsToRelation()
46
    {
47
        /**
48
         * @var TestUser
49
         */
50
        $user = TestUser::find([
51
            'user_id'         => 2,
52
            'organization_id' => 101,
53
        ]);
54
55
        try {
56
            $user->wrongConfiguredReferrer()->first();
57
        } catch (\Exception $exception) {
58
            $this->assertInstanceOf(WrongRelationConfigurationException::class, $exception);
59
        }
60
    }
61
62
    /** @test */
63
    public function validateAutomaticBelongsToRelation()
64
    {
65
        /**
66
         * @var TestUser
67
         */
68
        $user = TestUser::find([
69
            'user_id'         => 2,
70
            'organization_id' => 101,
71
        ]);
72
73
        $referrer = $user->automaticReferrer()->first();
74
        $this->assertNull($referrer);
75
    }
76
77
    /** @test */
78
    public function validateEagerBelongsToRelation()
79
    {
80
        /**
81
         * @var TestUser
82
         */
83
        $user = TestUser::find([
84
            'user_id'         => 2,
85
            'organization_id' => 101,
86
        ]);
87
88
        $referrer_user = $user->referrer;
89
90
        $this->assertNotNull($referrer_user);
91
92
        $this->assertInstanceOf(TestUser::class, $referrer_user);
93
    }
94
95
    /** @test */
96
    public function validateEagerWithBelongsToRelation()
97
    {
98
        /**
99
         * @var TestUser
100
         */
101
        $user = TestUser::with(['referrer'])->find([
102
            'user_id'         => 2,
103
            'organization_id' => 101,
104
        ]);
105
106
        $referrer_user = $user->referrer;
107
108
        $this->assertNotNull($referrer_user);
109
110
        $this->assertInstanceOf(TestUser::class, $referrer_user);
111
    }
112
113
    /** @test */
114
    public function validateEagerLoadBelongsToRelation()
115
    {
116
        /**
117
         * @var TestUser
118
         */
119
        $user = TestUser::find([
120
            'user_id'         => 2,
121
            'organization_id' => 101,
122
        ]);
123
124
        $user->load('referrer');
125
126
        $referrer_user = $user->referrer;
127
128
        $this->assertNotNull($referrer_user);
129
130
        $this->assertInstanceOf(TestUser::class, $referrer_user);
131
    }
132
133
    /** @test */
134
    public function validateUpdateBelongsToRRelation()
135
    {
136
        /**
137
         * @var TestUser
138
         */
139
        $user = TestUser::find([
140
            'user_id'         => 2,
141
            'organization_id' => 101,
142
        ]);
143
144
        $user->referrer->update([
145
            'counter' => 3333,
146
        ]);
147
148
        $this->assertEquals(3333, $user->referrer->counter);
149
    }
150
151
    /** @test */
152
    public function validateQuickUpdateBelongsToRelation()
153
    {
154
        /**
155
         * @var TestUser
156
         */
157
        $user = TestUser::find([
158
            'user_id'         => 2,
159
            'organization_id' => 101,
160
        ]);
161
162
        $user->referrer()->update([
163
            'counter' => 3333,
164
        ]);
165
166
        $this->assertEquals(3333, $user->referrer()->first()->counter);
167
    }
168
}
169