Debian Codegraph @

vendor/pear/mail/Mail/mock.php

raw
1 <?php
2 /**
3 * Mock implementation
4 *
5 * PHP version 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2010-2017, Chuck Hagenbuch
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * 3. Neither the name of the copyright holder nor the names of its
24 * contributors may be used to endorse or promote products derived from
25 * this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * @category Mail
40 * @package Mail
41 * @author Chuck Hagenbuch <[email protected]>
42 * @copyright 2010-2017 Chuck Hagenbuch
43 * @license http://opensource.org/licenses/BSD-3-Clause New BSD License
44 * @version CVS: $Id$
45 * @link http://pear.php.net/package/Mail/
46 */
47
48 /**
49 * Mock implementation of the PEAR Mail:: interface for testing.
50 * @access public
51 * @package Mail
52 * @version $Revision$
53 */
54 class Mail_mock extends Mail {
55
56 /**
57 * Array of messages that have been sent with the mock.
58 *
59 * @var array
60 */
61 public $sentMessages = array();
62
63 /**
64 * Callback before sending mail.
65 *
66 * @var callback
67 */
68 protected $_preSendCallback;
69
70 /**
71 * Callback after sending mai.
72 *
73 * @var callback
74 */
75 protected $_postSendCallback;
76
77 /**
78 * Constructor.
79 *
80 * Instantiates a new Mail_mock:: object based on the parameters
81 * passed in. It looks for the following parameters, both optional:
82 * preSendCallback Called before an email would be sent.
83 * postSendCallback Called after an email would have been sent.
84 *
85 * @param array Hash containing any parameters.
86 */
87 public function __construct($params)
88 {
89 if (isset($params['preSendCallback']) &&
90 is_callable($params['preSendCallback'])) {
91 $this->_preSendCallback = $params['preSendCallback'];
92 }
93
94 if (isset($params['postSendCallback']) &&
95 is_callable($params['postSendCallback'])) {
96 $this->_postSendCallback = $params['postSendCallback'];
97 }
98 }
99
100 /**
101 * Implements Mail_mock::send() function. Silently discards all
102 * mail.
103 *
104 * @param mixed $recipients Either a comma-seperated list of recipients
105 * (RFC822 compliant), or an array of recipients,
106 * each RFC822 valid. This may contain recipients not
107 * specified in the headers, for Bcc:, resending
108 * messages, etc.
109 *
110 * @param array $headers The array of headers to send with the mail, in an
111 * associative array, where the array key is the
112 * header name (ie, 'Subject'), and the array value
113 * is the header value (ie, 'test'). The header
114 * produced from those values would be 'Subject:
115 * test'.
116 *
117 * @param string $body The full text of the message body, including any
118 * Mime parts, etc.
119 *
120 * @return mixed Returns true on success, or a PEAR_Error
121 * containing a descriptive error message on
122 * failure.
123 */
124 public function send($recipients, $headers, $body)
125 {
126 if ($this->_preSendCallback) {
127 call_user_func_array($this->_preSendCallback,
128 array(&$this, $recipients, $headers, $body));
129 }
130
131 $entry = array('recipients' => $recipients, 'headers' => $headers, 'body' => $body);
132 $this->sentMessages[] = $entry;
133
134 if ($this->_postSendCallback) {
135 call_user_func_array($this->_postSendCallback,
136 array(&$this, $recipients, $headers, $body));
137 }
138
139 return true;
140 }
141
142 }