PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Mail/Storage/MboxTest.php

https://bitbucket.org/saifshuvo/zf2
PHP | 303 lines | 217 code | 61 blank | 25 comment | 15 complexity | 7eb9cb8e299cef60a4d4b44234cb23f6 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Mail
  9. */
  10. namespace ZendTest\Mail\Storage;
  11. use Zend\Config;
  12. use Zend\Mail\Storage;
  13. /**
  14. * @category Zend
  15. * @package Zend_Mail
  16. * @subpackage UnitTests
  17. * @group Zend_Mail
  18. */
  19. class MboxTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected $_mboxOriginalFile;
  22. protected $_mboxFile;
  23. protected $_tmpdir;
  24. public function setUp()
  25. {
  26. if ($this->_tmpdir == null) {
  27. if (TESTS_ZEND_MAIL_TEMPDIR != null) {
  28. $this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
  29. } else {
  30. $this->_tmpdir = __DIR__ . '/../_files/test.tmp/';
  31. }
  32. if (!file_exists($this->_tmpdir)) {
  33. mkdir($this->_tmpdir);
  34. }
  35. $count = 0;
  36. $dh = opendir($this->_tmpdir);
  37. while (readdir($dh) !== false) {
  38. ++$count;
  39. }
  40. closedir($dh);
  41. if ($count != 2) {
  42. $this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
  43. return;
  44. }
  45. }
  46. $this->_mboxOriginalFile = __DIR__ . '/../_files/test.mbox/INBOX';
  47. $this->_mboxFile = $this->_tmpdir . 'INBOX';
  48. copy($this->_mboxOriginalFile, $this->_mboxFile);
  49. }
  50. public function tearDown()
  51. {
  52. unlink($this->_mboxFile);
  53. }
  54. public function testLoadOk()
  55. {
  56. new Storage\Mbox(array('filename' => $this->_mboxFile));
  57. }
  58. public function testLoadConfig()
  59. {
  60. new Storage\Mbox(new Config\Config(array('filename' => $this->_mboxFile)));
  61. }
  62. public function testNoParams()
  63. {
  64. $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException');
  65. new Storage\Mbox(array());
  66. }
  67. public function testLoadFailure()
  68. {
  69. $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException');
  70. new Storage\Mbox(array('filename' => 'ThisFileDoesNotExist'));
  71. }
  72. public function testLoadInvalid()
  73. {
  74. $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException');
  75. new Storage\Mbox(array('filename' => __FILE__));
  76. }
  77. public function testClose()
  78. {
  79. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  80. $mail->close();
  81. }
  82. public function testHasTop()
  83. {
  84. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  85. $this->assertTrue($mail->hasTop);
  86. }
  87. public function testHasCreate()
  88. {
  89. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  90. $this->assertFalse($mail->hasCreate);
  91. }
  92. public function testNoop()
  93. {
  94. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  95. $mail->noop();
  96. }
  97. public function testCount()
  98. {
  99. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  100. $count = $mail->countMessages();
  101. $this->assertEquals(7, $count);
  102. }
  103. public function testSize()
  104. {
  105. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  106. $shouldSizes = array(1 => 397, 89, 694, 452, 497, 101, 139);
  107. $sizes = $mail->getSize();
  108. $this->assertEquals($shouldSizes, $sizes);
  109. }
  110. public function testSingleSize()
  111. {
  112. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  113. $size = $mail->getSize(2);
  114. $this->assertEquals(89, $size);
  115. }
  116. public function testFetchHeader()
  117. {
  118. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  119. $subject = $mail->getMessage(1)->subject;
  120. $this->assertEquals('Simple Message', $subject);
  121. }
  122. /*
  123. public function testFetchTopBody()
  124. {
  125. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  126. $content = $mail->getHeader(3, 1)->getContent();
  127. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  128. }
  129. */
  130. public function testFetchMessageHeader()
  131. {
  132. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  133. $subject = $mail->getMessage(1)->subject;
  134. $this->assertEquals('Simple Message', $subject);
  135. }
  136. public function testFetchMessageBody()
  137. {
  138. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  139. $content = $mail->getMessage(3)->getContent();
  140. list($content, ) = explode("\n", $content, 2);
  141. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  142. }
  143. public function testFailedRemove()
  144. {
  145. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  146. $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException');
  147. $mail->removeMessage(1);
  148. }
  149. public function testCapa()
  150. {
  151. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  152. $capa = $mail->getCapabilities();
  153. $this->assertTrue(isset($capa['uniqueid']));
  154. }
  155. public function testValid()
  156. {
  157. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  158. $this->assertFalse($mail->valid());
  159. $mail->rewind();
  160. $this->assertTrue($mail->valid());
  161. }
  162. public function testOutOfBounds()
  163. {
  164. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  165. $this->setExpectedException('Zend\Mail\Storage\Exception\OutOfBoundsException');
  166. $mail->seek(INF);
  167. }
  168. public function testSleepWake()
  169. {
  170. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  171. $count = $mail->countMessages();
  172. $content = $mail->getMessage(1)->getContent();
  173. $serialzed = serialize($mail);
  174. $mail = null;
  175. unlink($this->_mboxFile);
  176. // otherwise this test is to fast for a mtime change
  177. sleep(2);
  178. copy($this->_mboxOriginalFile, $this->_mboxFile);
  179. $mail = unserialize($serialzed);
  180. $this->assertEquals($mail->countMessages(), $count);
  181. $this->assertEquals($mail->getMessage(1)->getContent(), $content);
  182. }
  183. public function testSleepWakeRemoved()
  184. {
  185. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  186. $count = $mail->countMessages();
  187. $content = $mail->getMessage(1)->getContent();
  188. $serialzed = serialize($mail);
  189. $mail = null;
  190. $stat = stat($this->_mboxFile);
  191. chmod($this->_mboxFile, 0);
  192. clearstatcache();
  193. $statcheck = stat($this->_mboxFile);
  194. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  195. chmod($this->_mboxFile, $stat['mode']);
  196. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  197. return;
  198. }
  199. $check = false;
  200. try {
  201. $mail = unserialize($serialzed);
  202. } catch (\Exception $e) {
  203. $check = true;
  204. // test ok
  205. }
  206. chmod($this->_mboxFile, $stat['mode']);
  207. if (!$check) {
  208. if (function_exists('posix_getuid') && posix_getuid() === 0) {
  209. $this->markTestSkipped('seems like you are root and we therefore cannot test the error handling');
  210. } elseif (!function_exists('posix_getuid')) {
  211. $this->markTestSkipped('Can\t test if you\'re root and we therefore cannot test the error handling');
  212. }
  213. $this->fail('no exception while waking with non readable file');
  214. }
  215. }
  216. public function testUniqueId()
  217. {
  218. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  219. $this->assertFalse($mail->hasUniqueId);
  220. $this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
  221. $ids = $mail->getUniqueId();
  222. foreach ($ids as $num => $id) {
  223. $this->assertEquals($num, $id);
  224. if ($mail->getNumberByUniqueId($id) != $num) {
  225. $this->fail('reverse lookup failed');
  226. }
  227. }
  228. }
  229. public function testShortMbox()
  230. {
  231. $fh = fopen($this->_mboxFile, 'w');
  232. fputs($fh, "From \r\nSubject: test\r\nFrom \r\nSubject: test2\r\n");
  233. fclose($fh);
  234. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  235. $this->assertEquals($mail->countMessages(), 2);
  236. $this->assertEquals($mail->getMessage(1)->subject, 'test');
  237. $this->assertEquals($mail->getMessage(1)->getContent(), '');
  238. $this->assertEquals($mail->getMessage(2)->subject, 'test2');
  239. $this->assertEquals($mail->getMessage(2)->getContent(), '');
  240. }
  241. }