Skip to content

Commit 16241f0

Browse files
authored
Merge pull request #5947 from kenjis/fix-docs-fabricator.rst
docs: improve fabricator.rst
2 parents 2a5322b + d9d1530 commit 16241f0

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

user_guide_src/source/testing/fabricator.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ of the time you will want to specify the formatters to use as the second paramet
4444

4545
You can also change the formatters after a fabricator is initialized by using the ``setFormatters()`` method.
4646

47-
**Advanced Formatting**
47+
Advanced Formatting
48+
-------------------
4849

4950
Sometimes the default return of a formatter is not enough. Faker providers allow parameters to most formatters
5051
to further limit the scope of random data. A fabricator will check its representative model for the ``fake()``
@@ -162,29 +163,37 @@ Your model's fake method could look like this:
162163

163164
Now creating a new user will ensure it is a part of a valid group: ``$user = fake(UserModel::class);``
164165

166+
Methods
167+
-------
168+
165169
``Fabricator`` handles the counts internally but you can also access these static methods
166170
to assist with using them:
167171

168-
**getCount(string $table): int**
172+
getCount(string $table): int
173+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
169174

170175
Return the current value for a specific table (default: 0).
171176

172-
**setCount(string $table, int $count): int**
177+
setCount(string $table, int $count): int
178+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
173179

174180
Set the value for a specific table manually, for example if you create some test items
175181
without using a fabricator that you still wanted factored into the final counts.
176182

177-
**upCount(string $table): int**
183+
upCount(string $table): int
184+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
178185

179186
Increment the value for a specific table by one and return the new value. (This is what is
180187
used internally with ``Fabricator::create()``).
181188

182-
**downCount(string $table): int**
189+
downCount(string $table): int
190+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183191

184192
Decrement the value for a specific table by one and return the new value, for example if
185193
you deleted a fake item but wanted to track the change.
186194

187-
**resetCounts()**
195+
resetCounts()
196+
^^^^^^^^^^^^^
188197

189198
Resets all counts. Good idea to call this between test cases (though using
190199
``CIUnitTestCase::$refresh = true`` does it automatically).

user_guide_src/source/testing/fabricator/005.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class UserModel
66
{
7+
// ...
8+
79
public function fake(Generator &$faker)
810
{
911
return [
@@ -13,5 +15,18 @@ public function fake(Generator &$faker)
1315
'avatar' => Faker\Provider\Image::imageUrl(800, 400),
1416
'login' => config('Auth')->allowRemembering ? date('Y-m-d') : null,
1517
];
18+
19+
/*
20+
* Or you can return a return type object.
21+
22+
return new User([
23+
'first' => $faker->firstName,
24+
'email' => $faker->email,
25+
'phone' => $faker->phoneNumber,
26+
'avatar' => Faker\Provider\Image::imageUrl(800, 400),
27+
'login' => config('Auth')->allowRemembering ? date('Y-m-d') : null,
28+
]);
29+
30+
*/
1631
}
1732
}

0 commit comments

Comments
 (0)