MailTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_Application
  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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_MailTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. /**
  30. * Zend_Loader_Autoloader
  31. */
  32. require_once 'Zend/Loader/Autoloader.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Application
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Application
  40. */
  41. class Zend_Application_Resource_MailTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. // Store original autoloaders
  51. $this->loaders = spl_autoload_functions();
  52. if (!is_array($this->loaders)) {
  53. // spl_autoload_functions does not return empty array when no
  54. // autoloaders registered...
  55. $this->loaders = array();
  56. }
  57. Zend_Loader_Autoloader::resetInstance();
  58. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  59. $this->application = new Zend_Application('testing');
  60. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  61. Zend_Controller_Front::getInstance()->resetInstance();
  62. }
  63. public function tearDown()
  64. {
  65. Zend_Mail::clearDefaultTransport();
  66. // Restore original autoloaders
  67. $loaders = spl_autoload_functions();
  68. foreach ($loaders as $loader) {
  69. spl_autoload_unregister($loader);
  70. }
  71. foreach ($this->loaders as $loader) {
  72. spl_autoload_register($loader);
  73. }
  74. // Reset autoloader instance so it doesn't affect other tests
  75. Zend_Loader_Autoloader::resetInstance();
  76. }
  77. public function testInitializationInitializesMailObject()
  78. {
  79. $resource = new Zend_Application_Resource_Mail(array());
  80. $resource->setBootstrap($this->bootstrap);
  81. $resource->setOptions(array('transport' => array('type' => 'sendmail')));
  82. $resource->init();
  83. $this->assertTrue($resource->getMail() instanceof Zend_Mail_Transport_Abstract);
  84. $this->assertTrue($resource->getMail() instanceof Zend_Mail_Transport_Sendmail);
  85. }
  86. public function testInitializationReturnsMailObject()
  87. {
  88. $resource = new Zend_Application_Resource_Mail(array());
  89. $resource->setBootstrap($this->bootstrap);
  90. $resource->setOptions(array('transport' => array('type' => 'sendmail')));
  91. $resource->init();
  92. $this->assertTrue($resource->init() instanceof Zend_Mail_Transport_Abstract);
  93. $this->assertTrue(Zend_Mail::getDefaultTransport() instanceof Zend_Mail_Transport_Sendmail);
  94. }
  95. public function testOptionsPassedToResourceAreUsedToInitializeMailTransportSmtp()
  96. {
  97. // If host option isn't passed on, an exception is thrown, making this text effective
  98. $options = array('transport' => array('type' => 'smtp',
  99. 'host' => 'example.com',
  100. 'register' => true));
  101. $resource = new Zend_Application_Resource_Mail(array());
  102. $resource->setBootstrap($this->bootstrap);
  103. $resource->setOptions($options);
  104. $resource->init();
  105. $this->assertTrue(Zend_Mail::getDefaultTransport() instanceof Zend_Mail_Transport_Smtp);
  106. }
  107. public function testNotRegisteringTransport()
  108. {
  109. // If host option isn't passed on, an exception is thrown, making this test effective
  110. $options = array('transport' => array('type' => 'sendmail',
  111. 'register' => false));
  112. $resource = new Zend_Application_Resource_Mail(array());
  113. $resource->setBootstrap($this->bootstrap);
  114. $resource->setOptions($options);
  115. $resource->init();
  116. $this->assertNull(Zend_Mail::getDefaultTransport());
  117. }
  118. public function testDefaultFromAndReplyTo()
  119. {
  120. $options = array('defaultfrom' => array('email' => 'foo@example.com',
  121. 'name' => 'Foo Bar'),
  122. 'defaultreplyto' => array('email' => 'john@example.com',
  123. 'name' => 'John Doe'));
  124. $resource = new Zend_Application_Resource_Mail(array());
  125. $resource->setBootstrap($this->bootstrap);
  126. $resource->setOptions($options);
  127. $resource->init();
  128. $this->assertNull(Zend_Mail::getDefaultTransport());
  129. $this->assertEquals($options['defaultfrom'], Zend_Mail::getDefaultFrom());
  130. $this->assertEquals($options['defaultreplyto'], Zend_Mail::getDefaultReplyTo());
  131. }
  132. /**
  133. * Got notice: Undefined index: type
  134. */
  135. public function testDefaultTransport() {
  136. $options = array('transport' => array(//'type' => 'sendmail', // dont define type
  137. 'register' => true));
  138. $resource = new Zend_Application_Resource_Mail(array());
  139. $resource->setBootstrap($this->bootstrap);
  140. $resource->setOptions($options);
  141. $resource->init();
  142. $this->assertTrue(Zend_Mail::getDefaultTransport() instanceof Zend_Mail_Transport_Sendmail);
  143. }
  144. /**
  145. * @group ZF-8811
  146. */
  147. public function testDefaultsCaseSensivity() {
  148. $options = array('defaultFroM' => array('email' => 'f00@example.com', 'name' => null),
  149. 'defAultReplyTo' => array('email' => 'j0hn@example.com', 'name' => null));
  150. $resource = new Zend_Application_Resource_Mail(array());
  151. $resource->setBootstrap($this->bootstrap);
  152. $resource->setOptions($options);
  153. $resource->init();
  154. $this->assertNull(Zend_Mail::getDefaultTransport());
  155. $this->assertEquals($options['defaultFroM'], Zend_Mail::getDefaultFrom());
  156. $this->assertEquals($options['defAultReplyTo'], Zend_Mail::getDefaultReplyTo());
  157. }
  158. /**
  159. * @group ZF-8981
  160. */
  161. public function testNumericRegisterDirectiveIsPassedOnCorrectly() {
  162. $options = array('transport' => array('type' => 'sendmail',
  163. 'register' => '1')); // Culprit
  164. $resource = new Zend_Application_Resource_Mail(array());
  165. $resource->setBootstrap($this->bootstrap);
  166. $resource->setOptions($options);
  167. $resource->init();
  168. $this->assertTrue(Zend_Mail::getDefaultTransport() instanceof Zend_Mail_Transport_Sendmail);
  169. }
  170. /**
  171. * @group ZF-9136
  172. */
  173. public function testCustomMailTransportWithFQName() {
  174. $options = array('transport' => array('type' => 'Zend_Mail_Transport_Sendmail'));
  175. $resource = new Zend_Application_Resource_Mail(array());
  176. $resource->setBootstrap($this->bootstrap);
  177. $resource->setOptions($options);
  178. $this->assertTrue($resource->init() instanceof Zend_Mail_Transport_Sendmail);
  179. }
  180. /**
  181. * @group ZF-9136
  182. */
  183. public function testCustomMailTransportWithWrontCasesAsShouldBe() {
  184. $options = array('transport' => array('type' => 'Zend_Application_Resource_mailTestCAsE'));
  185. $resource = new Zend_Application_Resource_Mail(array());
  186. $resource->setBootstrap($this->bootstrap);
  187. $resource->setOptions($options);
  188. $this->assertTrue($resource->init() instanceof Zend_Application_Resource_mailTestCAsE);
  189. }
  190. }
  191. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_MailTest::main') {
  192. Zend_Application_Resource_MailTest::main();
  193. }