Applying TDD with Laravel and PHPUnit
In this videos series I have to show you How to build blog with cool feature Like Post feed , Add post , user can view posts
Also , We also do the 80% of work on the terminal with test-driven development(TDD) mode.
So What is TDD ? (( Courtesy)Read Here : https://medium.com/@bvipul/tdd-in-laravel-how-to-a5bfdcf7048c)
TDD stands for Testing Driven Development.
TDD(Testing Driven Development) is an approach of testing where the developer first writes off the tests before making the functionality or feature.
And tries to run that test. Of course it fails, but then the task remains is to make those failing tests to pass.
And that’s what TDD is about.
An Example**
We can take the most basic validation in Laravel to showcase an example.
Suppose, you have a form to create a blog post with just two fields title and body and both of them are mandatory.
So your tests would consists checking if you can store the blog post without giving a title or body.
/** @test */
public function a_blog_requires_a_title()
{
//Create a blog using factory and make title null manually
$blog = factory(‘App\Blog’)->create([‘title’ => null]);
//Make a post request to the route and pass the created blog attributes
$this->post(route(‘blog.store’), $blog->toArray());
//Make assertion that session would have error by key ‘title’
$this->assertSessionHasErrors(‘title’);
}/** @test */
public function a_blog_requires_a_body()
{
//Create a blog using factory and make body null manually
$blog = factory(‘App\Blog’)->create([‘body’ => null]);
//Make a post request to the route and pass the created blog attributes
$this->post(route(‘blog.store’), $blog->toArray());
//Make assertion that session would have error by key ‘body’
$this->assertSessionHasErrors(‘body’);
}
Create tests as simple as you will say it to other person.
So what our tests are doing is creating a blog object using Laravel Model factory function which lets us create objects of Model with fake data, which is the best way to use in tests.
So, first we create fake object of our Blog class, then we make a post request to the ‘blog.store’ route and pass it the object attributes that we just created. And then assert(check) that the session is having errors for that particular key that is missing.
Remember this test is prior writing the store logic of Blog. And I’m assuming that your routes for blog are already made.
Now we run the test by running ‘phpunit’ in the command line and see that our test fails.
Now that you know that your tests are failing and you know why they are failing, you probably know what you should do next. Make those tests pass.
So, for that, we would write our validation logic in the store method of our controller.
public function store(Request $request)
{
$request->validate([
‘title’ => ‘required’,
‘body’ => ‘required’
]);
}
So when you hit that endpoint (‘blog.store’), laravel checks the request for validation.
Now if you run your tests using phpunit, they will pass.
So for anything and everything, feature or unit test, you can follow TDD approach.
So Let’s start.
Install laravel.
Prepare the database.
Ready the .env file(make database connection).
Generate key : php artisan key:generate
Part -1
Part-2
Part-3
Part-4
Part-5
Hope, you liked this article, and probably gained something.
Don’t forget to clap for this article, and share it with your friends and colleagues and always love open-source.