ensi / test-factories
此包已被废弃且不再维护。未建议替代包。
0.3.3
2024-05-07 11:10 UTC
Requires
- php: ^8.0
- fakerphp/faker: ^1.10 || ^2.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- pestphp/pest: ^1.16 || ^2.0
- php-parallel-lint/php-var-dump-check: ^0.5.0
- phpunit/phpunit: ^9.0 || ^10.0 || ^11.0
This package is auto-updated.
Last update: 2024-06-07 11:19:27 UTC
README
定义生成任何类型对象或甚至用于单元测试的数组的工厂。
安装
您可以通过composer安装此包
composer require ensi/test-factories --dev
基本用法
让我们创建一个工厂并扩展抽象工厂。您只需要定义 definition
和 make
方法。
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();
如您所见,此包使用 fakerphp/faker
生成测试数据。
您可以在 make
方法中覆盖任何字段
$customerData1 = CustomerFactory::new()->make(['user_id' => 2]);
如果目标是数组,则可以使用辅助方法 makeArray
public function make(array $extra = []): array { return $this->makeArray($extra); }
建议在状态更改方法中使用 $this->immutableSet
以确保先前创建的工厂不受影响。
生成多个对象
$customerDataObjects = CustomerFactory::new()->makeSeveral(3); // returns Illuminate\Support\Collection with 3 elements
额外的Faker方法
$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
测试
- composer install
- npm i
- composer test
安全漏洞
请查阅 我们的安全策略 了解如何报告安全漏洞。
许可
MIT许可(MIT)。有关更多信息,请参阅 许可文件