jeffochoa/factory-stories
Composer 安装命令:
composer require jeffochoa/factory-stories
包简介
Create model factory stories in Laravel 5.*
README 文档
README
This package allows you to create complex model factories in separate classes reusable among your tests classes.
inspired by the Model Factories podcast on twentypercent.fm
Installing
$ composer require jeffochoa/factory-stories
If you are using Laravel 5.4 or lower, add the service provider to the app.php file:
FactoryStories\Providers\StoryFactoryServiceProvider::class
On Laravel 5.5+, the provider will be discovered automatically.
The problem
Let's say you have to do some tests over articles created with certain conditions like:
// Active articles, from Active Users, with tags attached
You may end creating something like
$user = factory(User::class)->states('active'); $tags = factory(Taxonomy::class, 3)->states('tag')->create(); $article = factory(Article::class)->create([ user_id => $user->id ]); $article->tags()->attach($tags->pluck('id')->toArray());
... or something like that.
Of course, you can always extract this to its own method in a helper class, but sometimes you may want to have each of these kinds of "stories" in its own class, even more when you need to add some extra methods to generate more complex data.
Creating new Factory Stories
After installing this package, you'll have access to a new artisan command
$ php artisan make:factory-story SomeClassName
After you run this command you should see the new file under the "database/" directory
<?php use App\Models\User; use FactoryStories\FactoryStory; class TestStory extends FactoryStory { public function build($params = []) { // here you can add your complex model factories with their relationships return factory(User::class)->create(); } // and You can add custom methods if You need to }
Using Factory Stories
Consider this UserTestClass.php
<?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ManageUsersTest extends TestCase { use DatabaseMigrations; /** @test **/ public function your_test_method() { // } }
You just need to create a new instance of the Story Class and call the create() method on it
<?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ManageUsersTest extends TestCase { use DatabaseMigrations; /** @test **/ public function your_test_method() { $article = (new ActiveUserArticleWithTags) ->times(5)->create(); } }
This will return a collection of 5 items with the object that you return in the build() method of the custom story class.
Laravel 5.4 includes "Real time facades", this allows you to use the Story Class as a facade as well just by adding the use Facades\{YourClassName} at the top of your file
<?php namespace Tests\Feature; use Tests\TestCase; use Facades\ActiveUserArticleWithTags; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ManageUsersTest extends TestCase { use DatabaseMigrations; /** @test **/ public function your_test_method() { $article = ActiveUserArticleWithTags::times(5)->create(); } }
jeffochoa/factory-stories 适用场景与选型建议
jeffochoa/factory-stories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63.37k 次下载、GitHub Stars 达 78, 最近一次更新时间为 2017 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「factory」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeffochoa/factory-stories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeffochoa/factory-stories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeffochoa/factory-stories 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing object factory for PHP
Testing object factory for PHP
Laravel 5 Model Factory Generator.
A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects
Extensions for Laravel model factories
This library tries to create a simple promise factory standard while waiting for a psr.
统计信息
- 总下载量: 63.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 78
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-02