Playing
25. Getting all friends

Transcript

00:00
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.
00:09
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.
00:19
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.
00:33
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.
00:43
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
00:56
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.
01:04
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,
01:12
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.
01:24
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,
01:32
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.
01:42
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.
01:54
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,
02:04
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,
02:15
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.
02:28
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.
02:39
So these should two merge in, and this friend and this friend basically have that connection, and another friend has zero.
02:48
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.
02:55
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.
03:08
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.
03:27
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.
03:37
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,
03:47
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
04:02
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,
04:14
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
04:24
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.
04:37
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
04:50
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.
05:00
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,
05:13
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.
05:23
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.
05:39
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.
05:53
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
06:04
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.
06:16
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

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!