Passed
Pull Request — master (#5)
by Maksim
05:00
created

BinaryKeysTestWithMutators   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 15
eloc 59
c 1
b 1
f 0
dl 0
loc 170
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSingleModelLookupModel() 0 5 1
A validateEagerRelations() 0 5 1
A validateLazyEagerRelations() 0 7 1
A validateSingleModelLookup() 0 13 1
A validateReverseLazyEagerRelations() 0 4 1
A validateReverseEagerRelations() 0 5 1
A validateBinaryModelRouteBinding() 0 6 1
A validateMultipleModelLookupModels() 0 8 1
A validateMultipleModelLookup() 0 16 1
A validateMissingBinaryModelRouteBinding() 0 4 1
A validateWrongBinaryKeyModelRouteBinding() 0 7 1
A validateBinaryValueRendering() 0 3 1
A validateSingleModelUpdate() 0 7 1
A validateHexInputModelLookup() 0 13 1
A getEnvironmentSetUp() 0 6 1
1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Tests;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Routing\Middleware\SubstituteBindings;
7
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestBinaryUserHex;
8
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestBinaryUserMutatorsMutators;
0 ignored issues
show
Bug introduced by
The type MaksimM\CompositePrimary...aryUserMutatorsMutators was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestRole;
10
11
class BinaryKeysTestWithMutators extends CompositeKeyBaseUnit
12
{
13
    protected function getEnvironmentSetUp($app)
14
    {
15
        $app['config']->set('database.default', 'testing');
16
        $app['router']->get('binary-users/{binaryUser}', function (TestBinaryUserHex $binaryUser) {
17
            return $binaryUser->toJson();
18
        })->middleware(SubstituteBindings::class);
19
    }
20
21
    /** @test */
22
    public function validateHexInputModelLookup()
23
    {
24
        /**
25
         * @var TestBinaryUserHex
26
         */
27
        $model = TestBinaryUserHex::find([
28
            'user_id'         => md5(20002),
29
            'organization_id' => 101,
30
        ]);
31
        $this->assertNotNull($model);
32
        $this->assertInstanceOf(TestBinaryUserHex::class, $model);
33
34
        return $model;
35
    }
36
37
    /** @test */
38
    public function validateSingleModelLookup()
39
    {
40
        /**
41
         * @var TestBinaryUserHex
42
         */
43
        $model = TestBinaryUserHex::find([
44
            'user_id'         => md5(20000),
45
            'organization_id' => 100,
46
        ]);
47
        $this->assertNotNull($model);
48
        $this->assertInstanceOf(TestBinaryUserHex::class, $model);
49
50
        return $model;
51
    }
52
53
    /** @test
54
     *  @depends  validateSingleModelLookup
55
     */
56
    public function validateSingleModelLookupModel(TestBinaryUserHex $model)
57
    {
58
        $this->assertEquals(strtoupper(md5(20000)), $model->user_id);
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist on MaksimM\CompositePrimary...Stubs\TestBinaryUserHex. Since you implemented __get, consider adding a @property annotation.
Loading history...
59
        $this->assertEquals(100, $model->organization_id);
0 ignored issues
show
Bug Best Practice introduced by
The property organization_id does not exist on MaksimM\CompositePrimary...Stubs\TestBinaryUserHex. Since you implemented __get, consider adding a @property annotation.
Loading history...
60
        $this->assertEquals('Foo', $model->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on MaksimM\CompositePrimary...Stubs\TestBinaryUserHex. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
    }
62
63
    /** @test
64
     *  @depends  validateSingleModelLookup
65
     */
66
    public function validateSingleModelUpdate(TestBinaryUserHex $model)
67
    {
68
        $model->update([
69
            'name' => 'FooBar'
70
        ]);
71
        $model->refresh();
72
        $this->assertEquals('FooBar', $model->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on MaksimM\CompositePrimary...Stubs\TestBinaryUserHex. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
    }
74
75
    /** @test */
76
    public function validateMultipleModelLookup()
77
    {
78
        /**
79
         * @var Collection|TestBinaryUserHex[]
80
         */
81
        $models = TestBinaryUserHex::find([[
82
            'user_id'         => md5(20000),
83
            'organization_id' => 100,
84
        ], [
85
            'user_id'         => md5(20001),
86
            'organization_id' => 101,
87
        ]]);
88
        $this->assertNotNull($models);
89
        $this->assertInstanceOf(Collection::class, $models);
90
91
        return $models;
92
    }
93
94
    /** @test
95
     *  @depends  validateMultipleModelLookup
96
     */
97
    public function validateMultipleModelLookupModels(Collection $models)
98
    {
99
        $this->assertEquals(strtoupper(md5(20000)), $models->get(0)->user_id);
100
        $this->assertEquals(100, $models->get(0)->organization_id);
101
        $this->assertEquals('Foo', $models->get(0)->name);
102
        $this->assertEquals(strtoupper(md5(20001)), $models->get(1)->user_id);
103
        $this->assertEquals(101, $models->get(1)->organization_id);
104
        $this->assertEquals('Bar', $models->get(1)->name);
105
    }
106
107
    /** @test
108
     *  @depends  validateSingleModelLookup
109
     */
110
    public function validateBinaryValueRendering(TestBinaryUserHex $model)
111
    {
112
        $this->assertContains(strtoupper(md5(20000)), $model->toJson());
113
    }
114
115
    /** @test
116
     */
117
    public function validateMissingBinaryModelRouteBinding()
118
    {
119
        $data = $this->call('GET', 'binary-users/FF___1');
120
        $this->assertEquals(404, $data->getStatusCode());
121
    }
122
123
    /** @test
124
     */
125
    public function validateWrongBinaryKeyModelRouteBinding()
126
    {
127
        $data = $this->call('GET', 'binary-users/foo');
128
        /*
129
         * will fire WrongKeyException
130
         */
131
        $this->assertEquals(500, $data->getStatusCode());
132
    }
133
134
    /** @test
135
     *  @depends  validateSingleModelLookup
136
     */
137
    public function validateBinaryModelRouteBinding(TestBinaryUserHex $model)
138
    {
139
        $model->refresh();
140
        $data = $this->call('GET', 'binary-users/'.$model->getKey());
141
        $this->assertEquals(200, $data->getStatusCode());
142
        $this->assertEquals($model->toJson(), $data->getContent());
143
    }
144
145
    /** @test
146
     *  @depends  validateSingleModelLookup
147
     */
148
    public function validateEagerRelations(TestBinaryUserHex $model)
149
    {
150
        $model->loadMissing(['role']);
151
        $this->assertNotNull($model->toArray()['role']);
152
        $this->assertNotNull($model->role);
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on MaksimM\CompositePrimary...Stubs\TestBinaryUserHex. Since you implemented __get, consider adding a @property annotation.
Loading history...
153
    }
154
155
    /** @test
156
     */
157
    public function validateLazyEagerRelations()
158
    {
159
        $model = TestBinaryUserHex::find([
160
            'user_id'         => md5(20000),
161
            'organization_id' => 100,
162
        ]);
163
        $this->assertNotNull($model->role);
164
    }
165
166
    /** @test
167
     */
168
    public function validateReverseEagerRelations()
169
    {
170
        $role = TestRole::with('users')->first();
171
        $this->assertNotNull($role->toArray()['users']);
172
        $this->assertNotNull($role->users);
173
    }
174
175
    /** @test
176
     */
177
    public function validateReverseLazyEagerRelations()
178
    {
179
        $role = TestRole::first();
180
        $this->assertNotNull($role->users);
181
    }
182
}
183