analogue/factory
Composer 安装命令:
composer require analogue/factory
包简介
Implementation of Illuminate database factory for Analogue orm
README 文档
README
This package adds support the laravel's Model Factory' functionnality to the Analogue datamapper ORM.
Installation
Add this line to your composer.json file :
composer require analogue/factory
Configuration
Add the Service Provider to config/app.php :
'Analogue\Factory\FactoryServiceProvider',
Usage
Analogue Factory uses the same definitions mechanism Eloquent's does. Out of the box, this file provided with the default Laravel's install contains one factory definition:
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
Within the Closure, which serves as the factory definition, you may return the default test values of all attributes on the model. The Closure will receive an instance of the Faker PHP library, which allows you to conveniently generate various kinds of random data for testing.
Of course, you are free to add your own additional factories to the ModelFactory.php file.
Multiple Factory Types
Sometimes you may wish to have multiple factories for the same Analogue Entity class. For example, perhaps you would like to have a factory for "Administrator" users in addition to normal users. You may define these factories using the defineAs method:
$factory->defineAs(App\User::class, 'admin', function ($faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => str_random(10),
'remember_token' => str_random(10),
'admin' => true,
];
});
Instead of duplicating all of the attributes from your base user factory, you may use the raw method to retrieve the base attributes. Once you have the attributes, simply supplement them with any additional values you require:
$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
$user = $factory->raw(App\User::class);
return array_merge($user, ['admin' => true]);
});
Using Factories In Tests
Once you have defined your factories, you may use them in your tests or database seed files to generate model instances using the global analogue_factory function. So, let's take a look at a few examples of creating models. First, we'll use the make method, which creates models but does not save them to the database:
public function testDatabase()
{
$user = analogue_factory(App\User::class)->make();
// Use entity in tests...
}
If you would like to override some of the default values of your entities, you may pass an array of values to the make method. Only the specified values will be replaced while the rest of the values remain set to their default values as specified by the factory:
$user = analogue_factory(App\User::class)->make([
'name' => 'Abigail',
]);
You may also create a Collection of many models or create models of a given type:
// Create three App\User instances...
$users = analogue_factory(App\User::class, 3)->make();
// Create an App\User "admin" instance...
$user = analogue_factory(App\User::class, 'admin')->make();
// Create three App\User "admin" instances...
$users = analogue_factory(App\User::class, 'admin', 3)->make();
Persisting Factory Entities
The create method not only creates the model instances, but also saves them to the database using Analogue's Mapper store() method.
public function testDatabase()
{
$user = analogue_factory(App\User::class)->create();
// Use entity in tests...
}
Again, you may override attributes on the model by passing an array to the create method:
$user = analogue_factory(App\User::class)->create([
'name' => 'Abigail',
]);
Building complex Entities
By recursively calling the analogue_factory function, you can generate complex Entities very easily :
$factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence,
'content' => $faker->text,
];
});
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'posts' => analogue_factory(App\Post::class, 10),
];
});
You can even persist these complex entities with a single call :
$users = analogue_factory(App\User::class, 3)->create();
analogue/factory 适用场景与选型建议
analogue/factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.43k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 analogue/factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 analogue/factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-02