Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Feature/check cc and bcc fields #12

Merged
merged 13 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ Install the package:

You mixin the assertions with the ```Spinen\MailAssertions\MailTracking``` trait. You get the following assertions...

* seeEmailBcc
* seeEmailCc
* seeEmailContains
* seeEmailEquals
* seeEmailFrom
* seeEmailReplyTo
* seeEmailSubject
* seeEmailTo
* seeEmailWasNotSent
Expand Down
73 changes: 59 additions & 14 deletions src/MailTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@
trait MailTracking
{
// TODO: Add check for attachments (number of & name)
// TODO: Add check for BCC
// TODO: Add check for CC
// TODO: Add check for header
// TODO: Add check for message type
// TODO: Add check for Priority
// TODO: Add check for ReplyTo
// TODO: Allow checking specific message not just most recent one

/**
Expand All @@ -58,7 +55,7 @@ public function setUpMailTracking()
/**
* Retrieve the appropriate swift message.
*
* @param Swift_Message $message
* @param Swift_Message|null $message
*
* @return Swift_Message
*/
Expand Down Expand Up @@ -89,11 +86,43 @@ public function recordMail(Swift_Message $email)
$this->emails[] = $email;
}

/**
* Assert that the last email was bcc'ed to the given address.
*
* @param string $bcc
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
protected function seeEmailBcc($bcc, Swift_Message $message = null)
{
$this->assertArrayHasKey($bcc, (array)$this->getEmail($message)
->getBcc(), "No email was bcc'ed to $bcc.");

return $this;
}

/**
* Assert that the last email was cc'ed to the given address.
*
* @param string $cc
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
protected function seeEmailCc($cc, Swift_Message $message = null)
{
$this->assertArrayHasKey($cc, (array)$this->getEmail($message)
->getCc(), "No email was cc'ed to $cc.");

return $this;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not allow someone to pass in an array of emails for the CC and BCC tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a separate issue for that #18 so I want to do that in a different feature.


/**
* Assert that the last email's body contains the given text.
*
* @param string $excerpt
* @param Swift_Message $message
* @param string $excerpt
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
Expand All @@ -108,8 +137,8 @@ protected function seeEmailContains($excerpt, Swift_Message $message = null)
/**
* Assert that the last email's body equals the given text.
*
* @param string $body
* @param Swift_Message $message
* @param string $body
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
Expand All @@ -124,8 +153,8 @@ protected function seeEmailEquals($body, Swift_Message $message = null)
/**
* Assert that the last email was delivered by the given address.
*
* @param string $sender
* @param Swift_Message $message
* @param string $sender
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
Expand All @@ -138,6 +167,22 @@ protected function seeEmailFrom($sender, Swift_Message $message = null)
return $this;
}

/**
* Assert that the last email was set to reply to the given address.
*
* @param string $reply_to
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
{
$this->assertArrayHasKey($reply_to, (array)$this->getEmail($message)
->getReplyTo(), "No email was set to reply to $reply_to.");

return $this;
}

/**
* Assert that the given number of emails were sent.
*
Expand All @@ -157,8 +202,8 @@ protected function seeEmailsSent($count)
/**
* Assert that the last email's subject matches the given string.
*
* @param string $subject
* @param Swift_Message $message
* @param string $subject
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
Expand All @@ -174,8 +219,8 @@ protected function seeEmailSubject($subject, Swift_Message $message = null)
/**
* Assert that the last email was sent to the given recipient.
*
* @param string $recipient
* @param Swift_Message $message
* @param string $recipient
* @param Swift_Message|null $message
*
* @return PHPUnit_Framework_TestCase $this
*/
Expand Down
43 changes: 41 additions & 2 deletions tests/MailTrackingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ public function it_records_emails_in_collection()
$this->assertCount(1, $this->mail_tracking->exposeMessage());
}

/**
* @test
* @group unit
*/
public function it_checks_email_bcc_address()
{
$message = $this->makeMessage();
$message->setBcc('[email protected]');
$this->mail_tracking->recordMail($message);

$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailBcc', ['[email protected]']));
}

/**
* @test
* @group unit
*/
public function it_checks_email_cc_address()
{
$message = $this->makeMessage();
$message->setCc('[email protected]');
$this->mail_tracking->recordMail($message);

$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailCc', ['[email protected]']));
}

/**
* @test
* @group unit
Expand All @@ -156,7 +182,7 @@ public function it_makes_sure_email_body_is_what_is_expected()

/**
* @test
* @group
* @group unit
*/
public function it_checks_email_from_address()
{
Expand All @@ -168,7 +194,20 @@ public function it_checks_email_from_address()

/**
* @test
* @group
* @group unit
*/
public function it_checks_email_reply_to_address()
{
$message = $this->makeMessage();
$message->setReplyTo('[email protected]');
$this->mail_tracking->recordMail($message);

$this->assertEquals($this->mail_tracking, $this->callProtectedMethod('seeEmailReplyTo', ['[email protected]']));
}

/**
* @test
* @group unit
*/
public function it_knows_how_many_emails_have_been_sent()
{
Expand Down