-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
DatabaseEloquentFactoryTest.php
566 lines (458 loc) · 17.8 KB
/
DatabaseEloquentFactoryTest.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Foundation\Application;
use Mockery;
use PHPUnit\Framework\TestCase;
class DatabaseEloquentFactoryTest extends TestCase
{
protected function setUp(): void
{
$container = Container::getInstance();
$container->singleton(\Faker\Generator::class, function ($app, $parameters) {
return \Faker\Factory::create('en_US');
});
$container->instance(Application::class, $app = Mockery::mock(Application::class));
$app->shouldReceive('getNamespace')->andReturn('App\\');
$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('name');
$table->string('options')->nullable();
$table->timestamps();
});
$this->schema()->create('posts', function ($table) {
$table->increments('id');
$table->foreignId('user_id');
$table->string('title');
$table->timestamps();
});
$this->schema()->create('comments', function ($table) {
$table->increments('id');
$table->foreignId('commentable_id');
$table->string('commentable_type');
$table->string('body');
$table->timestamps();
});
$this->schema()->create('roles', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
$this->schema()->create('role_user', function ($table) {
$table->foreignId('role_id');
$table->foreignId('user_id');
$table->string('admin')->default('N');
});
}
/**
* Tear down the database schema.
*
* @return void
*/
protected function tearDown(): void
{
Mockery::close();
$this->schema()->drop('users');
Container::setInstance(null);
}
public function test_basic_model_can_be_created()
{
$user = FactoryTestUserFactory::new()->create();
$this->assertInstanceOf(Eloquent::class, $user);
$user = FactoryTestUserFactory::new()->createOne();
$this->assertInstanceOf(Eloquent::class, $user);
$user = FactoryTestUserFactory::new()->create(['name' => 'Taylor Otwell']);
$this->assertInstanceOf(Eloquent::class, $user);
$this->assertSame('Taylor Otwell', $user->name);
$users = FactoryTestUserFactory::new()->createMany([
['name' => 'Taylor Otwell'],
['name' => 'Jeffrey Way'],
]);
$this->assertInstanceOf(Collection::class, $users);
$this->assertCount(2, $users);
$users = FactoryTestUserFactory::times(10)->create();
$this->assertCount(10, $users);
}
public function test_expanded_closure_attributes_are_resolved_and_passed_to_closures()
{
$user = FactoryTestUserFactory::new()->create([
'name' => function () {
return 'taylor';
},
'options' => function ($attributes) {
return $attributes['name'].'-options';
},
]);
$this->assertSame('taylor-options', $user->options);
}
public function test_make_creates_unpersisted_model_instance()
{
$user = FactoryTestUserFactory::new()->makeOne();
$this->assertInstanceOf(Eloquent::class, $user);
$user = FactoryTestUserFactory::new()->make(['name' => 'Taylor Otwell']);
$this->assertInstanceOf(Eloquent::class, $user);
$this->assertSame('Taylor Otwell', $user->name);
$this->assertCount(0, FactoryTestUser::all());
}
public function test_basic_model_attributes_can_be_created()
{
$user = FactoryTestUserFactory::new()->raw();
$this->assertIsArray($user);
$user = FactoryTestUserFactory::new()->raw(['name' => 'Taylor Otwell']);
$this->assertIsArray($user);
$this->assertSame('Taylor Otwell', $user['name']);
}
public function test_expanded_model_attributes_can_be_created()
{
$post = FactoryTestPostFactory::new()->raw();
$this->assertIsArray($post);
$post = FactoryTestPostFactory::new()->raw(['title' => 'Test Title']);
$this->assertIsArray($post);
$this->assertIsInt($post['user_id']);
$this->assertSame('Test Title', $post['title']);
}
public function test_lazy_model_attributes_can_be_created()
{
$userFunction = FactoryTestUserFactory::new()->lazy();
$this->assertIsCallable($userFunction);
$this->assertInstanceOf(Eloquent::class, $userFunction());
$userFunction = FactoryTestUserFactory::new()->lazy(['name' => 'Taylor Otwell']);
$this->assertIsCallable($userFunction);
$user = $userFunction();
$this->assertInstanceOf(Eloquent::class, $user);
$this->assertSame('Taylor Otwell', $user->name);
}
public function test_multiple_model_attributes_can_be_created()
{
$posts = FactoryTestPostFactory::new()->times(10)->raw();
$this->assertIsArray($posts);
$this->assertCount(10, $posts);
}
public function test_after_creating_and_making_callbacks_are_called()
{
$user = FactoryTestUserFactory::new()
->afterMaking(function ($user) {
$_SERVER['__test.user.making'] = $user;
})
->afterCreating(function ($user) {
$_SERVER['__test.user.creating'] = $user;
})
->create();
$this->assertSame($user, $_SERVER['__test.user.making']);
$this->assertSame($user, $_SERVER['__test.user.creating']);
unset($_SERVER['__test.user.making']);
unset($_SERVER['__test.user.creating']);
}
public function test_has_many_relationship()
{
$users = FactoryTestUserFactory::times(10)
->has(
FactoryTestPostFactory::times(3)
->state(function ($attributes, $user) {
// Test parent is passed to child state mutations...
$_SERVER['__test.post.state-user'] = $user;
return [];
})
// Test parents passed to callback...
->afterCreating(function ($post, $user) {
$_SERVER['__test.post.creating-post'] = $post;
$_SERVER['__test.post.creating-user'] = $user;
}),
'posts'
)
->create();
$this->assertCount(10, FactoryTestUser::all());
$this->assertCount(30, FactoryTestPost::all());
$this->assertCount(3, FactoryTestUser::latest()->first()->posts);
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.post.creating-post']);
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.post.creating-user']);
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.post.state-user']);
unset($_SERVER['__test.post.creating-post']);
unset($_SERVER['__test.post.creating-user']);
unset($_SERVER['__test.post.state-user']);
}
public function test_belongs_to_relationship()
{
$posts = FactoryTestPostFactory::times(3)
->for(FactoryTestUserFactory::new(['name' => 'Taylor Otwell']), 'user')
->create();
$this->assertCount(3, $posts->filter(function ($post) {
return $post->user->name == 'Taylor Otwell';
}));
$this->assertCount(1, FactoryTestUser::all());
$this->assertCount(3, FactoryTestPost::all());
}
public function test_belongs_to_relationship_with_existing_model_instance()
{
$user = FactoryTestUserFactory::new(['name' => 'Taylor Otwell'])->create();
$posts = FactoryTestPostFactory::times(3)
->for($user, 'user')
->create();
$this->assertCount(3, $posts->filter(function ($post) use ($user) {
return $post->user->is($user);
}));
$this->assertCount(1, FactoryTestUser::all());
$this->assertCount(3, FactoryTestPost::all());
}
public function test_morph_to_relationship()
{
$posts = FactoryTestCommentFactory::times(3)
->for(FactoryTestPostFactory::new(['title' => 'Test Title']), 'commentable')
->create();
$this->assertSame('Test Title', FactoryTestPost::first()->title);
$this->assertCount(3, FactoryTestPost::first()->comments);
$this->assertCount(1, FactoryTestPost::all());
$this->assertCount(3, FactoryTestComment::all());
}
public function test_morph_to_relationship_with_existing_model_instance()
{
$post = FactoryTestPostFactory::new(['title' => 'Test Title'])->create();
$posts = FactoryTestCommentFactory::times(3)
->for($post, 'commentable')
->create();
$this->assertSame('Test Title', FactoryTestPost::first()->title);
$this->assertCount(3, FactoryTestPost::first()->comments);
$this->assertCount(1, FactoryTestPost::all());
$this->assertCount(3, FactoryTestComment::all());
}
public function test_belongs_to_many_relationship()
{
$users = FactoryTestUserFactory::times(3)
->hasAttached(
FactoryTestRoleFactory::times(3)->afterCreating(function ($role, $user) {
$_SERVER['__test.role.creating-role'] = $role;
$_SERVER['__test.role.creating-user'] = $user;
}),
['admin' => 'Y'],
'roles'
)
->create();
$this->assertCount(9, FactoryTestRole::all());
$user = FactoryTestUser::latest()->first();
$this->assertCount(3, $user->roles);
$this->assertSame('Y', $user->roles->first()->pivot->admin);
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-role']);
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-user']);
unset($_SERVER['__test.role.creating-role']);
unset($_SERVER['__test.role.creating-user']);
}
public function test_belongs_to_many_relationship_with_existing_model_instances()
{
$roles = FactoryTestRoleFactory::times(3)
->afterCreating(function ($role) {
$_SERVER['__test.role.creating-role'] = $role;
})
->create();
FactoryTestUserFactory::times(3)
->hasAttached($roles, ['admin' => 'Y'], 'roles')
->create();
$this->assertCount(3, FactoryTestRole::all());
$user = FactoryTestUser::latest()->first();
$this->assertCount(3, $user->roles);
$this->assertSame('Y', $user->roles->first()->pivot->admin);
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-role']);
unset($_SERVER['__test.role.creating-role']);
}
public function test_sequences()
{
$users = FactoryTestUserFactory::times(2)->sequence(
['name' => 'Taylor Otwell'],
['name' => 'Abigail Otwell'],
)->create();
$this->assertSame('Taylor Otwell', $users[0]->name);
$this->assertSame('Abigail Otwell', $users[1]->name);
$user = FactoryTestUserFactory::new()
->hasAttached(
FactoryTestRoleFactory::times(4),
new Sequence(['admin' => 'Y'], ['admin' => 'N']),
'roles'
)
->create();
$this->assertCount(4, $user->roles);
$this->assertCount(2, $user->roles->filter(function ($role) {
return $role->pivot->admin == 'Y';
}));
$this->assertCount(2, $user->roles->filter(function ($role) {
return $role->pivot->admin == 'N';
}));
}
public function test_resolve_nested_model_factories()
{
Factory::useNamespace('Factories\\');
$resolves = [
'App\\Foo' => 'Factories\\FooFactory',
'App\\Models\\Foo' => 'Factories\\FooFactory',
'App\\Models\\Nested\\Foo' => 'Factories\\Nested\\FooFactory',
'App\\Models\\Really\\Nested\\Foo' => 'Factories\\Really\\Nested\\FooFactory',
];
foreach ($resolves as $model => $factory) {
$this->assertEquals($factory, Factory::resolveFactoryName($model));
}
}
public function test_resolve_non_app_nested_model_factories()
{
Container::getInstance()->instance(Application::class, $app = Mockery::mock(Application::class));
$app->shouldReceive('getNamespace')->andReturn('Foo\\');
Factory::useNamespace('Factories\\');
$resolves = [
'Foo\\Bar' => 'Factories\\BarFactory',
'Foo\\Models\\Bar' => 'Factories\\BarFactory',
'Foo\\Models\\Nested\\Bar' => 'Factories\\Nested\\BarFactory',
'Foo\\Models\\Really\\Nested\\Bar' => 'Factories\\Really\\Nested\\BarFactory',
];
foreach ($resolves as $model => $factory) {
$this->assertEquals($factory, Factory::resolveFactoryName($model));
}
}
public function test_model_has_factory()
{
Factory::guessFactoryNamesUsing(function ($model) {
return $model.'Factory';
});
$this->assertInstanceOf(FactoryTestUserFactory::class, FactoryTestUser::factory());
}
public function test_dynamic_has_and_for_methods()
{
Factory::guessFactoryNamesUsing(function ($model) {
return $model.'Factory';
});
$user = FactoryTestUserFactory::new()->hasPosts(3)->create();
$this->assertCount(3, $user->posts);
$post = FactoryTestPostFactory::new()
->forAuthor(['name' => 'Taylor Otwell'])
->hasComments(2)
->create();
$this->assertInstanceOf(FactoryTestUser::class, $post->author);
$this->assertSame('Taylor Otwell', $post->author->name);
$this->assertCount(2, $post->comments);
}
/**
* Get a database connection instance.
*
* @return \Illuminate\Database\ConnectionInterface
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}
/**
* Get a schema builder instance.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}
class FactoryTestUserFactory extends Factory
{
protected $model = FactoryTestUser::class;
public function definition()
{
return [
'name' => $this->faker->name,
'options' => null,
];
}
}
class FactoryTestUser extends Eloquent
{
use HasFactory;
protected $table = 'users';
public function posts()
{
return $this->hasMany(FactoryTestPost::class, 'user_id');
}
public function roles()
{
return $this->belongsToMany(FactoryTestRole::class, 'role_user', 'user_id', 'role_id')->withPivot('admin');
}
}
class FactoryTestPostFactory extends Factory
{
protected $model = FactoryTestPost::class;
public function definition()
{
return [
'user_id' => FactoryTestUserFactory::new(),
'title' => $this->faker->name,
];
}
}
class FactoryTestPost extends Eloquent
{
protected $table = 'posts';
public function user()
{
return $this->belongsTo(FactoryTestUser::class, 'user_id');
}
public function author()
{
return $this->belongsTo(FactoryTestUser::class, 'user_id');
}
public function comments()
{
return $this->morphMany(FactoryTestComment::class, 'commentable');
}
}
class FactoryTestCommentFactory extends Factory
{
protected $model = FactoryTestComment::class;
public function definition()
{
return [
'commentable_id' => FactoryTestPostFactory::new(),
'commentable_type' => FactoryTestPost::class,
'body' => $this->faker->name,
];
}
}
class FactoryTestComment extends Eloquent
{
protected $table = 'comments';
public function commentable()
{
return $this->morphTo();
}
}
class FactoryTestRoleFactory extends Factory
{
protected $model = FactoryTestRole::class;
public function definition()
{
return [
'name' => $this->faker->name,
];
}
}
class FactoryTestRole extends Eloquent
{
protected $table = 'roles';
public function users()
{
return $this->belongsToMany(FactoryTestUser::class, 'role_user', 'role_id', 'user_id')->withPivot('admin');
}
}