Skip to content

Commit

Permalink
add mailable assertions (#44563)
Browse files Browse the repository at this point in the history
* add mailable assertions

* formatting

* formatting

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
macbookandrew and taylorotwell authored Oct 12, 2022
1 parent 331bf9c commit 4bc8737
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 0 deletions.
176 changes: 176 additions & 0 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,149 @@ public function hasMetadata($key, $value)
(method_exists($this, 'envelope') && $this->envelope()->hasMetadata($key, $value));
}

/**
* Assert that the mailable is from the given address.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function assertFrom($address, $name = null)
{
$recipient = $this->formatAssertionRecipient($address, $name);

PHPUnit::assertTrue(
$this->hasFrom($address, $name),
"Email was not from expected address [{$recipient}]."
);

return $this;
}

/**
* Assert that the mailable has the given recipient.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function assertTo($address, $name = null)
{
$recipient = $this->formatAssertionRecipient($address, $name);

PHPUnit::assertTrue(
$this->hasTo($address, $name),
"Did not see expected recipient [{$recipient}] in email recipients."
);

return $this;
}

/**
* Assert that the mailable has the given recipient.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function assertHasTo($address, $name = null)
{
return $this->assertTo($address, $name);
}

/**
* Assert that the mailable has the given recipient.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function assertHasCc($address, $name = null)
{
$recipient = $this->formatAssertionRecipient($address, $name);

PHPUnit::assertTrue(
$this->hasCc($address, $name),
"Did not see expected recipient [{$recipient}] in email recipients."
);

return $this;
}

/**
* Assert that the mailable has the given recipient.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function assertHasBcc($address, $name = null)
{
$recipient = $this->formatAssertionRecipient($address, $name);

PHPUnit::assertTrue(
$this->hasBcc($address, $name),
"Did not see expected recipient [{$recipient}] in email recipients."
);

return $this;
}

/**
* Assert that the mailable has the given "reply to" address.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function assertHasReplyTo($address, $name = null)
{
$replyTo = $this->formatAssertionRecipient($address, $name);

PHPUnit::assertTrue(
$this->hasReplyTo($address, $name),
"Did not see expected address [{$replyTo}] as email 'reply to' recipient."
);

return $this;
}

/**
* Format the mailable recipeint for display in an assertion message.
*
* @param object|array|string $address
* @param string|null $name
* @return string
*/
private function formatAssertionRecipient($address, $name = null)
{
if (! is_string($address)) {
$address = json_encode($address);
}

if (filled($name)) {
$address .= ' ('.$name.')';
}

return $address;
}

/**
* Assert that the mailable has the given subject.
*
* @param string $subject
* @return $this
*/
public function assertHasSubject($subject)
{
PHPUnit::assertTrue(
$this->hasSubject($subject),
"Did not see expected text [{$subject}] in email subject."
);

return $this;
}

/**
* Assert that the given text is present in the HTML email body.
*
Expand Down Expand Up @@ -1329,6 +1472,39 @@ public function assertHasAttachmentFromStorageDisk($disk, $path, $name = null, a
return $this;
}

/**
* Assert that the mailable has the given tag.
*
* @param string $tag
* @return $this
*/
public function assertHasTag($tag)
{
PHPUnit::assertTrue(
$this->hasTag($tag),
"Did not see expected tag [{$tag}] in email tags."
);

return $this;
}

/**
* Assert that the mailable has the given metadata.
*
* @param string $key
* @param string $value
* @return $this
*/
public function assertHasMetadata($key, $value)
{
PHPUnit::assertTrue(
$this->hasMetadata($key, $value),
"Did not see expected key [{$key}] and value [{$value}] in email metadata."
);

return $this;
}

/**
* Render the HTML and plain-text version of the mailable into views for assertions.
*
Expand Down
Loading

0 comments on commit 4bc8737

Please sign in to comment.