2
0

MailTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * @see Zend_Application_Resource_Mail
  35. */
  36. require_once 'Zend/Application/Resource/Mail.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Application
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Application
  44. */
  45. class Zend_Application_Resource_MailTest extends PHPUnit_Framework_TestCase
  46. {
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. public function setUp()
  53. {
  54. // Store original autoloaders
  55. $this->loaders = spl_autoload_functions();
  56. if (!is_array($this->loaders)) {
  57. // spl_autoload_functions does not return empty array when no
  58. // autoloaders registered...
  59. $this->loaders = array();
  60. }
  61. Zend_Loader_Autoloader::resetInstance();
  62. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  63. $this->application = new Zend_Application('testing');
  64. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  65. Zend_Controller_Front::getInstance()->resetInstance();
  66. }
  67. public function tearDown()
  68. {
  69. Zend_Mail::clearDefaultTransport();
  70. // Restore original autoloaders
  71. $loaders = spl_autoload_functions();
  72. foreach ($loaders as $loader) {
  73. spl_autoload_unregister($loader);
  74. }
  75. foreach ($this->loaders as $loader) {
  76. spl_autoload_register($loader);
  77. }
  78. // Reset autoloader instance so it doesn't affect other tests
  79. Zend_Loader_Autoloader::resetInstance();
  80. }
  81. public function testInitializationInitializesMailObject()
  82. {
  83. $resource = new Zend_Application_Resource_Mail(array());
  84. $resource->setBootstrap($this->bootstrap);
  85. $resource->setOptions(array('transport' => array('type' => 'sendmail')));
  86. $resource->init();
  87. $this->assertTrue($resource->getMail() instanceof Zend_Mail_Transport_Abstract);
  88. $this->assertTrue($resource->getMail() instanceof Zend_Mail_Transport_Sendmail);
  89. }
  90. public function testInitializationReturnsMailObject()
  91. {
  92. $resource = new Zend_Application_Resource_Mail(array());
  93. $resource->setBootstrap($this->bootstrap);
  94. $resource->setOptions(array('transport' => array('type' => 'sendmail')));
  95. $resource->init();
  96. $this->assertTrue($resource->init() instanceof Zend_Mail_Transport_Abstract);
  97. $this->assertTrue(Zend_Mail::getDefaultTransport() instanceof Zend_Mail_Transport_Sendmail);
  98. }
  99. public function testOptionsPassedToResourceAreUsedToInitializeMailTransportSmtp()
  100. {
  101. // If host option isn't passed on, an exception is thrown, making this text effective
  102. $options = array('transport' => array('type' => 'smtp',
  103. 'host' => 'example.com',
  104. 'register' => true));
  105. $resource = new Zend_Application_Resource_Mail(array());
  106. $resource->setBootstrap($this->bootstrap);
  107. $resource->setOptions($options);
  108. $resource->init();
  109. $this->assertTrue(Zend_Mail::getDefaultTransport() instanceof Zend_Mail_Transport_Smtp);
  110. }
  111. public function testNotRegisteringTransport()
  112. {
  113. // If host option isn't passed on, an exception is thrown, making this test effective
  114. $options = array('transport' => array('type' => 'sendmail',
  115. 'register' => false));
  116. $resource = new Zend_Application_Resource_Mail(array());
  117. $resource->setBootstrap($this->bootstrap);
  118. $resource->setOptions($options);
  119. $resource->init();
  120. $this->assertNull(Zend_Mail::getDefaultTransport());
  121. }
  122. public function testDefaultFromAndReplyTo()
  123. {
  124. $options = array('defaultfrom' => array('email' => 'foo@example.com',
  125. 'name' => 'Foo Bar'),
  126. 'defaultreplyto' => array('email' => 'john@example.com',
  127. 'name' => 'John Doe'));
  128. $resource = new Zend_Application_Resource_Mail(array());
  129. $resource->setBootstrap($this->bootstrap);
  130. $resource->setOptions($options);
  131. $resource->init();
  132. $this->assertNull(Zend_Mail::getDefaultTransport());
  133. $this->assertEquals($options['defaultfrom'], Zend_Mail::getDefaultFrom());
  134. $this->assertEquals($options['defaultreplyto'], Zend_Mail::getDefaultReplyTo());
  135. }
  136. }
  137. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') {
  138. Zend_Application_Resource_LogTest::main();
  139. }