Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Factory States For Clean And Fluent Laravel Testing | StackCoder


Factory States For Clean And Fluent Laravel Testing


07th September 2020 2 mins read
Share On     Share On WhatsApp     Share On LinkedIn


In my previous article, I had explained why should we use Laravel factories. You can check the article at Factories To Speed Up Test-Driven Development In Laravel.


In this article let's see how we can use Factory states to write fluent test cases.


Without Factory States


First, let's see how does our test cases looks like without any factory states.


Basically, we are writing a test to check that the user cannot view any unpublished blog posts.


tests / Feature / PostsTest.php

/**
* @test
*/
public function user_cannot_view_unpublished_post()
{
    $post = factory(Post::class)->create([
        'published_at'  => null
    ]);
    $resource = $this->get("/posts/" . $post->slug);

    $resource->assertStatus(404);
}


database / factories / PostFactory.php

use App\Post;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
    return [
        'title'         => 'My first blog',
        'slug'          => Str::slug('My first blog'),
        'summary'       => 'First blog summary',
        'body'          => 'First blog body',
        'author_id'     => function () {
            return factory(User::class)->create()->id;
        },
    ];
});

With Factory States


Basically there is no problem with the above approach. Since we can write more cleaner we can use this approach.


From the above code snippet we can write the code


From

$post = factory(Post::class)->create([
    'published_at'  => null
]);

To

$post = factory(Post::class)->states(['unpublished'])->create();


Now your test case will look like the following very lean, clean & fluent


tests / Feature / PostsTest.php

/**
* @test
*/
public function user_cannot_view_unpublished_post()
{
    $post = factory(Post::class)->states(['unpublished'])->create();
    $resource = $this->get("/posts/" . $post->slug);
    $resource->assertStatus(404);
}


database / factories / PostFactory.php

use App\Post;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
    return [
        'title'         => 'My first blog',
        'slug'          => Str::slug('My first blog'),
        'summary'       => 'First blog summary',
        'body'          => 'First blog body',
        'author_id'     => function () {
            return factory(User::class)->create()->id;
        },
    ];
});

$factory->state(Post::class, 'unpublished', function (Faker $faker) {
    return [
        'published_at' => null
    ];
});

/** Similarly you can have published state too */

$factory->state(Post::class, 'published', function (Faker $faker) {
    return [
        'published_at' => Carbon::parse('-1 week')
    ];
});

Conclusion


I hope this article helped you. Please share it with your friends.


Factories To Speed Up Test-Driven Development In Laravel

Stripe Payment Integration With Laravel

Writing Clean Code In PHP

Foreign Key Migrations Simplified In Laravel 7.x

Global Data In All Laravel Blade Pages

Select2 Dropdown

Observers In Laravel

Relationship Table Data With Route Model Binding In Laravel

Getting Started With AMP (Accelerated Mobile Pages)

Setup AMP (Accelerated Mobile Pages) In PHP Laravel

Add Analytics To AMP (Accelerated Mobile Pages) HTML Pages

Add Google ADS In AMP (Accelerated Mobile Pages) Website







Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing