09/12/2015 - BEHAT
You can use custom context file and the behat steps for testing your emails.
Feature: Dummy feature
Scenario: I can send an email message
Given there is an email with tag "custom-tag" contains "greetings_email"
And the email sent from "sender@somedomain.com" to "receiver@somedomain.com"
And the email subject is "Greetings"
And the email content is:
"""
Hi,
How are you today?
Bye
"""
use Behat\Gherkin\Node\PyStringNode;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use LogicException;
use Swift_Message;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
class EmailContext extends MinkContext implements KernelAwareInterface
{
/* @var KernelInterface */
private $kernel;
private $emailTagName;
private $emailTagValue;
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* @BeforeScenario
*/
public function purgeSpool()
{
$spoolDir = $this->getSpoolDir();
$filesystem = new Filesystem();
$filesystem->remove($spoolDir);
}
/**
* @param string $tagName
* @param string $tagValue
*
* @Given /^there is an email with tag "([^"]*)" contains "([^"]*)"$/
*/
public function thereIsAnEmailWithTag($tagName, $tagValue)
{
$emails = $this->getAllEmails();
if (!$this->checkTag($emails, $tagName, $tagValue)) {
throw new LogicException('Given tag with value cannot be found.');
}
}
/**
* @param string $fromAddress
* @param string $toAddress
*
* @Given /^the email sent from "([^"]*)" to "([^"]*)"$/
*/
public function theEmailSentFromTo($fromAddress, $toAddress)
{
$emails = $this->getAllEmails();
$email = $this->getRelevantEmail($emails);
if ($email instanceof Swift_Message) {
if (array_key_exists($fromAddress, $email->getFrom()) && array_key_exists($toAddress, $email->getTo())) {
return;
}
}
throw new LogicException('Email with given properties cannot be found.');
}
/**
* @param string $subject
*
* @Given /^the email subject is "([^"]*)"$/
*/
public function theEmailSubjectIs($subject)
{
$emails = $this->getAllEmails();
$email = $this->getRelevantEmail($emails);
if ($email instanceof Swift_Message) {
if ($email->getSubject() == $subject) {
return;
}
}
throw new LogicException('Email with given properties cannot be found.');
}
/**
* @param PyStringNode $pyStringNode
*
* @Given /^the email content is:$/
*/
public function theEmailContentIs(PyStringNode $pyStringNode)
{
$emails = $this->getAllEmails();
$email = $this->getRelevantEmail($emails);
if ($email instanceof Swift_Message) {
if ($email->getBody() == $pyStringNode->getRaw()) {
return;
}
}
throw new LogicException('Email with given properties cannot be found.');
}
/**
* @return string
*/
private function getSpoolDir()
{
return $this->kernel->getContainer()->getParameter('swiftmailer.spool.default.file.path');
}
/**
* @return array
*/
private function getAllEmails()
{
$spoolDir = $this->getSpoolDir();
$filesystem = new Filesystem();
$emails = [];
if ($filesystem->exists($spoolDir)) {
$finder = new Finder();
$finder
->in($spoolDir)
->ignoreDotFiles(true)
->files();
foreach ($finder as $file) {
/* @var Swift_Message $message */
$emails[] = unserialize(file_get_contents($file));
}
}
return $emails;
}
/**
* @param array $emails
*
* @return bool|Swift_Message
*/
private function getRelevantEmail(array $emails)
{
/** @var Swift_Message $email */
foreach ($emails as $email) {
$headers = $email->getHeaders();
if ($headers->has($this->emailTagName) &&
in_array($this->emailTagValue, $headers->get($this->emailTagName)->getValue())) {
return $email;
}
}
return false;
}
/**
* @param array $emails
* @param string $tagName
* @param string $tagValue
*
* @return bool
*/
private function checkTag(array $emails, $tagName, $tagValue)
{
/** @var Swift_Message $email */
foreach ($emails as $email) {
$headers = $email->getHeaders();
if ($headers->has($tagName) && in_array($tagValue, $headers->get($tagName)->getValue())) {
$this->emailTagName = $tagName;
$this->emailTagValue = $tagValue;
return true;
}
}
return false;
}
}