Make WordPress Core

source: trunk/tests/phpunit/includes/mock-mailer.php @ 37358

Last change on this file since 37358 was 37358, checked in by boonebgorges, 9 years ago

Tests: Introduce reset_phpmailer_instance() function.

This function provides a more convenient method for resetting the
PHPMailer instance than the previous technique of reaching into the global.

Props welcher.
Fixes #36658.

File size: 2.5 KB
Line 
1<?php
2require_once( ABSPATH . '/wp-includes/class-phpmailer.php' );
3
4class MockPHPMailer extends PHPMailer {
5        var $mock_sent = array();
6
7        function preSend() {
8                $this->Encoding = '8bit';
9                return parent::preSend();
10        }
11
12        /**
13         * Override postSend() so mail isn't actually sent.
14         */
15        function postSend() {
16                $this->mock_sent[] = array(
17                        'to'      => $this->to,
18                        'cc'      => $this->cc,
19                        'bcc'     => $this->bcc,
20                        'header'  => $this->MIMEHeader,
21                        'subject' => $this->Subject,
22                        'body'    => $this->MIMEBody,
23                );
24
25                return true;
26        }
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        }
71}
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}
87
88/**
89 * Helper method to reset the phpmailer instance.
90 *
91 * @since 4.6.0
92 *
93 * @return bool
94 */
95function reset_phpmailer_instance() {
96        $mailer = tests_retrieve_phpmailer_instance();
97        if ( $mailer && isset( $mailer->mock_sent ) ) {
98                unset( $mailer->mock_sent );
99                return true;
100        }
101
102        return false;
103}
Note: See TracBrowser for help on using the repository browser.