FileTransportTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * Zend_Mail
  28. */
  29. require_once 'Zend/Mail.php';
  30. /**
  31. * Zend_Mail_Transport_File
  32. */
  33. require_once 'Zend/Mail/Transport/File.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Mail
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Mail
  41. */
  42. class Zend_Mail_FileTransportTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_params;
  45. protected $_transport;
  46. protected $_tmpdir;
  47. public function setUp()
  48. {
  49. $this->createdTmpDir = false;
  50. $tmpDir = null;
  51. if (defined('TESTS_ZEND_MAIL_TEMPDIR')) {
  52. $tmpDir = constant('TESTS_ZEND_MAIL_TEMPDIR');
  53. }
  54. if (empty($tmpDir)) {
  55. $tmpDir = sys_get_temp_dir() . '/zend_test_mail.file/';
  56. }
  57. $this->_tmpdir = $tmpDir;
  58. if (!is_dir($this->_tmpdir)) {
  59. if (!mkdir($this->_tmpdir)) {
  60. $this->markTestSkipped('Unable to create temporary dir for testing Zend_Mail_Transport_File');
  61. }
  62. $this->createdTmpDir = true;
  63. }
  64. $this->_cleanDir($this->_tmpdir);
  65. }
  66. public function tearDown()
  67. {
  68. $this->_cleanDir($this->_tmpdir);
  69. if ($this->createdTmpDir) {
  70. rmdir($this->_tmpdir);
  71. }
  72. }
  73. protected function _cleanDir($dir)
  74. {
  75. $entries = scandir($dir);
  76. foreach ($entries as $entry) {
  77. if ($entry == '.' || $entry == '..') {
  78. continue;
  79. }
  80. $fullname = $dir . DIRECTORY_SEPARATOR . $entry;
  81. if (is_dir($fullname)) {
  82. $this->_cleanDir($fullname);
  83. rmdir($fullname);
  84. } else {
  85. unlink($fullname);
  86. }
  87. }
  88. }
  89. public function testTransportSetup()
  90. {
  91. $transport = new Zend_Mail_Transport_File();
  92. $transport = new Zend_Mail_Transport_File(array(
  93. 'path' => $this->_tmpdir,
  94. 'callback' => 'test_function'
  95. ));
  96. }
  97. protected function _prepareMail()
  98. {
  99. $mail = new Zend_Mail();
  100. $mail->setBodyText('This is the text of the mail.');
  101. $mail->setFrom('alexander@example.com', 'Alexander Steshenko');
  102. $mail->addTo('oleg@example.com', 'Oleg Lobach');
  103. $mail->setSubject('TestSubject');
  104. return $mail;
  105. }
  106. public function testNotWritablePathFailure()
  107. {
  108. $transport = new Zend_Mail_Transport_File(array(
  109. 'path' => $this->_tmpdir . '/not_existing/directory'
  110. ));
  111. $mail = $this->_prepareMail();
  112. $this->setExpectedException('Zend_Mail_Transport_Exception');
  113. $mail->send($transport);
  114. }
  115. public function testTransportSendMail()
  116. {
  117. $transport = new Zend_Mail_Transport_File(array('path' => $this->_tmpdir));
  118. $mail = $this->_prepareMail();
  119. $mail->send($transport);
  120. $entries = scandir($this->_tmpdir);
  121. $this->assertTrue(count($entries) == 3);
  122. foreach ($entries as $entry) {
  123. if ($entry == '.' || $entry == '..') {
  124. continue;
  125. }
  126. $filename = $this->_tmpdir . DIRECTORY_SEPARATOR . $entry;
  127. }
  128. $email = file_get_contents($filename);
  129. $this->assertContains('To: Oleg Lobach <oleg@example.com>', $email);
  130. $this->assertContains('Subject: TestSubject', $email);
  131. $this->assertContains('From: Alexander Steshenko <alexander@example.com>', $email);
  132. $this->assertContains("This is the text of the mail.", $email);
  133. }
  134. public function prependCallback($transport)
  135. {
  136. // callback utilizes default callback and prepends recipient email
  137. return $transport->recipients . '_' . $transport->defaultCallback($transport);
  138. }
  139. public function testPrependToCallback()
  140. {
  141. $transport = new Zend_Mail_Transport_File(array(
  142. 'path' => $this->_tmpdir,
  143. 'callback' => array($this, 'prependCallback')
  144. ));
  145. $mail = $this->_prepareMail();
  146. $mail->send($transport);
  147. $entries = scandir($this->_tmpdir);
  148. $this->assertTrue(count($entries) == 3);
  149. foreach ($entries as $entry) {
  150. if ($entry == '.' || $entry == '..') {
  151. continue;
  152. } else {
  153. break;
  154. }
  155. }
  156. // file name should now contain recipient email address
  157. $this->assertContains('oleg@example.com', $entry);
  158. // and default callback part
  159. $this->assertContains('ZendMail', $entry);
  160. }
  161. }