Issues (64)

tests/BinaryKeysTestWithMutators.php (5 issues)

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