24. Accepting friend requests


Okay, so let's take a look at accepting friend requests. Just before we do that, we didn't write a test in the last episode
that covered whether we can duplicate adding a friend request. Now, we already knew to use sync without detaching, so this didn't add additional records. But let's go ahead and just cover this with a test.
So we want to make sure that if I choose to add a friend twice or add someone by their email address twice, that it doesn't duplicate that friend request because that just doesn't make sense.
So let's say does not create duplicate friend request. And we've already got the functionality here, so really what we need to do is just have two people and we need to add this friend twice.
So we're going to go ahead and say user add friend and we're going to add that friend and then we're going to do that twice. So we can just copy this line down.
Now, what we want to do is expect that the count of the pending friends doesn't equal two or only equals one. There's a couple of ways around this we can do, but we're going to demonstrate a chain of methods
that we can use to make this a little bit clearer. So what we would normally do is say expect and we would say user pending friends of mine and we would say to be or to have count one.
So we're saying that we expect this just to be one. Let's go ahead and run that test and sure enough we get green. So there's actually another way that we can write this test really just depending on how you want this to read.
We do want to expect that it only has one, but what we can actually do is say not to be two, for example. Maybe that's a little bit clearer because we're doing this twice, so we're expecting it not to be two.
It's going to work in exactly the same way and still pass or we could say expect to have count two, not to have count two. So this is the kind of expressive syntax you'd expect from PEST. It reads really, really nicely.
So it's really up to you what you do. I'll leave it like this because I think this is a little bit better, but that just goes to show us how we can negate what we're doing here that we previously just used on its own.
Okay, so now that we've got that out of the way and looked at that interesting piece of PEST functionality, we're going to go ahead and write a test here to see that it can accept friends.
So let's go ahead and just map this out how we would want it to actually look in our controllers. Remember, we've not done anything with our controllers just yet and then we will go and fill in the functionality.
So we're going to go ahead and say user add friend. So I am adding this other user. Now this user needs to accept my friend request. So let's say accept friend and let's pass myself back into that.
So this is basically just an exchange between two people. So what are our expectations going to be? So let's say expect user and we've got pending friends of mine. Why don't we also implement accepted friends of mine
and we're going to go and say that we want to expect the count here or to have count to be one. That makes sense. Now if we run this at the moment, of course, it's not going to work.
Let's go over and sure enough, we don't have an accept friend method. So let's go ahead and implement this accept friend method to get this to work and then we're going to add another assertion to this just to properly cover it. Okay, so let's go ahead and say accept friend
and let's, of course, accept in here a friend. Now we're going to do this kind of in reverse. We're not going to say that we want to update our friends. We're going to say from that friend,
we're going to go ahead and grab their friends of mine relationship and we're going to update the existing pivot on that way to ourselves. So that just does it in the reverse order, but it works a little bit better this way and let's go ahead and say accepted to true.
Now we'll go ahead and break this test in a minute just to see how doing it the other way doesn't quite work. Let's just run that test again and just see what we get. Okay, so assert count must have count or must be iterable.
So of course, over in our friend test, we don't have an accepted friend relationship. So let's go down to our pending friends of mine and let's create an accepted friends of mine. And you can see now why we broke this out into another method
because we can just reuse the friends of mine relationship, but set the condition of this to true for accepted. Let's go ahead and run our test and see what we get. There we go. It works.
Now let's just fiddle around with this and just see because if we said user, for example, or this friends of mine and we updated this for the friend ID, let's go ahead and run this test.
And you can see it actually fails because technically it's the friends relationship that we need to update to have this work nicely. So let's go ahead and switch it back.
We've got a test now to cover that. So we know that even if we wrote it the other way, it would still work. Okay, so let's look at what else we need to do. And if we go back to our test,
let's see what else would strengthen this expectation. Well, it might make sense to grab a list, to pluck out a list of the friends that we have in accepted friends and check that the user's ID is in there or the friend's ID is in there.
So what we could do is write another expectation here and say user accepted friends of mine. And we could maybe go ahead and pluck out the ID of the friends and see if it contains.
So let's look at another pest method here to contain friend ID. So we want to make sure that our friends contain that friend that we actually added. We could have technically done this for the other tests. I've added this in here purely so we can learn
about refactoring pests test a little bit later to make them read a little bit nicer. And we're going to get to that in a later episode. So let's just go ahead and run this test.
Sure enough, we get green. That works. And we're going to come back to this a little bit later on in the course and refactor it to make it look a little bit nicer.
Okay, there we go. We have added the ability now to accept a friend request. We've written up a couple of expectations here that we can refactor later. Let's move on to the next episode where we're going to get a list of all of our friends.
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.