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

Extract Logic into Job0:00

Okay, let's continue and test this NotifyAllVoters feature. So before we do that, I want to move this logic to an actual job first because it's easier to test. So let's start with that, php artisan make:job NotifyAllVoters, okay, and let's grab all of this logic and move it in there. But we don't need this anymore, we're going to dispatch the job directly in here. So first let's go to that, NotifyAllVoters, this should take in an Idea. So let's do that again, say $idea, and that looks good. Let's type into here and import it, and let's make this public, okay. Let's indent this as well, and let's paste the logic in the handle method here, okay.

Let's type into here and import it, and let's make this public, okay. Let's indent this as well, and let's paste the logic in the handle method here, okay. Make sure to import these, and this should work now. But now we have it isolated into its own job. So let's go ahead and make sure to dispatch this in setStatus instead of calling the method here. So we can do NotifyAllVoters and call the dispatch method and pass through the idea, okay. And make sure to import this, it's imported already, and I think I can get rid of these ones since I moved it to the job, okay.

And make sure to import this, it's imported already, and I think I can get rid of these ones since I moved it to the job, okay. So that should still work. Let's verify that. So I'm going to go to one, actually, let's do one to 20 as well, or 21. Change this, in progress, NotifyAllVoters, update, it's processing, and we do see those 21 emails coming in, okay. So let me just clear that out, delete all. And now we can go ahead and make use of Horizon, which is what I like to use whenever I work with queues.

Install and Configure Horizon2:10

And now we can go ahead and make use of Horizon, which is what I like to use whenever I work with queues. So let's install this, horizon. What's next? We have to run php artisan horizon:install, okay. Let's leave, we have to run php artisan migrate as well. I'm okay with the defaults here. I guess we don't have to migrate, but we do have to set whoever can see the Horizon dashboard. And I like dark mode, so let's set that. And we can set the admin here.

And I like dark mode, so let's set that. And we can set the admin here. So this will be the same as the isAdmin array we have here. So we can grab this, paste it in here. And we have to set our queue driver to Redis. Okay, so now we set up horizon, and let's see if this works now. So let's run php artisan horizon here, okay. And we can test this out again, but now, since we're using a real queue driver, this should happen instantly and be thrown on the queue. So before it took some time to process, but now it should happen instantly.

happen instantly and be thrown on the queue. So before it took some time to process, but now it should happen instantly. Actually, let me open the Horizon dashboard. So let's open a new tab, lcVoting/horizon is the endpoint. Okay, so it is running. And you'll see all the jobs, or the one job and the 21 emails be thrown on the queue here once I hit that endpoint. So let's do this again. So refresh, change the status, notify all voters, it should happen instantly. And it does.

Feature Test Job Dispatch4:21

And you can see the metrics here, 22 jobs. Okay, so now let's go ahead and write our tests. So we do have that adminSetStatusTest that we had a few videos ago. So let's work with this first. So it'll be similar to this canSetStatusCorrectly, but it's also going to use the notifyAllVoters feature. So it's a canSetStatusCorrectly while notifying all voters. Oh, yeah, one more thing here. Let's go back to the Job. notifyAllVoters.

So I'll just leave it for now. And there should be an option to unsubscribe from the mail provider itself anyways. Okay, so we have the same setup as before. We actually don't need this category to even up here. We don't need them. So let me remove those quickly. So we're changing the status to this status inProgress. We're calling the method. But before we call the method, let's go ahead and set that new flag to notifyAllVoters.

But before we call the notifyAllVoters method, let's go ahead and set that new flag to notify all voters. So it's called notifyAllVoters. Okay, set that to true. So this is them checking that checkbox. And then we call the method. So we want to verify or for now, we just want to verify that this job. So notifyAllVoters is being thrown onto the queue for the actual functionality. We'll do that in a unit test. So we can use fakes here. So we can do queueFake.

So we can use fakes here. So we can do queueFake. And let's make sure to import this. Okay. And we can say, or make sure that nothing is pushed to the queue yet. So let's say, assert, nothing pushed at this point. So after we do all this Livewire stuff, it should hit that piece of code that throws this job on the queue. So after this, and I don't care about this anymore, because we have that in the previous test, we can do something like this.

So after this, and I don't care about this anymore, because we have that in the previous test, we can do something like this. So let's say, let's assert that that job was pushed onto the queue. So assertPushed NotifyAllVoters class. And if you want to inspect the contents of it, you can pass in a closure as well. But I'm okay with just asserting that the actual job was pushed onto the queue. So yeah, let's test this out. And it's not passing. I did not spell assert correctly. Try that again.

Create Job Unit Test7:19

I did not spell assert correctly. Try that again. And it does pass. So now I want to make a unit test that tests that the job actually does what it's supposed to do. So in this case, I'm going to test that the job actually sends out an email to everyone who voted for the Idea. So let's go ahead and make a new job. I'm going to stop arising here. And let's say, make test.

I'm going to stop arising here. And let's say, make:test. And let's call it NotifyAllVotersTest. And let's make a unit test, like I said. Okay. And actually, I want to move it to a jobs folder in the tests. So NotifyAllVotersTest. Let's move it to a jobs folder. jobs, let's move this in there. And let's change the namespace here, jobs.

Jobs, let's move this in there. And let's change the namespace here, jobs. And as always, let's just use the Laravel test case. And let's use refreshDatabase as well. Use refreshDatabase. Okay. So we only have one test here. And that test is it sends an email to all voters. Okay. So I'm going to paste in some setup here.

Okay. So I'm going to paste in some setup here. Okay. So I have two Users, User and User B. Let me import User. And we have a Category. Again, import all this stuff, Status, Idea. And the important thing here is we have two votes. So one from User A and one from User B. We're voting on the same Idea here. So if we go to that job, we're going to want to fake the mail here and just assert that it actually was queued.

Assert Emails Were Queued9:06

So if we go to that job, we're going to want to fake the mail here and just assert that it actually was queued. So let's do that. So let's say mail fake. Okay. Let's import mail. And here we can actually dispatch the job. So it hits that code. So notifyAllVoters, dispatch, and we'll pass in the idea that we have here in our setup.

So notify all voters, dispatch, and we'll pass in the Idea that we have here in our setup. Okay. Let's import this. And I spelled it wrong, of course. import. Okay. And we can use the assertQueued method on the Mail facade. So Mail::assertQueued(). And we want to assert against this mailable.

So mail assert queued. And we want to assert against this mailable. So Idea, status, updated mailable. Okay. And we can also pass in a closure here, accept the mail. And we can make some assertions here as well. Or not assertions, but just make sure that some of the mail properties are correct. So mail, the sender should be, in this case, it will be this first User. So in this case, my email here and the other user is user@user.com. So we can say hasTwo and pass in my email here.

So in this case, my email here and the other user is user at user.com. So we can say hasTwo and pass in my email here. And if you want, you can also check if the subject is correct. So we can do Mail. You have to call the build method first. So let's do that. And then we can call the subject or grab the subject. Okay. And the subject was, let me just paste it in. Okay.

And the subject was, let me just paste it in. Okay. So we're asserting that the mail was queued and it has these properties. So let's see if this works. And it does. Cool. So you can do this for each voter. In our case, there are only two. So we can just duplicate it and then change the email here for the two recipients. So it's user@example.com.

So we can just duplicate it and then change the email here for the two recipients. So it's user at user.com. And this should pass as well. Okay. Let's run our entire test suite. I should make an alias for this, for the parallel feature, which I'll be using a lot more now. Okay. And everything is passing. So let's go ahead and make a commit here. This is 35 episode 35 notifyAllVotersTest.

Commit the Changes11:28

So let's go ahead and make a commit here. This is episode 35 notifyAllVotersTest.

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