定制 jainec/hyperf-fakie 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jainec/hyperf-fakie

最新稳定版本:v0.1.7

Composer 安装命令:

composer require jainec/hyperf-fakie

包简介

Test object factory for Hyperf PHP

README 文档

README

This PHP Hyperf library aim to help you generate objects fully populated with fake/random data for testing purposes.

Latest Stable Version Total Downloads License PHP Version Require

Installation

composer require jainec/hyperf-fakie

Configuration

Publish the config file so you can define your own rules

php bin/hyperf.php vendor:publish jainec/hyperf-fakie

The config file will show up in the following path:

config/autoload/fakie.php

Here in the file you can define your own creation rules for specific classes and properties

<?php

declare(strict_types=1);

return [
    'rules' => [
        'App\Entity\User' => [
            'type' => array_rand(['CONSUMER', 'SELLER']),
        ],
        'App\Entity\Order' => [
            'type' => array_rand(['VIRTUAL', 'PHYSICAL']),
        ],
        'Class' => [
            'property1' => 'rule1',
            'property2' => 'rule2',
        ],
    ],
];

Usage

Simple usage

Without hyperf-fakie

// Ex.: Generating an OrderHistory object for testing purposes
$order = new Order(
    id: rand(),
    type: array_rand(['VIRTUAL', 'PHYSICAL']),
    amount: rand(),
    value: rand() / 100,
);

$user = new User(
    name: Str::random(),
    telephone: Str::random(),
    city: Str::random(),
);

$order_history = new OrderHistory(
    id: rand(),
    description: Str::random(),
    order: $order,
    user: $user,
);

See how it's simple with hyperf-Fakie

// Ex.: Generating an OrderHistory object for testing purposes with fakie
$order_history = Fakie::object(OrderHistory::class)->create();

Overriding properties values

$user = Fakie::object(OrderHistory::class)->create([
    'description' => 'Specific description for specific test case'
]);

Using a method to build the object other than the __construct()

You can use this feature only if your object can be built using a method that accepts an array with the [properties => values] as argument

$user = Fakie::object(UserDTO::class, 'fromArray')->create();

In this case Fakie will create and assign random values to all properties of the class. If there are properties your method don't need to receive, you can remove them using the excludeProperties

$user = Fakie::object(UserDTO::class, 'fromArray')->excludeProperties(['type', 'age'])->create();

In the example above the properties type and age will not be used and will not be passed to fromArray method

Important

  • Fakie uses the classes properties types to generate and assign random values to them
  • If a property doesn't have a defined type, a default string value will be assigned to it
  • If a property type is array or array<Type> it's recommended to you define your own rules in the config fakie.php file. Because we know with PHP we cannot be sure about what is expected inside an array
  • If a property type is abstract/interface, you need to define your own rules for these cases specifying a concrete class
  • You can also use Fakie objects to specify rules in you config file:
<?php

declare(strict_types=1);

return [
    'rules' => [
        'App\Entity\OrderHistory' => [
            'order' => Fakie::object(Order::class), // Don't call the create() method here
            'user' => Fakie::object(User::class, 'fromArray'), // Don't call the create() method here
        ],
    ],
];

Feel free to contribute! Help us improve Fakie! 🎉

MIT © 2022 Jaine Conceição Santos

统计信息

  • 总下载量: 27.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-30