تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Creating LikeBehaviorTest0:00

I like the way that this is going. I think, Well, I think now is a good time to make a new test file because you can make the argument that this is part of hosts, but really it's the functionality of liking a post from a profile. So it, it's not like making a post or reposting or things like that. So I think creating a new file, which we can call LikeBehaviorTest if, if I could spell that right.

So I think creating a new file, which we can call BehaviorTest if, if I could spell that right and that's gonna be great. That way our test file doesn't get just this huge large thing. And of course I'm gonna copy and paste some. So let's open up that BehaviorTest file. And the very first thing that we need to do is be sure that, you know, we can like a Post. So we will test, that Profile can like Post,

you know, we can like a Post. So we will test that Profile can like Post, and this is kind of like a repost in that we only want to allow a Profile to like once or at least like a Post once. So we're gonna need to test that as well. And we'll start with our Profile. And once again, we don't necessarily need a Profile in this case because we can just create a Post and use the Profile from that Post.

case because we can just create a Post and use the profile from that post. But for the sake of clarity, I think we need a profile so that we can then say, this profile likes this post. So we will use our factory to create our Post. And then we just want to like that Post. Now we have been using the Post model class in order to, you know, publish a post reply and repost. But I, I don't think it makes sense to do that here. I think since we are liking something, we'll do

Designing Like Model API1:54

But I, I don't think it makes sense to do that here. I think since we are liking something, we'll do that on the Like model. So we'll have a method called create, like I want to stay away from using things like, like, because then that starts to, you know, kind of muddle the, the API because, well is this a like query or is it not? And it, it just kind of makes sense to, to do something like create like in this particular case. And what do we need? We need the profile

to do something like create like in this particular case. And what do we need? We need the profile and then we need the post and that's gonna be it. But of course this is going to return a like, which means that we need to do some tests. And I think there's quite a few things that we need to test. We need to test from the profile side, from the post side as well as from the like side. And we need a use statement for our Like model. So let's pull that in.

And we need a use statement for our Like model. So let's pull that in and let's say that we will start with the profile. So we'll check to see if the number of likes will be one. Although in this case, let's start with something that we know shouldn't be the case. So we'll say that the count will be zero, but then let's also check that the profile likes and we want to see that it contains

but then let's also check that the profile likes and we want to see that it contains the like that we created. And that will be, once again, let's start with something that we know is false. And from here we essentially want to do the same thing for the post. So let's do that. Let's just copy that and then we will say, and, but of course we need to make a few changes here so

Let's just copy that and then we will say, and, but of course we need to make a few changes here so that the post, but well, no, we'll keep that as false 'cause we want to be sure that it works on both the profile and the post side. And then finally we need to test this from the like side. So that's basically the like post is the provided post and we will first of all say to be false. And then we will do the same thing for the profile so

Implementing createLike Method4:04

and we will first of all say to be false. And then we will do the same thing for the profile so that the profile is the profile to be false. So several things that we are testing here, and of course if we were to run this, it's not gonna work. So let's go to our Like model because we need to implement this method because all that we have are the, the profile and the post those relational methods. So we want a static method called create

and the Post those relational methods. So we want a static method called create like, and this is going to return a like that we can work with. But first we're going to have the profile and then the post that we are going to like here. And let's go ahead and think in terms of that. This needs to be unique. So we will say that it returns static::firstOrCreate, and then we just need to specify the profile_id,

So we will say that it returns static first or create, and then we just need to specify the profileId, which is coming from the profile. And then we have the postId, which of course comes from postId. So that's, that should be it. Uh, before we do anything else though, let's open up the create_likes_table.php migration, because I want to be sure that we have that unique constraint set up so that the profileID

because I want to be sure that we have that unique constraint set up so that the profile_id and the post_id are unique there. So we're good to go there. Everything else should be in place. So I don't think we need anything from Post. So let's close that file. And we don't need this PostBehaviorTest anymore, so let's run our tests and let's see what happens. We should have one that fails and sure enough we do,

so let's run our tests and let's see what happens. We should have one that fails and sure enough we do, and it is failing at where we expected it to because the actual size is one and it matches the expected size of zero. So we could do one of two things. We can change these things one at a time and test to see if they fail, which we would hope that they would, or we can just change everything to what it should be.

that they would, or we can just change everything to what it should be. And then go from there. I'm gonna do that because it would, it would take a little bit of extra time to step through each one of those tests. And you don't wanna see me do that. I don't wanna see me do that. So this should give us nine passing tests and we're good to go there. But the next thing that we need to test is to be sure

Testing Duplicate Like Prevention6:27

and we're good to go there. But the next thing that we need to test is to be sure that we cannot create duplicate likes. We should already be there, but I want to test to be sure that that is indeed the case. So let's once again create a profile and a post. And we're gonna do this a lot like what we did with the repost. So that's, we will have the like one which will be createLike,

So that's, we will have the like one which will be like create like, and then we will pass in the profile and the post. Then we will create a second like, and we will expect that the like one id is going to be the like two id. And let's just run the test. We should have 10 passing tests. Sure enough, we do. So we have the ability to like a post, we have the ability

Implementing removeLike Method7:15

We should have 10 passing tests. Sure enough, we do. So we have the ability to like a Post, we have the ability to ensure that there are no duplicate likes between a Profile and a Post. So now we just need one other test that we can remove a like, so let's do this, let's go back up to our Profile can like a Post. We want the Profile, we want the Post, and we want to like that Post.

We want the profile, we want the post, and we want to like that post. That's going to be the starting point of this particular test. But then from there, uh, let's do this kind of like how we removed a repost. We're gonna do the same thing to where we will get a success variable that will be true or false based upon if we remove a, like I'm gonna call it removeLike as opposed to unlike, because the, the terminology that we are using right now.

a, like I'm gonna call it remove alike as opposed to unlike, because the, the terminology that we are using right now is called create. Like, so it kind of makes sense to have a create like, and then a remove. Like I'm just trying to keep things a little expectable. Expectable, that's not a word. What's the word I'm thinking of predictable. That's what I'm thinking of. Okay, so we want to remove a, like for a profile

That's what I'm thinking of. Okay, so we want to remove a, like for a Profile and a Post that's going to return true or false. And what it's almost like we need to take the tests from creating a Like and doing the exact opposite basically. So that if the Profile likes have account of one, then it should be a zero. And uh, there should not be a Like contained within the Profile likes.

And uh, there should not be a like contained within the profile likes the post should have counted zero. The posts should not have a, like that contains the like, so we're just reversing everything here except here. Uh, we don't need to check the like, although do we, let's yeah, let, let's do that. We'll, we'll keep going on here or no, we don't need to do that. All we need to do is check

or no, we don't need to do that. All we need to do is check to see if success is going to be true. So we will change this. So that's success to be true. And of course if we were to run this right now, it's not gonna work because we don't have that removeLike method. Sure enough. So let's go back to our Like model and let's create that public static function called removeLike,

and let's create that public static function called remove like, and this is going to look very similar to removing a repost so that we will get where the profileId is the provided profileId and also where the postId is the same as the postId that was passed. And then we will attempt to delete that and we will return if that is greater than zero. So same logic, just a different table.

and we will return if that is greater than zero. So same logic, just a different table. So that's now if we run our tests, we should have 11 passing tests. We do. So now we have our like functionality. Now I'm sure that there are other things that we need to implement or test as far as likes are concerned, but we have the basic functionality down so that I think we can move on to implementing the follow functionality.

Transition to Follow Functionality10:36

that I think we can move on to implementing the follow functionality.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟