26. Removing friends


Before we move into our feature tests, let's finish off with the ability to remove a friend. So let's just get started. It can remove a friend and let's go ahead and create this out.
So we're going to need to add a friend first. So once again, let's just bring in two users. That'll be enough. And let's go ahead and add that friend from here.
So add friend and we'll add that friend. Now we don't necessarily need to accept the friend request before we delete it because we're just going to be removing that record regardless. So to decline a friend request, we're just going to delete this
and to remove an already accepted friend, we're just going to delete this as well. So let's just go ahead and accept this friend request anyway. So friend and accept friend and user. And then from that user, we're then going to say something like remove friend friend.
And we want to expect that the user friends has a count, have count of zero. And we also want to do the reverse. So we want to make sure that for the friend friends that also has a count of zero. Let's go ahead and run our tests.
Of course, this fails. We don't have a remove friend method. So let's go up to where we are adding a friend and let's just do this under accept. Let's create out a remove friend method.
And of course, we're going to accept a friend into there and basically just detach this record. So we're going to say this friends of mine. Now what that also does is not care about the accepted or unaccepted status because we're referencing the sort of base relationship here.
And we're just going to go ahead and detach that friend. Let's run this again and we get green. Great. So we could change this around or create another test to not accept the friend request first.
As you can see, though, if I get rid of accept friend, it still passes. So regardless of whether it's accepted or not, it just detaches that friend relationship completely. And they're no longer friends or the pending friend request is no longer there.
Okay. So over the last episodes, we've written enough here to cover all of our base functionality over in our user method. Now it's time to head over to the next few episodes and actually build out the UI.
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.