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

Writing User Purchase Test0:00

Okay, so this means we can already move on to the next test. It stores pedal purchase for a given User. All right, I think I'm just going to start here by copying the whole test because it will be quite similar. And let's place it in here and let's see what we have to change. So here I'm not so interested anymore that the users table is empty and the purchases table is empty because now we need to have already a User inside our database. So let's create a new one, factory, and let's create it with an email, a specific one. Let's just use test@test.it again. So this means we want to make sure that later that there is a specific User with this email.

Let's just use our test at test.it again. So this means we want to make sure that later that there is a specific User with this email inside the database. All right, now this stays the same. We still need a course. We still need a webhook call. We still need to act the same way. But now the first thing that I want to make sure is that we still only have one User inside the database. So let's use the assertDatabaseCount again for the users table and we want to make sure

the database. So let's use the assertDatabaseCount again for the user table and we want to make sure that there is still only one User inside the database. And then we also want to make sure that it is a specific one and here we now can use the User which we already have. So we're taking the email from this User and the same we can do for the name, username. And then down here we don't need to find the User because now we already have it and this now should stay the same. All right, let's run this test. It's failing because User's email.

Diagnosing Unique Constraint Failure1:45

All right, let's run this test. It's failing because user's email. So we have a unique constraint failed. It seems we're missing some fields. Which one are we missing? User's email. Okay, let's see. We're creating here new User with the email address. All right. And oh yeah, let's check again.

All right. And oh yeah, let's check again. I believe I got this error wrong because yeah, unique constraint. So we only should have one User inside our users table with the email address. So do we have it, this one. And now we are creating inside our Job, get back to a Job, immediately try to create a new one with the same email, which we of course cannot do. That's also not what we want. So I got this message wrong, but yeah, this is the correct error because we're not dealing with the situation that we already have this User.

Finding or Creating User2:44

So I got this message wrong, but yeah, this is the correct error because we're not dealing with the situation that we already have this User. So this means we can here at the beginning of our job, just try to look for the specific User. Do we already have it? User where the email is the one which we get for the webhook call, payload, email. And let's just try to get the first, again, I'm not interested in any details, how I can write this better, more readable, easier. Just try to make my tasks work. So this means here, if we don't have a User, then we're going to create a new one.

Just try to make my tasks work. So this means here, if we don't have a User, then we're going to create a new one. But if we already have a User, we don't create a new one. And then we're going to use the one which we already created up here. I think this should already be enough to make this test pass and it does. All right. So now we have made sure that if we have already a given User, then we're going to use this one and we don't create a new one. Nice. All right.

Testing Email Notification3:56

Nice. All right. So this means we only have one missing test here. We'll make sure that we send out an email to the User. So let's see, how are we going to start here first? Since I'm sending out an email, I want to make sure that I fake my email system. Then we're going to need a Course and a Call again. So again, I'm going to copy this here. And again, I'm not thinking about if it's a good idea if we copy all this code or not, because we can deal with this later when we try to refactor the code.

And again, I'm not thinking about if it's a good idea if we copy all this code or not, because we can deal with this later when we try to refactor the code. Then also what is the same here where we're going to act? You're calling the handle method of our job. And now we want to make sure or to assert something that for our email service, that a specific email was being sent, assertSent. And we don't have it yet, but let's just name it NewPurchaseMail class. So that's how we want to test it. So we're going to fake our system. Then we need to have a Course and a Call inside our application.

So we're going to fake our system. Then we need to have a Course and a Call inside our application. Then we're going to call our job. And then we want to make sure that a specific email, which we don't have yet, is being sent or was sent. And of course, this is going to fail. Let's see why the expected Mailable was not sent. All right. So this means inside our job, inside our handle method, we now also want to make sure that we send out an email.

So this means inside our job, inside our handle method, we now also want to make sure that we send out an email. So let's just do this together. We're going to mail to our User. We're going to provide here the user email, and then we want to send out a new PurchaseMail. I think that's how we call it inside our test. Run the test again. It will fail because we don't have this mail. So let's create a new one.

It will fail because we don't have this Mail. So let's create a new one. New PurchaseMail. All right. Let's run the test again. Still failing because we haven't imported the class yet. Let's do this. Import the class. Let's run it again. The expected new PurchaseMail was not sent.

Let's run it again. The expected new purchase mailable was not sent. All right. Let's see what this is about. Let's go back to our test. Let's import the class. I think this was the issue. Let's run again. Yeah, because now our test is passing. All right.

Yeah, because now our test is passing. All right. So far so good. So we have now three tests already. It stores pedalPurchase. It stores pedalPurchase for a given User, and it sends out an email, and all of them are working. Let's try to run all of them. All right. This looks good, but still, as I think I told you in the last video or the video before.

Faking Mail in Tests6:56

All right. This looks good, but still, as I think I told you in the last video or the video before that sometimes when you add a new feature to the same thing that you're going to test, so in our case, it's the Shop, now we're sending an email inside our Shop. Now we need to also make sure for the other tests that we don't really send out an email. So this means I'm going to add here, I'm going to fake here the Mail system also inside those other tests at the beginning, which you should do as well. And again, let's call our tests here, and they are working. Perfect. So this means we have now made this work, and the next video we need to see how we can

Planning Code Cleanup7:32

Perfect. So this means we have now made this work, and the next video we need to see how we can clean this new code up a little bit.

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