PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/trunk/tests/Zend/Mail/Pop3Test.php

http://firephp.googlecode.com/
PHP | 439 lines | 305 code | 72 blank | 62 comment | 28 complexity | 639bd5f1044d5e09d04a729b9e762a30 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Pop3Test.php 23772 2011-02-28 21:35:29Z ralph $
  21. */
  22. /**
  23. * Zend_Mail_Storage_Pop3
  24. */
  25. require_once 'Zend/Mail/Storage/Pop3.php';
  26. /**
  27. * Zend_Mail_Protocol_Pop3
  28. */
  29. require_once 'Zend/Mail/Protocol/Pop3.php';
  30. /**
  31. * Zend_Config
  32. */
  33. require_once 'Zend/Config.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Mail
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2011 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_Pop3Test extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_params;
  45. public function setUp()
  46. {
  47. $this->_params = array('host' => TESTS_ZEND_MAIL_POP3_HOST,
  48. 'user' => TESTS_ZEND_MAIL_POP3_USER,
  49. 'password' => TESTS_ZEND_MAIL_POP3_PASSWORD);
  50. if (defined('TESTS_ZEND_MAIL_SERVER_TESTDIR') && TESTS_ZEND_MAIL_SERVER_TESTDIR) {
  51. if (!file_exists(TESTS_ZEND_MAIL_SERVER_TESTDIR . DIRECTORY_SEPARATOR . 'inbox')
  52. && !file_exists(TESTS_ZEND_MAIL_SERVER_TESTDIR . DIRECTORY_SEPARATOR . 'INBOX')) {
  53. $this->markTestSkipped('There is no file name "inbox" or "INBOX" in '
  54. . TESTS_ZEND_MAIL_SERVER_TESTDIR . '. I won\'t use it for testing. '
  55. . 'This is you safety net. If you think it is the right directory just '
  56. . 'create an empty file named INBOX or remove/deactived this message.');
  57. }
  58. $this->_cleanDir(TESTS_ZEND_MAIL_SERVER_TESTDIR);
  59. $this->_copyDir(dirname(__FILE__) . '/_files/test.' . TESTS_ZEND_MAIL_SERVER_FORMAT,
  60. TESTS_ZEND_MAIL_SERVER_TESTDIR);
  61. }
  62. }
  63. protected function _cleanDir($dir)
  64. {
  65. $dh = opendir($dir);
  66. while (($entry = readdir($dh)) !== false) {
  67. if ($entry == '.' || $entry == '..') {
  68. continue;
  69. }
  70. $fullname = $dir . DIRECTORY_SEPARATOR . $entry;
  71. if (is_dir($fullname)) {
  72. $this->_cleanDir($fullname);
  73. rmdir($fullname);
  74. } else {
  75. unlink($fullname);
  76. }
  77. }
  78. closedir($dh);
  79. }
  80. protected function _copyDir($dir, $dest)
  81. {
  82. $dh = opendir($dir);
  83. while (($entry = readdir($dh)) !== false) {
  84. if ($entry == '.' || $entry == '..' || $entry == '.svn') {
  85. continue;
  86. }
  87. $fullname = $dir . DIRECTORY_SEPARATOR . $entry;
  88. $destname = $dest . DIRECTORY_SEPARATOR . $entry;
  89. if (is_dir($fullname)) {
  90. mkdir($destname);
  91. $this->_copyDir($fullname, $destname);
  92. } else {
  93. copy($fullname, $destname);
  94. }
  95. }
  96. closedir($dh);
  97. }
  98. public function testConnectOk()
  99. {
  100. try {
  101. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  102. } catch (Exception $e) {
  103. $this->fail('exception raised while loading connection to pop3 server');
  104. }
  105. }
  106. public function testConnectConfig()
  107. {
  108. try {
  109. $mail = new Zend_Mail_Storage_Pop3(new Zend_Config($this->_params));
  110. } catch (Exception $e) {
  111. $this->fail('exception raised while loading connection to pop3 server');
  112. }
  113. }
  114. public function testConnectFailure()
  115. {
  116. $this->_params['host'] = 'example.example';
  117. try {
  118. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  119. } catch (Exception $e) {
  120. return; // test ok
  121. }
  122. // I can only hope noone installs a POP3 server there
  123. $this->fail('no exception raised while connecting to example.example');
  124. }
  125. public function testNoParams()
  126. {
  127. try {
  128. $mail = new Zend_Mail_Storage_Pop3(array());
  129. } catch (Exception $e) {
  130. return; // test ok
  131. }
  132. $this->fail('no exception raised with empty params');
  133. }
  134. public function testConnectSSL()
  135. {
  136. if (!TESTS_ZEND_MAIL_POP3_SSL) {
  137. return;
  138. }
  139. $this->_params['ssl'] = 'SSL';
  140. try {
  141. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  142. } catch (Exception $e) {
  143. $this->fail('exception raised while loading connection to pop3 server with SSL');
  144. }
  145. }
  146. public function testConnectTLS()
  147. {
  148. if (!TESTS_ZEND_MAIL_POP3_TLS) {
  149. return;
  150. }
  151. $this->_params['ssl'] = 'TLS';
  152. try {
  153. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  154. } catch (Exception $e) {
  155. $this->fail('exception raised while loading connection to pop3 server with TLS');
  156. }
  157. }
  158. public function testInvalidService()
  159. {
  160. $this->_params['port'] = TESTS_ZEND_MAIL_POP3_INVALID_PORT;
  161. try {
  162. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  163. } catch (Exception $e) {
  164. return; // test ok
  165. }
  166. $this->fail('no exception while connection to invalid port');
  167. }
  168. public function testWrongService()
  169. {
  170. $this->_params['port'] = TESTS_ZEND_MAIL_POP3_WRONG_PORT;
  171. try {
  172. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  173. } catch (Exception $e) {
  174. return; // test ok
  175. }
  176. $this->fail('no exception while connection to wrong port');
  177. }
  178. public function testClose()
  179. {
  180. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  181. try {
  182. $mail->close();
  183. } catch (Exception $e) {
  184. $this->fail('exception raised while closing pop3 connection');
  185. }
  186. }
  187. public function testHasTop()
  188. {
  189. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  190. $this->assertTrue($mail->hasTop);
  191. }
  192. public function testHasCreate()
  193. {
  194. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  195. $this->assertFalse($mail->hasCreate);
  196. }
  197. public function testNoop()
  198. {
  199. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  200. try {
  201. $mail->noop();
  202. } catch (Exception $e) {
  203. $this->fail('exception raised while doing nothing (noop)');
  204. }
  205. }
  206. public function testCount()
  207. {
  208. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  209. $count = $mail->countMessages();
  210. $this->assertEquals(7, $count);
  211. }
  212. public function testSize()
  213. {
  214. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  215. $shouldSizes = array(1 => 397, 89, 694, 452, 497, 101, 139);
  216. $sizes = $mail->getSize();
  217. $this->assertEquals($shouldSizes, $sizes);
  218. }
  219. public function testSingleSize()
  220. {
  221. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  222. $size = $mail->getSize(2);
  223. $this->assertEquals(89, $size);
  224. }
  225. public function testFetchHeader()
  226. {
  227. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  228. $subject = $mail->getMessage(1)->subject;
  229. $this->assertEquals('Simple Message', $subject);
  230. }
  231. /*
  232. public function testFetchTopBody()
  233. {
  234. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  235. $content = $mail->getHeader(3, 1)->getContent();
  236. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  237. }
  238. */
  239. public function testFetchMessageHeader()
  240. {
  241. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  242. $subject = $mail->getMessage(1)->subject;
  243. $this->assertEquals('Simple Message', $subject);
  244. }
  245. public function testFetchMessageBody()
  246. {
  247. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  248. $content = $mail->getMessage(3)->getContent();
  249. list($content, ) = explode("\n", $content, 2);
  250. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  251. }
  252. /*
  253. public function testFailedRemove()
  254. {
  255. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  256. try {
  257. $mail->removeMessage(1);
  258. } catch (Exception $e) {
  259. return; // test ok
  260. }
  261. $this->fail('no exception raised while deleting message (mbox is read-only)');
  262. }
  263. */
  264. public function testWithInstanceConstruction()
  265. {
  266. $protocol = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  267. $mail = new Zend_Mail_Storage_Pop3($protocol);
  268. try {
  269. // because we did no login this has to throw an exception
  270. $mail->getMessage(1);
  271. } catch (Exception $e) {
  272. return; // test ok
  273. }
  274. $this->fail('no exception raised while fetching with wrong transport');
  275. }
  276. public function testRequestAfterClose()
  277. {
  278. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  279. $mail->close();
  280. try {
  281. $mail->getMessage(1);
  282. } catch (Exception $e) {
  283. return; // test ok
  284. }
  285. $this->fail('no exception raised while requesting after closing connection');
  286. }
  287. public function testServerCapa()
  288. {
  289. $mail = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  290. $this->assertTrue(is_array($mail->capa()));
  291. }
  292. public function testServerUidl()
  293. {
  294. $mail = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  295. $mail->login($this->_params['user'], $this->_params['password']);
  296. $uids = $mail->uniqueid();
  297. $this->assertEquals(count($uids), 7);
  298. $this->assertEquals($uids[1], $mail->uniqueid(1));
  299. }
  300. public function testRawHeader()
  301. {
  302. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  303. $this->assertTrue(strpos($mail->getRawHeader(1), "\r\nSubject: Simple Message\r\n") > 0);
  304. }
  305. public function testUniqueId()
  306. {
  307. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  308. $this->assertTrue($mail->hasUniqueId);
  309. $this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
  310. $ids = $mail->getUniqueId();
  311. foreach ($ids as $num => $id) {
  312. foreach ($ids as $inner_num => $inner_id) {
  313. if ($num == $inner_num) {
  314. continue;
  315. }
  316. if ($id == $inner_id) {
  317. $this->fail('not all ids are unique');
  318. }
  319. }
  320. if ($mail->getNumberByUniqueId($id) != $num) {
  321. $this->fail('reverse lookup failed');
  322. }
  323. }
  324. }
  325. public function testWrongUniqueId()
  326. {
  327. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  328. try {
  329. $mail->getNumberByUniqueId('this_is_an_invalid_id');
  330. } catch (Exception $e) {
  331. return; // test ok
  332. }
  333. $this->fail('no exception while getting number for invalid id');
  334. }
  335. public function testReadAfterClose()
  336. {
  337. $protocol = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  338. $protocol->logout();
  339. try {
  340. $protocol->readResponse();
  341. } catch (Exception $e) {
  342. return; // test ok
  343. }
  344. $this->fail('no exception while reading from closed socket');
  345. }
  346. public function testRemove()
  347. {
  348. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  349. $count = $mail->countMessages();
  350. $mail->removeMessage(1);
  351. $this->assertEquals($mail->countMessages(), --$count);
  352. unset($mail[2]);
  353. $this->assertEquals($mail->countMessages(), --$count);
  354. }
  355. public function testDotMessage()
  356. {
  357. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  358. $content = '';
  359. $content .= "Before the dot\r\n";
  360. $content .= ".\r\n";
  361. $content .= "is after the dot\r\n";
  362. $this->assertEquals($mail->getMessage(7)->getContent(), $content);
  363. }
  364. }