Testing Author Resources and Routes11:19 with Jonathan Barrios
Now that we’ve finished the @store function, it’s time to move away from the browser and start testing with software such as POSTMAN or an IDE plugin such as REST Client. To send and test a POST request, you’ll need to download and install POSTMAN yourself and REST Client, in the Visual Studio Code extensions marketplace. I will walk you through the steps to installing a REST Client by Huachao Mao so let’s get started.
Welcome back. 0:00 Now that we finished the @store function, it's time to move away from 0:02 the browser and start testing with software such as postman, or 0:06 an IDE plugin, such as REST client. 0:10 To send and test a post request, you'll need to download and install Postman 0:14 yourself and REST client in the Visual Studio Code extensions marketplace. 0:19 I'll walk you through the steps of installing REST client by Chow, 0:25 so let's get started. 0:29 Now that we have Postman installed, let's test the @store function like this. 0:33 To test the at index end point, select GET. 0:39 And add this URL, And click Send. 0:45 It works as expected. 0:55 Nice, now, let's install REST client so 0:57 we can do our testing without having to leave Visual Studio code like this. 1:00 I've already installed it, so go ahead and install it here. 1:16 Finally, we're gonna create a new 1:22 file named rest_test.http like this. 1:27 If you have any trouble with REST client like connection is being rejected, 1:39 try PHP artisan serve without that dash dash host equals local host. 1:44 First, let's test all authors like this. 1:51 Nice, here are all the authors. 2:00 As you can see, REST client is a wonderful tool for testing endpoints. 2:04 We received our HTTP response of 200 okay, 2:09 we have some extra information in the header. 2:14 And we have all of our authors returned right inside of Visual Studio code. 2:17 Next, let's test a single author like this. 2:24 Perfect, author number three. 2:34 How about all books like this? 2:40 Perfect, here are all of the books. 2:48 Code update. 2:57 In this code snippet, 2:59 be sure to remove the pipe unique because we're not checking for unique emails. 3:00 Check the teachers notes below to learn more. 3:05 Before we can test the @store function, 3:09 let's import the validator at the top like this. 3:11 Next, we're going to refractor the current validation code like this. 3:18 Next, we're gonna update the return response as well, like this. 3:30 Finally, let's test the @store function like this. 3:38 To learn more about validation, check the teacher's notes below. 3:47 Nice job, we successfully sent a post request and 3:51 received an HTTP status of 201 created. 3:55 201 means that the request has been fulfilled and has resulted in one or 3:59 more resources being created, so everything is working as expected. 4:04 To verify that our new author was created, use the default welcome view, 4:09 Postman or REST client. 4:14 Try submitting a git request using REST client like this. 4:17 As you can see, the last author is indeed our newly created author, so 4:24 everything is working, nice job. 4:29 I like using REST client for testing, but 4:32 feel free to use post man or the default welcome view 4:35 as demonstrated earlier as each one of these will test the same results. 4:38 Great job so far, we successfully created, and tested the index, 4:44 show, and store functions in order to display all authors, 4:50 one specific author, and store newly created authors respectively. 4:55 In the next section, we're going to finish building the remaining crud 5:01 functionality for our REST API by creating the update and destroy functions. 5:05 Before we get started, let's discuss why the update function 5:12 needs to accept both put in a patch method. 5:16 Put is a method for 5:21 modifying resources where the client sends data that updates the entire resource. 5:22 It's used to set a resources information and 5:29 if a resource already exists, creates a new resource. 5:31 For example, when you want to change the first name of a person in a database, 5:35 you need to send the entire resource when making a put request. 5:40 Patch applies to a partial update to the resource, 5:45 which means you're only required to send the data that you want to update and 5:48 it won't affect or change anything else. 5:53 For example, if you want to update the first name of a resource in the database, 5:56 you'll only need to send the first name. 6:01 Let's get started by building the put and patch routes for the update function. 6:04 And to conclude this section on CRUD operations, 6:09 we'll finish with the destroy function. 6:12 See you there. 6:15 Now that the store function is working as expected, 6:17 we have two more functions to go, update and destroy. 6:20 Let's start by building the put and patch routes for the update function like this. 6:25 Now let's verify our routes using php artisan route lists like this. 6:57 Great, our routes are ready to go. 7:16 So let's navigate to the AuthorController.php file so we can update 7:18 the validation and add the author resource to the update function like this. 7:23 First, let's refactor the validation once again to make this as explicit 7:30 as possible by copying the add store validation. 7:35 Finally, we'll wrap our author model with the new 7:47 author resource in the update function like this. 7:52 Before we move on, let's change the 200OK, 8:23 HTTP status to 201 created which is a better status code for this action. 8:27 Great, now let's head over to the REST client and test our routes like this. 8:34 This is the comment for our first PUT request. 8:41 And this is a PUT method for the first author and 8:47 here is the information that we're going to update. 8:51 Nice job, you should see a 201 created status, 8:59 which means we're almost done testing the update function. 9:03 So let's go ahead and verify that our last record was updated successfully. 9:07 If everything is correct, 9:13 you should see an updated author with the changes we made earlier. 9:15 We're almost done with our CRUD operations for our authors so 9:19 let's build the destroy function starting with the route like this. 9:23 Before we move on, let's verify our route using PHP artisan route list like this. 9:38 Great, our route is verified and we're ready to test the destroy function. 9:56 But let's make sure the destroy function is in order. 10:01 Our first destroy function looks to be in order, so let's test it out like this. 10:13 And we will delete the second author. 10:26 Great, we deleted our author with an ID of 2, 10:33 and our request was returned with a status of 204 No Content. 10:36 Fantastic work, we've successfully tested all of the author resources and 10:43 routes using the REST client plugin 10:48 without having to leave the Visual Studio Code IDE. 10:51 We also used resources to hide database specific information like ID and 10:56 timestamps, while exposing fields, such as name, title, 11:02 company, and email for each one of our authors. 11:07 In the next section, we're gonna do the same for 11:12 our authors books by creating the book resource. 11:14 See you there. 11:18
You need to sign up for Treehouse in order to download course files.Sign up