<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Testing Archives : vedmant.com : coding blog</title>
	<atom:link href="https://vedmant.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>https://vedmant.com/tag/testing/</link>
	<description>Sharing my personal experience in web development</description>
	<lastBuildDate>Thu, 18 May 2017 17:25:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>Testing emails with Laravel</title>
		<link>https://vedmant.com/testing-emails-laravel/</link>
					<comments>https://vedmant.com/testing-emails-laravel/#respond</comments>
		
		<dc:creator><![CDATA[vedmant]]></dc:creator>
		<pubDate>Thu, 18 May 2017 12:32:34 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Emails]]></category>
		<guid isPermaLink="false">https://vedmant.com/?p=172</guid>

					<description><![CDATA[<p>Reliability of emails subsystem is essential for current web applications, but testing emails is not as easy at it looks at first glance. Laravel doesn&#8217;t provide a good solution for testing emails out of the box, so after a bit of Googling I come up with following solution for testing emails. Helper Basic idea is &#8230; <a href="https://vedmant.com/testing-emails-laravel/" class="more-link">Continue reading <span class="screen-reader-text">Testing emails with Laravel</span></a></p>
<p>The post <a href="https://vedmant.com/testing-emails-laravel/">Testing emails with Laravel</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Reliability of emails subsystem is essential for current web applications, but testing emails is not as easy at it looks at first glance. Laravel doesn&#8217;t provide a good solution for testing emails out of the box, so after a bit of Googling I come up with following solution for testing emails.</p>
<h3>Helper</h3>
<p>Basic idea is build on attaching event listener to Swift emails package, for this I extended base class <code>Swift_Events_EventListener</code> that takes as parameter our test case.</p>
<pre class="brush: php; title: ; notranslate">
/**
 * Class TestingMailEventListener
 *
 * @package Tests
 */
class TestingMailEventListener implements Swift_Events_EventListener
{
    protected $test;

    /**
     * TestingMailEventListener constructor.
     *
     * @param $test
     */
    public function __construct($test)
    {
        $this-&gt;test = $test;
    }

    /**
     * Before email sent
     * 
     * @param Swift_Events_SendEvent $event
     */
    public function beforeSendPerformed(Swift_Events_SendEvent $event)
    {
        $this-&gt;test-&gt;addEmail($event-&gt;getMessage());
    }
}
</pre>
<p>Then I created a Trait with following method:</p>
<pre class="brush: php; title: ; notranslate">
/**
 * Trait TracksEmails
 *
 * @package Tests
 */
trait TracksEmails
{
    /**
     * Delivered emails.
     * @var array
     */
    protected $emails = &#x5B;];

    /**
     * Register a listener for new emails.
     *
     * @before
     */
    public function setUpMailTracking()
    {
        Mail::getSwiftMailer()
            -&gt;registerPlugin(new TestingMailEventListener($this));
    }
...
</pre>
<p>Which registers event listener and collects all emails sent with Swift.</p>
<p>Having this done it&#8217;s easy to check whether particular email was sent, I also added bunch of helpers to make it easier:</p>
<pre class="brush: php; title: ; notranslate">
/**
 * Assert that at least one email was sent.
 * @return $this
 */
protected function seeEmailWasSent()

/**
 * Assert that the given number of emails were sent.
 *
 * @param integer $count
 * @return $this
 */
protected function seeEmailsSent($count)

/**
 * Assert that the last email's body contains the given text.
 *
 * @param string $excerpt
 * @param Swift_Mime_Message $message
 * @return $this
 */
protected function seeEmailContains($excerpt, Swift_Mime_Message $message = null)
...
</pre>
<p>And several more.</p>
<p>Here you can see full helper trait code <a href="https://gist.github.com/vedmant/57a908908457e160f5cab39cc7a93d5b"  target="_blank">click</a>.</p>
<h3>Usage</h3>
<p>Usage of this helper is quite simple, here is simple example:</p>
<pre class="brush: php; title: ; notranslate">
public function testEmail()
{
    \Mail::raw('Some content', function (Message $message) {
        $message-&gt;from('laravel@test.com', 'Laravel');
        $message-&gt;to('testmail@test.com');
    });

    $this-&gt;seeEmailWasSent()
        -&gt;seeEmailTo('testmail@test.com')
        -&gt;seeEmailContains('Some content');
}
</pre>
<p>And a bit more complex example of how I test my password reset form:</p>
<pre class="brush: php; title: ; notranslate">
public function testPasswordReset()
{
    $user = $this-&gt;makeUser();

    Event::listen(NotificationSent::class, function (NotificationSent $notification) use ($user) {
        self::$token = $notification-&gt;notification-&gt;token;
        $this-&gt;seeEmailWasSent()
            -&gt;seeEmailTo($user-&gt;Login)
            -&gt;seeEmailContains(self::$token);
    });

    $this-&gt;visitRoute('password.request')
        -&gt;assertResponseOk()
        -&gt;type($user-&gt;Login, 'email')
        -&gt;press('Reset')
        -&gt;assertResponseOk()
        -&gt;see(trans('passwords.sent'))
        -&gt;seeInDatabase('password_resets', &#x5B;'email' =&gt; $user-&gt;Login]);

    $this-&gt;visitRoute('password.reset', &#x5B;'token' =&gt; self::$token])
        -&gt;assertResponseOk()
        -&gt;type($user-&gt;Login, 'email')
        -&gt;type('123123', 'password')
        -&gt;type('123123', 'password_confirmation')
        -&gt;press('Reset Password')
        -&gt;assertResponseOk()
        -&gt;seeRouteIs('dashboard');

    $this-&gt;assertTrue($this-&gt;isAuthenticated());

    $this-&gt;assertTrue(app('hash')-&gt;check('123123', $user-&gt;fresh()-&gt;Password));
}
</pre>
<p>In this example I used events listener to listen for notification to check if sent token is the correct one.</p>
<p>Currently there are some probably better alternatives for testing emails with Laravel, like this one <a href="https://github.com/tightenco/mailthief" target="_blank">tightenco/mailthief</a>.</p>
<p>The post <a href="https://vedmant.com/testing-emails-laravel/">Testing emails with Laravel</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vedmant.com/testing-emails-laravel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
