PageRenderTime 157ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Mail/Pop3Test.php

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