|
9 | 9 | use Illuminate\Contracts\Foundation\Application; |
10 | 10 | use Illuminate\Database\Capsule\Manager as DB; |
11 | 11 | use Illuminate\Database\Eloquent\Attributes\UseFactory; |
| 12 | +use Illuminate\Database\Eloquent\Casts\Attribute; |
12 | 13 | use Illuminate\Database\Eloquent\Collection; |
13 | 14 | use Illuminate\Database\Eloquent\Factories\CrossJoinSequence; |
14 | 15 | use Illuminate\Database\Eloquent\Factories\Factory; |
@@ -982,6 +983,23 @@ public function test_factory_model_morph_many_relationship_has_pending_attribute |
982 | 983 | $this->assertEquals('other body', FactoryTestComment::first()->body); |
983 | 984 | } |
984 | 985 |
|
| 986 | + public function test_factory_can_insert() |
| 987 | + { |
| 988 | + (new FactoryTestPostFactory()) |
| 989 | + ->count(5) |
| 990 | + ->recycle([ |
| 991 | + (new FactoryTestUserFactory())->create(['name' => Name::Taylor]), |
| 992 | + (new FactoryTestUserFactory())->create(['name' => Name::Shad, 'created_at' => now()]) |
| 993 | + ]) |
| 994 | + ->state(['title' => 'hello']) |
| 995 | + ->insert(); |
| 996 | + $this->assertCount(5, $posts = FactoryTestPost::query()->where('title', 'hello')->get()); |
| 997 | + $this->assertEquals(strtoupper($posts[0]->user->name), $posts[0]->upper_case_name); |
| 998 | + $this->assertEquals(2, ($users = FactoryTestUser::query()->get())->count()); |
| 999 | + $this->assertCount(1, $users->where('name', 'shaedrich')); |
| 1000 | + $this->assertCount(1, $users->where('name', 'totwell')); |
| 1001 | + } |
| 1002 | + |
985 | 1003 | /** |
986 | 1004 | * Get a database connection instance. |
987 | 1005 | * |
@@ -1072,6 +1090,13 @@ class FactoryTestPost extends Eloquent |
1072 | 1090 |
|
1073 | 1091 | protected $table = 'posts'; |
1074 | 1092 |
|
| 1093 | + protected $appends = ['upper_case_name']; |
| 1094 | + |
| 1095 | + public function upperCaseName(): Attribute |
| 1096 | + { |
| 1097 | + return Attribute::get(fn ($attr) => Str::upper($this->user->name)); |
| 1098 | + } |
| 1099 | + |
1075 | 1100 | public function user() |
1076 | 1101 | { |
1077 | 1102 | return $this->belongsTo(FactoryTestUser::class, 'user_id'); |
@@ -1193,3 +1218,9 @@ class FactoryTestUseFactoryAttribute extends Eloquent |
1193 | 1218 | { |
1194 | 1219 | use HasFactory; |
1195 | 1220 | } |
| 1221 | + |
| 1222 | +enum Name: string |
| 1223 | +{ |
| 1224 | + case Taylor = 'totwell'; |
| 1225 | + case Shad = 'shaedrich'; |
| 1226 | +} |
0 commit comments