Skip to content

Add cookbook for e-mail testing with Symfony2 profiler #2201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cookbook/email/testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.. index::
single: Emails; Testing

How to functionally test an Email is sent
=========================================

Sending e-mails with Symfony2 is pretty straight forward thanks to the
``SwiftmailerBundle``, which leverages the power of the `Swiftmailer`_ library.

To functionally test that e-mails are sent, and even assert their subjects,
content or any other headers we can use :ref:`the Symfony2 Profiler <internals-profiler>`.

Let's start with an easy controller action that sends an e-mail::

public function indexAction($name)
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send@example.com')
->setTo('recipient@example.com')
->setBody('You should see me from the profiler!')
;

$this->get('mailer')->send($message);

return $this->render(...);
}

.. note::

Don't forget to enable profiler as explained in :doc:`/cookbook/testing/profiling`.

And the ``WebTestCase`` to assert the e-mail content should be similar to::

// src/Acme/DemoBundle/Tests/Controller/MailControllerTest.php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MailControllerTest extends WebTestCase
{
public function testMailIsSentAndContentIsOk()
{
$client = static::createClient();
$crawler = $client->request('GET', 'your_action_route_here');

$mailCollector = $client->getProfile()->getCollector('swiftmailer');

// Check that an e-mail was sent
$this->assertEquals(1, $mailCollector->getMessageCount());

$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];

// Asserting e-mail data
$this->assertInstanceOf('Swift_Message', $message);
$this->assertEquals('Hello Email', $message->getSubject());
$this->assertEquals('send@example.com', key($message->getFrom()));
$this->assertEquals('recipient@example.com', key($message->getTo()));
$this->assertEquals('You should see me from the profiler!', $message->getBody());
}
}