stwarog/fuel-fixtures

为Fuel/ORM包提供简单的固定数据工厂工具

2.0.0 2022-01-28 13:07 UTC

This package is auto-updated.

Last update: 2024-09-28 19:26:02 UTC


README

此包是一个生成常用数据固定数据的简单实用工具。

基本用法

文档将完成。考虑以下Currency模型固定数据示例

<?php

namespace Tests\Fixtures;

use Model_Orm_Currency;
use Stwarog\FuelFixtures\Fuel\Factory;

final class CurrencyFixture extends Factory
{
    # Constants representing the "state" name
    public const USD = 'usd';
    public const EUR = 'eur';
    public const PLN = 'pln';

    
    /** @return array<string, string> */
    public function getDefaults(): array
    {
        # This values must be as random as possible
        return [
            'code' => $this->faker->countryISOAlpha3,
            'rate' => $this->faker->randomFloat(1, 1, 10),
        ];
    }

    # Class name for wchich we are going to create a new instance
    public static function getClass(): string
    {
        return Model_Orm_Currency::class;
    }

    /** array<string, callable> */
    public function getStates(): array
    {
        # Main method, returning list of available states (used by calling "with" method)
        return [
            self::USD => function (Model_Orm_Currency $model, array $attributes = []) {
                $model->code = 'USD';
                $model->rate = 1.0;
            },
            self::EUR => function (Model_Orm_Currency $model, array $attributes = []) {
                $model->code = 'EUR';
                $model->rate = 0.8418;
            },
            self::PLN => function (Model_Orm_Currency $model, array $attributes = []) {
                $model->code = 'PLN';
                $model->rate = 3.5896;
            },
        ];
    }
}

每个工厂提供一些非常重要的方法(类似于Laravel的)

    /**
     * @param array<string, mixed> $attributes
     * @return Model<array>
     */
    public function makeOne(array $attributes = []): Model;

    /**
     * @param array<string, mixed> $attributes
     * @return array<Model>
     */
    public function makeMany(array $attributes = [], int $count = 5): array;

    /**
     * @param array<string, mixed> $attributes
     * @return Model<array>
     */
    public function createOne(array $attributes = []): Model;

    /**
     * @param array<string, mixed> $attributes
     * @return array<Model>
     */
    public function createMany(array $attributes = [], int $count = 5): array;

MakeOne/Many - 创建新实例。

CreateOne/Many - 创建新实例并使用提供的持久性策略持久化。

使用with的示例调用

    $fixture = CurrencyFixture::initialize();
    # All states will be executed in the consecutive order
    $fixture->with('usd', 'rate1000', fn(Model $m, array $attrs = []) => $m->rate = 1.5)->makeOne();

开发

标准

此包遵循PSR-4自动加载和PSR-12样式。

有用命令

整个项目已通过单元测试,并由强大的静态代码分析(phpstan)保护。

make unit # for unit testing
make phpstan # for phpstan validation
make cs     # for phpcs validation
make cs_fix # for phpcbf auto fix attempt

代码已通过Docker容器化,并通过makefile简化。只需运行

make # to execute all mandatory quality check commands

如果您无法在本地运行make文件,则请检查composer.json中的直接命令。

事件

存在一个事件派发器的抽象(默认为NullObject实现)PSR-14

目的是在具体情况下添加从外部修改准备好的模型数据的能力。

您可以通过依赖关系初始化具体的分发器,然后访问预定义的事件

变更日志

1.2.0 (2021-12-02) 重大变更

  • 重构 - 添加了Factory依赖项的Config,因为它的数量迅速增长
  • 添加PSR容器依赖项
  • 修复了引用类型(在getStates中),以了解容器并避免在条目作为DI存在时重新创建新实例

1.1.0 (2021-12-02)

  • 添加了事件派发器抽象和事件:BeforePersisted,ModelPrepared