FileTransportTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * Zend_Mail
  24. */
  25. require_once 'Zend/Mail.php';
  26. /**
  27. * Zend_Mail_Transport_File
  28. */
  29. require_once 'Zend/Mail/Transport/File.php';
  30. /** For some reason these classed have to be manually loaded, because PHPUnit fails to autoload them */
  31. require_once 'PHPUnit/Framework/SyntheticError.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Mail
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Mail
  39. */
  40. class Zend_Mail_FileTransportTest extends PHPUnit_Framework_TestCase
  41. {
  42. protected $_params;
  43. protected $_transport;
  44. protected $_tmpdir;
  45. public function setUp()
  46. {
  47. $this->createdTmpDir = false;
  48. $tmpDir = null;
  49. if (defined('TESTS_ZEND_MAIL_TEMPDIR')) {
  50. $tmpDir = constant('TESTS_ZEND_MAIL_TEMPDIR');
  51. }
  52. if (empty($tmpDir)) {
  53. $tmpDir = sys_get_temp_dir() . '/zend_test_mail.file/';
  54. }
  55. $this->_tmpdir = $tmpDir;
  56. if (!is_dir($this->_tmpdir)) {
  57. if (!mkdir($this->_tmpdir)) {
  58. $this->markTestSkipped('Unable to create temporary dir for testing Zend_Mail_Transport_File');
  59. }
  60. $this->createdTmpDir = true;
  61. }
  62. $this->_cleanDir($this->_tmpdir);
  63. }
  64. public function tearDown()
  65. {
  66. $this->_cleanDir($this->_tmpdir);
  67. if ($this->createdTmpDir) {
  68. rmdir($this->_tmpdir);
  69. }
  70. }
  71. protected function _cleanDir($dir)
  72. {
  73. $entries = scandir($dir);
  74. foreach ($entries as $entry) {
  75. if ($entry == '.' || $entry == '..') {
  76. continue;
  77. }
  78. $fullname = $dir . DIRECTORY_SEPARATOR . $entry;
  79. if (is_dir($fullname)) {
  80. $this->_cleanDir($fullname);
  81. rmdir($fullname);
  82. } else {
  83. unlink($fullname);
  84. }
  85. }
  86. }
  87. public function testTransportSetup()
  88. {
  89. $transport = new Zend_Mail_Transport_File();
  90. $transport = new Zend_Mail_Transport_File(array(
  91. 'path' => $this->_tmpdir,
  92. 'callback' => 'test_function'
  93. ));
  94. }
  95. protected function _prepareMail()
  96. {
  97. $mail = new Zend_Mail();
  98. $mail->setBodyText('This is the text of the mail.');
  99. $mail->setFrom('alexander@example.com', 'Alexander Steshenko');
  100. $mail->addTo('oleg@example.com', 'Oleg Lobach');
  101. $mail->setSubject('TestSubject');
  102. return $mail;
  103. }
  104. public function testNotWritablePathFailure()
  105. {
  106. $transport = new Zend_Mail_Transport_File(array(
  107. 'path' => $this->_tmpdir . '/not_existing/directory'
  108. ));
  109. $mail = $this->_prepareMail();
  110. $this->setExpectedException('Zend_Mail_Transport_Exception');
  111. $mail->send($transport);
  112. }
  113. public function testTransportSendMail()
  114. {
  115. $transport = new Zend_Mail_Transport_File(array('path' => $this->_tmpdir));
  116. $mail = $this->_prepareMail();
  117. $mail->send($transport);
  118. $entries = scandir($this->_tmpdir);
  119. $this->assertTrue(count($entries) == 3);
  120. foreach ($entries as $entry) {
  121. if ($entry == '.' || $entry == '..') {
  122. continue;
  123. }
  124. $filename = $this->_tmpdir . DIRECTORY_SEPARATOR . $entry;
  125. }
  126. $email = file_get_contents($filename);
  127. $this->assertContains('To: Oleg Lobach <oleg@example.com>', $email);
  128. $this->assertContains('Subject: TestSubject', $email);
  129. $this->assertContains('From: Alexander Steshenko <alexander@example.com>', $email);
  130. $this->assertContains("This is the text of the mail.", $email);
  131. }
  132. public function prependCallback($transport)
  133. {
  134. // callback utilizes default callback and prepends recipient email
  135. return $transport->recipients . '_' . $transport->defaultCallback($transport);
  136. }
  137. public function testPrependToCallback()
  138. {
  139. $transport = new Zend_Mail_Transport_File(array(
  140. 'path' => $this->_tmpdir,
  141. 'callback' => array($this, 'prependCallback')
  142. ));
  143. $mail = $this->_prepareMail();
  144. $mail->send($transport);
  145. $entries = scandir($this->_tmpdir);
  146. $this->assertTrue(count($entries) == 3);
  147. foreach ($entries as $entry) {
  148. if ($entry == '.' || $entry == '..') {
  149. continue;
  150. } else {
  151. break;
  152. }
  153. }
  154. // file name should now contain recipient email address
  155. $this->assertContains('oleg@example.com', $entry);
  156. // and default callback part
  157. $this->assertContains('ZendMail', $entry);
  158. }
  159. }