hey guys welcome back in today's video we're going to look at how we can test our inertia application
using php unit and lot of well in one of the previous episodes which i'll link down below
i showed you how to create a registration page using laravel inertia.js and react
we will be testing the full registration process as well as the inertia
endpoints and because we use react as our front-end framework we are not able to use
phpunit in order to test that in that case we might need to look at a different library
like cypress or a lot of our dusk to do our end-to-end testing okay so i'm going to quickly
show you in the browser what we're going to test today if we navigate to the slash dashboard
endpoint you'll see that this dashboard endpoint sits behind authentication so we're going to
register a user we are going to automatically log in after our registration and we're going
to display the dashboard so now if we go to the slash register endpoint and complete the fields
firstly if we don't complete any fields you'll see we're going to get validation errors we're going
to test that we get the correct validation errors back and let's complete our fields
and we register a user now after registration you see now we are able to automatically log in
and navigate to the dashboard endpoint and this is the complete registration process we're going to
test using phpunit and laravel now if we look at our first test it says it should show the
registration page so basically what we're going to test is once we hit the get endpoint
for the route register we need to assert that we get a 200 response which is assert okay
then we need to assert whether the response is an inertia response and we do this by calling
the assert inertia method on our response and as a parameter we pass in a closure function
which basically takes a suitable inertia which is a test class from inertia and we pass in the page
and on this page parameter we then call the component method to assert if the register
component is the one we receive we also check if it has errors and then we check that this errors
is an empty array because we're not expecting any errors on our page now the where method is
basically an extension of the has method the has method only checks whether a certain key
is available in the response you check but the way method checks whether the key has a certain value
now before the where key is based or the where method is executed under the hood it automatically
executes the has method as well so in this case we don't need to call the has method first
we only need to call the where method okay so let's save our file and run php unit
and see if our test executes and there we go we've got one test and 10 assertions our registration
page shows successfully okay and now for our second test we're going to check it should return
errors when the required fields fail validation so if we check in on the right hand side you'll see
all three fields are required now to start off we call the get method on the register
route this is to simulate that we're landing on the registration page
as usual then we call our next test method and our first method we call is the follow redirects
now we call this follow redirects method because when laravel validation fails it automatically
redirects you back to the previous page with a 302 error or 302 response code with your validation
errors if we don't follow the redirect then our test is basically going to stop at this point
with a 302 response code instead of the normal 200 which normally happens after the redirect
has happened and we want to test the redirect to the inertia response so that we can test whether
the inertia response has the correct error codes to display on our component okay so
next what we do is we are posting information to our register route we are not including any fields
because we expect the response to have error codes and because we are following the redirect our next
response should be okay which will then display the inertia component which we're gonna assert
similar to the previous test again we're gonna check to see if we get the auth register component
and then we're going to do three where methods to check whether the name the email and the password
has got specific error messages just a tip when using the uh method if you have a deeply nested
object you can call the object properties using the dot notation okay so let's run php unit again
and see what happens there we go we've got two tests and of 24 assertions
our second test was successful all right before we head over to the last test if you enjoyed this
video please give it a thumbs up if you found it useful and you would like to see more please
consider subscribing and remember to turn on the notifications okay so for our last test it should
register successfully and then redirect to the dashboard now again if we look at our test
our first method is calling the get method on the route again to simulate that we've landed
on the registration page next we do a new request and again we follow that redirects we then post
the information to our registered endpoint and this time we're actually sending valid
data we then assert that our response is okay and then we assert again that we receive an inertia
response now this time in our inertia response we need to assert that our landing component
after the registration is going to be our dashboard that's why we are going to assert that
it's going to be the dashboard show component and then we're going to assert that our errors object
or prop is empty then we're going to see whether the auth user prop or object matches the currently
logged in user now the auth user object is an object that inertia automatically sends
every time to every page in your application and this can be customized if you go to your handle
inertia request middleware you'll see that it creates the auth user and then basically
just calls the authenticated user and sends it to the front end now this is also where you can add
additional data that you absolutely need on every page within your application you can add it within
this section just be careful and mindful of the information you send with this method
because if you send too much information and because this is sent on every request to every
page it could severely impact the performance of your application okay so back to our test
and we can now save run php unit again and see if it's successful and there we go we've got
three tests 36 assertions and our third test was successful and that is how you can write tests for
inertia using php unit and laravel thank you so much for watching guys and see you in the next one