Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add mailable assertions #44563

Merged
merged 4 commits into from
Oct 12, 2022
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
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