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