Playing
14. Book creation form

Transcript

00:00
So let's go ahead and create out the add a book form so we can actually fill in the details and store this in the database. That's going to be just over here under add a book. So let's just go and create the controller and route out first for this.
00:14
So we'll make a controller here and we're going to call this book create controller. And let's go over to our web routes and just fill this in. So this is going to be a get route and it's going to be books slash and create. Switch that over to book create controller and we can now head over to book create controller and just fill this in.
00:36
So let's go ahead and create out an invoke magic method here and let's go ahead and create our constructor. We know that we need to be authenticated for this so it's pretty straightforward. We'll just fill it in now but we will add a test for in a moment. Say auth and for invoke let's go ahead and return a view and really quickly create this out. So
00:53
that's going to be books and create. So if we head over to our views really quickly let's just create out a folder in here called books and we'll create out create dot blade dot php. Okay so this is our create page. So if we head over now we should be able to go over to books slash create and see that. Let's hook it up to our navigation really quickly and then we can start writing some
01:16
tests. So add a book and we want this to be books slash create. Great. Okay so I can click add a book now and go through to this page. Of course at the moment it doesn't look great. Okay so let's just start to write some tests and we'll sort of hop in between fixing the ui getting the ui working and writing tests at the same time. So we're going to go ahead and make our test here and this is
01:39
going to be book create test. Not too much in here but it's worth doing this. So book create test and of course what I didn't do is provide the pest flag. I think we can force create this. No we can't with tests. So let's just manually delete this. Great and let's just try that again without force and there we go. Great. Okay so book create test. What do we need to write for this? Well we need to be
02:08
authenticated. So we're going to grab the same assertion that we used for this here. We're going to switch this up a little bit later to make it a little bit clearer and we just come over to book create test and add this in here. Let's also make sure that we have in our database stuff and we should be good. So of course this is different now. It is getting books slash create
02:36
and we want to make sure that we're redirected with a 302. So we'll just get rid of this last line so the actual test makes sense and let's go ahead and run this test out now. So pest and tests feature and book create test and there we go that passes. So we know that we need to be authenticated to access this. Now the only other thing we really want to check on this page here is that we see the
03:00
list of statuses within that form in order because remember these now come from that pivot class that we've created. So let's go ahead and build out the UI first and then we'll just write a quick test to cover this and see how we can do it. So if we come over to create.blade.php at the moment of course it doesn't look great. Let's just steal the template from the login page that we've already got
03:22
and let's just change this around. So add a book and this is going to go through to slash books to create that book. We're still going to have our cross-site request forgery token in there. Let's get rid of this password maybe and let's switch these out create a book or create book and let's change this to the book title and let's switch this to title and change the type to text and
03:50
we'll get rid of the placeholder in there as well. So let's just have a look at this and I think that looks okay and let's just duplicate this down. This is going to be the author of course change that to author and there we go. Now the only other thing that we need are the list of statuses. Just before we do that let's add in some errors for the title and author
04:16
just so we can see these on the page. So we'll make these red 500 and small and in there we'll just grab the message as part of that component and now what should happen is when we click create a book we see the title field is required. I've put that in the wrong place but you kind of get the idea. So let's add this for the title and let's add one here for the author. So we know
04:41
that the validation rules work because we tested them and sure enough they're coming through properly. Okay so down here let's go ahead and iterate over all of the statuses and we'll put these inside of an input. So let's come and grab all of this paste this in here and of course we don't want an input now we want a select. So the type doesn't make sense here the name will be the
05:06
status you can change that up as well status and we'll put status there as well. Okay so that should be just about it of course what we just need to do is finish that select off and provide some options inside of here. So for example want to read and then the value of this is going to be the key that we have want to read for example. So we should end up with something like this. Great
05:29
okay so why don't we write the test now and then we'll just fill in this with a for each loop to actually output the statuses and then finally we'll finish up by posting this through just to make sure that it works. So if we head to book create test we want to make sure that we see all of the statuses on the form. So let's say it shows the available statuses on the form and again we're
05:53
going to make this really basic we're not going to sort of fine-tune this and dive in too much just going to keep this really simple. So we're not using a user in any of our other tests so a before each doesn't really make sense here you could do if you wanted to like we did for the last test. I'm just going to create the user directly inside of here and then we just want
06:11
to go ahead and say acting as this user let's go ahead and get slash book slash create and then we can use a Laravel assertion like assert see text in order remember it needs to be in the correct order and we can reference that book user pivot class and the statuses so that's just an array of the values we just want to make sure we see them in order so let's go ahead and pull the
06:35
namespace in for the model here let's run our test just to make sure okay it fails so of course we can't see these in the correct order because we're not iterating through them so now we just need to iterate through them and we should be good so let's go ahead and add a for each loop in here and for this we're just going to need to provide the full namespace a little bit awkward but we
06:56
can refactor this later of course once we know that our test passed so let's say pivot book user and statuses as key which is the key that we want to put into the value here and status let's end that for each just here pull this in replace this out with the key and then replace this out with the status and we should be good let's have a look and yeah that looks good and of course our
07:24
tests should also pass brilliant so you can make them a little bit more thorough but of course it doesn't really matter too much at the moment since we're just learning our pest syntax okay so finally we're going to go ahead and look at actually posting this through now this says the author field is required let's just change our error here to status and just make sure and yeah that's
07:45
good and let's just say book one and author one create that out and there we go so we've redirected back over to the home page where we're eventually going to be listing out our books more importantly over in the database we've got this record in here which we already knew would work because we have tests to back it up and the book's been stored here as well so everything is now working
08:07
nicely what we're now going to do is head over to the home page and write some tests to cover this and see that we can see all of our books grouped by the statuses and we're going to look at a really really good pest feature that's going to allow us to really condense writing this test so let's head over and look at that in the next episode
35 episodes4 hrs 19 mins

Overview

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!

Episode discussion

No comments, yet. Be the first!