goldfinch / mill
Mill is a factory component that helps you to generate fake records for Silverstripe
Installs: 447
Dependents: 11
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:silverstripe-vendormodule
Requires
- php: >=8.0
- fakerphp/faker: ^1.23
- goldfinch/taz: ^2.0
- silverstripe/admin: ^2.0
- silverstripe/framework: ^5.0
README
Mill is a factory component 🏗️ that helps you to generate fake records for Silverstripe whether to test your application or handle some automation.
If you got tired of constantly adding test content and placeholders to test your application and its components, you can forget about it, Mill will handle it for you.
Mill is using FakerPHP as a fake content supplier. To learn more about available formatters you can use in your own mills, please refer to the full list of available formatters.
Install
composer require goldfinch/mill
Available Taz commands
If you haven't used Taz🌪️ before, taz file must be presented in your root project folder cp vendor/goldfinch/taz/taz taz
Create new mill
php taz make:mill
Use Case example
Let's create a new mill calling Article that will generate fake records for our Article model.
1. Create new mill
Use Taz🌪️ to generate your Mill. It will quickly lead you through the setup and take care of it for you.
php taz make:mill Article
# What [class name] does this mill is going to work with? : App\Models\Article
2. Prepare your mill
Modify further the recently created mill by Taz and prepare suitable fake data for its fields.
namespace App\Mills; use Goldfinch\Mill\Mill; class ArticleMill extends Mill { public function factory(): array { return [ 'Title' => $this->faker->catchPhrase(), 'Summary' => $this->faker->sentence(200), 'Content' => $this->faker->paragraph(10), 'Date' => $this->faker->dateTimeBetween('-8 week')->format('Y-m-d H:i:s'), 'Publisher' => $this->faker->name(), 'Email' => $this->faker->email(), 'Phone' => $this->faker->e164PhoneNumber(), 'Address' => $this->faker->address(), 'Country' => $this->faker->country(), ]; } }
3. Make your model millable
Lastly, you need to add Millable
trait to the model this mill is going to work with:
namespace App\Models; use SilverStripe\ORM\DataObject; use Goldfinch\Mill\Traits\Millable; class Article extends DataObject { use Millable; private static $db = [ 'Title' => 'Varchar', 'Summary' => 'Text', 'Content' => 'HTMLText', 'Date' => 'Datetime', 'Publisher' => 'Varchar', 'Email' => 'Varchar', 'Phone' => 'Varchar', 'Address' => 'Varchar', 'Country' => 'Varchar', ]; // .. }
4. Use mill
Now, we should be able to call mill on our Article model to generate fake records. There are several ways to do so:
Generate 10 articles:
App\Models\Article::mill(10)->make();
Generate one article, overwriting some of its fields:
App\Models\Article::mill(1)->make([ 'Title' => 'Custom article title', 'Content' => 'Custom text', ]);
Generate 10 articles and add random categories for each (mapping):
App\Models\Article::mill(10) ->make() ->each(function ($item) { $categories = App\Models\ArticleCategory::get()->shuffle()->limit(rand(0, 4)); foreach ($categories as $category) { $item->Categories()->add($category); } });
Recommendation
This module plays nicely with harvest seeder goldfinch/harvest
License
The MIT License (MIT)