Skip to content

Instantly share code, notes, and snippets.

@drakakisgeo
Last active August 29, 2015 14:17
Show Gist options
  • Save drakakisgeo/591649190564fe57a4cd to your computer and use it in GitHub Desktop.
Save drakakisgeo/591649190564fe57a4cd to your computer and use it in GitHub Desktop.
Mailcatcher Codeception Nth($num)
<?php
/**
* See In nth Email
*
* Look for a string in Nth($num) email
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function seeInEmailNth($num,$expected)
{
$email = $this->nthMessage($num);
$this->seeInEmail($email, $expected);
}
/**
* See In Nth Email subject
*
* Look for a string in Nth($num) email subject
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function seeInNthEmailSubject($num,$expected)
{
$email = $this->nthMessage($num);
$this->seeInEmailSubject($email, $expected);
}
/**
* Don't See In Nth Email subject
*
* Look for the absence of a string in Nth($num) email subject
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function dontSeeInNthEmailSubject($num,$expected)
{
$email = $this->nthMessage($num);
$this->dontSeeInEmailSubject($email, $expected);
}
/**
* Don't See In Nth Email
*
* Look for the absence of a string in Nth($num) email
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function dontSeeInNthEmail($num,$unexpected)
{
$email = $this->nthMessage($num);
$this->dontSeeInEmail($email, $unexpected);
}
/**
* See In Nth Email To
*
* Look for a string in Nth($num) email sent to $address
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function seeInNthEmailTo($num,$address, $expected)
{
$email = $this->nthMessageFrom($num,$address);
$this->seeInEmail($email, $expected);
}
/**
* Don't See In Nth Email To
*
* Look for the absence of a string in Nth($num) email sent to $address
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function dontSeeInNthEmailTo($num,$address, $unexpected)
{
$email = $this->nthMessageFrom($num,$address);
$this->dontSeeInEmail($email, $unexpected);
}
/**
* See In Nth Email Subject To
*
* Look for a string in Nth($num) email subject sent to $address
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function seeInNthEmailSubjectTo($num,$address, $expected)
{
$email = $this->nthMessageFrom($num,$address);
$this->seeInEmailSubject($email, $expected);
}
/**
* Don't See In Nth Email Subject To
*
* Look for the absence of a string in Nth($num) email subject sent to $address
*
* @return void
* @author George Drakakis <lolly@lollypop.gr>
**/
public function dontSeeInNthEmailSubjectTo($num,$address, $unexpected)
{
$email = $this->nthMessageFrom($num,$address);
$this->dontSeeInEmailSubject($email, $unexpected);
}
/**
* Grab Matches From Nth Email
*
* Look for a regex in the email source and return it's matches
*
* @return array
* @author George Drakakis <lolly@lollypop.gr>
**/
public function grabMatchesFromNthEmail($num,$regex)
{
$email = $this->nthMessage($num);
$matches = $this->grabMatchesFromEmail($email, $regex);
return $matches;
}
/**
* Grab From Nth Email
*
* Look for a regex in the email source and return it
*
* @return string
* @author George Drakakis <lolly@lollypop.gr>
**/
public function grabFromNthEmail($num,$regex)
{
$matches = $this->grabMatchesFromNthEmail($num,$regex);
return $matches[0];
}
/**
* Grab Matches From Nth Email To
*
* Look for a regex in most recent email sent to $addres email source and
* return it's matches
*
* @return array
* @author George Drakakis <lolly@lollypop.gr>
**/
public function grabMatchesFromNthEmailTo($num,$address, $regex)
{
$email = $this->nthMessageFrom($num,$address);
$matches = $this->grabMatchesFromEmail($email, $regex);
return $matches;
}
/**
* Grab From Nth Email To
*
* Look for a regex in most recent email sent to $addres email source and
* return it
*
* @return string
* @author George Drakakis <lolly@lollypop.gr>
**/
public function grabFromNthEmailTo($num,$address, $regex)
{
$matches = $this->grabMatchesFromNthEmailTo($num,$address, $regex);
return $matches[0];
}
/**
* Last Message
*
* Get Nth($num) email
*
* @return obj
* @author George Drakakis <lolly@lollypop.gr>
**/
protected function nthMessage($key=null)
{
$messages = $this->messages();
if (empty($messages)) {
$this->fail("No messages received");
}
if (is_null($key)) {
$this->fail("You need to provide a number to be used as the index key.");
}
$target = $messages[$key];
return $this->emailFromId($target['id']);
}
/**
* Nth($num) Message From
*
* Get the Nth($num) sent to $address
*
* @return obj
* @author George Drakakis <lolly@lollypop.gr>
**/
protected function nthMessageFrom($key=null,$address)
{
$ids = [];
$messages = $this->messages();
if (empty($messages)) {
$this->fail("No messages received");
}
if (is_null($key)) {
$this->fail("You need to provide a number to be used as the index key.");
}
foreach ($messages as $message) {
foreach ($message['recipients'] as $recipient) {
if (strpos($recipient, $address) !== false) {
$ids[] = $message['id'];
}
}
}
if (count($ids) > 0)
return $this->emailFromId($ids[$key]);
$this->fail("No messages sent to {$address}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment