Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update fabricator.rst #8476

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Generator;

paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
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 Generator;
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved

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