Passed
Pull Request — master (#5)
by Maksim
04:51 queued 01:17
created

BinaryKeysTest::validateMultipleModelLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 1
nc 1
nop 0
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\TestBinaryUser;
8
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestRole;
9
10
class BinaryKeysTest extends CompositeKeyBaseUnit
11
{
12
    protected function getEnvironmentSetUp($app)
13
    {
14
        $app['config']->set('database.default', 'testing');
15
        $app['router']->get('binary-users/{binaryUser}', function (TestBinaryUser $binaryUser) {
16
            return $binaryUser->toJson();
17
        })->middleware(SubstituteBindings::class);
18
    }
19
20
    /** @test */
21
    public function validateSingleModelLookup()
22
    {
23
        /**
24
         * @var TestBinaryUser
25
         */
26
        $model = TestBinaryUser::find([
27
            'user_id'         => md5(20000, true),
28
            'organization_id' => 100,
29
        ]);
30
        $this->assertNotNull($model);
31
        $this->assertInstanceOf(TestBinaryUser::class, $model);
32
33
        return $model;
34
    }
35
36
    /** @test
37
     *  @depends  validateSingleModelLookup
38
     */
39
    public function validateSingleModelLookupModel(TestBinaryUser $model)
40
    {
41
        $this->assertEquals(md5(20000, true), $model->user_id);
42
        $this->assertEquals(100, $model->organization_id);
43
        $this->assertEquals('Foo', $model->name);
44
    }
45
46
    /** @test
47
     *  @depends  validateSingleModelLookup
48
     */
49
    public function validateSingleModelUpdate(TestBinaryUser $model)
50
    {
51
        $model->update([
52
            'name' => 'FooBar',
53
        ]);
54
        $model->refresh();
55
        $this->assertEquals('FooBar', $model->name);
56
    }
57
58
    /** @test */
59
    public function validateMultipleModelLookup()
60
    {
61
        /**
62
         * @var Collection|TestBinaryUser[]
63
         */
64
        $models = TestBinaryUser::find([[
65
            'user_id'         => md5(20000, true),
66
            'organization_id' => 100,
67
        ], [
68
            'user_id'         => md5(20001, true),
69
            'organization_id' => 101,
70
        ]]);
71
        $this->assertNotNull($models);
72
        $this->assertInstanceOf(Collection::class, $models);
73
74
        return $models;
75
    }
76
77
    /** @test
78
     *  @depends  validateMultipleModelLookup
79
     */
80
    public function validateMultipleModelLookupModels(Collection $models)
81
    {
82
        $this->assertEquals(md5(20000, true), $models->get(0)->user_id);
83
        $this->assertEquals(100, $models->get(0)->organization_id);
84
        $this->assertEquals('Foo', $models->get(0)->name);
85
        $this->assertEquals(md5(20001, true), $models->get(1)->user_id);
86
        $this->assertEquals(101, $models->get(1)->organization_id);
87
        $this->assertEquals('Bar', $models->get(1)->name);
88
    }
89
90
    /** @test
91
     *  @depends  validateSingleModelLookup
92
     */
93
    public function validateBinaryValueRendering(TestBinaryUser $model)
94
    {
95
        $this->assertContains(strtoupper(md5(20000)), $model->toJson());
96
    }
97
98
    /** @test
99
     */
100
    public function validateMissingBinaryModelRouteBinding()
101
    {
102
        $data = $this->call('GET', 'binary-users/FF___1');
103
        $this->assertEquals(404, $data->getStatusCode());
104
    }
105
106
    /** @test
107
     */
108
    public function validateWrongBinaryKeyModelRouteBinding()
109
    {
110
        $data = $this->call('GET', 'binary-users/foo');
111
        /*
112
         * will fire WrongKeyException
113
         */
114
        $this->assertEquals(500, $data->getStatusCode());
115
    }
116
117
    /** @test
118
     *  @depends  validateSingleModelLookup
119
     */
120
    public function validateBinaryModelRouteBinding(TestBinaryUser $model)
121
    {
122
        $model->refresh();
123
        $data = $this->call('GET', 'binary-users/'.$model->getKey());
124
        $this->assertEquals(200, $data->getStatusCode());
125
        $this->assertEquals($model->toJson(), $data->getContent());
126
    }
127
128
    /** @test
129
     *  @depends  validateSingleModelLookup
130
     */
131
    public function validateEagerRelations(TestBinaryUser $model)
132
    {
133
        $model->loadMissing(['role']);
134
        $this->assertNotNull($model->toArray()['role']);
135
        $this->assertNotNull($model->role);
136
    }
137
138
    /** @test
139
     */
140
    public function validateLazyEagerRelations()
141
    {
142
        $model = TestBinaryUser::find([
143
            'user_id'         => md5(20000, true),
144
            'organization_id' => 100,
145
        ]);
146
        $this->assertNotNull($model->role);
147
    }
148
149
    /** @test
150
     */
151
    public function validateReverseEagerRelations()
152
    {
153
        $role = TestRole::with('users')->first();
154
        $this->assertNotNull($role->toArray()['users']);
155
        $this->assertNotNull($role->users);
156
    }
157
158
    /** @test
159
     */
160
    public function validateReverseLazyEagerRelations()
161
    {
162
        $role = TestRole::first();
163
        $this->assertNotNull($role->users);
164
    }
165
}
166