{"id":55,"date":"2022-12-11T17:53:08","date_gmt":"2022-12-11T22:53:08","guid":{"rendered":"https:\/\/kimsal.com\/blog\/?p=55"},"modified":"2022-12-11T17:53:08","modified_gmt":"2022-12-11T22:53:08","slug":"testing-laravel-mailable-content","status":"publish","type":"post","link":"https:\/\/kimsal.com\/blog\/2022\/12\/11\/testing-laravel-mailable-content\/","title":{"rendered":"Testing Laravel mailable content"},"content":{"rendered":"\n<p>This has been a thorny issue for a while, and *may* have been resolved in Laravel 9 (have not upgraded projects to 9 yet).  Testing both that an email was triggered in a Laravel test, as well as verifying the content of the mail body &#8211; that has often seemed to be a tricky step.  If it&#8217;s resolved in modern times&#8230; great.  If not (or you&#8217;re dealing with legacy code to test), read on.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public function testCustomerOrderedItems()\n{\n\n    $customer = Customer::factory()->create();\n    $item = Item::factory()->create();\n    $service = $this->app->make(OrderService::class);\n    $service->order($item, $customer);\n\n    Mail::assertSent(ItemOrdered::class, function ($mail) use ($customer, $item) {\n        $mail->build();\n        $markdown = $this->app->make(Markdown::class);\n        $body = $markdown->renderText($mail->view, $mail->buildViewData())->toHtml();\n        $this->assertStringContainsString('Ordered', $body);\n        $this->assertStringContainsString($item->number, $body);\n\n        return $mail->hasTo($customer->email);\n    });\n\n} <\/pre>\n\n\n\n<p>Of course, I can&#8217;t specifically remember where I discovered this series of steps, so apologies to whomever I may have copied this from.<br><br>A couple of things to keep in mind&#8230;<br><br>1.  This only works with Markdown mailable.  I&#8217;ve not used anything else during testing, so&#8230; if you&#8217;re using something else, you&#8217;re on your own here.<br>2.  The $mail->view &#8211; this is a public property I needed to add to mailables (like ItemOrdered mailable) to be able to reference in the tests.  It&#8217;s the name of the markdown\/blade file &#8211; item.ordered.blade, for example.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This has been a thorny issue for a while, and *may* have been resolved in Laravel 9 (have not upgraded projects to 9 yet). Testing both that an email was triggered in a Laravel test, as well as verifying the content of the mail body &#8211; that has often seemed to be a tricky step&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}