1. sfTesterMailer.class.php
  2. /** * sfTesterMailer implements tests for the symfony mailer object. * * @package symfony * @subpackage test * @author Fabien Potencier * @version SVN: $Id: sfTesterMailer.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfTesterMailer extends sfTester
  4. {
  5. protected
  6. $logger = null,
  7. $message = null;
  8. /**
  9. * Prepares the tester.
  10. */
  11. public function prepare()
  12. {
  13. }
  14. /**
  15. * Initializes the tester.
  16. */
  17. public function initialize()
  18. {
  19. $this->logger = $this->browser->getContext()->getMailer()->getLogger();
  20. if ($this->logger->countMessages())
  21. {
  22. $messages = $this->logger->getMessages();
  23. $this->message = $messages[0];
  24. }
  25. }
  26. /**
  27. * Tests if message was send and optional how many.
  28. *
  29. * @param int $nb number of messages
  30. *
  31. * @return sfTestFunctionalBase|sfTester
  32. */
  33. public function hasSent($nb = null)
  34. {
  35. if (null === $nb)
  36. {
  37. $this->tester->ok($this->logger->countMessages() > 0, 'mailer sent some email(s).');
  38. }
  39. else
  40. {
  41. $this->tester->is($this->logger->countMessages(), $nb, sprintf('mailer sent %s email(s).', $nb));
  42. }
  43. return $this->getObjectToReturn();
  44. }
  45. /**
  46. * Outputs some debug information about mails sent during the current request.
  47. */
  48. public function debug()
  49. {
  50. foreach ($this->logger->getMessages() as $message)
  51. {
  52. echo $message->toString()."\n\n";
  53. }
  54. exit(1);
  55. }
  56. /**
  57. * Changes the context to use the email corresponding to the given criteria.
  58. *
  59. * @param string|array $to the email or array(email => alias)
  60. * @param int $position address position
  61. *
  62. * @return sfTestFunctionalBase|sfTester
  63. */
  64. public function withMessage($to, $position = 1)
  65. {
  66. $messageEmail = $to;
  67. if(is_array($to))
  68. {
  69. $alias = current($to);
  70. $to = key($to);
  71. $messageEmail = sprintf('%s <%s>', $alias, $to);
  72. }
  73. $matches = 0;
  74. foreach ($this->logger->getMessages() as $message)
  75. {
  76. $email = $message->getTo();
  77. if ($to == key($email))
  78. {
  79. $matches++;
  80. if ($matches == $position)
  81. {
  82. $this->message = $message;
  83. if(isset($alias) AND $alias != current($email))
  84. {
  85. break;
  86. }
  87. $this->tester->pass(sprintf('switch context to the message number "%s" sent to "%s"', $position, $messageEmail));
  88. return $this;
  89. }
  90. }
  91. }
  92. $this->tester->fail(sprintf('unable to find a message sent to "%s"', $messageEmail));
  93. return $this;
  94. }
  95. /**
  96. * Tests for a mail message body.
  97. *
  98. * @param string $value regular expression or value
  99. *
  100. * @return sfTestFunctionalBase|sfTester
  101. */
  102. public function checkBody($value)
  103. {
  104. if (!$this->message)
  105. {
  106. $this->tester->fail('unable to test as no email were sent');
  107. }
  108. $body = $this->message->getBody();
  109. $ok = false;
  110. $regex = false;
  111. $mustMatch = true;
  112. if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match))
  113. {
  114. $regex = $value;
  115. if ($match[1] == '!')
  116. {
  117. $mustMatch = false;
  118. $regex = substr($value, 1);
  119. }
  120. }
  121. if (false !== $regex)
  122. {
  123. if ($mustMatch)
  124. {
  125. if (preg_match($regex, $body))
  126. {
  127. $ok = true;
  128. $this->tester->pass(sprintf('email body matches "%s"', $value));
  129. }
  130. }
  131. else
  132. {
  133. if (preg_match($regex, $body))
  134. {
  135. $ok = true;
  136. $this->tester->fail(sprintf('email body does not match "%s"', $value));
  137. }
  138. }
  139. }
  140. else if ($body == $value)
  141. {
  142. $ok = true;
  143. $this->tester->pass(sprintf('email body is "%s"', $value));
  144. }
  145. if (!$ok)
  146. {
  147. if (!$mustMatch)
  148. {
  149. $this->tester->pass(sprintf('email body matches "%s"', $value));
  150. }
  151. else
  152. {
  153. $this->tester->fail(sprintf('email body matches "%s"', $value));
  154. }
  155. }
  156. return $this->getObjectToReturn();
  157. }
  158. /**
  159. * Tests for a mail message header.
  160. *
  161. * @param string $key entry to test
  162. * @param string $value regular expression or value
  163. *
  164. * @return sfTestFunctionalBase|sfTester
  165. */
  166. public function checkHeader($key, $value)
  167. {
  168. if (!$this->message)
  169. {
  170. $this->tester->fail('unable to test as no email were sent');
  171. }
  172. $headers = array();
  173. foreach ($this->message->getHeaders()->getAll($key) as $header)
  174. {
  175. $headers[] = $header->getFieldBody();
  176. }
  177. $current = implode(', ', $headers);
  178. $ok = false;
  179. $regex = false;
  180. $mustMatch = true;
  181. if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match))
  182. {
  183. $regex = $value;
  184. if ($match[1] == '!')
  185. {
  186. $mustMatch = false;
  187. $regex = substr($value, 1);
  188. }
  189. }
  190. foreach ($headers as $header)
  191. {
  192. if (false !== $regex)
  193. {
  194. if ($mustMatch)
  195. {
  196. if (preg_match($regex, $header))
  197. {
  198. $ok = true;
  199. $this->tester->pass(sprintf('email header "%s" matches "%s" (%s)', $key, $value, $current));
  200. break;
  201. }
  202. }
  203. else
  204. {
  205. if (preg_match($regex, $header))
  206. {
  207. $ok = true;
  208. $this->tester->fail(sprintf('email header "%s" does not match "%s" (%s)', $key, $value, $current));
  209. break;
  210. }
  211. }
  212. }
  213. else if ($header == $value)
  214. {
  215. $ok = true;
  216. $this->tester->pass(sprintf('email header "%s" is "%s" (%s)', $key, $value, $current));
  217. break;
  218. }
  219. }
  220. if (!$ok)
  221. {
  222. if (!$mustMatch)
  223. {
  224. $this->tester->pass(sprintf('email header "%s" matches "%s" (%s)', $key, $value, $current));
  225. }
  226. else
  227. {
  228. $this->tester->fail(sprintf('email header "%s" matches "%s" (%s)', $key, $value, $current));
  229. }
  230. }
  231. return $this->getObjectToReturn();
  232. }
  233. }

Debug toolbar