From f8e1ca7a86fadc56d2cbe2424c68b4e0f4268c80 Mon Sep 17 00:00:00 2001 From: kenjis <kenji.uui@gmail.com> Date: Mon, 29 Jan 2024 15:53:26 +0900 Subject: [PATCH 1/4] docs: remove "fzaninotto's" --- user_guide_src/source/testing/fabricator.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/testing/fabricator.rst b/user_guide_src/source/testing/fabricator.rst index da621c83b233..750c67a2d7df 100644 --- a/user_guide_src/source/testing/fabricator.rst +++ b/user_guide_src/source/testing/fabricator.rst @@ -3,7 +3,7 @@ Generating Test Data #################### Often you will need sample data for your application to run its tests. The ``Fabricator`` class -uses fzaninotto's `Faker <https://github.com/FakerPHP/Faker>`_ to turn models into generators +uses `Faker <https://github.com/FakerPHP/Faker>`_ to turn models into generators of random data. Use fabricators in your seeds or test cases to stage fake data for your unit tests. .. contents:: From 0e00cacff3cf81a3d00d518631aa952008a6be4b Mon Sep 17 00:00:00 2001 From: kenjis <kenji.uui@gmail.com> Date: Mon, 29 Jan 2024 15:53:46 +0900 Subject: [PATCH 2/4] docs: add empty line --- user_guide_src/source/testing/fabricator.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/testing/fabricator.rst b/user_guide_src/source/testing/fabricator.rst index 750c67a2d7df..e2a4e5910ead 100644 --- a/user_guide_src/source/testing/fabricator.rst +++ b/user_guide_src/source/testing/fabricator.rst @@ -56,6 +56,7 @@ method where you can define exactly what the faked data should look like: Notice in this example how the first three values are equivalent to the formatters from before. However for ``avatar`` we have requested an image size other than the default and ``login`` uses a conditional based on app configuration, neither of which are possible using the ``$formatters`` parameter. + You may want to keep your test data separate from your production models, so it is a good practice to define a child class in your test support folder: From 9dbbe1840169d0cb89c23749c0a53c5b53643b24 Mon Sep 17 00:00:00 2001 From: kenjis <kenji.uui@gmail.com> Date: Mon, 29 Jan 2024 15:54:42 +0900 Subject: [PATCH 3/4] docs: update sample code --- .../source/testing/fabricator/005.php | 18 ++++++++++-------- .../source/testing/fabricator/006.php | 3 ++- .../source/testing/fabricator/008.php | 3 ++- .../source/testing/fabricator/021.php | 7 +++++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/user_guide_src/source/testing/fabricator/005.php b/user_guide_src/source/testing/fabricator/005.php index c61fa527125e..1c233d220b9e 100644 --- a/user_guide_src/source/testing/fabricator/005.php +++ b/user_guide_src/source/testing/fabricator/005.php @@ -2,6 +2,8 @@ namespace App\Models; +use Generator; + class UserModel { // ... @@ -9,10 +11,10 @@ class UserModel public function fake(Generator &$faker) { return [ - 'first' => $faker->firstName, - 'email' => $faker->email, - 'phone' => $faker->phoneNumber, - 'avatar' => Faker\Provider\Image::imageUrl(800, 400), + 'first' => $faker->firstName(), + 'email' => $faker->email(), + 'phone' => $faker->phoneNumber(), + 'avatar' => \Faker\Provider\Image::imageUrl(800, 400), 'login' => config('Auth')->allowRemembering ? date('Y-m-d') : null, ]; @@ -20,10 +22,10 @@ public function fake(Generator &$faker) * Or you can return a return type object. return new User([ - 'first' => $faker->firstName, - 'email' => $faker->email, - 'phone' => $faker->phoneNumber, - 'avatar' => Faker\Provider\Image::imageUrl(800, 400), + 'first' => $faker->firstName(), + 'email' => $faker->email(), + 'phone' => $faker->phoneNumber(), + 'avatar' => \Faker\Provider\Image::imageUrl(800, 400), 'login' => config('Auth')->allowRemembering ? date('Y-m-d') : null, ]); diff --git a/user_guide_src/source/testing/fabricator/006.php b/user_guide_src/source/testing/fabricator/006.php index 56f34781622c..4cbe439a19a3 100644 --- a/user_guide_src/source/testing/fabricator/006.php +++ b/user_guide_src/source/testing/fabricator/006.php @@ -3,10 +3,11 @@ namespace Tests\Support\Models; use App\Models\UserModel; +use Faker\Generator; class UserFabricator extends UserModel { - public function fake(&$faker) + public function fake(Generator &$faker) { // ... } diff --git a/user_guide_src/source/testing/fabricator/008.php b/user_guide_src/source/testing/fabricator/008.php index 1a0d56cf95e2..e39dbe5208cb 100644 --- a/user_guide_src/source/testing/fabricator/008.php +++ b/user_guide_src/source/testing/fabricator/008.php @@ -1,7 +1,8 @@ <?php use CodeIgniter\Test\Fabricator; +use Tests\Support\Models\UserFabricator; -$fabricator = new Fabricator(\UserFabricator::class); +$fabricator = new Fabricator(UserFabricator::class); $testUser = $fabricator->make(); print_r($testUser); diff --git a/user_guide_src/source/testing/fabricator/021.php b/user_guide_src/source/testing/fabricator/021.php index 236dd553d492..a7afe497f330 100644 --- a/user_guide_src/source/testing/fabricator/021.php +++ b/user_guide_src/source/testing/fabricator/021.php @@ -2,6 +2,9 @@ namespace App\Models; +use CodeIgniter\Test\Fabricator; +use Generator; + class UserModel { protected $table = 'users'; @@ -9,8 +12,8 @@ class UserModel public function fake(Generator &$faker) { return [ - 'first' => $faker->firstName, - 'email' => $faker->email, + 'first' => $faker->firstName(), + 'email' => $faker->email(), 'group_id' => mt_rand(1, Fabricator::getCount('groups')), ]; } From dcd94511c4a009f2f2816458f587d29393a44db5 Mon Sep 17 00:00:00 2001 From: kenjis <kenji.uui@gmail.com> Date: Tue, 30 Jan 2024 14:28:33 +0900 Subject: [PATCH 4/4] docs: fix Generator -> Faker\Generator --- user_guide_src/source/testing/fabricator/005.php | 2 +- user_guide_src/source/testing/fabricator/021.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/testing/fabricator/005.php b/user_guide_src/source/testing/fabricator/005.php index 1c233d220b9e..5a601cebc050 100644 --- a/user_guide_src/source/testing/fabricator/005.php +++ b/user_guide_src/source/testing/fabricator/005.php @@ -2,7 +2,7 @@ namespace App\Models; -use Generator; +use Faker\Generator; class UserModel { diff --git a/user_guide_src/source/testing/fabricator/021.php b/user_guide_src/source/testing/fabricator/021.php index a7afe497f330..af6297f53946 100644 --- a/user_guide_src/source/testing/fabricator/021.php +++ b/user_guide_src/source/testing/fabricator/021.php @@ -3,7 +3,7 @@ namespace App\Models; use CodeIgniter\Test\Fabricator; -use Generator; +use Faker\Generator; class UserModel {