1
1
.. index ::
2
2
single: Emails; Testing
3
3
4
- How to functionally test an Email is sent
5
- =========================================
4
+ How to test that an Email is sent in a functional Test
5
+ ======================================================
6
6
7
- Sending e-mails with Symfony2 is pretty straight forward thanks to the
7
+ Sending e-mails with Symfony2 is pretty straightforward thanks to the
8
8
``SwiftmailerBundle ``, which leverages the power of the `Swiftmailer `_ library.
9
9
10
- To functionally test that e-mails are sent, and even assert their subjects ,
11
- content or any other headers we can use :ref: `the Symfony2 Profiler <internals-profiler >`.
10
+ To functionally test that an email was sent, and even assert the email subject ,
11
+ content or any other headers, you can use :ref: `the Symfony2 Profiler <internals-profiler >`.
12
12
13
- Let's start with an easy controller action that sends an e-mail::
13
+ Start with an easy controller action that sends an e-mail::
14
14
15
- public function indexAction ($name)
15
+ public function sendEmailAction ($name)
16
16
{
17
17
$message = \Swift_Message::newInstance()
18
18
->setSubject('Hello Email')
@@ -28,9 +28,10 @@ Let's start with an easy controller action that sends an e-mail::
28
28
29
29
.. note ::
30
30
31
- Don't forget to enable profiler as explained in :doc: `/cookbook/testing/profiling `.
31
+ Don't forget to enable the profiler as explained in :doc: `/cookbook/testing/profiling `.
32
32
33
- And the ``WebTestCase `` to assert the e-mail content should be similar to::
33
+ In your functional test, use the ``swiftmailer `` collector on the profiler
34
+ to get information about the messages send on the previous request::
34
35
35
36
// src/Acme/DemoBundle/Tests/Controller/MailControllerTest.php
36
37
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@@ -40,7 +41,7 @@ And the ``WebTestCase`` to assert the e-mail content should be similar to::
40
41
public function testMailIsSentAndContentIsOk()
41
42
{
42
43
$client = static::createClient();
43
- $crawler = $client->request('GET ', 'your_action_route_here ');
44
+ $crawler = $client->request('POST ', '/path/to/above/action ');
44
45
45
46
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
46
47
@@ -59,3 +60,4 @@ And the ``WebTestCase`` to assert the e-mail content should be similar to::
59
60
}
60
61
}
61
62
63
+ .. _Swiftmailer : http://swiftmailer.org/
0 commit comments