Skip to content

Commit

Permalink
Add see attachment in last mail
Browse files Browse the repository at this point in the history
  • Loading branch information
margori committed Jul 20, 2020
1 parent a7b5ff8 commit 5feb2c3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ Example:

* Param $expectCount

### seeAttachmentInLastEmail

Checks that last email contains an attachment with filename.

Example:

<?php
$I->seeAttachmentInLastEmail('image.jpg');
?>

* Param $filename

### seeInLastEmail

Checks that an email contains a value. It searches the full raw text of the
Expand Down
22 changes: 22 additions & 0 deletions src/Module/MailCatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,28 @@ public function grabUrlsFromLastEmail()
return $results;
}

/**
* See Attachment In Last Email
*
* Look for a attachement with certain filename in the most recent email
*
* @param string $expectedFilename
* @return void
* @author Marcelo Briones <[email protected]>
**/
public function seeAttachmentInLastEmail($expectedFilename)
{
$email = $this->lastMessage();
$message = Message::from($email->getSource());

foreach ($message->getAllAttachmentParts() as $attachmentPart) {
if ($attachmentPart->getFilename() == $expectedFilename ) {
return;
}
}
$this->fail("Filename not found in attachments");
}

/**
* Test email count equals expected value
*
Expand Down
37 changes: 37 additions & 0 deletions tests/acceptance/MailcatcherCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,43 @@ public function test_grab_urls_from_last_email_with_encoding(
$I->assertEquals($example[0], $urls[0]);
}

/**
* @param AcceptanceTester $I
*/
public function test_see_attachment_in_last(AcceptanceTester $I)
{
$user = "[email protected]";

$attachments = [
"image.jpg" => codecept_data_dir('image.jpg')
];

$I->sendEmail($user, 'Email with attachments', "I have attachments.", null, $attachments);

$I->seeAttachmentInLastEmail("image.jpg");
}

/**
* @param AcceptanceTester $I
*/
public function test_fail_see_attachment_in_last(AcceptanceTester $I)
{
$user = "[email protected]";

$attachments = [
"image.jpg" => codecept_data_dir('image.jpg')
];

$I->sendEmail($user, 'Email with attachments', "I have attachments.", null, $attachments);

try{
$I->seeAttachmentInLastEmail("no.jpg");
$I->fail("seeAttachmentInLastEmail should fail");
} catch (Exception $e) {
// test successful
}
}

/**
* @param AcceptanceTester $I
*/
Expand Down

0 comments on commit 5feb2c3

Please sign in to comment.