Playing
31. Deleting a friend

Transcript

00:00
So deleting a friend is going to be pretty straightforward because we're just going to follow the same pattern,
00:04
create a controller, write our test, and we are done. So let's start out with our test as usual. So let's go ahead and create a test in here. And we will call this friend destroy test.
00:16
It's going to be very similar looking to our patch test. We're going to send pretty much all of the same requests. So let's just grab this entire test and copy this over to our friend destroy test
00:27
and paste this in. Now we're going to go ahead and skip this one for now. So we looked at that earlier. Within Pest, we can just use the skip method
00:34
chained onto here to skip this one. And we want to go ahead and send a delete request to this with the ID of the user that we want to remove. So let's go ahead and run this test.
00:46
Pest tests feature and friend destroy test. And it doesn't work, of course, because we don't have anything in here. So again, let's follow the same pattern.
00:57
Let's go ahead and create our controller in here. Friend destroy controller. Let's hook this up in our web routes. So we'll just copy this down to here
01:07
because it's very similar. Sending a delete request now through to friend destroy controller. Let's head over to friend destroy controller
01:16
and let's hit add in our constructor. And let's go ahead and add in our middleware. And our first test should pass nicely. Okay, there we go.
01:25
Let's go ahead and rerun our test. And once again, we don't have our invoke magic method. There we go. So this one works.
01:33
Of course, we've skipped this one. So we get a warning. Okay, so over in our test, let's go and unskip this and fill this in
01:41
to see what we need it to do. Now, regardless of whether we have accepted the friend request, it doesn't matter. It's going to delete this anyway.
01:48
We're going to use this to remove a request, but also remove the friend entirely. So we're just going to add the friend in here, not accept the request and just assume that this works.
01:58
We could go ahead and create two different requests or two different tests for this, but we'll just leave it at one for now. Okay, so it deletes the friend request.
02:06
We want to act as this friend or we can act as the user. And we want to patch through or actually delete through to friends and then friend ID. And then we want to assert that the database
02:19
is missing this record. So we're going to use assert database missing. And under friends, we want these two to not exist. So let's run our test.
02:29
And of course this fails because this is in the database with an accepted status of zero or false. So now that all we need to do is come over to our friend destroy controller and fill this in.
02:41
So let's bring the request into here and also bring that user in here as well. And let's go ahead and do this now. So we already know that we created a method
02:49
and tested a method to delete a friend. So all we need to do here is say remove friend and pass that friend in and go ahead and redirect the user back.
03:01
Let's go ahead and run our test. Sure enough, we get green. So now we need to do is come over to our template here, fill this in.
03:09
So again, we can copy and paste a lot of this here because this form is going to be very similar. Let's go ahead and grab all of that form, come up to our friends list, pop this underneath
03:19
and let's go ahead and send a delete request through and just say remove like so. And that should be everything we need. So let's go over.
03:31
Of course, just go ahead and switch that to friend and there we go. So I can remove Mabel as a friend now. And of course, if we were signed in as Mabel,
03:39
that would no longer show in our friends list. So now our friend system is pretty much complete. We're going to go ahead and work on our feed in the next couple of episodes.
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!