butschster / entity-faker
基于您的类创建假实体
v2.0.0
2022-07-13 20:13 UTC
Requires (Dev)
- php: ^8.1
- fakerphp/faker: ^1.19
- laminas/laminas-hydrator: ^4.2
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
README
此包将帮助您生成假实体并将它们持久化到您的ORM中。
<?php use Butschster\EntityFaker\LaminasEntityFactory; use Laminas\Hydrator\ReflectionHydrator; use Faker\Factory as Faker; $factory = new \Butschster\EntityFaker\Factory( new LaminasEntityFactory( new ReflectionHydrator() ), Faker::create() ); class User { private string $id; private string $username; private string $email; public function __construct(string $id, string $username, string $email) { $this->id = $id; $this->username = $username; $this->email = $email; } } class SuperUser extends User { private bool $isAdmin = false; public function __construct(string $id, string $username, string $email, bool $isAdmin) { parent::__construct($id, $username, $email); $this->isAdmin = $isAdmin; } } $factory->define(User::class, function (Faker $faker, array $attributes) { return [ 'id' => $faker->uuid, 'username' => $faker->username, 'email' => $faker->email ]; }); $factory->define(SuperUser::class, function (Faker $faker, array $attributes) use($factory) { $userAttributes = $factory->raw(User::class); return $userAttributes + [ 'isAdmin' => $faker->boolean ]; });
创建并持久化实体
$user = $factory->of(User::class)->create(); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; //}
创建并持久化多个实体
$users = $factory->of(User::class)->times(10)->create(); //[ // class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; // }, // ... //]
创建并持久化具有预定义属性的实体
$user = $factory->of(User::class)->create([ 'email' => 'admin@site.com' ]); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "admin@site.com"; //}
创建实体
$user = $factory->of(User::class)->make(); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; //}
创建多个实体
$users = $factory->of(User::class)->times(10)->make(); //[ // class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "tsteuber@hotmail.com"; // }, // ... //]
创建具有预定义属性的实体
$user = $factory->of(User::class)->make([ 'email' => 'admin@site.com' ]); //class User { // private string $id = "0b13e52d-b058-32fb-8507-10dec634a07c"; // private string $username = "zetta86"; // private string $email = "admin@site.com"; //}
获取实体的原始属性
$attributes = $factory->of(SuperUser::class)->raw(); //[ // 'id' => "0b13e52d-b058-32fb-8507-10dec634a07c", // 'username' => 'zetta86', // 'email' => 'tsteuber@hotmail.com', //]
获取具有预定义值的实体的原始属性
$attributes = $factory->of(SuperUser::class)->raw([ 'email' => 'test@site.com' ]); //[ // 'id' => "0b13e52d-b058-32fb-8507-10dec634a07c", // 'username' => 'zetta86', // 'email' => 'test@site.com', //]
生成所有定义的实体的数组
$repository = $factory->make(1000); $seeds = $repository->get(User::class)->random(100); $seeds = $repository->get(SuperUser::class)->take(50);
生成所有定义的实体的原始数据数组
$repository = $factory->raw(1000); $seeds = $repository->get(User::class)->random(100); $seeds = $repository->get(SuperUser::class)->take(50);
将给定实体的原始数据数组导出到PHP文件中
$path = $factory->of(SuperUser::class)->times(1000)->export('path/to/store'); // path/to/store/SuperUser.php
将所有定义的实体的原始数据数组导出到文件中
$repository = $factory->export('path/to/store', 1000); $seeds = $repository->get(User::class)->random(100);
自定义实体构建器
您可以使用自定义持久化逻辑定义自己的EntityBuilder类。
use Butschster\EntityFaker\EntityFactoryInterface; use Faker\Factory as Faker; use Cycle\ORM\ORMInterface; use Cycle\ORM\TransactionInterface; class CycleOrmEntityFactory implements EntityFactoryInterface { private array $afterCreation = []; private array $beforeCreation = []; protected ORMInterface $orm; protected Transaction $transaction; public function __construct(ORMInterface $orm) { $this->orm = $orm; $this->beforeCreation(function () { $this->transaction = new Transaction($this->orm); }); $this->afterCreation(function () { $this->transaction->run(); }); } public function store(object $entity): void { $this->transaction->persist($entity); } public function hydrate(object $entity, array $data): object { return $this->orm->getMapper($entity)->hydrate($entity, $data); } /** * Add a callback to run after creating an entity or array of entities. * @param callable $callback */ public function afterCreation(callable $callback): void { $this->afterCreation[] = $callback; } public function afterCreationCallbacks(): array { return $this->afterCreation; } /** * Add a callback to run before creating an entity or array of entities. * @param callable $callback */ public function beforeCreation(callable $callback): void { $this->beforeCreation[] = $callback; } public function beforeCreationCallbacks(): array { return $this->beforeCreation; } } $factory = new \Butschster\EntityFaker\Factory( new CycleOrmEntityFactory(...), Faker::create() );