Model Factory
Introduction
Models can use factories to automatically generate data for your application. They're incredibly useful to get 'real' data in place without the bloat of a database dump.
Factories are defined in the database/factories
folder of the application. You
can use Faker
to generate data easily.
Defining Factories for a Model
A factory can be registered directly to a model.
use App\Models\Post;
/**
* Factory definition.
*
* @var \Mantle\Database\Factory\Factory $factory
*/
$factory->define(
Post::class,
function ( Faker $faker ) {
return [
'post_title' => $faker->sentence,
'post_content' => $faker->paragraph,
'post_status' => 'publish',
'post_type' => 'post',
];
}
);
Faking Blocks
In addition to faking normal content, Mantle includes a block provider for faking Gutenberg blocks.
Registering the Provider
To use the provider, register the provider with Faker
use Mantle\Framework\Faker\Faker_Provider;
use App\Models\Post;
$factory->define(
Post::class,
function ( Faker $faker ) {
$this->faker->addProvider( new Faker_Provider( $this->faker ) );
// ...
}
);
$this->faker->addProvider( new Faker_Provider( $this->faker ) );
Generating Blocks
Use the block
method on a Faker instance to generate a block. You can
optionally include attributes and content.
$faker->block(
'namespace/block',
'The Content',
[
'exampleAttr' => true,
'another' => false,
]
);
Which would produce this:
<!-- wp:namespace/block {"exampleAttr":true,"another":false} -->
The Content
<!-- /wp:namespace/block -->