-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathDatabaseEloquentHasManyThroughIntegrationTest.php
415 lines (350 loc) · 11.3 KB
/
DatabaseEloquentHasManyThroughIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
namespace Illuminate\Tests\Database;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
class DatabaseEloquentHasManyThroughIntegrationTest extends TestCase
{
public function setUp()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
$this->createSchema();
}
/**
* Setup the database schema.
*
* @return void
*/
public function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('email')->unique();
$table->unsignedInteger('country_id');
$table->string('country_short');
$table->timestamps();
$table->softDeletes();
});
$this->schema()->create('posts', function ($table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->text('body');
$table->string('email');
$table->timestamps();
});
$this->schema()->create('countries', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('shortname');
$table->timestamps();
});
}
/**
* Tear down the database schema.
*
* @return void
*/
public function tearDown()
{
$this->schema()->drop('users');
$this->schema()->drop('posts');
$this->schema()->drop('countries');
}
public function testItLoadsAHasManyThroughRelationWithCustomKeys()
{
$this->seedData();
$posts = HasManyThroughTestCountry::first()->posts;
$this->assertEquals('A title', $posts[0]->title);
$this->assertCount(2, $posts);
}
public function testItLoadsADefaultHasManyThroughRelation()
{
$this->migrateDefault();
$this->seedDefaultData();
$posts = HasManyThroughDefaultTestCountry::first()->posts;
$this->assertEquals('A title', $posts[0]->title);
$this->assertCount(2, $posts);
$this->resetDefault();
}
public function testItLoadsARelationWithCustomIntermediateAndLocalKey()
{
$this->seedData();
$posts = HasManyThroughIntermediateTestCountry::first()->posts;
$this->assertEquals('A title', $posts[0]->title);
$this->assertCount(2, $posts);
}
/**
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
* @expectedExceptionMessage No query results for model [Illuminate\Tests\Database\HasManyThroughTestPost].
*/
public function testFirstOrFailThrowsAnException()
{
HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
->users()->create(['id' => 1, 'email' => '[email protected]', 'country_short' => 'us']);
HasManyThroughTestCountry::first()->posts()->firstOrFail();
}
/**
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
* @expectedExceptionMessage No query results for model [Illuminate\Tests\Database\HasManyThroughTestPost].
*/
public function testFindOrFailThrowsAnException()
{
HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
->users()->create(['id' => 1, 'email' => '[email protected]', 'country_short' => 'us']);
HasManyThroughTestCountry::first()->posts()->findOrFail(1);
}
public function testFirstRetrievesFirstRecord()
{
$this->seedData();
$post = HasManyThroughTestCountry::first()->posts()->first();
$this->assertNotNull($post);
$this->assertEquals('A title', $post->title);
}
public function testAllColumnsAreRetrievedByDefault()
{
$this->seedData();
$post = HasManyThroughTestCountry::first()->posts()->first();
$this->assertEquals([
'id',
'user_id',
'title',
'body',
'email',
'created_at',
'updated_at',
'country_id',
], array_keys($post->getAttributes()));
}
public function testOnlyProperColumnsAreSelectedIfProvided()
{
$this->seedData();
$post = HasManyThroughTestCountry::first()->posts()->first(['title', 'body']);
$this->assertEquals([
'title',
'body',
'country_id',
], array_keys($post->getAttributes()));
}
public function testIntermediateSoftDeletesAreIgnored()
{
$this->seedData();
HasManyThroughSoftDeletesTestUser::first()->delete();
$posts = HasManyThroughSoftDeletesTestCountry::first()->posts;
$this->assertEquals('A title', $posts[0]->title);
$this->assertCount(2, $posts);
}
public function testEagerLoadingLoadsRelatedModelsCorrectly()
{
$this->seedData();
$country = HasManyThroughSoftDeletesTestCountry::with('posts')->first();
$this->assertEquals('us', $country->shortname);
$this->assertEquals('A title', $country->posts[0]->title);
$this->assertCount(2, $country->posts);
}
/**
* Helpers...
*/
protected function seedData()
{
HasManyThroughTestCountry::create(['id' => 1, 'name' => 'United States of America', 'shortname' => 'us'])
->users()->create(['id' => 1, 'email' => '[email protected]', 'country_short' => 'us'])
->posts()->createMany([
['title' => 'A title', 'body' => 'A body', 'email' => '[email protected]'],
['title' => 'Another title', 'body' => 'Another body', 'email' => '[email protected]'],
]);
}
/**
* Seed data for a default HasManyThrough setup.
*/
protected function seedDefaultData()
{
HasManyThroughDefaultTestCountry::create(['id' => 1, 'name' => 'United States of America'])
->users()->create(['id' => 1, 'email' => '[email protected]'])
->posts()->createMany([
['title' => 'A title', 'body' => 'A body'],
['title' => 'Another title', 'body' => 'Another body'],
]);
}
/**
* Drop the default tables.
*/
protected function resetDefault()
{
$this->schema()->drop('users_default');
$this->schema()->drop('posts_default');
$this->schema()->drop('countries_default');
}
/**
* Migrate tables for classes with a Laravel "default" HasManyThrough setup.
*/
protected function migrateDefault()
{
$this->schema()->create('users_default', function ($table) {
$table->increments('id');
$table->string('email')->unique();
$table->unsignedInteger('has_many_through_default_test_country_id');
$table->timestamps();
});
$this->schema()->create('posts_default', function ($table) {
$table->increments('id');
$table->integer('has_many_through_default_test_user_id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
$this->schema()->create('countries_default', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Get a database connection instance.
*
* @return Connection
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}
/**
* Get a schema builder instance.
*
* @return Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}
/**
* Eloquent Models...
*/
class HasManyThroughTestUser extends Eloquent
{
protected $table = 'users';
protected $guarded = [];
public function posts()
{
return $this->hasMany(HasManyThroughTestPost::class, 'user_id');
}
}
/**
* Eloquent Models...
*/
class HasManyThroughTestPost extends Eloquent
{
protected $table = 'posts';
protected $guarded = [];
public function owner()
{
return $this->belongsTo(HasManyThroughTestUser::class, 'user_id');
}
}
class HasManyThroughTestCountry extends Eloquent
{
protected $table = 'countries';
protected $guarded = [];
public function posts()
{
return $this->hasManyThrough(HasManyThroughTestPost::class, HasManyThroughTestUser::class, 'country_id', 'user_id');
}
public function users()
{
return $this->hasMany(HasManyThroughTestUser::class, 'country_id');
}
}
/**
* Eloquent Models...
*/
class HasManyThroughDefaultTestUser extends Eloquent
{
protected $table = 'users_default';
protected $guarded = [];
public function posts()
{
return $this->hasMany(HasManyThroughDefaultTestPost::class);
}
}
/**
* Eloquent Models...
*/
class HasManyThroughDefaultTestPost extends Eloquent
{
protected $table = 'posts_default';
protected $guarded = [];
public function owner()
{
return $this->belongsTo(HasManyThroughDefaultTestUser::class);
}
}
class HasManyThroughDefaultTestCountry extends Eloquent
{
protected $table = 'countries_default';
protected $guarded = [];
public function posts()
{
return $this->hasManyThrough(HasManyThroughDefaultTestPost::class, HasManyThroughDefaultTestUser::class);
}
public function users()
{
return $this->hasMany(HasManyThroughDefaultTestUser::class);
}
}
class HasManyThroughIntermediateTestCountry extends Eloquent
{
protected $table = 'countries';
protected $guarded = [];
public function posts()
{
return $this->hasManyThrough(HasManyThroughTestPost::class, HasManyThroughTestUser::class, 'country_short', 'email', 'shortname', 'email');
}
public function users()
{
return $this->hasMany(HasManyThroughTestUser::class, 'country_id');
}
}
class HasManyThroughSoftDeletesTestUser extends Eloquent
{
use SoftDeletes;
protected $table = 'users';
protected $guarded = [];
public function posts()
{
return $this->hasMany(HasManyThroughSoftDeletesTestPost::class, 'user_id');
}
}
/**
* Eloquent Models...
*/
class HasManyThroughSoftDeletesTestPost extends Eloquent
{
protected $table = 'posts';
protected $guarded = [];
public function owner()
{
return $this->belongsTo(HasManyThroughSoftDeletesTestUser::class, 'user_id');
}
}
class HasManyThroughSoftDeletesTestCountry extends Eloquent
{
protected $table = 'countries';
protected $guarded = [];
public function posts()
{
return $this->hasManyThrough(HasManyThroughSoftDeletesTestPost::class, HasManyThroughTestUser::class, 'country_id', 'user_id');
}
public function users()
{
return $this->hasMany(HasManyThroughSoftDeletesTestUser::class, 'country_id');
}
}