laravel unit testing tutorial | laravel unit test controller method [ Real world example ]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome guys welcome to this video in the last video we saw the unit test and php now in this video we are going to explore unit tests in larval we're going to see a real world example so what exactly unit test is so what is testing in laravel well testing helps us identify problems in portion of our code by running simple command you can check if certain module or unit of code is working or not if we fold the wiki definition unit testing is a type of software testing where individual units and components of a software are tested the purpose is to validate that each unit of the software code performs as expected unit testing is done during the development coding phase of an application by developers now let's see an example here where we have a demo project where we have some books and we can add new books from here now let's say you are a developer in an organization your senior ask you to create a test that checks how this is storing the book so you don't want to add a book from here you want to test that if all the values provided or all the required values are provided will your code will save this book details in the database and we'll book will this will appear here so that is the kind of thing a unit test dust and does so in this video we are going to create a test which is going to validate can we add a book uh so that just by running a single command you can verify that your functionality for storing a book in database is working or not [Music] all right so i have the uh current project open in my text later here this one and i want to show you how things are given in the larval by default so when you go into the directory structure you will see app and then inside here you have routes and resource and there you will see a folder with the name of tests now when you open this folder you will get two folders feature and unit so basically we can pay create the test based on features which is going to check for a big portion of code and a unit test which is going to check a small portion of code so if you open any of these directories let's check the feature first so we have the example test.php there the same way if i go to unit we have example test there too now if i open this example test from feature we are going to see that there is example test class and there is a test given inside it it's a function now what happens is we are going to use two commands given by larawal and so first command is dot vendor slash bin slash php unit when you run this command it is going to run this test and basically what is happening is that it's going to run this function and we're going to see the example of this soon so it is going to run this function it's going to go to this url and it's going to get a response if that response is equal to 200 and here we have the assertion and what this assertion does is it's going to check for that particular there are so many type of assertions so just like this method using a cert status we can also use more assertions we have a cert true which is supported by larval we have a certain equals we have more options here assert array has key and we're going to see the examples of these how these work we have a certain view as so this is currently i'm on the github page of laravel framework so there these assertions are being used and the examples are given here so here basically if these status matches this is going to return as a past test test so we have a unit test here where we have example dot php uh example test.php if i see here here we are doing the same thing we are running another test and here we have a true the same way we just saw here on the larval website and what exactly it is doing it is looking for if the return from this method is equal to true this test going to be passed so let's see if uh this test is passing or not so i'm going to open the so i'm in the terminal and i'm in the current project yeah which is the sky folder for me i have the application folder where i have zap and inside hd docs i have the sky folder and that's the project we have here this simple project so what i'm going to do i want to see that if these assertions these tests are working or how we can check these tests and then we are going to modify and further see more tests so here first of all i have the terminal open so i want to check i want to run these tests so the first command we need to run for running the test is dot slash vendor slash bin slash php unit and i hit the return now here what we are getting basically we got two tests are running so here first of all we have the php unit package used in laravel so the version is 9.5.10 now here we have uh the time it took in the memory we have two tests and both of those tests gone past we have 100 success rate we have two tests here two assertions here so if you uh say remember maybe just saw these two tests so if one test is here with one assertion present inside it then we have another test here where we have the another insertion going inside here so currently it is going to this url getting the response to 200 and then this passing the same way here now one test can have multiple assertions what i mean is we can pass multiple assertions like this so there is assertion for checking equals [Music] now if i say here one and then one that me is going this assertion going to check if this value is equal to this value and you can you know that this is equal so this is also going to pass so if i go back here and run this test again now this time we have two tests and notice here we have three assertions so basically we just added the assertion here [Music] now i ran this test using this command which is quite a bit bit tough i believe to type so in laravel we have another command that we can use to run the test we can simply say php artisan and that is basically the larval waste we should use this one so php artisan test when you run this command it is going to give you the similar result with the better of you out we have here test one it is telling us which test it is it is giving us a green tick mark that this test passed then we have another test from feature so you can see this is from unit and this is from features that it's telling us to test pass however we don't have the information about assertion in this one now let's see what we can do now i want to run the test but not the feature when i want to work with the unit because uh that is the one we are going to work with most so here we need to work on the unit test not the feature so how i can run that so for that when i when you run this command at the end you need to pause dash dash test and shoot and then you're going to say which one it is so you might either say feature or unit so i'm going to say here unit now if i run this you will notice we get one test and this is just the unit test so this is how it works now if i go to the enlarable project directory and here in the root of this application we have a phpunit.xml if you open this file you will notice there there we have test suit suits or suites whatever you want to call it we have two directories given here so this is the basic uh basically the main configuration file which is telling that we have tests now these are the feature tests these are the unit tests and unit tests are in this directory and feature tests in this directory so here it is all defined in by default by larval so if you want to change anything or want to add more type of tests you will be adding here so that's the reason when i ran the command here i mentioned the test suit going to be a unit and it is it noticed that okay we are just talking about this we are not talking about this so it ran only one test all right so far we saw that we have the unit and feature and there is one example testing each so let's do this let's create our first test so for that i open the terminal and we have a command in larval that will help us to create a new test so i simply clear the screen here and i'm going to say here php artisan make test now you need to tell the test name and i want to say let's say book so i said book test and here i want to make it as a unit test so what i need to define is dash dash you're going to get the enter so this is going to put this test inside the unit directory so if i hit return it is test created now if i go to the unit folder we have another tests here now i open this one we have the test example and this are true we have book test on top and yeah that's the basically the exact structure that we had in here from the example test now here we are going to change this test according to our needs so what i want to do i want to first change its name now it starts with test because it's a test it's going to be starting with tests and that's how it is going to work inside it we can use our functionality so what i want to do first of all i want to see if this test is running so what i do is simply go back and run the command to see if the test is currently running so i simply say here and we use the command for unit test now this time you can see we have two tests passed so we have two tests running in the unit directory and that's perfectly fine now what i want to do i want to go to our application and here we have this store book so i want to check this so in the code if i go to the controller of this particular file so here in http controllers books controller here i have a store method and this store method is basically we have validator on top which is checking for title cover content price year published and if you remember notice the same fields we have here cover title detail price year published and here once the validation is done after that we are checking for image if there is image and we are setting a name and after that we are creating and storing it here and after that we are uploading the image now let's do this let's work on this i want to check if this method is working the store method for us so for this what i do i go into my test here we are going to change the code inside here so here i say test book [Music] you can say book store [Music] all right this is our story all right better because we have underscore given here so we can do it that way so i say store bookstore okay so it's gonna run this way so now if i do here we i have the laravel debugger bar you can see here at the bottom so you can check the routes here uh which route is responsible for what for storing this particular book if i open the uh i can also show you using php artisan rob list now here let me make it a bit smaller so we can see here so it fits the screen now here if i see uh basically i have the store methods going here so basically here is the store method for this book so we have a book controller store and this is the route you can also check in the code in the code currently if i go to uh the routes are here then web.php i'm using resource on this one so for this slash box is the route so you can see that box here post method then it is going to store so here in our test we need to write function accordingly so i'm going to say here dollar this and then i'm going to say call and i'm going to pass here post method now why i'm passing post because we have post on this uh particular route then the second parameter in this function going to be the route so i say books because i want to store books now here if you remember in the controller we have these fields titled cover content and we need to provide these fields to store so what i do i go into test and here i'm going to pass an array and this array going to take the all values so first is title and let's add some tiles so some um book title with the tree now let's second that content and i'm going to just copy paste this as some bug [Music] content [Music] next we need to pause the price and here if i go back to the controller we have cover we are uh getting the image for now i'm just going to come in this field and i'm just going to check out the wheels so we have price in a year published so i'm going back to the test so price let's say add any number that doesn't matter what number you pass here basically just needs to match with the fields so here you're published and here we can pause the year so i said two thousand uh let's do that okay all right so we have this here now we can we are going to get the response here so i'm going to put that response in a variable and here we can just see the variable if you want to so we can even dd here to see what response we get okay so if i all looks good and let's run this test so i go to the terminal again clear the screen php artisan run test unit test it again we get here uh call to undefined method call uh some for some reason this is not working from php unit and if i go to laravel documentation there they use the uh tests slash uh test case so what i'm gonna do i'm gonna copy this i'm going to pass this in our code here just going to replace it but this one i'm going to save this going to go to terminal clear the screen and run the test again this time we have a different output you can see we have status quo's code 302 found and if i scroll up we have so here we have all the fields so we have title content price published so you can see that so basically in code if you see we just got this response and just printing it straight away so what i want to do i want to uh check this status of this code the same way we did in the uh here here we are checking this status and it's going to copy this code and we want to check that what status we get here so status quo we're going to check so i'm going to come in this one out and i'm going to to get this status we need to use it this way so i'm gonna copy this let's place off here and we are going to pause status like this and this is how you take the status out of response and we want to check what says we are getting so currently if i go to the browser here and if i go to the database and here in books i just have this new book saved here you can see so this is the same if i we go back to terminal so we have the uh book some book title one two three some book title one two three and the same content so it's saved so what happened is when the we ran it it's saved by running this kind of code and after that it gave us a response so here currently it is giving us three zero two so if we want to check what response we're getting or we want to check if the test is passing i'm going to pause here three zero two and that is happening with the reason of threes or two because we are using the redirects inside the test here and 302 is a status code for redirect so if i go here and let's clear the screen and run this test again and if i go here and run it again so we get to pause now so our test is getting past book test is here book store okay so it's working now but when you say we should get 200 why we are getting 300 302 now in that case what we can do what is happening currently if i go to database we are not storing it again and the reason is because when you run this test it runs this code it tries to store this data but if you notice in the controller we have validation set so this is a unique so it is not going to add it again so if you check basically if i simply go here and od and i pause the validator inside it and save it and i go here and run the test again now this time we we have a message in the validator the title has already been taken and that's the reason why it is giving us the uh redirection because we have a redirect set here so if you want to get the 200 in that case we need we must have uh let's let's put 200 here so so for that simply i'm going to add here at this point response so we're going to return response it's going to be json response and i'm going to simply return book and here i passed the status code 200. now we are not storing book here so i go down here where we are storing the book we are storing book here and just after storing the book i want to return instead of doing anything else and we should get 200 so here in the code we can see that what we are getting so here dd and i'm going to put the response again and if i put here and run this test again so i say clear here run the test again now we get three zero two the reason is if you remember we still have the title inside database so if i go back there i delete this title so now we don't have it so this time it is going to because what happened is last time it is it gone here and it's simply validation failed because it is already present in the database and it went here so it redirected now this time it is going to go here so let's do that and see if this fires and we get the 200 okay so we go back to the terminal clear the screen and we're on the test and this time you can see that status quo 200 and still is okay and we just stored the new data so if i go here you will see that we have a new data stored now if i want to check it through the test only i don't want to display anything i delete it again now you don't have to delete it and i think it's just to demonstrate you how it is working so here in the text editor what i'm going to do going to go and test you need to remove this line and i'm checking this time for 200. so if this is the code and this matches that means our test is passed and let's do that so we go here and we run the test and we see book stored so we are test passed successfully so if you got the understanding basically we simply uh provide so whatever route your controller function is working on we provide all the required fields in the test and then we run an assertion to check if we are getting the desired output or not and you can get the understanding we are not doing this through the page web page we are not doing anything in here and this gives you the power to check multiple functions multiple controllers just by using commands and you can see that we are running a single command that is running two tests so in one test i have this here now if i want to add another test i can have multiple functions there those are going to check other controllers and other functions so you get the idea it it works that way if you want to explore more laravel testing you should check the laravel documentation also the github page where there are a few examples of using different assertions in larval also there is the php unit the link is in the description you can check the different type of assertions available in php unit so these are the bias and php and it is used by our so it is something that you can use and it is helpful if you like the content of this channel and you want me to create more videos like this please support me on patreon you can also subscribe this channel like this video and share with others thank you for watching [Music]
Info
Channel: Zarx Biz
Views: 206
Rating: undefined out of 5
Keywords: laravel tutorial, laravel 8, laravel php, how to start writing tests with laravel, laravel testing code, how to start writing tests with laravel 8, laravel 8 testing tutorial, getting started laravel 8 testing, laravel testing with examples, laravel run specific test, how to do testing in laravel 8, laravel unit testing best practices, start laravel testing, laravel unit testing, laravel test with php, how to unit test php, laravel unit, unit laravel, testing laravel
Id: X9AwbSK7Ero
Channel Id: undefined
Length: 25min 44sec (1544 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.