From 24294d1c073e15631c993675c16e7bb986ac2b4a Mon Sep 17 00:00:00 2001 From: Mathieu TUDISCO Date: Wed, 21 Oct 2020 18:29:02 +0200 Subject: [PATCH 1/2] Add lazy method in eloquent factory --- .../Database/Eloquent/Factories/Factory.php | 14 ++++++++++++++ tests/Database/DatabaseEloquentFactoryTest.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 9b658295ad8c..29aa90fb922a 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -240,6 +240,20 @@ public function create($attributes = [], ?Model $parent = null) return $results; } + /** + * Create a model and persist it in the database if requested. + * + * @param array $attributes + * @param \Illuminate\Database\Eloquent\Model|null $parent + * @return \Closure + */ + public function lazy(array $attributes = [], ?Model $parent = null) + { + return function () use ($attributes, $parent) { + return $this->create($attributes, $parent); + }; + } + /** * Set the connection name on the results and store them. * diff --git a/tests/Database/DatabaseEloquentFactoryTest.php b/tests/Database/DatabaseEloquentFactoryTest.php index e2eaff33f921..203df06d27e7 100644 --- a/tests/Database/DatabaseEloquentFactoryTest.php +++ b/tests/Database/DatabaseEloquentFactoryTest.php @@ -154,6 +154,20 @@ public function test_expanded_model_attributes_can_be_created() $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(); From 8653ee0a9b0cacac60f6ebfa088900958e5fa569 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 21 Oct 2020 12:01:14 -0500 Subject: [PATCH 2/2] Update Factory.php --- src/Illuminate/Database/Eloquent/Factories/Factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 29aa90fb922a..bb5383c57e25 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -241,7 +241,7 @@ public function create($attributes = [], ?Model $parent = null) } /** - * Create a model and persist it in the database if requested. + * Create a callback that persists a model in the database when invoked. * * @param array $attributes * @param \Illuminate\Database\Eloquent\Model|null $parent