ensi/test-factories
最新稳定版本:0.3.3
Composer 安装命令:
composer require ensi/test-factories
包简介
无描述信息
README 文档
README
Define factories to generate any kind of object or even arrays for unit tests.
Installation
You can install the package via composer:
composer require ensi/test-factories --dev
Basic usage
Let's create a factory and extend abstract Factory. All you need is to define definition and make methods.
use Ensi\TestFactories\Factory; class CustomerFactory extends Factory { public ?int $id = null; public ?FileFactory $avatarFactory = null; public ?array $addressFactories = null; protected function definition(): array { return [ 'id' => $this->whenNotNull($this->id, $this->id), 'user_id' => $this->faker->randomNumber(), 'is_active' => $this->faker->boolean(), 'date_start' => $this->faker->dateTime(), 'avatar' => $this->avatarFactory?->make(), 'addresses' => $this->executeNested($this->addressFactories, new FactoryMissingValue()), ]; } public function make(array $extra = []): CustomerDTO { static::$index += 1; return new CustomerDTO($this->mergeDefinitionWithExtra($extra)); } public function withId(?int $id = null): self { return $this->immutableSet('id', $id ?? $this->faker->randomNumber()); } public function withAvatar(FileFactory $factory = null): self { return $this->immutableSet('avatarFactory', $factory ?? FileFactory::new()); } public function includesAddresses(?array $factories = null): self { return $this->immutableSet('addressFactories', $factories ?? [CustomerAddressFactory::new()]); } public function active(): self { return $this->state([ 'is_active' => true, 'date_start' => $this->faker->dateTimeBetween('-30 years', 'now'), ]); } } // Now we can use Factory like that $customerData1 = CustomerFactory::new()->make(); $customerData2 = CustomerFactory::new()->active()->make(); $customerData3 = CustomerFactory::new()->withId()->withAvatar(FileFactory::new()->someCustomMethod())->make();
As you can see the package uses fakerphp/faker to generate test data.
You can override any fields in make method:
$customerData1 = CustomerFactory::new()->make(['user_id' => 2]);
If you target is an array, then you can use a helper method makeArray:
public function make(array $extra = []): array { return $this->makeArray($extra); }
It's recommended to use $this->immutableSet in state change methods to make sure previously created factories are not affected.
Making several objects
$customerDataObjects = CustomerFactory::new()->makeSeveral(3); // returns Illuminate\Support\Collection with 3 elements
Additional Faker methods
$this->faker->randomList(fn() => $this->faker->numerify(), 0, 10) // => ['123', ..., '456'] $this->faker->nullable() // equivalent for $this->faker->optional(), but work with boolean parameter or global static setting $this->faker->exactly($value) // return $value. Example: $this->faker->nullable()->exactly(AnotherFactory::new()->make()) $this->faker->carbon() // return CarbonInterface $this->faker->dateMore() $this->faker->modelId() // return unsigned bit integer value
Contributing
Please see CONTRIBUTING for details.
Testing
- composer install
- npm i
- composer test
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.
ensi/test-factories 适用场景与选型建议
ensi/test-factories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「factory」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ensi/test-factories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ensi/test-factories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ensi/test-factories 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing object factory for PHP
Testing object factory for PHP
Laravel 5 Model Factory Generator.
A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects
Extensions for Laravel model factories
This library tries to create a simple promise factory standard while waiting for a psr.
统计信息
- 总下载量: 62.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04