در حال بارگذاری ...

Defining Scheduled Logic0:00

Let's move on to wormholes. Here I have a test for a Post, and maybe I need to check if the post is currently scheduled to go live on the site. Okay, we can start with our model factories, like so. And now you'll see right here, every Post has a publish column. And now note that every Post has a publish column that will initially be set to null. If it's null, that means it's in a draft state. So that means if I were to say, check if the post is scheduled, well, it's in a draft state, so that should of course return false. Let's give it a shot.

Creating isScheduled Method0:36

so that should of course return false. Let's give it a shot. We run it, and it fails because there is no method isScheduled. All right, so let's run that now, and we can just slime it real quick, and it passes. Let's tweak it though. What if I instead set the publish.column equal to now, now, and maybe add one week? Okay, well, that should return true. So let's give that a run. If we update it, it still fails, and of course it does. We hard-coded that there.

If we update it, it still fails, and of course it does. We hard-coded that there. Okay, so now let's refactor this. Why don't we check if the publish.column is greater than right now? All right, run it again, but now it fails for a different reason. We assumed that publish.dat was a timestamp, but we just discussed that initially it will be null. So yeah, it sounds like we need to first check, well, if it's in a draft state, then of course it's not scheduled. All right, let's do that now.

Adding Wormhole Time Travel1:36

it's not scheduled. All right, let's do that now. If $isDraft, then return false, and then I will create that method. And all we have to check there is if publish.dat is null. Okay, run our test, and now it works. So there's nothing new here yet. This will all work in Laravel 7 and below. But if we now move on to time-traveling with the new Wormhole class, have a look. I could say, well, if I were to travel, and let's see, the post was scheduled a week from now.

Exploring Travel API Options2:44

Give it a run, and that will work as well. And it works specifically because of this time-traveling feature here. So if I did not run that, of course it's going to fail. Now what's neat is how this API is pretty flexible. So here we're traveling eight days, but I could travel eight minutes or hours. In fact, just have a look. This travel method returns a new Wormhole. It accepts the value, which was eight in our case, and the API is everything you'd expect. So if I say travel eight weeks, we're going to set the test now equal to addWeeks and then the number eight.

Practical Testing Use Case3:58

If you need a way to tell Carbon, imagine that right now is when I tell you. And in this case, it would be a month in the past. If you need a way to do that, the Travel and Wormhole class is exactly what you want. Now here's a practical use case. At Lericast, we sell gift certificates. And when you buy one, you can specify when it should be delivered to the recipient. So maybe you're buying a gift certificate for your sister whose birthday is a month from now. Well, you could write a test that says, okay, purchase the gift certificate and then travel to the date of her birthday or the date she should receive the certificate.

Resetting Time Travel4:26

Well, you could write a test that says, okay, purchase the gift certificate and then travel to the date of her birthday or the date she should receive the certificate. And if we then run such and such artisan command, an email should be fired off. That's the sort of functionality you can allow for with the wormhole class. But again, none of this functionality wasn't available to you in the past. Laravel just wraps it up in this new trait. So for example, if you just wanted to do it by hand, it's just delegating to Carbon::setTestNow, which has been available for quite a while. So finally, last little thing, if you need to travel back, you would just say this, travel back, and that'll reset it effectively.

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