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

Refactor After Feature0:00

Let's don't forget that for every new feature we build, we will spend some time to refactor the code as well. Now is the best time to refactor because we still have a good understanding of what we just built. In our case, it was that the User can see purchased courses. First, let's run our test again. Yeah, so 47 tests are passing. That's great. And now is a good time to refactor our code. And as we've learned, when we start testing our code, I like to use static text, but now since everything is working,

And as we've learned, when we start testing our code, I like to use static text, but now since everything is working, I like to get rid of that. So let's take this and create a new variable for the video because we're going to use this quite a bit. video, then we can use the video here. And we can use it here as well. So we need the video title, video description. Here we go.

video description. Here we go. And then video duration. And for the duration, we want to see a specific string. So let's use some {} braces here around our duration. And we also want to make sure we see the minutes as a string, I think like this. And it's working and it's way less code than before. Next here, also we want to do the same. We want to get rid here of the whole state method.

Next here, also we want to do the same. We want to get rid here of the whole state method. We don't need that anymore. And again, we can grab here this call. And let's make a new video variable. And we're going to use this here now. When we pass the video and also here, instead of providing the ID, what we're now doing is calling the video.email.ID. And since we're already using double quotes and single quotes,

the video email ID. And since we're already using double quotes and single quotes, it's best to get out of the string, provide the video. And yeah, perfect. So this code is also now working. And also here, we have a little bit less of code. All right, so far so good. What is next? Yeah, so we have named this duration. But again, I think we have talked about this already. It's quite difficult to see what we get back here is this duration.

Rename Duration Column2:11

But again, I think we have talked about this already. It's quite difficult to see what we get back here is this duration. The minutes and hours and seconds. I always like to add some kind of type here as well to the name. So I would go here for duration in minutes. So it's pretty clear that we get minutes back. Of course, if we run this now, this will fail. And it does, because this does not exist yet. But since we're still working locally here, and we haven't shared the application with anyone else,

But since we're still working locally here, and we haven't shared the application with anyone else, it's pretty easy to just change it inside the videos migration. Let's run the test again. What's now the error? Has no column named duration. And that's now because inside our video factory, I think, that's where we still have to find the duration. Yeah, but what we want to fill here is duration in minutes. Let's run the test again.

Yeah, but what we want to fill here is duration in minutes. Let's run the test again. And yeah, this is now passing here. Good, and I think it's better now that we have a better naming here, which is better to read. But again, here we have to be very careful, because every time we change a column like this one, then we probably see our test passing like it does, but actually it's not passing. And again, let me show you by bringing in our state method here.

but actually it's not passing. And again, let me show you by bringing in our state method here. And now we are saying the duration in minutes is 10. And now let's make sure that we see 10 minutes. And what's wrong here? Yeah, we have used minutes, but actually we're using it without an S. Let's try it again. Okay, now it fails because it doesn't contain 10 minutes. And if we search for minutes here,

Okay, now it fails because it doesn't contain 10 minutes. And if we search for minutes here, we will see... Yeah, here we don't get anything back. So we don't have a duration. So this means what we have to do now is change it inside our video player. Here we go. And now we're using duration in minutes. And if we run our test again,

And now we're using duration in minutes. And if we run our test again, now it is passing. And now it's also the time where I want to get rid again of the state method, because now I know that it's working. And we can change this back from the string to using our video variable with duration and all inside braces to this. And yeah, this is now working. Okay, perfect. So far, so good.

Add Readable Duration Method4:37

And yeah, this is now working. Okay, perfect. So far, so good. But yeah, there is still one little thing that annoys me a bit. So if we go here where we have not a video, we get not a duration in minutes. And then we are providing here a string to get minutes to wrap this like this. So yeah, I think we can do better. And I think this is a good idea where we can use method instead of just using the duration like this. So what I think

use method instead of just using the duration like this. So what I think would be better, if we could call a method here, getReadableDuration like this, so that we have some place where we define how we want our duration to be displayed. And here in our view file, we're only going to call this method. Of course, this will fail now. That's exactly what we expected. And as we have learned, we are not going

Write Model Test5:25

That's exactly what we expected. And as we have learned, we are not going to create this method now because first we're going to write a new test, but we can copy the name. So let's start by creating this new test. It will be a feature test. It will be inside the models directory and we're going to call it VideoTest. Yep, like this. What do we want to test? To test, it gives back readable video duration.

To test, it gives back readable video duration like this one. Okay, so this means we're going to need a video, videoFactory create. As always, we're going to start by providing a specific text here. Import the namespaces, then we're going to act. And I think we can already do this together with the expectation API of past. So we're going to

together with the expectation API of past. So we're going to expect that when we call video and then the method name, and this should equal to a string called 10 minutes. I think that's how we used it, right? Let's run this test. It's failing. Not null constraint failed for videos course_id.

Not null constraint failed for videos course_id. Okay, that's because let's check this out inside our video factory. I guess we just haven't defined a video_id or course_id to be correct course_id. So in case we don't provide any course that comes with the video for the factory itself, we can do this here. So let's say the course_id should be

with the video for the factory itself, we can do this here. So let's say the course_id should be connected to the CourseFactory. And that's what we can do in Layouts. We don't have to create this or we don't have to provide the ID ourself. We just can provide the factory. And inside our video test, if we run this again now, it should pass, but it does not get readable duration. Yeah, of course we haven't created this method yet. Inside our Video model, this is something we need, of course.

we haven't created this method yet. Inside our Video model, this is something we need, of course. Let's get in here, create a new method. We're going to return here a string and let's make use of string helper level. So string of I'm going to use the current video model duration. We have renamed this to inMinutes. And we're just going to append

this to in minutes. And we're just going to append minutes like that. I think this should already do it. Alright, this test is now passing, but let's also try the one from the video player test, this one here. This is working as well. And let's also check out, have we already changed this inside our player? Yes, we have. So this means that this test is now working, also means that we have correctly implemented this now.

Clean Up Livewire Assertions8:18

So this means that this test is now working, also means that we have correctly implemented this now. Okay, next, something else that I saw is here. Yeah, when we use Livewire for testing, there is the assertSee method like we also have for page responses in levels itself. But with Livewire, we also have assertSeeHTML. And this means here we don't need the second parameter because now

HTML. And this means here we don't need the second parameter because now Livewire is already escaping our output. And by the way, under the hood, this is pretty much exactly the same what assertSee does. But now we don't have to provide the second argument. Let's give this a try. Yes, this test is also working. Good. And now this is also a little bit cleaner. And as always, last, we're going to check out if all our tests are still

also a little bit cleaner. And as always, last, we're going to check out if all our tests are still passing. And I think this looks good. Yeah. And we also got one test more, 48 tests now, which is pretty cool so far.

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