1 minute read

The idea is to test that an email is sent by a contact form by inspecting the SwiftMailler Collector.

We generate a fake unique content and loop through all sent emails to verify that is was sent.

Note that this does not actually test if the email is sent to the server but merely tells you if Symfony is trying to send it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
# ContactControllerTest.php

namespace Acme\DemoBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ContactControllerTest extends WebTestCase
{

  public function testContact() {
    $client = static::createClient();
    $router = $client->getContainer()->get('router');
    $em = $client->getContainer()->get('doctrine');

    // Find any
    $person = $em->getRepository('AcmeDemoBundle:Person')->findOneBy(array());

    $crawler = $client->request('GET', $router->generate('person_show', array('id' => $person->getId())));
    $this->assertEquals(200, $client->getResponse()->getStatusCode());

    // Generate fake message, but unique to find it.
    $msg = '== Test ' . uniqid() . ' ==';

    // Find form in page
    $form = $crawler->filter('#sidebar form')->form(array(
      'contact[name]' => 'test',
      'contact[email]' => 'test@example.com',
      'contact[message]' => $msg,
    ));
    $client->submit($form);

    // Form redirectes
    $this->assertEquals(302, $client->getResponse()->getStatusCode());

    $profile = $client->getProfile();
    $collector = $profile->getCollector('swiftmailer');
    $email = $person->getEmail();
    $found = false;

    foreach ($collector->getMessages() as $message) {
        // Checking the recipient email and the X-Swift-To
        // header to handle the RedirectingPlugin.
        // If the recipient is not the expected one, check
        // the next mail.
        $correctRecipient = array_key_exists(
            $email, $message->getTo()
        );
        $headers = $message->getHeaders();
        $correctXToHeader = false;
        if ($headers->has('X-Swift-To')) {
            $correctXToHeader = array_key_exists($email,
                $headers->get('X-Swift-To')->getFieldBodyModel()
            );
        }

        if (!$correctRecipient && !$correctXToHeader) {
            continue;
        }

        if (strpos($message->getBody(), $msg) !== false) {
          $found = true;
          break;
        }
    }

    $this->assertTrue($found, 'Email was not sent to ' . $person->getEmail());
  }
}

View Gist Inspiration: http://docs.behat.org/cookbook/using_the_profiler_with_minkbundle.html