25. Getting all friends


If we think about the relationship between friends, let's say we had a user with ID of 1 and a user of ID of 2, and maybe a third user just here.
Let's say that user 1 adds user 2 as a friend. That has been initiated from this side. And of course, we have two different relationships to work that out.
As we saw from our database, we have got friend ID and user ID. Now, if 2 adds 1, that means that these are both friends. User 1 has one friend. User 2 has one friend.
But these technically relationships need to be combined. So if a third user adds user 1 and user 1 accepts, they now have two friends.
So the relationship goes both ways. Effectively, what we want to end up doing is merging the two relationships we have over in the user model. So we want to merge accepted friends of mine
and another relationship accepted friends of, which we haven't created yet. But once we've written a test for this, we'll be able to get this working.
So if that doesn't make sense, let's go ahead and write a test to make this make sense. So can get all friends. We basically want to know that we can add a friend,
add another friend, maybe that hasn't been accepted, and we can get a list of our friends. So let's go and just start to scaffold this out as we've done before and see what we can do.
So we've got two people there. And we want another friend here as well. So we want another friend. So we're bringing another person into the mix,
much like we just demoed. So for this first user, let's pretend that's us. We're going to go ahead and add a friend. And we're going to add this friend.
And we're going to go ahead and add another friend. Let's say another friend. So that friend, that first friend that we've added, is going to accept our friend request.
So let's accept that user. But this one is not going to accept. So this one has not been accepted. So let's think about, in terms of these three users,
who has friends and who doesn't. Well, the first user, so that's called user, should have one friend. The second user, which is friend,
oops, friend, let's go back down here, friend, should have one friend. And another friend should have no friends because this has not been accepted.
Now, we could make this more complicated, but we've got enough to see that this user has friends because we've been added by this, and this one has been accepted by this friend.
So these should two merge in, and this friend and this friend basically have that connection, and another friend has zero.
So let's go ahead and write some expectations to fulfill this. And if it still doesn't quite make sense, we can work on this.
So we want a general friends property on here. It's not quite gonna be a relationship because that sort of gets a little bit more complicated. And let's change that to expect.
So we're gonna say to have count one, and the friend, friends, should have a count of one. And the another friend, friends, should have a count of zero. Let's go ahead and run our test.
Of course, it's gonna fail because we don't have a friends relationship or property over on the user model. So we're gonna make this a little bit rough.
It's a little bit restrictive, but let's just come up to the top here, and we're gonna create out a friends method. So we could create out a friends method,
or we could just say get friends attribute, which will effectively create a relationship type thing, but this is technically not a relationship. And we could go ahead and grab the accepted friends of mine
and just the collection, and we can merge in the accepted friends of. So we're putting that two-way relationship in here. Now, if we come down to accepted friends of mine,
we don't have a accepted friends of, and we know that that's easy because we already saw this with the pending friends of mine. All we really need to do here
is just reference the friend of relationship that we had down here. So it's just basically the reverse. And again, accepted needs to be true.
So for this attribute, let's go back up to here. We're gonna merge in this accepted friends of, like so. So we're just merging two collections. Now, obviously, this makes it a little bit trickier
to implement things like pagination, but this is probably the fastest and quickest way to build out a friends system. Let's go over and run that test.
And sure enough, it works. So we know now that all three of them have been satisfied. Now, we could expand on this if we wanted to. So for example, we could bring another user into here,
and then we could add this for this particular user. Let's just really quickly do this. And this isn't the best way to do things, but we're gonna say yet another friend.
And let's go and say user add friend, yet another friend. And we'll actually do this at the bottom. And then we're gonna say yet another friend, accept friend, and we'll accept that user.
So now, this user should have two friends. If we just run the test, of course, that's gonna fail. And yet another friend should have one friend, because this user has two friends via that addition.
And yet another friend should have one. There we go. So we know that that is pretty much all working now, because through everything that we've already created
and tested so far, we get all of these counts is what we would expect if we'd have done this through the UI. Okay, so we're nearly wrapped up on this friends unit test.
In the next episode, we're just gonna look at removing a friend, whether that be a request or a friend that we've already accepted.
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.