silvertipsoftware/factorygirl
Composer 安装命令:
composer require --dev silvertipsoftware/factorygirl
包简介
A port of FactoryGirl to Laravel4/Eloquent
关键字:
README 文档
README
FactoryGirl is a (or hopes to be) a relatively faithful port of thoughtbot/factory_girl, a Ruby-land object factory library for testing.
Installation
Composer
Add silvertipsoftware/factorygirl to the require-dev section of your composer.json:
"require-dev": { "silvertipsoftware/factorygirl": "dev-master" }
You can obviously choose any available version(s). Run composer update to get it.
Laravel
Add the FactoryGirl ServiceProvider to your Laravel application:
'providers' => array( ... 'SilvertipSoftware\FactoryGirl\FactoryGirlServiceProvider', ... ),
(optional) Add the following snippets to your app/config/app.php file:
'aliases' => array( ... 'Factory' => 'SilvertipSoftware\FactoryGirl\Facades\FactoryGirl', ... ),
Supported PHP Versions
Currently, PHP 5.3+ is supported. PHP 5.4 would make some things nicer, so that may change for future releases.
Documentation
See FactoryGirl documentation to get a feel for what it does and is used for. The syntax is hopefully a straightforward port to PHP.
Factory Definitions
The app/tests/factories.php file is used to store the factory definitions, and is automatically loaded on the first build/create on an object.
Basic Factory
Factory::define('room', function($f) { return array( 'name' => 'Meeting Room', 'capacity' => 5, 'notes' => 'Great views' }); });
defines a factory for a Room Eloquent model, and sets the name,capacity, and notes attributes to the given values. The model class name is inferred from the factory name. Where that's not intended, the model class name can also be specified:
Factory::define('large_room', function($f) { return array( 'name' => 'Banquet Hall', 'capacity' => 200 ); }, array( 'class' => 'Room' ));
Building/Creating Objects
With the factory defined above, your test code can do:
$room = Factory::build('room'); $another = Factory::build('large_room');
to get new room instances, which are not saved to the database. If you want them persisted, use create:
$room = Factory::create('room');
In either case, attributes may be overridden by passing an array as a second parameter:
$extra_large_room = Factory::create('large_room', array( 'capacity' => 1000 ));
Sequences
Sequences return a value based on a increasing index passed to the closure. Useful for creating uni que attributes in a standard way.
Factory::sequence('email', function($n) { return 'noreply'.$n'.@somedomain.com'; });
Then, in a factory, you can use the sequence by:
Factory::define('user', function($f) { 'username' => 'Joe Public', 'email' => $f->next('email'), 'status' => 'active' });
Associations
Given a factory for an account model, you can associate a user model to it with:
Factory::define('user', function($f) { return array( 'username' => 'Joe Public', 'email' => $f->next('email'), 'account' => $f->associate() ); });
When a user is built, an account instance will be created and the account_id of user will be set to reference the new account. Currently, only belongsTo relations are supported, and the only "build strategy" is to save the associated object in the database.
Attribute values in the associated object can be overridden by passing an array:
... 'account' => $f->associate( array( 'plan' => 'platinum' )) ...
The factory to use is inferred from the attribute name. If a different factory is desired, pass it to the associate call:
Factory::define('user', function($f) { return array( 'username' => 'Richie Rich', 'email' => $f->next('email'), 'account' => $f->associate('paid_account') ); });
Or, specify both the factory and overrides:
Factory::define('user', function($f) { return array( 'username' => 'Richie Rich', 'email' => $f->next('email'), 'account' => $f->associate('paid_account', array( 'plan' => 'platinum' )) ); });
Closures as Attributes
A closure can also be passed as an attribute value, and it is evaluated during model build. This lets you do more complex logic at build-time. This is particularly useful for 3-object associations. For example:
Factory::define('room', function($f) { return array( 'name' => 'Meeting Room', 'account' => $f->associate(), 'location' => function($room,$f) { return $f->associate( array( 'account' => $room['account'] ); } ); });
Attribute values are evaluated in the order they are given in the factory definition, so swapping account and location above would not have worked.
Inheritance
Factories can be chained to reuse common attribute definitions as follows:
Factory::define('room', function($f) { return array( 'name' => 'Room 100', 'capacity' => 5 ); }); Factory::define('room_with_notes', function($f) { return array( 'notes' => 'This is a nice room.' ); }, array( 'parent' => 'room' )); $room = Factory::build('room_with_notes'); echo $room->name; // Room 100 echo $room->notes; // This is a nice room.
silvertipsoftware/factorygirl 适用场景与选型建议
silvertipsoftware/factorygirl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.06k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2013 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「laravel」 「factorygirl」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 silvertipsoftware/factorygirl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 silvertipsoftware/factorygirl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 silvertipsoftware/factorygirl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy test stubs
Testing Suite For Lumen like Laravel does.
Fixtures replacement with a straightforward definition syntax
Convenient creation of Doctrine entities in tests. Like Ruby's FactoryGirl.
The PHP SDK for Checkmango
Alfabank REST API integration
统计信息
- 总下载量: 13.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-03-17