Refactoring Test Setup0:00
So, in the last videos, we have created here four new tests, and I think before we move on, it's good to refactor the code. And we can already see, and you probably have already seen, that it is quite messy here. For example, where we're providing all the validator here to one and a second request here to two requests. So, I think it's a good idea to refactor this probably to some kind of helper method. So, let's see, can I do this with phpStorm? Extract method, get validPedalRequestData.
So, let's see, can I do this with phpStorm? Extract method, get valid request data. And yeah, this already looks way much better than before. We have changed it here. I think we still need to change it here as well. Oop, did I change too much? No, here we are inside the method. Okay, the method is fine. I just want it to be at the end of the file. Okay, so this is good.
I just want it to be at the end of the file. Okay, so this is good. But let's check again. We're not using it here. We have used it here. And here we also need to change it right. Get validPedalRequestData. And tests are still working, which is good. Now, what about here? Does not store invalid pedalPurchaseRequest.
Creating Invalid Request Helper1:14
Now, what about here? Does not store invalid pedal purchase request. And here we're providing just an empty array. I think this is already quite good to read, but I still would prefer to do something like this. Get invalidPedalRequestData. Because wouldn't this be even better to read? Because we're not interested here in what is an invalid request. We just want to make sure that it is an invalid request. So this means down here, let's copy this.
We just want to make sure that it is an invalid request. So this means down here, let's copy this. And let's call this getInvalidPedalRequest. And here we're just going to return an empty array. So yeah, it's not really a big deal here. It's just providing an empty array, but I still believe that this is better to read. And this is why I don't care if I have a little bit more code. We need it here, here, and I think somewhere up here. Check again, getValidPedalRequest.
Faking Queue in Tests2:12
We need it here, here, and I think somewhere up here. Check again, get valid PedalRequest. Oh, here we're using the invalid one. And let's also make sure that the tests are all green and they are. Okay, what is next? So we've been using faking the queue system now for only the tests about the job itself, but actually we're calling the same request. So this means actually we also have to do this here to fake the queue system.
So this means actually we also have to do this here to fake the queue system because then the job is not going to being run because currently it would run. So we need it here. We probably don't need it here because the request is invalid. And here we have it. And here we have it as well. So this is always something that I run into and that you have to be careful about.
So this is always something that I run into and that you have to be careful about that you are just testing one feature and then you're adding another feature to the same endpoint, to the same code. And then you probably have to fake a mail system or fake a queue system also in tests that already work. And what else here inside my SignatureValidator? Yeah, so here's the isValid method. And here we are making sure that the request is valid.
Extracting Validator Method3:22
Yeah, so here's the isValid method. And here we are making sure that the request is valid. But actually here, this is not a good place to have all this code here for the check. So let's also extract your new method and let's call it isPedalRequestValid. All right, so now we have the same code just in a different method down here. But what I think or what I believe is now way better because the first time you get to this class,
But what I think or what I believe is now way better because the first time you get to this class, the isValid method is what you have to check. And then you're seeing, okay, we want to make sure that the pedalRequest is valid. So you already know what it is about. And then if you want to dig deeper, you can go to this class and check out what we are doing here. As always, we're going to run the tests again, but they should still work.
As always, we're going to run the tests again, but they should still work. Yes, they do. And I think now looking at those tests here, I think we are good here. They're already quite super simple. Let's add here an empty line. And yes, I really like how they turned out and they are helping us already a lot. And now since this is cleaned up,
Moving to Course Purchase4:25
and they are helping us already a lot. And now since this is cleaned up, we can move on with purchasing a course inside our application.
