Skip to content

Commit 31afcfa

Browse files
committed
Add documentation
1 parent be5e8a2 commit 31afcfa

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Testing
4444
- **CLI:** The new ``InputOutput`` class was added and now you can write tests
4545
for commands more easily if you use ``MockInputOutput``.
4646
See :ref:`using-mock-input-output`.
47+
- **Fabricator:** The Fabricator class now has the ``setUnique()``, ``setOptional()`` and ``setValid()``
48+
methods to allow calling of Faker's modifiers on each field before faking their values.
4749
- **TestResponse:** TestResponse no longer extends ``PHPUnit\Framework\TestCase`` as it
4850
is not a test. Assertions' return types are now natively typed ``void``.
4951

user_guide_src/source/testing/fabricator.rst

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

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

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

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)