19. Updating a book


Let's work on our UI a little bit and then we will go ahead and create our tests to make sure that when we do submit this through it actually persists the changes in the database. Now first of all this my books link doesn't work so let's head over to our base layout and let's go to my books, let's switch this to the home page, remember that just goes over to here,
and we want to go ahead and add an edit link here so we can actually go through to that page that we have just written a test for. So over in book.blade.php that component we created we can just create a link out here saying edit and we know that we need to go through to books slash and then the book id so let's grab that and slash edit. Okay let's come over and that's
that button done let's just add some styling to this so let's say text blue 500. Okay and maybe make that small so text small great so let's click on that and there we go we can go through to edit that book and of course we know we can only do this for books that we own so when we submit this through of course we want to actually grab all that data validate it and
store it in the database or at least update it in the database so we're going to go ahead and create our book put test so let's say php artisan make test book put test and of course pass our pest flag in let's open up bookstore test grab the refresh database trait and we could also add this as well so let's actually open up book edit test we pretty much want this so book put test
and we'll just paste this all in here and we will say redirects unauthenticated users and that's going to be a put method on books slash and then an id so we can go ahead and run this let's just pull up a command from before and say book put test okay of course it doesn't work at the moment we get a 404 let's build the page out for this first of all so php artisan make controller
book put controller there we go and let's head over to our web routes and register this so of course this is going to be put on that book and book put controller great okay so opening this up let's go ahead and add our invoke magic method do nothing and let's have our constructor and make sure we add our middleware and our test for this should pass so let's run it and there we
go we can get on with the rest of the functionality okay so what do we need to test for first validation we could also write a quick test to see if it fails if the book doesn't exist we could have done this over on the other one root model binding will always throw a 404 but it's kind of worth doing this if you do switch around your root model logic so let's just write
another test just for the sake of it and say fails if the book does not exist this is going to be super simple we know that we need to act as a user so let's make it simple and use a closure based and let's just say acting as we'll create a user out in here really quickly and we'll go ahead and make a put request to books slash one at this point in our test that
won't exist so we can assert that the status is 404 let's go ahead and pull our user model in and let's run our tests and it doesn't look like it works so we do get a 200 here that's probably because over in our book put controller we're not actually accepting that book in so let's do that first of all rerun and we get green great so the next test we want to write is similar to
in our book store test where it requires a book title author and status however in this case the book already exists so we need to kind of create this first and make sure we attach it to the user so let's go ahead and say it validates the request details like so and let's go ahead and create this as a closure based what i'm also going to do just
before we forget is pop a note down here to say that we need to validate that the user owns the book because there's no point in protecting this page if we're not going to protect the page that actually updates the book so let's go ahead and create our user in here and once we've done that and once we've done that we're going to attach a book to this user so let's say user
books attach and again we'll assign that book at the same time as creating that out and also once again setting that pivot information so let's just set this to once or want to read and let's pull the book model in okay so what do we want to do from here while acting as that user so let's say acting as that user we want to go ahead
and make a put request to slash books slash and then the book id and then not sending any data down we want to assert that the session has the following errors that's going to be title author and status now the good thing about writing a test like this is if we head over to our book store test or our book store controller we have these validation rules in here now what we can
do is we can refactor this to a form request and then we can share that between the update and the creation of a book and then we can rerun our test to make sure that we didn't break anything doing that so over in our book put test let's run this just to make sure and of course it fails because we get false is true the session does not have these errors so what we're going
to do is over in our book store test or book store controller we're going to copy these validation rules over just to make this pass and then we can refactor it later so over in the book put controller let's paste this in make sure we bring in our request object in here and make sure we pull that rule in from illuminate validation make sure we pull that pivot model in
and let's run our test and sure enough we get green so that's working nicely and that's now good to refactor a little bit later and keep an eye on so back over to our book put test now the next thing we're going to do is make sure that it fails if the user does not own the book now once again if we just come over to our book edit test we have a very similar test in here
fails if the user does not own the book so i'm actually going to go ahead and copy this test just to save a little bit of time and we can include that in here now once again we're going to go ahead and create a user create another user that owns this book this time we're going to make a put request through to book slash book id and make sure that fails with a 403
if we run this it doesn't we get a 302 just because we're not passing any information down so let's go ahead and pass in a title say new title author and let's say new author and status and let's just set that to want to read again and now we've got that data down so the validation is passing and if we come here now we get a 200 not good we can technically
update someone else's book so once again over in our book edit controller if we can find that we're doing something like this so let's just do that again over in our book put controller down here if the book can't be found within that user we are bought with a 403 and once again it's very similar to the other check that we're doing but we can refactor this later and still make sure
it passes so let's go ahead and rerun our tests and we get green so we now know we can't access this unless we own the book back to our test let's finish this off by writing a test that actually checks that we can update the book so it updates the book and let's go ahead and write this completely from scratch so we need a user that we can act as and once again we could leverage
before each in here to make this a little bit easier so we can refactor our tests in a minute and again i'm just going to copy and paste from here just to make this a little bit easier switch that over to that user though and then down here we want to go ahead and now make a successful request so acting as that user we want to put through to slash books slash and the book id
which should be successful now because we own the book the status is reading so that should change so let's go and set the title to updated title we'll set the author to updated author and we'll set the status to something different so want to read for example okay so now we just need a couple of database assertions so the first one is going to be on the actual book itself so let's come down
here and say this assert database has we want to look in that books table and we want to make sure that for the book id that we've actually created up here we have a title of in fact we can paste this so let's grab these two there we go let's just run that of course it's going to fail because we haven't done anything in our actual code to make this pass yet and let's grab well we can
just do this from scratch assert database has book underscore user and book id should match and the status should now be let's just check what we did want to read there we go okay so what i'm going to do is comment this part of this assertion now we'll just check that the actual book is updated first of all of course that fails at the moment
and then we'll pull that in and we'll add the functionality to make that work so in our book put controller the first thing to do is actually update that book so that's going to be as simple as saying book update and from the request grabbing only the title and the author let's run our test we get green let's bring in that other assertion so this one just here which is the pivot table
run our test it fails so that's not been updated in our pivot so over to our book hook controller we want to go ahead and say request user books and we want to update an existing pivot for that book and the pivot is now going to be the status that we get through from the request finally we're going to go ahead and redirect the user
over to the home page or we could redirect back to the book and flash a message but we'll just kind of leave it for now okay let's run our test and there we go so we get a successful book update let's head over to the next episode do a little bit of refactoring and of course make sure all of our tests are still passing
35 episodes4 hrs 19 mins


Pest is a PHP testing framework that brings beautifully simple syntax to your tests, without sacrificing on features. In this course, we'll get up and running with Pest in a Laravel project and write tests for a real-world application that we'll build along the way.

You'll learn how to set Pest up in a Laravel project, write tests with Pest's built-in assertions, generate code coverage, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.