Refactoring brittle tests0:00
Now that we have added some code, it's also a good idea to refactor the code. So we're going to start here in our tests again. In the last video, we have made sure that we see a list of all the videos of a course. And as I told you, I always love to use some static text first. And now let's get rid of that. And let's try to make it work without the static text. Okay, how can we do this now? Of course, when we now run the test, it will fail because we don't have first video. Inside our string, we have some other strings that now Faker creates for us. So we're not going to see those strings here.
Using spread for titles0:34
Inside our string, we have some other strings that now Faker creates for us. So we're not going to see those strings here. But what we can use is we can use the spread operator in php to spread out a specific array into multiple items here. And we can do this by getting all the videos of a course. And then let's say we only want to get the titles of those videos like this. And we want to provide an array. All right, this should already do the trick. We're getting all the videos and then all the titles from those videos. And then we're going to spread out with the spread operator.
We're getting all the videos and then all the titles from those videos. And then we're going to spread out with the spread operator so that we have all those items here inside this array. And this should now fail, but for a different error. But let's check missing required parameter for a route where we have defined this. And yeah, so now we have an error for this one because now we don't get anything back here. And what we can do here is we can use the videos of the course and now getting just the first one, we can use this with this array syntax, which also works on the collection in Laravel.
and now getting just the first one, we can use this with this array syntax, which also works on the Collection in level. And let's also do this now for the first item, which is in fact the second item because of how php array is counting. And now the same thing goes here and we are using the third item of the videos. Let's run this test and this is passing now. Okay, much better because we have way less text as before. So far so good. Let's check the other one here. Yeah, let's try to do this here as well.
Let's check the other one here. Yeah, let's try to do this here as well. Mark's video as completed. Let's get rid of the state method at all. And now we want to make sure, let's check here we have been using this text. So this text should be the title of the first video. I think this should do it. Yes, it does. Okay, perfect. So I think I'm fine now with this test here.
Okay, perfect. So I think I'm fine now with this test here. Let's check the last one here. Yeah, here we have the same. Let's also get rid of the state method here at all. And let's scroll down here. Yeah, now we don't even need the text anymore. So I think we could have already deleted it in the last video, but glad we checked it now. And I think all the tests here are still passing and yes, they do. Okay, what else?
Renaming watchedVideos relationship3:05
And I think all the tests here are still passing and yes, they do. Okay, what else? Let's talk about this one real quick. So we have a relationship called videos, but we have already defined that the table is called watched_videos. So I think it's a good idea now to also change the relationship method name here. So this shouldn't be videos. Let's rename this to watchedVideos so that it's pretty clear what we are actually doing. Then inside here, this should fail now. Yes, because the first argument which we provide here is now, because now we need to use watched
Then inside here, this should fail now. Yes, because the first argument which we provide here is now, because now we need to use watchedVideos, but not the method, like the property. And still not working. Let's check what is the case here. Yeah, we need to also do this now here, watchedVideos. I think this should make this test pass, but I'm pretty sure we have no other tests failing. Yeah, so here we probably have the same issue. Yeah, where we're crawling the videos relationship, it's now called watchedVideos and the same is here.
Yeah, where we're crawling the videos relationship, it's now called watched videos and the same is here. Okay, good. Let's run all the tests of the file and all of the tests of the application just to be sure. And yeah, we have one which is failing. Let's check out what this is about. Oh, it's our UserTest where we check the relationship. All right, yeah, this is the one failing. Yes.
All right, yeah, this is the one failing. Yes. So I think we just have to change this here as well and still failing. Let's check call to undefined method videos here. Where is this call? Yeah, so here we have defined that the relationship is called videos, but actually it's now called watched videos. Yeah, exactly. And now this is working. And again, we're checking that all of our tests are still passing, they do.
Renaming purchasedCourses relationship5:11
And now this is working. And again, we're checking that all of our tests are still passing, they do. That's good. But I have also seen here that we need to do the same here. So we have a relationship between the User and the courses, but also this actually are purchased courses. And I really love to use those custom names because they really give some value and provide some very readable code to yourself, to your future self, or your colleagues. And again, our tests will tell us what is not working anymore. And there will be a few tests now failing.
And again, our tests will tell us what is not working anymore. And there will be a few tests now failing. None of them. Let's start here with the page response test. What is this about? Let's run it again. For each argument, null given. Okay, let's check our DashboardController. Yeah, this must be the issue.
DashboardController. Yeah, this must be the issue. We now need to call this purchaseCourses. Let's run this. Yeah, this looks better. Let's run all of the tests again. I can't remember what the other failing tests were. What's the next one? PageDashboardTest. Here we go.
Page dashboard test. Here we go. Let's run the whole file because now we see this here a little bit better. So it lists purchasedCourses and the issue is undefined method course. Let's see. Yeah, so here we're providing a course to a User and level thinks that there's probably a course relationship, but this has now a different name. And we can provide this here. Yes, relationship name, purchasedCourses, exactly. And I'm going to copy this.
Yes, relationship name, purchasedCourses, exactly. And I'm going to copy this. We probably need this in another test here as well, probably here. Now let's check again. I didn't check the error call to undefined method courses. All right, where are we actually doing this? Oh yeah, here. It should be purchasedCourses. Yes. Let's run this.
Yes. Let's run this. Yes, better. What about this one? Still failing. Yeah, here. We also need to provide here the new relationship name. Yes, exactly. And I think this was the last one, right? Yeah, it looks so.
And I think this was the last one, right? Yeah, it looks so. Let's run all of our tests again. Okay. We still have some failing. I guess it's probably another file here inside the UserTest. What should we use? This looks good. Let's check again. Which one was it?
Let's check again. Which one was it? It was the first one. Yeah, this one here. This should also be purchased courses, right? Yes, I think so. Still not working. Assert::count must be countable. So we get null back. It's probably because of this call here.
Fixing failures via tests8:19
So we get null back. It's probably because of this call here. Yeah, I'm pretty sure. All right, yes, better. And I really love how all my tests are telling me where we have been using this relationship, where we have to change this. It seems like mostly it is inside tests, but there are also some other cases, and it's very good to find those as well. So we are now inside the VideoPlayerTest. And I see it already here.
So we are now inside the video player test. And I see it already here. It's not just a video, it's a watchedVideos relationship. The other one, purchaseCourses. Still failing. Call to undefined method watchedVideos. Let me check. Did I get that wrong? The Video belongs to a Course. Oh, yeah, that's wrong.
The video belongs to a course. Oh, yeah, that's wrong. We don't need this here. I think we just don't need it, right? Yes. Okay. I was a little bit too fast here. Let's run the other tests. There's still one failing here, and it's this one. Should be purchaseCourses, and we're going to add one.
There's still one failing here, and it's this one. Should be purchaseCourses, and we're going to add one. Yes, exactly. And I think that's already it for the file. Let's check if there's any other file which is failing, but I think we're good now. Yes, we are. And again, I really love how all my tests are telling me where I have to change this. Let's get back to our User and let's check again our two new method names, watchedVideos and purchaseCourses. And I really like how they give a lot of value and readability to those relationships.
and purchase courses. And I really like how they give a lot of value and readability to those relationships.
