Diagnosing Mailable Bug0:00
Hey there, welcome back! As we wrote the software, or well, as I wrote the software, I introduced some bugs, which is pretty common in software development, and one of them is related to sending the user an email. It's related to the OrderReceived mailable. On this lesson, we're going to fix it, write a task, and also make some improvements. So let's jump into the code. Here's our OrderReceived mailable, pretty standard. We received a localized order total, and then we simply spit out some HTML content. I went ahead and I wrote a very simple task, a task to ensure that the mailable can be
Writing Render Test0:32
We received a localized order total, and then we simply spit out some HTML content. I went ahead and I wrote a very simple task, a task to ensure that the mailable can be rendered. As you can see, it doesn't do much. We instantiate it, and then we call the render method and assert that it is a string. If I run this, you're going to notice it fails. It says the view we have received your order of $1.95 was not found. Well, that's because I am passing the wrong parameter here. It's not HTML, it's HTML string. If we fix this and rerun the tasks, they pass.
Refactoring to OrderedDTO1:06
It's not HTML, it's HTML string. If we fix this and rerun the tasks, they pass. We can, however, make a few improvements to this mailable. First in the constructor, you can see that we are passing a localized orderTotal, but we now have an OrderedDTO, so we can pass that instead. Let's start by going into our task and writing the task for the implementation we wished we had. I'm going to instantiate an OrderedDTO and pass some data. There we go. So I wrote a very simple DTO, and since we have the DTO, we don't have to necessarily
There we go. So I wrote a very simple DTO, and since we have the DTO, we don't have to necessarily create an order in the database. We could also do that and create a DTO based on that order, but it's not necessary for what we want to do. Now let's go to the line where we instantiate this, and instead of passing a localized orderTotal, we're going to pass the ordered DTO. If we run this, this is obviously going to fail. And yes, it's failing. We're expecting a localized orderTotal, which is a string, and we're passing an ordered
And yes, it's failing. We're expecting a localized order total, which is a string, and we're passing an ordered DTO. So let's go into the OrderReceived Mailable and fix this. First, we expect an ordered DTO, and let's call it order. Let's rerun the tasks. Still failing because we don't have the localized order total property. So let's go all the way down, go to this line, and we can fetch this from the DTO that we have. Let's rerun this, and it's passing.
Updating Controller Signature2:32
have. Let's rerun this, and it's passing. However, if we run all of our tasks, we can see that we have a task failing, the CheckoutController task, and that's because we must also fix the signature there. So let's go to sendOrderConfirmationEmail, which is where the error is being generated from, and fix this. Instead of passing the total, we can just pass the orderedDto, which we have from the OrderFulfilled event. Let's rerun all of our tasks, and now they're passing. Another improvement we can make is instead of passing an HTML string, we can actually
Switching to Blade View2:59
Let's rerun all of our tasks, and now they're passing. Another improvement we can make is instead of passing an HTML string, we can actually pass a view, and if we check our OrderServiceProvider, we can see that we have already listed UIViews as the namespace for our order views. So let's go ahead and go to UIViews. Let's create a directory called emails, and then let's create a file called order_received.blade.php. And now we can write something. Okay, that's simple enough. We forgot to add .blade.php, so let's add the blade suffix. There we go.
We forgot to add .blade.php, so let's add the blade suffix. There we go. Now let's go into our OrderReceivedTest. Let's try this out. Still passing. Okay, let's go into orderReceived, and now we can get rid of this and simply pass the view, which is order.emails.orderReceived. Let's rerun our tasks, and they're still passing. If I were to call some property that did not exist, well, the task is going to fail. Now that we have a task specifically for the OrderReceived Mailable, we can be assured
Wrapping Up and Verification4:00
If I were to call some property that did not exist, well, the task is going to fail. Now that we have a task specifically for the orderReceived mailable, we can be assured that it renders, and on the other tasks, we just need to make sure it was sent. And to wrap it up, let's run all of our tasks, and they're all passing. As you can see, pretty simple. That error just somehow snuck past me. It's good though, because we've now had an opportunity to improve the orderReceived mailable by expecting ATTO instead of a string. So if in the future we want to add more information into that email, we don't have to change anything. We just have to add new information to the template.
So if in the future we want to add more information into that email, we don't have to change anything. We just have to add new information to the template. And we've also had an opportunity to write a task for the OrderReceived Mailable. Very simple, but it goes a long way. All right, that's pretty much all we have for this video. See you in the next one.
