Skip to content

Commit cc8c99e

Browse files
committed
Add documentation
1 parent c332c29 commit cc8c99e

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

user_guide_src/source/testing/fabricator.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ a child class in your test support folder:
6262

6363
.. literalinclude:: fabricator/006.php
6464

65+
Setting Modifiers
66+
=================
67+
68+
Faker provides three special providers, ``unique()``, ``optional()``, and ``valid()``,
69+
to be called before any provider. Fabricator fully supports these modifiers by providing
70+
dedicated methods.
71+
72+
.. literalinclude:: fabricator/022.php
73+
74+
The arguments passed after the field name are passed directly to the modifiers as-is. You can refer
75+
to Faker's documentation on these modifiers for details.
76+
77+
Instead of calling each method on Fabricator, you may use Faker's modifiers directly if you are using
78+
the ``fake()`` method on your models.
79+
80+
.. literalinclude:: fabricator/023.php
81+
6582
Localization
6683
============
6784

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use App\Models\UserModel;
4+
use CodeIgniter\Test\Fabricator;
5+
6+
$fabricator = new Fabricator(UserModel::class);
7+
$fabricator->setUnique('email'); // sets generated emails to be always unique
8+
$fabricator->setOptional('group_id'); // sets group id to be optional, with 50% chance to be `null`
9+
$fabricator->setValid('age', static fn (int $age): bool => $age >= 18); // sets age to be 18 and above only
10+
11+
$users = $fabricator->make(10);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use CodeIgniter\Test\Fabricator;
6+
use Faker\Generator;
7+
8+
class UserModel
9+
{
10+
protected $table = 'users';
11+
12+
public function fake(Generator &$faker)
13+
{
14+
return [
15+
'first' => $faker->firstName(),
16+
'email' => $faker->unique()->email(),
17+
'group_id' => $faker->optional()->passthrough(mt_rand(1, Fabricator::getCount('groups'))),
18+
];
19+
}
20+
}

0 commit comments

Comments
 (0)