Skip to content

Instantly share code, notes, and snippets.

@Seldaek
Created May 25, 2011 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Seldaek/991003 to your computer and use it in GitHub Desktop.
Save Seldaek/991003 to your computer and use it in GitHub Desktop.
Mailer service mocking for Symfony2
<?php
namespace Nelmio\HomeBundle\Tests\Controller;
use Nelmio\HomeBundle\Controller\ContactController;
use Liip\FunctionalTestBundle\Test\WebTestCase;
class ContactControllerTest extends WebTestCase
{
public function testEmail()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/contact');
$form = $crawler->selectButton('submit')->form();
$form['contact[name]'] = 'Test';
$form['contact[email]'] = 'test@example.org';
$form['contact[message]'] = 'Test message';
$client->submit($form);
$response = $client->getResponse();
$collector = $client->getProfile()->getCollector('swiftmailer');
$this->assertEquals(1, $collector->getMessageCount());
list($mail) = $collector->getMessages();
$this->assertEquals(302, $response->getStatusCode());
$this->assertTrue($mail instanceof \Swift_Message);
$this->assertContains('Test message', $mail->getBody());
$this->assertContains('test@example.org', $mail->getBody());
$this->assertEquals(array('hello@nelm.io' => ''), $mail->getTo());
$this->assertEquals('/contact/thanks', $response->headers->get('Location'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment