Playing
04. Simple authenticated visibility tests

Transcript

00:00
So let's start out with our first very very simple set of tests and that is going to be specifically for the home page and the navigation. Now at the moment we've got no way of authenticating the user, we're not
00:11
actually going to create the login and registration process on the app just yet but we're going to test for this and make sure that when we are signed in we see the correct items in the navigation and when we're not signed in we see the correct items in the navigation plus a little greeting just here asking the
00:28
user to sign up and get started. Okay so we're going to use Laravel Fortify for this which is a headless authentication package for Laravel. So let's head straight down to the installation section and go ahead and get this installed. Okay so let's go ahead and pop the command in to pull that down we'll
00:45
go ahead and publish the resources just here so there we go and let's come down and finally run phpr to migrate which I don't think should do much it will just add some two-factor columns we're not going to be including any of that functionality anyway. Okay so we're going to start to write tests out before we
01:02
touch our app so let's go and create our first test just making sure that we've got rid of the example tests which we have. So the first test is going to be our home test so let's go ahead and say home test this is a feature test so we're not going to go ahead and pass the unit flag and of course we're going to
01:21
run that pest flag to generate out a pest scaffolded test. So over in home test then let's think about the first thing that we want to do so we're going to go ahead and say it greets the user if they are signed out. So what do we want to see when we make a request to the home page? Well we want to see a
01:44
home header and we also want to see some text for example sign up to get started so let's go ahead and work through this. Okay so at the moment we've got this response assigned to this get we're not going to need to do that in this case let's just go ahead and say this get there's an alternative way to do this in
02:03
pest which I'll show you in a second and we're going to go ahead and say assert C which is a Laravel assertion from the Laravel testing framework home and we also want to see when we make a request here sign up to get started so that's the two things that we want to see on this page. Now at this point we're not
02:25
authenticating the user so we've not signed in as a user up here or anything like that so this should pretty much be very very simple to get working. Okay let's go ahead and run pest and see if this passes. Of course it doesn't pass at the moment because within that we don't see sign up to get started we've just
02:43
got the hello text which we implemented. So let's head over to home.blade.php let's change this to home and let's change this to sign up to get started and that should just about pass that test so let's run pest and sure enough we get green. Now let's go ahead and wrap these two things in the guest directive
03:04
so that will only be shown if we are a guest and let's actually change the header to book friends because that would probably be a better greeting we'll update our test to say book friends and we should be good so let's rerun our tests and there we go we get green. So let's now focus on what we can
03:21
see if we are signed in so we're going to focus on the navigation items over on that base layout. So the first thing that we'll do is write the test out for this so it shows authenticated menu items if the user is signed in. You can make that a little bit less verbose if you wanted to. Let's go ahead and create our
03:43
closure out in here and just finish that off and here obviously what we want to do is sign in as a user and then acting as that user we want to check if we can see the correct navigation items and that will include the user's name that we're signed in as as well if you remember over in app.blade.php the very
04:05
top item here is going to be the user's name. Okay so how do we sign in as a user if you've never tested within Laravel before this will be a really good lesson. Okay so if we just open up our directory here and we come over to the database section under factories by default we have a user factory. What this is going
04:25
to allow us to do is when we run this generate out a fake user in the database and this uses the Faker PHP library to create a name for the user an email address for the user and all of the other information as well and this password hash is password if we do need to use it. Okay so how do we use this
04:44
factory? Well let's go ahead and say user so we assign that user we're going to go ahead and say user factory and create so that will create out a fake user in the database ready for us to use. We just need to make sure that we put in the namespace for user at the top here. Now at the moment this isn't quite going to
05:05
work. Let's go over and run pest again and you can see that we've got an error here. This is actually a database error so there's no such table users. Well we need to go ahead and create that table e.g. we need to run our migrations. So at the top of any testing file where we want to run our database migrations we
05:25
need to use the refresh database trait. Now with a standard Laravel test let's just go ahead and generate one of them out so we can compare. So let's say make test example test and let's go over to that example test and have a look at this class. You can see that we get this refresh database trait in here. Now what
05:44
we would do is use that within this class and that would go ahead and create our migrations or run our migrations for us so we actually have that users table. Remember this is all within an SQLite testing database. So that's not what we want to do because we don't have a class so we can't use a trait. So instead what
06:03
we do is we use the uses function within PEST and that will allow us to say refresh database and just pass that in as we normally would. Now for some reason my IDE doesn't like this so I'm just going to go ahead and based in the import for this but that's pretty much what we need to do. Now what this is
06:22
going to do is for every test it's going to run our migrations so we will then have the users table in here. So let's go ahead and just try and run PEST again and sure enough this time it works. The only thing that it is warning us of is we have one risky test because within here we're not performing any assertions.
06:42
So now what we want to do is make a request to the home page but we want to act as this user. Now again using Laravel's testing framework we can say something like acting as user and then we can chain on what we want to make a get request to. So we can say get home page and then we can just continue to
07:01
write assertions. So let's just say what we want to see. So we're going to use assert see text here and we're going to pass an array of menu items that we should see. So we should see the feed menu item, we should see my books, we should see add a book and we should see friends. We should also see the user's
07:24
name but because we've assigned this we can go ahead and just grab the user's name out here. We can also say things like assert see in order but we'll just kind of keep this as simple as possible for now. Okay so we've done this let's go ahead and run this test and see what we get. And of course it
07:41
fails because we've not satisfied this by adding in the correct items. So over to app.blade.php let's go ahead and fill this in. So Alex at the moment is hardcoded, that's my name, so it doesn't make sense. Here we want to go ahead and say auth, user and name. So that makes sense. We've got our feed in there. What
08:01
else do we need to do? We've got my books, add a book. So let's just duplicate this list item down here and say add a book. And we also want a friends option as well. So let's go and duplicate this down and put friends just in there. And we've got a logout link down here but let's just kind of ignore that for now just to
08:23
keep things simple. And of course let's wrap this in an auth directive. So this only shows if the user is authenticated. Same with logout as well. So we'll just end auth there and we'll just go up and indent all of this so it's nice and tidy. Okay so now let's run pest and sure enough we get green. So we know that now
08:43
that we're signed in we can see their menu items. Now ideally what you'd have to do in this test is check that you can't see these if you're not signed in. So you could come over and duplicate this test down or you could go ahead and include this within this test. So you could change the name of the test and
09:01
you could see that you don't see. So we could say assert don't see and then pass in say the user's name which we don't know. So we could say feed for example and go ahead and run that. And yeah I think that's the wrong method name. In fact no it's not the wrong method name. It looks like we've got some
09:22
sort of error here. So I'm guessing that is because over in app.blade.php we are not wrapping this within auth as well. So let's go ahead and fix that. Our test has told us that that is not working. So let's pull that up to here. Go ahead and indent this unordered list if I can catch it. And we should be good. So let's
09:43
run pest again and there we go we get green. So it's really up to you how sort of thorough you create these as. We'll leave this very simple one in here at the moment. We're not really necessarily focused on really thoroughly testing this. We're more looking at the kind of syntax that we can use. Okay so if we
09:59
head over to our app we're not signed in at the moment so we don't see any items in here. Let's just write one more test to verify that we can see specific menu items if we're not signed in. So let's say it shows unauthenticated menu items if the user is not signed in. Again you can make that name a little bit less for
10:21
both if you want to. And I'm gonna go ahead and get rid of this user. Get rid of acting at and we'll just say this get. So we want to see a specific set of items if we're not signed in. So we want to see login. We want to see register and we want to not see add a book. Not see that. Not see that. And I think that
10:43
should be just about it. Maybe also a homepage as well. Homepage link. So let's go ahead and just write home in there. Okay let's go ahead and run pest. Of course that fails because we don't have any of their menu items in there. So let's head down to the bottom here. There are lots of different ways that we could
10:58
structure this but we'll just keep it a little bit messy for now. We can always refactor it later. And we'll add our guest directive in here. And let's go ahead and grab an unordered list for the login and register links. So we'll have that there. And then at the top we will have our homepage link. So let's switch
11:17
this to home. And let's switch this to login. And then let's duplicate this down and switch this to register. Okay let's run pest and there we go. It passes. So we're pretty confident now that we can see the correct items if we are signed in or if we're signed out. Let's just talk quickly about shortening this up
11:39
with some pest specific functions. The moment we're calling this which is pretty much like what we would do within a Laravel test. What we can actually do is emit that all together and we can go ahead and import the get function from pest. And let's see if we can find it. We probably won't be able to
11:57
again in my IDE. So I'm just gonna go ahead and pull this in from my notes. We basically want to pull in this pest Laravel get function from the Laravel package. So now what we can do is shorten this down even more by just saying get rid of this like so. So let's go and just run pest again and sure enough we
12:15
get green. So we'll come across loads of stuff like this a little bit later but it helps just to shorten things down a bit. We can even pull that up. Later on we're gonna look at higher order tests as well which we could do this with. Okay so hopefully that's a sort of good introduction to writing our first test
12:31
just to check that these navigation items are working nicely despite the fact that we didn't really come over to our app. Let's head over to the next episode where we're going to take a look at writing some register specific tests to check the registration process.
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!