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