stwarog/fuel-fixtures
Composer 安装命令:
composer require stwarog/fuel-fixtures
包简介
Simple fixtures factory utils for Fuel/ORM package
README 文档
README
This package is a simple util for generating commonly used data fixtures.
Basic usage
Documentation will be fulfilled. Consider the following Fixture example for Currency model:
<?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; }, ]; } }
Each of factory provides few very important methods (like Laravel`s one):
/** * @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 - creates new instance.
CreateOne/Many - creates new instance and persists using provided persistence strategy.
Sample call using 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();
Development
Standards
This package follows PSR-4 for autoloading and PSR-12 for styling.
Useful commands
The whole project is Unit tested and protected with strong static code analytics (phpstan).
make unit # for unit testing
make phpstan # for phpstan validation
make cs # for phpcs validation make cs_fix # for phpcbf auto fix attempt
Code is dockerized and simplified by makefile. Simply run:
make # to execute all mandatory quality check commands
If you can't run make file locally, then checkout the direct commands in composer.json.
Events
There is an abstraction of event dispatcher PSR-14 with NullObject implementation by default.
The intention is to add a capability to modify prepared model data in concrete situations, from the outside.
You can initialize concrete dispatcher by dependency and then access predefined events:
| Name | Class | Description |
|---|---|---|
| model.before.prepared | BeforePrepared | Called right before any states (closures) has been applied. |
| model.after.prepared | AfterPrepared | Called right after all states (closures) has been applied and before persistence event. |
| model.before.persisted | BeforePersisted | Called right before prepared model is persisted in DB e.g. classes UowPersistence, FuelPersistence. |
| model.after.persisted | AfterPersisted | Called right after prepared model is persisted in DB. |
Change Log
1.2.0 (2021-12-02) BREAKING CHANGE
- Refactor - added Config for Factory Dependency as amount of it grows up to quickly
- Added PSR container dependency
- Fixed Reference type (in getStates), to be aware of Container and avoid re-creating new instance if entry is present as DI
1.1.0 (2021-12-02)
- Added event dispatcher abstraction, and events: BeforePersisted, ModelPrepared
stwarog/fuel-fixtures 适用场景与选型建议
stwarog/fuel-fixtures 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 870 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 stwarog/fuel-fixtures 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stwarog/fuel-fixtures 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 870
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-26