Skip to content

Commit

Permalink
Merge pull request codeigniter4#8476 from kenjis/docs-fabricator.rst
Browse files Browse the repository at this point in the history
docs: update fabricator.rst
  • Loading branch information
kenjis authored Jan 30, 2024
2 parents 057bcbd + dcd9451 commit f05859e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
3 changes: 2 additions & 1 deletion user_guide_src/source/testing/fabricator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -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:

Expand Down
18 changes: 10 additions & 8 deletions user_guide_src/source/testing/fabricator/005.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@

namespace App\Models;

use Faker\Generator;

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,
];

/*
* 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,
]);
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/testing/fabricator/006.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
// ...
}
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/testing/fabricator/008.php
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 5 additions & 2 deletions user_guide_src/source/testing/fabricator/021.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace App\Models;

use CodeIgniter\Test\Fabricator;
use Faker\Generator;

class UserModel
{
protected $table = 'users';

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')),
];
}
Expand Down

0 comments on commit f05859e

Please sign in to comment.