承接 sizuhiko/fabricate 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

sizuhiko/fabricate

Composer 安装命令:

composer require sizuhiko/fabricate

包简介

PHP data generator for Testing

README 文档

README

Build Status Coverage Status Total Downloads Latest Stable Version

Fabricate

PHP data generator for Testing

It's inspired on Fabrication and factory-girl from the Ruby world.

Fabricate is a simple fake object generation core library for PHP. Quickly Fabricate objects as needed anywhere in your app or test case.

If you use CakePHP2, please see cakephp2 branch.

Install

Add require-dev in your composer.json

composer require --dev sizuhiko/fabricate

Usage

Adaptor

At first, Fabricate require to config for using. For example, to override these settings, put a bootstrap.php in your app folder and append the path to phpunit.xml

use Fabricate\Fabricate;
use CakeFabricate\Adaptor\CakeFabricateAdaptor;

Fabricate::config(function($config) {
    $config->adaptor = new CakeFabricateAdaptor();
});

Fabricate doesn't provide adaptors. If you will make adaptor of any frameworks, send us your pull request. The pull request will include suggestion into composer.json and link of repository on README(Comunity Adaptors).

Comunity Adaptors

The Basics

The simplest way to generate objects

Fabricate::create('Post')

That will generate and save to database an instance of Post using the schema information.

To set additional attributes or override what is in the Fabricator, you can pass a array to Fabricate with the fields you want to set.

Fabricate::create('Post', ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"])

Fabricating With Blocks

In addition to the array, you can pass a callback function to Fabricate and all the features of a Fabricator definition are available to you at object generation time.

Fabricate::create('Post', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

The hash will overwrite any fields defined in the callback function.

APIs

Configuration

To override these settings, put a bootstrap.php in your app folder and append the path to phpunit.xml

Fabricate::config(function($config) {
    $config->sequence_start = 1;
    $config->adaptor = new Fabricate\Adaptor\CakePHPAdaptor();
    $config->faker = \Faker\Factory::create('ja_JP');
});

Supported Options

sequence_start

Allows you to specify the default starting number for all sequences. This can still be overridden for specific sequences.

Default: 1

adaptor

Adapters ease the population of databases through the Database accessor provided by an ORM library(or framework).

Default: null

faker

Allow you to specify the default Faker instance to return localized data.

Default: default locale(en_EN) instance

text_size_limit

Allow you to specify the limit size for generation of text field.

Default: 200

from v1.2.3

Generate model attributes as array (not saved)

Fabricate::attributes_for(:model_name, :number_of_generation, :array_or_callback) generate only attributes.

  • model_name: Model class name.
  • number_of_generation: Generated number of records
  • array_or_callback: it can override each generated attributes

Example

$results = Fabricate::attributes_for('Post', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

// $results is followings :
array (
  0 =>
  array (
    'id' => 1,
    'title' => 'Lorem ipsum dolor sit amet',
    'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
    'created' => '2013-10-09 12:40:28',
    'updated' => '2013-10-09 12:40:28',
  ),
  1 =>
  array (
  ....

Generate a model instance (not saved)

Fabricate::build(:model_name, :array_or_callback) generate a model instance (using ClassRegistry::init).

  • model_name: Model class name.
  • array_or_callback: it can override each generated attributes

Example

$result = Fabricate::build('Post', function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

// $results are depends adaptor result.
 ......

Generate records to database

Fabricate::create(:model_name, :number_of_generation, :array_or_callback) generate and save records to database.

  • model_name: Model class name.
  • number_of_generation: Generated number of records
  • array_or_callback: it can override each generated attributes

Example

Fabricate::create('Post', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

Defining

Fabricate has a name and a set of attributes when fabricating objects. The name is used to guess the class of the object by default,

Fabricate::define('Post', ['published'=>'1']);
// or using callback block
Fabricate::define('Post', function($data, $world) {
    return ['published'=>'1']
});

To use a different name from the class, you must specify 'class'=>:class_name into first argument as array.

Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);

Fabricate::create('PublishedPost');

You can inherit attributes from other defined set of attributes by using the 'parent' key.

Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::define(['Author5PublishedPost', 'parent'=>'PublishedPost'], ['author_id'=>'5']);

Fabricate::create('Author5PublishedPost');

Associations

It's possible to set up associations(hasOne/hasMany/belongsTo) within Fabricate::create(). You can also specify a FabricateContext::association(). It will generate the attributes, and set(merge) it in the current array.

Usage

Fabricate::create('User', function($data, $world) {
    return [
        'user' => 'taro',
        'Post' => $world->association('Post', 3),
    ];
});
// or can overwritten by array or callback block.
Fabricate::create('User', function($data, $world) {
    return [
        'user' => 'taro',
        'Post' => $world->association('Post', 3, function($data, $world) {
            return ['title'=>$world->sequence('Post.title',function($i){ return "Title-${i}"; })];
        }),
    ];
});
// can use defined onbject.
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('User', function($data, $world) {
    return [
        'user' => 'taro',
        'Post' => $world->association(['PublishedPost', 'association'=>'Post'], 3),
    ];
});
// can use association alias (Post belongs to Author of User class)
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('PublishedPost', 3, function($data, $world) {
    return [
        'Author' => $world->association(['User', 'association'=>'Author'], ['id'=>1,'user'=>'taro']),
    ];
});

Sequences

A sequence allows you to get a series of numbers unique within the each generation function. Fabrication provides you with an easy and flexible means for keeping track of sequences.

Config

Allows you to specify the default starting number for all sequences. This can still be overridden for specific sequences.

Fabricate::config(function($config) {
    $config->sequence_start = 100;
});

Usage

Fabricate::config(function($config) {
    $config->sequence_start = 100;
});
$results = Fabricate::attributes_for('Post', 10, function($data, $world){
    return [
        'id'=> $world->sequence('id'),
        'title'=> $world->sequence('title', 1, function($i){ return "Title {$i}"; })
    ];
});

// $results is followings :
array (
  0 =>
  array (
    'id' => 100,           // starting configure sequence
    'title' => 'Title 1',  // closure function returned
    ...
  ),
  1 =>
  array (
    'id' => 101,           // starting configure sequence
    'title' => 'Title 2',  // closure function returned
    ...

If you want use sequence within generation function, callback has second attribute. $world is FabricateContext instance. It have sequence method.

FabricateContext#sequence API

You should set name argument to sequence.

$world->sequence('id')

If you want to specify the starting number, you can do it with a second parameter. It will always return the seed number on the first call and it will be ignored with subsequent calls.

$world->sequence('id', 10)

If you are generating something like an unique string, you can pass it a callback function and the callback function response will be returned.

$world->sequence('title', function($i){ return "Title {$i}"; }
// or with start number
$world->sequence('title', 1, function($i){ return "Title {$i}"; }

Traits

Traits allow you to group attributes together and then apply them to any fabricating objects.

Fabricate::define(['trait'=>'published'], ['published'=>'1']);
Fabricate::create('Post', function($data, $world) {
    $world->traits('published');
    return ['author_id'=>5];
});

traits can specify defined names as array

Fabricate::define(['trait'=>'published'], ['published'=>'1']);
Fabricate::define(['trait'=>'author5'],   function($data, $world) { return ['author_id'=>5]; });
Fabricate::create('Post', function($data, $world) {
    $world->traits(['published','author5']);
    return [];
});

Faker

Faker is a PHP library that generates fake data for you. Fabrication provides you with generation custom value for own rule.

Config

Faker supports a localization. The default locale is en_EN. Allows you to specify the locale. This can still be overridden for specific Faker Factory.

Fabricate::config(function($config) {
    $config->faker = Faker\Factory::create('ja_JP');
});

Usage

Fabricate::config(function($config) {
    $config->faker = Faker\Factory::create('ja_JP'); // this is optional
});

$results = Fabricate::attributes_for('User', function($data, $world){
    return [
        'user'=> $world->faker()->name
    ];
});

Reloading

If you need to reset fabricate back to its original state after it has been loaded.

Fabricate::clear();

Contributing to this Library

Please feel free to contribute to the library with new issues, requests, unit tests and code fixes or new features. If you want to contribute some code, create a feature branch from develop, and send us your pull request.

sizuhiko/fabricate 适用场景与选型建议

sizuhiko/fabricate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 636.6k 次下载、GitHub Stars 达 64, 最近一次更新时间为 2013 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sizuhiko/fabricate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sizuhiko/fabricate 我们能提供哪些服务?
定制开发 / 二次开发

基于 sizuhiko/fabricate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 636.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 64
  • 点击次数: 8
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 64
  • Watchers: 9
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-10-09