23. Pending and accepted friends


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,
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,
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.
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.
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.
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
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.
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.
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
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.
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
is we're going to be having pending friends of, pending friends of mine, accepted friends of, and accepted friends of mine.
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
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.
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.
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,
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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,
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,
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,
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.
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.
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
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.
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.
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,
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.
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.
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
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.
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


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.