mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-12-06 12:51:58 +00:00
Tests: Add decorators to PHPMailer mock object.
The new `get_recipient()` and `get_sent()` methods greatly simplify the syntax required when writing tests for `wp_mail()`. Props welcher. Fixes #34161. git-svn-id: https://develop.svn.wordpress.org/trunk@36594 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -14,13 +14,73 @@ class MockPHPMailer extends PHPMailer {
|
||||
*/
|
||||
function postSend() {
|
||||
$this->mock_sent[] = array(
|
||||
'to' => $this->to,
|
||||
'cc' => $this->cc,
|
||||
'bcc' => $this->bcc,
|
||||
'header' => $this->MIMEHeader,
|
||||
'body' => $this->MIMEBody,
|
||||
'to' => $this->to,
|
||||
'cc' => $this->cc,
|
||||
'bcc' => $this->bcc,
|
||||
'header' => $this->MIMEHeader,
|
||||
'subject' => $this->Subject,
|
||||
'body' => $this->MIMEBody,
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorator to return the information for a sent mock.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param int $index Optional. Array index of mock_sent value.
|
||||
* @return object
|
||||
*/
|
||||
public function get_sent( $index = 0 ) {
|
||||
$retval = false;
|
||||
if ( isset( $this->mock_sent[ $index ] ) ) {
|
||||
$retval = (object) $this->mock_sent[ $index ];
|
||||
}
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a recipient for a sent mock.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param string $address_type The type of address for the email such as to, cc or bcc.
|
||||
* @param int $mock_sent_index Optional. The sent_mock index we want to get the recipient for.
|
||||
* @param int $recipient_index Optional. The recipient index in the array.
|
||||
* @return bool|object Returns object on success, or false if any of the indices don't exist.
|
||||
*/
|
||||
public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) {
|
||||
$retval = false;
|
||||
$mock = $this->get_sent( $mock_sent_index );
|
||||
if ( $mock ) {
|
||||
if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) {
|
||||
$address_index = $mock->{$address_type}[ $recipient_index ];
|
||||
$recipient_data = array(
|
||||
'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set',
|
||||
'name' => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set',
|
||||
);
|
||||
|
||||
$retval = (object) $recipient_data;
|
||||
}
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to return the global phpmailer instance defined in the bootstrap
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @return object|bool
|
||||
*/
|
||||
function tests_retrieve_phpmailer_instance() {
|
||||
$mailer = false;
|
||||
if ( isset( $GLOBALS['phpmailer'] ) ) {
|
||||
$mailer = $GLOBALS['phpmailer'];
|
||||
}
|
||||
return $mailer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user