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

Refactor watched check0:00

Okay, but inside our template here, we have now introduced this new check here, which is quite complicated, where we want to make sure if a User has already watched a Video. And I probably need to read this two or three times until I understand it every time I get here. So this is already a good sign that we should refactor this. And as I already said, I want to make sure that this Video is already watched by a specific User. Which means this already indicates a good name for how we want to do this. So what I want to do is on the Video itself, I want to make sure if the current User has already watched this Video.

So what I want to do is on the video itself, I want to make sure if the current User has already watched this video. So let's call this alreadyWatchedByCurrentUser. So I think this would now be way better to read and to understand for everybody coming to this file. And we also don't need to pass any arguments here because we have the video, which is important and the User we can get through the global authentication helper. All right, so back in our test now, this should now fail. And it does because call to undefined method alreadyWatchedByCurrentUser. All right, so this means we need to create this one, but I don't want to test it just

Add model method tests1:08

And it does because call to undefined method already watched by current user. All right, so this means we need to create this one, but I don't want to test it just through this test here. I also want to create a separate test on our VideoTest for our Video model. So let's add here a new test. It tells if current User has already watched a given Video. All right, what do we need here? We're going to need a Video. So let's create one. Video::factory()->create().

So let's create one. VideoFactory create. We don't care about any details of the Video. And then when we act, we want to, we need to be locked in as User so that we have an authenticated user. And now we want to expect that when we call this new method, which we don't have yet, videoAlreadyWatchedByCurrentUser, it is a method and we want to make sure that this is false. So we want to make sure that the current user has not yet watched this Video. And since we haven't connected the User to the Video yet, it will fail.

Implement model helper method2:13

So we want to make sure that the current User has not yet watched this video. And since we haven't connected the User to the video yet, it will fail. But before that, it will also fail because the method is not given yet. So let's start by creating this one. And this will be inside our Video model. So let's create a new public function. We already have the name. We're not going to provide anything. We're going to return a boolean. Now we can just copy the check from our template here.

We're going to return a Boolean. Now we can just copy the check from our template here. Okay. We don't have it anymore. So let's do it again ourselves. We want to make sure to return a Boolean and then through the authenticated user watched videos where the video_id equals to here. We have now the id of the current video because we're inside a Video model and then we're calling the count method. I think this should do it.

calling the count method. I think this should do it. Let's run the test. Okay. The test is now working. Let's check our test again. So we've now made sure that we can check if a User has not yet watched a given video and this test is working. And now we also have to take care of the opposite. And I think we need to adapt the name here.

Test already-watched case3:25

And now we also have to take care of the opposite. And I think we need to adapt the name here. It tells if currentUser has not yet watched a given video. Let's copy this. And now we want to make sure it tells if currentUser has already watched a given video. Yes. Now we need to make sure that the user is connected to the video. So now it's better to start with the user here. So we're going to create a User and the user has a Video. So we already know that the video is connected to the user, calling the factory.

So we're going to create a User and the User has a Video. So we already know that the Video is connected to the User, calling the factory. And we also have to provide here the name of the relationship. And then we're going to create the User and the Video. And let's also import the namespaces. Then we're going to log in now as our given User. And then the Video, which we need, we get through the User. And then the Video we get now through the User itself. And we call the watched videos relationship. And we're just getting the first one because there is only one Video.

And we call the watched videos relationship. And we're just getting the first one because there is only one video. And this should now be true to be true. All right. Let's see if this is also working. And yes, it is. Now I think we can go back to our VideoPlayerTest and this should now also work. Yes. And just to be sure, we're going to run all the tests and some of them are failing now. Let's check why.

Fix unauthenticated test failures4:44

And just to be sure, we're going to run all the tests and some of them are failing now. Let's check why. Call to a member function watchedVideos on null. Let's check what's failing here. Let's run the test again. Member function watchedVideo on null. All right. And where does it come from? From here. So yes, this means what we get back here is null because we don't have an authenticatedUser.

From here. So yes, this means what we get back here is null because we don't have an authenticated User because we aren't logged in for this test here. So let's do this. Log in as User. And this is not working. Perfect. And I think we're going to need here as well. And for this one, and for this one, I think this should be it. And now all tests are green again.

Refactor login with beforeEach5:30

And for this one, and for this one, I think this should be it. And now all tests are green again. Okay, cool. So this works now. But yeah, it seems we are now using this helper method in, yeah, I think in all of our tests here because here as testing our Livewire component, we need to make sure that we have now an authenticated User because we're making a check for this User inside our template. And every time we use a specific task in every of our tests, it might be good to refactor this out to a method. And similar to phpunit, to the setUp method in the past, we have beforeEach method, which

this out to a method. And similar to PHPUnit, to the setUp method in the past, we have beforeEach method, which also runs before every single test. So this means what we can do now is log in as a User here before every test. Now we can get rid of this. And now this also works. Let's get rid of this. And this as well. I think we're good now. And yeah, all the tests are still green.

I think we're good now. And yeah, all the tests are still green. And we now are running this before each helper method before every test. But also if we check out our two tests here about marking a video as complete, here we are still creating a User and we're also logging in as a User. So I think we can improve our helper here by also getting here back to currently User. And similar as if we are in a current class, we can also create a property here. So let's call this one loggedInUser, because this is what we get back from our helper method. And now we have access to this inside all of our tests. So let's go back down here.

And now we have access to this inside all of our tests. So let's go back down here. Yeah, so here's the first one. So we don't need to create a User because we already have one. And now instead of calling the User directly, we're going to call it through our new property. And we need to do this here, and here, and here. And yes, we didn't break anything. That's good. Now we can also do the same here, here, here, and here, and here. This should be the last one.

Now we can also do the same here, here, here, and here, and here. This should be the last one. Yes, it's passing. And one last time, we're going to run all of our tests from our Livewire component tests, and they all are good. Perfect. So let's go back here to our helper. Normally, I tend to not use helpers like this too often, because I really like to start with a clean state in my test, and then everything I need, I'm going to add in the arrange part here because then every test, it is very clear what we're going to do and what is happening.

with a clean state in my test, and then everything I need, I'm going to add in the arrange part here because then every test, it is very clear what we're going to do and what is happening. Because if you only look at this test, we don't really know that we are locked in. But if you really need something in like every test, then I think this is still a good idea to use. And that's why we created now this little helper here for getting the locked in User. Nice. Also with the new additions through my Livewire assertion package, we are now also checking that when we test some features of our component, that also a method or a property is wired is connected to a specific HTML element inside our Livewire template.

that when we test some features of our component, that also a method or a property is wired and connected to a specific HTML element inside our Livewire template.

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