apie/faker
Composer 安装命令:
composer require apie/faker
包简介
Composer package of the apie library: faker
README 文档
README
faker
This package is part of the Apie library. The code is maintained in a monorepo, so PR's need to be sent to the monorepo
Documentation
This package adds a method to the library Faker to fake domain object and value object contents.
Because of the recursive nature this is the easiest setup:
<?php use Apie\Faker\ApieObjectFaker; use Faker\Factory; $faker = Factory::create(); $faker->addProvider(ApieObjectFaker::createWithDefaultFakers($faker)); // returns a random Gender enum value. $faker->fakeClass(Gender::class); class User implements EntityInterface { // ... public function __construct(private Gender $gender, private FirstName $firstName, private LastName $lastName) { } } // creates a User with random constructor arguments. $faker->fakeClass(User::class);
String value objects with regular expressions.
String value objects that have the trait IsStringWithRegexValueObject and implement HasRegexValueObjectInterface will be faked easily by using the regular expression to make a valid value object.
Adding custom support
You can create a class implementing ApieClassFaker to make your own fake methods. You can reuse the methods available in Faker itself.
<?php use Apie\Faker\Interfaces\ApieClassFaker; use Faker\Generator; use ReflectionClass; class SpecificClassFaker implements ApieClassFaker { public function supports(ReflectionClass $class): bool { return $class->name === SpecificValueObject::class; } public function fakeFor(Generator $generator, ReflectionClass $class): SpecificValueObject { return new SpecificValueObject($generator->randomElement([1, 2, 3])); } }
Adding a createRandom method in the object itself.
Instead of making many ApieClassFaker methods you can make a method on the value object that will result in a random value object.
<?php use Apie\Core\Attributes\FakeMethod; use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; #[FakeMethod("createRandom")] class SpecificClass implements ValueObjectInterface { public static function createRandom(): self { return new self(rand(1, 3)); } }
You can also provide arguments to this method or the Faker generator itself:
<?php use Apie\Core\Attributes\FakeMethod; use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; use Faker\Generator; #[FakeMethod("createRandom")] class SpecificClass implements ValueObjectInterface { public static function createRandom(Generator $generator, int $maximum): self { return new self($generator->numberBetween(1, $maximum)); } }
In this case calling $faker->fakeClass(SpecificClass::class); will run SpecificClass::createRandom() with the faker and a random integer.
apie/faker 适用场景与选型建议
apie/faker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.05k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 apie/faker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 apie/faker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-21