
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.
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.
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.
/**
* @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);
}
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;
},
];
});
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
$post = factory(Post::class)->create([
'published_at' => null
]);
$post = factory(Post::class)->states(['unpublished'])->create();
Now your test case will look like the following very lean, clean & fluent
/**
* @test
*/
public function user_cannot_view_unpublished_post()
{
$post = factory(Post::class)->states(['unpublished'])->create();
$resource = $this->get("/posts/" . $post->slug);
$resource->assertStatus(404);
}
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')
];
});
I hope this article helped you. Please share it with your friends.
Create A Composer Package? Test It Locally And Add To Packagist Repository
Install Apache Web Server On Ubuntu 20.04 / Linux & Manage It
Setup MAMP Virtual Hosts For Local PHP Development
Create Zip File On The Fly With Streaming Download In PHP Laravel
Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu
Why You Should Run Cronjobs In Laravel?
Multiple GIT Key Pairs Or Account In The Same Computer
Redirect www to a non-www Website Or Vice Versa
Upload File From Frontend Server {GuzzleHttp} To REST API Server In PHP {Laravel}
PHP Built-In Web Server & Testing Your Development Project In Mobile Without Any Software
Automate Repeating Tasks In Linux Server With Cronjobs