Mill 是一个工厂组件,可以帮助您为 Silverstripe 生成假记录

安装: 428

依赖关系: 11

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:silverstripe-vendormodule

v2.0.3 2024-05-16 03:01 UTC

README

Silverstripe Version Package Version Total Downloads License

Mill 是一个工厂组件 🏗️,可以帮助您为 Silverstripe 生成假记录,无论是用于测试应用程序还是处理某些自动化。

如果您已经厌倦了不断添加测试内容和占位符来测试应用程序及其组件,您现在可以不用再管它了,Mill 会为您处理。

Mill 使用 FakerPHP 作为假内容供应商。有关您可以在自己的 Mill 中使用的可用格式器的更多信息,请参阅 可用格式器的完整列表

安装

composer require goldfinch/mill

可用的 Taz 命令

如果您之前没有使用过 Taz🌪️,则必须在您的根项目文件夹中存在 taz 文件 cp vendor/goldfinch/taz/taz taz

创建新的 Mill

php taz make:mill

用例示例

让我们创建一个新的名为 Article 的 Mill,它将为我们的 Article 模型生成假记录。

1. 创建新的 Mill

使用 Taz🌪️ 生成您的 Mill。它将快速引导您完成设置,并为您处理。

php taz make:mill Article
# What [class name] does this mill is going to work with? : App\Models\Article

2. 准备您的 Mill

使用 Taz 进一步修改最近创建的 Mill,并为它的字段准备合适的假数据。

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. 使您的模型可 Mill

最后,您需要将 Millable 特性添加到 Mill 将要与之一起工作的模型中

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. 使用 Mill

现在,我们应该能够调用 Article 模型上的 Mill 来生成假记录。有几种方法可以实现

生成 10 篇文章

App\Models\Article::mill(10)->make();

生成一篇文章,覆盖其中一些字段

App\Models\Article::mill(1)->make([
    'Title' => 'Custom article title',
    'Content' => 'Custom text',
]);

生成 10 篇文章,并为每一篇添加随机类别(映射)

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);
    }
  });

推荐

此模块与 harvest seeder goldfinch/harvest 一起使用得很好

许可证

MIT 许可证 (MIT)