Playing
23. Pending and accepted friends

Transcript

00:00
So pretty much for the rest of the course we're going to be focusing on the friend functionality. The first thing that we're going to do before we start to look at the database,
00:09
models or anything is write a test. What this is going to do is give us a good opportunity to think about how we want this functionality to work. And then of course once we've got our failing test,
00:20
we'll fill in the functionality to make this work. So the first thing is just to write a test. Now we're going to put this under the unit directory. We are going to be hitting the database which is not always a great idea for unit tests.
00:33
But I like to keep the unit tests separate from the feature tests like the HTTP tests that we've been creating. And I don't necessarily mind if they hit the database as long as they are pretty quick. Okay so we're going to go ahead and make a friend test.
00:48
We're going to of course use the pest flag and we're going to use that unit flag. So let's go ahead and open up that friend test and we can start writing. Now I'm going to go ahead and bring over from any of these tests that we've already got, the refresh database trait.
01:03
So let's pull that over to this test really quickly. And we can get rid of these two because we're not going to be making any get or post requests here. So the first thing that we want to do and very very simply is test it can have pending friends. So we want to be able to from a user add another user
01:25
and see if a relationship contains that person that has been added. So let's go and create two users out here. We could technically do this without the database but we're going to go ahead and just hit the database anyway.
01:37
So let's go ahead and pull that user model in. And we're going to have another user which we're just going to call friend here. So we've got a user who wants to add a potential friend. This test is going to give us the opportunity to see how we want this to look.
01:53
And it's probably going to look something like this. User add friend and friend. That makes sense. And then we want to go ahead and use a pest expectation
02:03
and check that the count of the pending friend requests for that user is one. Because in this case we've just added one user. So we're going to be writing a lot more of these expectations throughout all of the tests. We're going to be writing in this friend test.
02:18
But we'll see how we can clear them up later with some cool pest functionality. So bear with me here. We're going to have a relationship on here called pending friends of mine. Now the reason that we're having pending friends of mine
02:34
is we're going to be having pending friends of, pending friends of mine, accepted friends of, and accepted friends of mine.
02:47
Now the reason that we're going to have this is we're going to set up a relationship and a pivot table with two user IDs in. And the reason that this is important and having of is because we could be sending a pending friend request to someone
03:02
and we could be a pending friend request. So hopefully that makes sense. But we'll get to the relationships in just a moment. Let's just make sure this works.
03:10
So we want to make sure that the count of the items we get back from this relationship that we'll eventually create is one. Now with pest, there's a couple of ways that we could do this. We could either use the count method on here and then say something to be one.
03:27
Or we could say to equal one. Or what we could do is get rid of this and we could say to have count one. That kind of makes a little bit more sense and it reads a little bit better. And again, all of these expectations, we're not going to cover all of them,
03:44
but they're all over in the pest documentation for you to check out. So just have a really good browse and choose the one that is most suitable. But I think this reads really nicely. Okay, let's go ahead and run this test.
03:57
So we're going to go into that unit directory and specifically run that friend test. And of course, if we just head up here, so this is probably because over in our pest configuration, if we just head up to the top here, we have got this test case being run just for feature tests.
04:16
Let's go ahead and switch this up. So we use this for unit tests as well. That's going to allow Faker to kick in and we won't see that error. We will see another error, which is that we don't have an add friend method.
04:28
So that's what we want to fill in. So let's go ahead and start to set this up. So let's go over to our user model and let's create our method in here called add friend. And of course, into this, we're going to accept a user in.
04:46
And we're just going to do nothing at the moment because we're not really sure what we're doing here. So let's go ahead and run our test again here. And of course, this still fails because we don't have a relationship set up.
04:58
So we can't get a count of that relationship. So now what we're going to do is go ahead and create out the friends table, hook up the relationships and implement the add friend functionality. So time to create that migration.
05:12
Let's go ahead and create this out here. We're going to call this, we just actually use make migration, create friends table. And let's open up create friends table. And let's think about what we need to see in here.
05:28
So the ID and the timestamp we can leave. We're going to want two foreign IDs. One is going to be the user ID who has made the request. And the other one is going to be the friend ID.
05:41
That's who is being added. Now with this, because friend ID references the users table under the constrained method, we need to pass in the actual table name. Otherwise, Laravel is not going to be able to work out where this comes from.
05:54
And inside of this pivot table, we, of course, need a way to check if this has been accepted or not. So this will create a record in this table that say, Alex has added Mabel. But we'll need a Boolean to see if Mabel has accepted this friend request.
06:09
So we're going to add a Boolean of accepted. And we're going to set a default in here of false or zero. So now that we've done that, let's go ahead and run phps and migrate. And we can get on with creating that relationship.
06:21
So that relationship is exactly what we were testing for, which is pending friends of mine. So this is the pending friend request that the user has made that the friends have not yet accepted. So let's go over to the user model.
06:34
Let's go ahead and create this relationship out down here. And let's fill this in to see what we need to do. So because we have a many-to-many relationship here, this is going to be a belongs-to-many setup.
06:47
Of course, we're referencing users because users are adding users as friends. We're going to need to define the table name in here because it's non-standard. And then we're going to provide the user ID here. It's the foreign pivot.
06:59
And the friend ID is the related pivot. And then what we want to do, really importantly, is make sure we use with pivot because we want to know that accepted status in there as well. Now, what we also need to do is add a where clause onto this.
07:14
So we're going to say where pivot accepted. And we want to say false because, of course, pending friends of mine are from requests that have not yet been accepted. So that is the relationship.
07:31
Let's go and see what happens when we run this. So let's go ahead and pull up that pest test here. And if we come up, you can see that now that the relationship is in there, it is working. But there are no pending from requests when we should see one.
07:44
That's pretty obvious because our add friend method here is not implemented yet. So let's go ahead and add this relationship in. So let's go ahead and say this pending friends of mine. We're going to go ahead and sync here without detaching
08:00
because we don't want to detach any existing. We want to pass the friend in. And actually, it might be better if we call that friend. So this attachment works.
08:10
And we're going to go ahead and say accepted and false. So we're explicitly passing that in, even though we've defined it at the database level. But that's fine.
08:18
Let's go ahead and run our tests. And sure enough, we get green. Now with this, we kind of want to refactor this because we might want to have a sort of base friends of mine relationship.
08:31
And then from that, have then a pending. Because this doesn't really make sense to me. What I want to do is say, add this to my friends with the accepted status as false. So let's do a little bit of refactoring here,
08:43
just so we have a little bit more flexibility. Now that we've got a passing test, we can just refactor. We don't really care too much. So we're going to go and create a friends of mine relationship,
08:57
which is going to be pretty much all of this. So we'll put this in here. But it's not going to have that pivot. The pending friend request of mine is going to reference friends of mine,
09:09
but then where the accepted pivot is false. So we're just referencing that base relationship, which I'm actually going to put at the bottom. But then we're adding that constraint onto that.
09:24
And now here, what we can do is access the base relationship to say that we want to add this to the friends of that user. Let's go over, run our tests. Sure enough, we get green.
09:36
So what we've done so far is in this friend test, acting as this user, adding a friend. And we know that we now have pending requests that we have initiated. But what we want to do here is create another test to see
09:51
if we can have pending friend requests from someone else. And that's where the other relationship is going to come in. So let's create another test in here. Let's get rid of this just so we can sort of start from scratch.
10:02
And let's say can have friend requests. So I can receive friend requests. So we'll pull these two in, user and friend. And now the friend is going to add us.
10:18
So the friend adds this user. So now we want to expect that this user has pending friend request. And that's going to be that pending friends of relationship, not friends of mine because I've not initiated this,
10:35
pending friends of. And again, we're going to say expect the count or to have count to be one. So let's go ahead and run this test. And it fails because we don't have that relationship in there.
10:48
So this is just going to be the reverse of what we've already done. We've got friends of mine. And now we want to create a friends of relationship, which is going to be just very slightly different.
10:58
And you may have already guessed, we're just going to switch over the friend ID and the user ID. So they are just reversed like so. And we can pretty much copy what we did here with pending friends of mine
11:12
and say pending friends of and say friends of, and where the pivot accepted is false. Let's run our test, see what we get. Sure enough, we get green.
11:26
So now we confidently know that we can add friends, but friends can also add us. And we know that friend requests pending can go both ways, both from us to another user and another user back to us.
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.