Default Icon Test0:00
In the last lesson, once I implemented all of these various achievement types, I did notice that for the icon, in every single instance, it was the kebab case version of the class name. So with that in mind, I think we can make a sensible default. Let's implement that now. So we'll go to our AchievementTypeTest, and we already have one for the default name. We'll do another one. It sets a default icon. All right, so let's scroll down here. I'm now going to remove the icon property from that old test, because now it shouldn't
All right, so let's scroll down here. I'm now going to remove the icon property from that old test, because now it shouldn't be required. If I switch back, however, let's do this, let's grab that, and this time, we want the icon to be, let's see, it should be fakeAchievementType in kebab case. That's what I expect. And once again, you'll see I'm calling it as a method. And this is why often, rather than requesting properties, they will ask you to implement a method where you return the string. And again, that just protects and adds a bit more flexibility in the future.
Implement Icon Method0:58
a method where you return the string. And again, that just protects and adds a bit more flexibility in the future. All right, so let's give that a run. But it fails. Unknown property icon. Now if we take a look at that abstract class, here's the issue. We're trying to call icon, but it doesn't exist. So now we'll call an icon method, run it again. Now it's saying, well, no icon method exists. Okay, that's our next step.
Now it's saying, well, no icon method exists. Okay, that's our next step. Run it again. It's now saying, well, we have that method, but you didn't return anything. You returned null, and we're trying to insert that into the database, and that does not work. All right, just return an empty string, and now we are at the error we care about. So we expected the kebabCase, but at the moment, we are returning nothing. Okay, well, here's what we could do. Once again, we can get the class_basename.
Okay, well, here's what we could do. Once again, we can get the class base name. So let's just do this in steps. Run it again. Now here is what we are returning. Well, we need that SVG, all right? So let's come back, and we will stack on .svg. Run it again. All right, but it's still the class name when it should be kebabCase. Now Laravel includes a kebabCase method out of the box.
All right, but it's still the class name when it should be kebab case. Now Laravel includes a kebabCase method out of the box. Let's take a look. Kebab case, there it is. So we can access that globally. Let's run that. Give it another run, and there we go. We get green. So let's close that out. That ended up being pretty darn easy to implement.
So let's close that out. That ended up being pretty darn easy to implement. Paste that in, fetch the icon, file name for the achievement, and there we go. Really easy. So now I'll close this out. Here is what a typical stub will look like. Now you just have to provide the description, but you know what? We'll come back to that in just a second. But until then, one thing at a time. So this means all of my achievement types no longer require this.
Switch Description to Methods4:02
It gives you more flexibility. If for some reason it needs to be swapped out or faked or stubbed or something like that, you just have a lot of flexibility there. So with that in mind, why don't we switch to using entirely methods? So if you add a description method, here is where you would return some fake description. Okay. Let's run the full test suite, because I think everything's going to fail. Yeah, we're back to that situation where there's no description property. Okay. So I'm going to go to achievementType.
Okay. So I'm going to go to AchievementType. And once again here, description is now a method as well. So we'll have name, description. But I can never set a default for this, or at least not a sensible default. So like we did with the qualifier, I'm going to make that abstract. Like so. And what we'll do is we'll grab that, paste it in, fetch the description for the Achievement. Like so. Now let's give it another run, and we do get green.
Update Achievement Type Classes5:00
Like so. Now let's give it another run, and we do get green. However, that doesn't take into consideration the ones that I have here. And in fact, you'll see each of these are now going to squawk, because there's an abstract method we're not implementing. So once again, I will do one with you. Add the method stub. I'm now going to do that, and return it as a string. Like so. And then we can delete this.
Like so. And then we can delete this. And we'll bring that, let's bring that up. Okay. So now I'm going to rinse and repeat for every other class. I'll see you in a minute. Okay, and once again, a bit of a tedious job, but I've gone through all of those files, and I've updated them to use description as a method. So I can close all of those now. Finally, once again, we have to update our achievement stub.
Update Achievement Stub5:46
So I can close all of those now. Finally, once again, we have to update our achievement.stub. So now we will have a public function description method. And there is your stub. So now for any achievement that we have, maybe one like this, I'm thinking this looks pretty good. Great. In the next lesson, we'll move on to caching.
