PHP code example of jeffochoa / factory-stories

1. Go to this page and download the library: Download jeffochoa/factory-stories library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jeffochoa / factory-stories example snippets


FactoryStories\Providers\StoryFactoryServiceProvider::class

$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());



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
}



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()
    {
         //
    }

}



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();
    }
}



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();
    }
}
bash
$ php artisan make:factory-story SomeClassName