13. Validating book statuses


We're now going to test that as part
of the book creation process, we require a valid status. We're checking that it's required here. But technically, the user could pass anything in. And that would be added to our status pivot.
So let's say it requires a valid status. We'll put this rule into our validation rules very simply. And then we'll go ahead and refactor this. So let's go ahead and again tap, because we
want to make sure we are authenticating as a user. And we already have that user as part of our before each hook. So let's just say this user. And then let's come down and post some data
through to books. And we don't need to pass a title and author. We just want to pass in a status. So let's just put something silly in here
like eating, which is obviously not a valid status for this. And we want to go ahead and assert that the session has an error for that particular status. And I'm just going to array that just so it's a bit clearer.
OK, let's go ahead and run our test. Of course, this is going to fail, because we're just requiring the status. We're not checking it against a preset list of statuses
that we're allowing. Let's make this test pass first of all. And then we'll go ahead and update this so it's a little bit clearer or it makes more sense.
So let's go and convert this status here to an array. And then we can pass in here a validation rule via this class. We're going to say in.
So we want to make sure if we just pull in the rule from illuminate validation that this is in a particular set of keys. So we're going to say in here, let's
think about what we've got. Want to read, reading, and read. So that should be enough to pass. This status can only be want to read, reading, or read.
So let's go over, run our test, and sure enough, it passes. But doing this is a little bit unsustainable because we want to use these values elsewhere. If we want to add another status later on,
we're going to have to come back to pretty much everywhere we've referenced this array and update it. Now we're going to go and make a custom pivot class for this. There's a good reason for that later on.
We'll see why. So if we come over to our models section, so let's go over to our models. Let's go and create a pivot folder in here.
And let's create a custom pivot class for the book user pivot table. So let's create this out, book user. This needs to implement pivot from Laravel or extend pivot.
There we go. And let's pull that in. And now what we can do is over in our user model, just say using book user.
And now we have access to things inside of this class. For example, we could pluck out a nice way of saying what the user is reading. So if we could say is reading, for example,
rather than the status itself. We'll focus on that later. But this is just somewhere to keep it. It doesn't matter if you don't want to keep it in here.
But we're just going to have a status's static property, which we can access from anywhere. And we're going to say want to read. And let's map these up now because basically we
want to be able to output these in sort of plain English. So we're mapping these up to the headings eventually that we're going to have. This is the whole reason we're doing this.
But this is going to allow us to just keep these in one place. So let's say reading. And for read, of course, we're just going to say read. So the status is in here now.
And we can now use these over in our bookstore controller within our rules rather than relying on hard coding them. The important part is they are in one place. There are lots of different ways to do this.
But we'll just keep it simple. So let's say book user and statuses. Now, that's not quite going to work. Let's make sure we pull that class in.
Because statuses is an array. And it contains these values here. We want the keys for this. So if we now run our test, it's going to fail.
So what we want to do is instead say array keys on these. And that will grab the keys for that, which are the valid statuses that we can pass in and make sure we capitalize that.
So let's go over and run our test. And yeah, it fails again. So let's have a look at why. And I'm not sure.
So let's check this out. And let's see. Yeah, so that's not being pulled in properly. We just need to make sure we namespace this.
Really important. So namespace is now under models and pivot. And let's just make sure we call that pivot. Yeah, great.
OK, let's rerun our tests. And we still get false because we have not pulled in the correct class. And let's run this once more.
OK, so now we've got another test failing. The table is empty over in this test just here. So want to read is a valid status. So why is that not working?
If we go ahead and grab the response from this, we can just die dump on this. And hopefully see what's going on. So yeah, if we come up to the response here,
we've got book user is not found. So that's just, again, a namespacing issue. There are a lot easier ways to do this if you've got some software like Ray to dump this out for you.
If we come over to our user table, it's this now that's not working. So the reason that wasn't working is because we didn't have our namespace before.
So once we have fixed this issue up, we should now know that when we run this test, it all works. So now all we need to really do is hook up the form to post through to this.
And we are pretty confident that this is now going to pass. That's the beauty of writing these tests before we start to implement any of the UI. OK, let's finally just finish off
with running all of our tests. And we're looking good. So we can head over to the next episode and actually implement the form to create a book.
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.