mock.php 4.8 KB

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