hey guys welcome back today's video we're going to talk about api testing more specifically testing
adjacent response using php unit in the context of a lot of our application i'm going to show you
five common methods you can use in order to test different aspects of your json response i'm also
going to show you a sixth method which is going to help you to make your testing a lot easier so
make sure you stay tuned until the end now if you look at our code we've got a simple get route that
returns the specific comment for a specific post we utilize scope bindings in order to match our
post and our comment and then in the show method of a post comment controller we simply return a
new post comment a resource with that comment and if we look at our post comment resource it simply
returns the id it returns the comment and then it returns the post which is also a post resource and
if we look at our post resource we can see that it returns the id the name the description of the
post as well as the status okay now i've got an example of what our json response is going to look
like now this is what we're going to test to make sure that this is the response we're going to get
back from our api endpoint now in our first test we're going to assert that the data key returns
the correct number of fields now if we look at our json response our data key should return one two
three fields now in our test we're gonna call the api endpoint we're going to assert that the
response is okay and then we're going to assert the json count equals to 3 and we want to assert
this on the data key now let's run php unit and see if our test is passing and there we go one
test two assertions our first test is successful okay so now in our second test we want to assert
that the json structure or the json response has a specific structure so again we call the api
endpoint we assert that the response is okay and then we use the assert json structure method and
the first parameter of this method would be the structure of the response we expect in an array
format so we expected to have a key called data the data should then be an array with a key of
id comment and post and then post should be an array with the key id name description status
similar to what our response looks like okay so let's run php unit and see if this test passed
there we go we've got two tests eleven assertions our second test was successful okay now for our
third test we're gonna assert that certain values exist within our response now again we're going to
call the api endpoint assert that the response is okay and then we're going to call the assert
adjacent fragment method our first parameter would be an array of the expected values we want to see
within the response so in this case we're going to expect that it returns an id of one and it has
a comment that says this is a common for post one okay so let's run php unit and see if it works and
there we go three test bars fourteen assertions our third test was also successful now for our
fourth test we're gonna basically assert that certain information is missing in our adjacent
response this is helpful if you definitely know that certain information is not supposed to be
in your json response so in our test we're going to again call the api endpoint we're
going to assert that the response is okay and then we're going to use the assert json missing method
which is basically the opposite of the assert json fragment again our first parameter is the
information that we expect to be missing which in this case would be we expect the id of 2 to
be missing as well as the comment that says this is a common for post 2. ok so let's run php unit
and see if it passes and there we go four tests 17 assertions test 4 was successful just something
to keep in mind in this example i've split all the main assertions into different tests this
is basically just for readability but if you want to you can combine all these assertions like the
third json count the structure the fragment and the missing into one test and one statement now as
you can see the more methods you chain the longer your statement gets and the harder it gets to read
so i would suggest to keep them in different tests just so it is better and easier to read but if you
want to do it this way there's an alternative that makes your statement a lot shorter and we can use
the assert json statement now the first parameter of our assert json method is we're going to pass
in a callback function we're going to type in the json variable as an assertable json type and then
we're going to call all our methods on this json variable now our first method is the count method
which basically counts the data key and it should have three values similar to a cert json count up
here our second method is the has all method which is basically similar to json structure
which we can check the json structure in dot notation like this and our last methods are
the where method where we check the value of a certain field in the response which is similar to
the assert json fragment method okay and if we run php unit again all our tests should still pass and
there we go six test 45 assertions okay and before we move on to the last test i'd like to remind you
if you like this video please give it a thumbs up also if you haven't subscribed yet please do so
now and yeah remember to turn on the notifications so you don't miss out when i upload more content
okay so the last test i want to show you is snapshot testing now snapshot testing is by far
the easiest way to test and it can basically combine all the tests i've shown you so far
into one test and one statement and snapshot testing is something you can use for the majority
of api responses where you receive some sort of information back from your api okay so the first
thing you need to do in order to use snapshot testing is to install a package called php unit
snapshot assertions then in your test you just use the matches snapshot trait and then you call
the this ascert mashes json snapshot method and then within that method you pass in your response
where you'll get your response from the calling the api endpoint and then we do an assertion to
see that the response was okay and then we just need to chain on the get content method in order
to return the json response to the assert matches json snapshot method okay and if we run phpunit
now again we should see we've got seven tests 47 assertions and one test is incomplete now that
means if you look in your test section you've got a new snapshot folder and in your snapshot folder
we get the response of our api now the first time a snapshot is run it creates this api snapshot and
then it marks it as incomplete now if we run this again it's going to take the api response
and then compare it to the current snapshot that we've got and now as we can see we've got seven
tests and 47 assertions and if we look at our snapshot we can see the data is exactly the same
as the snapshot we used in order to create all our other tests so basically this one
snapshot test can replace or in most cases can replace all the other tests we've created and
that's it for this episode guys thank you so much for watching and see you in the next one