ensi / laravel-test-factories
laravel 测试工厂
1.0.0
2024-06-26 11:41 UTC
Requires
- php: ^8.1
- fakerphp/faker: ^1.10 || ^2.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
- laravel/framework: ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.2
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- pestphp/pest: ^1.22 || ^2.0
- pestphp/pest-plugin-laravel: ^1.1 || ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.11
- spaze/phpstan-disallowed-calls: ^2.15
README
此包提供典型数据结构的工厂
安装
您可以通过composer安装此包
composer require ensi/laravel-test-factories --dev
版本兼容性
基本用法
让我们创建一个工厂并扩展抽象工厂。您只需要定义 definition
和 make
方法。
use Ensi\LaravelTestFactories\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
父类
BaseApiFactory
此类是您各种API响应/请求工厂的基础。
它还提供了 generateResponseSearch
方法,允许您生成指定端点 :search
格式的响应(此处)
BaseModelFactory
此类是您Eloquent模型工厂的基类
工厂
PaginationFactory
- 生成响应片段和分页请求的工厂PromiseFactory
- 生成GuzzleHttp\Promise\PromiseInterface
贡献
请参阅CONTRIBUTING 获取详细信息。
测试
- composer install
- composer test
安全漏洞
请查看我们的安全策略了解如何报告安全漏洞。
许可
MIT许可(MIT)。请参阅许可文件获取更多信息。