Refactoring Test Setup0:00
It's refactoring time again, so let's see if we can improve the code from the last video. And the first thing that I can see, which I don't like, is here that we create this webhook call with a lot of data in, I believe, every test. Yeah, it seems so. Okay, so it's a good idea to do this before, and with phpUnit there is a setUp method, with beforeEach is a before each method, which is almost the same. So let's create it, we're going to provide here a callable, and now we're going to set a specific property, and we're going to call this dummyWebhookCall, and now we're just going to use what we've been doing here, and we were copying this here, and I think this should do it.
going to use what we've been doing here, and we were copying this here, and I think this should do it. So before every test, we now have access to this dummy webhook call, which we create before every test. So this means we don't need this here anymore, and here we can provide directly our webhook call, like this. Let me copy this, because we're going to change this now here as well, get rid of this part here, and now for our job, we're going to provide the dummy webhook call, and one more time, we don't need this here, and we are just providing here our webhook call. All right, let's run all the tests, they are still passing, awesome.
time, we don't need this here, and we are just providing here our webhook call. All right, let's run all the tests, they are still passing, awesome. And since we're already here, let's clean this up a little bit, and it seems we're also not using the course, which I always love about phpStorm, showing me directly which variables I'm not going to use, and then we can just get rid of them if they don't have any purpose. All right, so these tests here already look way cleaner than before, I think they are not too long, and they are quite readable, so that's what I like about them, but also something which we have added in the last video is this new email, and let's check out the view as well.
Creating Mailable Feature Test2:05
something which we have added in the last video is this new email, and let's check out the view as well. So this is the view, the markdown view of our PurchaseEmail, and there's just a dummy content inside, and of course, we want to provide some specific content, and of course, we also want to test this. So let's start again by creating a new feature test, and we're calling it similar to our NewPurchaseMailTest. It includes purchase details, so we want to make sure that we tell the user of the specific Course which he or she just bought. So this means in order to test this, we need, of course, a new Course, let's create one,
course which he or she just bought. So this means in order to test this, we need, of course, a new Course, let's create one, course factory, and create one. I don't think we need any specific data here, and inside the act method, we are now creating a new Mailable, just by creating a new instance of it, going to import the namespace, and now we can make some assertion on this Mailable, and we want to make sure that we see something specific inside the HTML. Maybe let's start by making sure that we thank the user. So thanks for purchasing, and let's also add here the course title, and then I also want to make sure that I see a button for logging in, and then probably we also want.
So thanks for purchasing, and let's also add here the course title, and then I also want to make sure that I see a button for logging in, and then probably we also want to make sure that we see the route for logging in, and there is one for login, yes, there is. Let me just see, is there also some specific, like it's just assertSee, I think we might can use also here assertSee in text. Let's try this one. All right, okay, let's start by running this test, and let's see what we find here. Did not see expected text within the email body. All right, let's see if we can fix this by just adding the text to the mailable.
Updating Email Content4:05
Did not see expected text within the email body. All right, let's see if we can fix this by just adding the text to the Mailable. So here for the title, let's just use the title, thanks for purchasing. Here we need some kind of courseTitle, so we haven't provided it yet to this view, but I don't care about it, and then let's provide some text here, and I also want to make sure that I tell the User if they have not an account yet that one was created for them. If this is your first purchase on our site, which we can get through config('app'), then a new account was created for you, and then maybe just have fun with the new course. Then a new account was created for you, and you just need to reset your password, because we have already set the password for the User, but now we want to let the User find.
Then a new account was created for you, and you just need to reset your password, because we have already set the password for the User book, but now we want to let the user find a new password. And then this button, let's call this now login, and the URL is the one from our login route. Here we are. Thanks, app name. Yeah, I'm fine with this. Throw on the test again. Let's see, undefined variable course.
Passing Course Data5:31
Throw on the test again. Let's see, undefined variable course. Yeah, so we're using here the course variable, which we have not passed yet to our mailable. So what do we have? A new purchase made. Yeah, so through the constructor, I want to add here public property, and it's the course, course. So this means now we have access to it if we provide it. It still should fail, because we have now not enough arguments inside our test here. So this means here we now need to provide the course, which we have created before.
It still should fail, because we have now not enough arguments inside our test here. So this means here we now need to provide the course, which we have created before. Let's run it again. Okay, it's failing. Let's see what this is about. A few exception, HTML special characters, argument one must be a string array given. So a few, let's give this a look. Let's see, we're providing the course title. This is not an array, and then the config app, oh yeah, it's app name. This is what we're interested in.
This is not an array, and then the config app, oh yeah, it's app name. This is what we're interested in. Let's run it again, and now this test is passing. Let's check again. So we've made sure that we see some specific text to inform the user, a button to log in and the log in link. All right. I think this is already it for this video. We now have also made sure that we test the email, which we're going to send out after a valid purchase.
Running Full Test Suite6:58
We now have also made sure that we test the Email, which we're going to send out after a valid purchase. Maybe it's always a good idea to run all our tests just to be sure that we didn't break anything here and there. 72 tests, we are growing our test suite, which is amazing. And yeah, this is how we've now made new tests for Mailable. And I think it's always a good idea to test your mails as well so that you have the content you're looking for. And this is a very easy way to test an email by just creating new Mailable. And then you have this assert methods on the Mailable, which you can use for testing the
And this is a very easy way to test an email by just creating new Mailable. And then you have this assert methods on the Mailable, which you can use for testing the content.
