Behat ile emailin gönderilip gönderilmediğini test etmeyi biliyoruz ama bu örnekte emailde attachment olup olmadığını test edeceğiz.


FeatureContext method


Sitemizin behat bölümünde tüm emaillerin nasıl alınacağına dair örnek var. Ayrıca ben email gönderirken email headerlerine kişisel olarak o:tag tag ekliyorum, bu nedenle sizde istediğiniz birşek kullanabilirsiniz. Bu konu ile ilgili örneğimizde mevcut.


/**
* @param string $tag
*
* @Then /^an email with o:tag "([^"]*)" should contain attachments with PNG and DOC extensions$/
*
* @throws BehaviorException
*/
public function anEmailWithOTagShouldContainAttachmentsWithPngAndDocExtensions($tag)
{
$extensions['png'] = null;
$extensions['doc'] = null;

$messages = $this->getAllSentMessages();
foreach ($messages as $message) {
$headers = $message->getHeaders();
if ($headers->has('o:tag')) {
/** @var Swift_Mime_Headers_UnstructuredHeader $oTagHeader */
$oTagHeader = $headers->get('o:tag');
$oTags = (array) $oTagHeader->getValue();
if (in_array($tag, $oTags)) {
foreach ($message->getChildren() as $child) {
$attachment = str_replace(
'attachment; filename=',
null,
$child->getHeaders()->get('content-disposition')->getFieldBody()
);

if (array_key_exists(pathinfo($attachment, PATHINFO_EXTENSION), $extensions)) {
unset($extensions[pathinfo($attachment, PATHINFO_EXTENSION)]);
}
}
}
}
}

if ($extensions) {
throw new BehaviorException('Attachment with relevant extensions cannot be found.');
}
}