diff --git a/user_guide_src/source/testing/fabricator.rst b/user_guide_src/source/testing/fabricator.rst index da621c83b233..e2a4e5910ead 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 `_ to turn models into generators +uses `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:: @@ -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: diff --git a/user_guide_src/source/testing/fabricator/005.php b/user_guide_src/source/testing/fabricator/005.php index c61fa527125e..5a601cebc050 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 Faker\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 @@ 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..af6297f53946 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 Faker\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')), ]; }