Make WordPress Core


Ignore:
Timestamp:
02/20/2016 03:40:49 AM (8 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/mock-mailer.php

    r33124 r36594  
    1515    function postSend() {
    1616        $this->mock_sent[] = array(
    17             'to'     => $this->to,
    18             'cc'     => $this->cc,
    19             'bcc'    => $this->bcc,
    20             'header' => $this->MIMEHeader,
    21             'body'   => $this->MIMEBody,
     17            'to'      => $this->to,
     18            'cc'      => $this->cc,
     19            'bcc'     => $this->bcc,
     20            'header'  => $this->MIMEHeader,
     21            'subject' => $this->Subject,
     22            'body'    => $this->MIMEBody,
    2223        );
    2324
    2425        return true;
    2526    }
     27
     28    /**
     29     * Decorator to return the information for a sent mock.
     30     *
     31     * @since 4.5.0
     32     *
     33     * @param int $index Optional. Array index of mock_sent value.
     34     * @return object
     35     */
     36    public function get_sent( $index = 0 ) {
     37        $retval = false;
     38        if ( isset( $this->mock_sent[ $index ] ) ) {
     39            $retval = (object) $this->mock_sent[ $index ];
     40        }
     41        return $retval;
     42    }
     43
     44    /**
     45     * Get a recipient for a sent mock.
     46     *
     47     * @since 4.5.0
     48     *
     49     * @param string $address_type    The type of address for the email such as to, cc or bcc.
     50     * @param int    $mock_sent_index Optional. The sent_mock index we want to get the recipient for.
     51     * @param int    $recipient_index Optional. The recipient index in the array.
     52     * @return bool|object Returns object on success, or false if any of the indices don't exist.
     53     */
     54    public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) {
     55        $retval = false;
     56        $mock = $this->get_sent( $mock_sent_index );
     57        if ( $mock ) {
     58            if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) {
     59                $address_index = $mock->{$address_type}[ $recipient_index ];
     60                $recipient_data = array(
     61                    'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set',
     62                    'name'    => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set',
     63                );
     64
     65                $retval = (object) $recipient_data;
     66            }
     67        }
     68
     69        return $retval;
     70    }
    2671}
     72
     73/**
     74 * Helper method to return the global phpmailer instance defined in the bootstrap
     75 *
     76 * @since 4.4.0
     77 *
     78 * @return object|bool
     79 */
     80function tests_retrieve_phpmailer_instance() {
     81    $mailer = false;
     82    if ( isset( $GLOBALS['phpmailer'] ) ) {
     83        $mailer = $GLOBALS['phpmailer'];
     84    }
     85    return $mailer;
     86}
Note: See TracChangeset for help on using the changeset viewer.