Playing
05. Registering a user

Transcript

00:00
OK. So we're now going to cover the registration process. We're going to start off by creating some pretty standard tests for this,
00:07
just to make sure that we can register a user, submit the form, and go ahead and see them details in the database. Then in the next couple of episodes,
00:17
we'll look at some PEST-specific functionality or syntax, which is going to really clean our tests up for us and allow us to write tests that little bit faster. OK.
00:28
So let's head over and just run phpArtisan route list, just so we can see the kind of routes we've got here. And you can see that we've got lots of routes that have been registered
00:39
with Laravel Fortify. Now we're going to go ahead and create our own register page. And then we're going to post through to slash register. So the first thing to do would just
00:50
be to create our controller, which would show the registration form. So let's go ahead and say make controller. And we'll just say register index controller.
00:59
And let's go over to register index controller and just fill in an invoke magic method. We'll go ahead and return a view here. And we'll just say register.
01:10
And I think that should be just about it. We're going to need to include some middleware here later, just to make sure that guests can't or authenticated users can't access this, which we'll write a test for as well.
01:21
OK. So let's create out this view. So let's come down to our views. And go and create out register.
01:29
In fact, we'll put this in an auth directory. That kind of makes more sense. So let's create a register.blade.php file out in here. And we will just update that to go through to there.
01:42
OK, so inside of register.blade.php, I'm going to paste in some pre-made markup, which you can find in the course resources, just to make your life a little bit easier
01:50
so we don't have to go through and create all of this out manually. And essentially, that is just a name, an email address, and a password, with, of course, a button to create an account.
02:03
So over to Roots and Web, let's go and just register this out really quickly. So we'll say auth slash register. And of course, hook that up to the register index controller.
02:14
And we should be good. So let's go over to app.blade.php and hook up the registration link to go through to slash auth slash register.
02:24
And we should now be able to click on that and see our form. There we go. Now, at the moment, this doesn't look great. So why don't we pull in Tailwind form package?
02:33
So npm install tailwind-css-forms. And if we come over to tailwind.config.js, we can go ahead and just include that package. So let's require in tailwind-css-forms.
02:50
So as long as you've got npm run watch running, that should now look a little bit better. OK, so we're not going to touch this page just yet. There's one modification we need to make with Laravel Fortify
03:03
to get this working. And that is come over to the Actions section. These actions are published by the Laravel Fortify package. And under Create New User, we've got these password validation
03:14
rules which are used when we go ahead and pass our password rules in. So we're actually going to update this because I've only included one password field.
03:25
So let's open this up. And we just want to get rid of the fact that this needs to be confirmed. So let's do that now.
03:30
And we should be good. OK, so let's start to write a test for this. And we will first of all focus on some validation messages. I'm going to close everything off.
03:40
And let's get started with our first test. So phpartisan make test. And of course, we'll just call this something like register test.
03:48
And of course, we'll pass our pest flag in. So let's open register test. And let's get started. So I'm going to change this to it.
03:56
And the first thing we want to do, like I said, is check that this errors if the correct details are not provided. So for example, if we don't enter a name, an email,
04:05
or password. Now again, we're going to keep this really simple just so we can switch this over and demonstrate pest functionality.
04:12
So I'm going to say shows or has errors if the details are not provided. You could write really specific tests to grab out the actual error messages that you get here.
04:25
You could write tests that actually show that you can see the error on the page if you wanted to. It really depends on what you're doing. But we're just going to keep this really simple
04:33
and just check inside of our session to make sure that when we post through to slash register, which if we just open up register.blade.php is what we're doing, posting through to that Fortify route,
04:45
that we see some errors. So we're going to go ahead and say assert. And again, this is a Laravel specific testing framework method, has errors.
04:55
And then we're going to pass in name, email, and password. So essentially what we're saying is post through to this without any data whatsoever. And we should see these session errors inside of here.
05:09
So let's run pest. And by the way, if you wanted to hit a specific file and not run all of your tests, it's just as easy as going into the feature directory or the unit
05:19
directory and going ahead and picking the file that you want to run the tests for. And it looks like that's actually failed. So we should be seeing these.
05:30
Let's just head over to our app and submit this. And yes, so creates new users is not instantiable. The reason that this has failed is because we haven't registered our Fortify service provider.
05:42
We should have done that earlier. So let's head down to our service provider section over in config and app. And let's go ahead and pull in the Fortify service provider.
05:55
OK, let's rerun pest or that specific test. And there we go. That has passed. So if we don't provide the name, email, and password,
06:03
we should see these as errors. And like I said, you could get really specific about this if you wanted to. OK, so let's go and create a test that actually
06:14
shows that the user is created in the database when we submit the correct details through. So we'll just say it registers the user. And we'll post through to register.
06:26
But we'll actually send some details here. So let's go ahead and just pass in some name details. So say Mabel. Add the email.
06:35
Let's pass in Mabel at CodeCourse.com. And let's pass in a password. And let's just say, yeah, I am a cat because I think there's a password rule that it has
06:46
to be greater than a certain length. So now that we've done this, we can go ahead and just use the standard Laravel framework testing methods. And we can say assert, for example, redirect.
06:59
So we could say that we're redirected to the home page. Then we could say assert database has. And we can check that it has that information in a specific table, for example, users.
07:12
And we know that we want to check that the user with the email Mabel at CodeCourse.com exists. And we can even expand on this. So we could pull this down and include the name in here
07:22
if we wanted to just to make sure that that has been inserted correctly. And we won't worry about the password for now. OK, so the next thing we want to do
07:31
is assert that we are actually authenticated. Because when we register a user, we should be authenticated. So post through with the correct information,
07:42
check that we're redirected to the home page, and check that the information is in the database and we're authenticated. Let's rerun this test.
07:50
And it fails. That should probably be just after here because we want to chain this on from the request. So assert redirect to the home page.
07:59
Let's pull this in. And then we're going to say this assert database has users. Now what we're going to be doing in the next couple of parts is refactoring some of these to PEST-specific syntax.
08:09
So this is going to reduce in size massively. We'll see that in action soon. So let's go ahead and rerun register test. And yeah, it fails again.
08:17
So what has failed? Well, it looks like we have got a 500 error here. And that is because, again, the users table does not exist. So let's just pop over to our home test
08:29
and let's grab the refresh database trait and just pull that into there. And we can get rid of get because we're not going to use that.
08:36
We also have access to post as well. So we could use that as well if we wanted to just to cut down on the amount that we are typing. Let's go and rerun that test.
08:47
And yes, so yeah, we're redirected to the home page. That's not what we want because our home page is now not here. It's here.
08:56
So this test is now failing. We know that that's not redirecting us round to the right place. So if we head over to our root service provider,
09:04
we can change this around. So let's set the home to just slash. Let's rerun our test. And there we go.
09:11
It passes. Great. Okay, so I think that's just about it for now. What we're going to do in the next couple of episodes
09:17
is talk about some specific PEST functionality, which is going to allow us to shorten down this test and this test. So let's head over and find out how we're going to do that.
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!

Comments

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