PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/zendframework/zf2
PHP | 338 lines | 240 code | 68 blank | 30 comment | 16 complexity | 3443d2e71f52bb801bf520f1be3595b7 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Mail\Storage;
  10. use Zend\Config;
  11. use Zend\Mail\Storage;
  12. /**
  13. * @group Zend_Mail
  14. */
  15. class MboxTest extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $_mboxOriginalFile;
  18. protected $_mboxFile;
  19. protected $_mboxFileUnix;
  20. protected $_tmpdir;
  21. public function setUp()
  22. {
  23. if ($this->_tmpdir == null) {
  24. if (TESTS_ZEND_MAIL_TEMPDIR != null) {
  25. $this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
  26. } else {
  27. $this->_tmpdir = __DIR__ . '/../_files/test.tmp/';
  28. }
  29. if (!file_exists($this->_tmpdir)) {
  30. mkdir($this->_tmpdir);
  31. }
  32. $count = 0;
  33. $dh = opendir($this->_tmpdir);
  34. while (readdir($dh) !== false) {
  35. ++$count;
  36. }
  37. closedir($dh);
  38. if ($count != 2) {
  39. $this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
  40. return;
  41. }
  42. }
  43. $this->_mboxOriginalFile = __DIR__ . '/../_files/test.mbox/INBOX';
  44. $this->_mboxFile = $this->_tmpdir . 'INBOX';
  45. copy($this->_mboxOriginalFile, $this->_mboxFile);
  46. }
  47. public function tearDown()
  48. {
  49. unlink($this->_mboxFile);
  50. if ($this->_mboxFileUnix) {
  51. unlink($this->_mboxFileUnix);
  52. }
  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. /**
  131. * @group 6775
  132. */
  133. public function testFetchMessageHeaderUnix()
  134. {
  135. $mail = new Storage\Mbox(array('filename' => $this->getUnixMboxFile(), 'messageEOL' => "\n"));
  136. $subject = $mail->getMessage(1)->subject;
  137. $this->assertEquals('Simple Message', $subject);
  138. }
  139. public function testFetchMessageHeader()
  140. {
  141. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  142. $subject = $mail->getMessage(1)->subject;
  143. $this->assertEquals('Simple Message', $subject);
  144. }
  145. public function testFetchMessageBody()
  146. {
  147. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  148. $content = $mail->getMessage(3)->getContent();
  149. list($content) = explode("\n", $content, 2);
  150. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  151. }
  152. /**
  153. * @group 6775
  154. */
  155. public function testFetchMessageBodyUnix()
  156. {
  157. $mail = new Storage\Mbox(array('filename' => $this->getUnixMboxFile(), 'messageEOL' => "\n"));
  158. $content = $mail->getMessage(3)->getContent();
  159. list($content) = explode("\n", $content, 2);
  160. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  161. }
  162. public function testFailedRemove()
  163. {
  164. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  165. $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException');
  166. $mail->removeMessage(1);
  167. }
  168. public function testCapa()
  169. {
  170. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  171. $capa = $mail->getCapabilities();
  172. $this->assertTrue(isset($capa['uniqueid']));
  173. }
  174. public function testValid()
  175. {
  176. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  177. $this->assertFalse($mail->valid());
  178. $mail->rewind();
  179. $this->assertTrue($mail->valid());
  180. }
  181. public function testOutOfBounds()
  182. {
  183. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  184. $this->setExpectedException('Zend\Mail\Storage\Exception\OutOfBoundsException');
  185. $mail->seek(INF);
  186. }
  187. public function testSleepWake()
  188. {
  189. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  190. $count = $mail->countMessages();
  191. $content = $mail->getMessage(1)->getContent();
  192. $serialzed = serialize($mail);
  193. $mail = null;
  194. unlink($this->_mboxFile);
  195. // otherwise this test is to fast for a mtime change
  196. sleep(2);
  197. copy($this->_mboxOriginalFile, $this->_mboxFile);
  198. $mail = unserialize($serialzed);
  199. $this->assertEquals($mail->countMessages(), $count);
  200. $this->assertEquals($mail->getMessage(1)->getContent(), $content);
  201. }
  202. public function testSleepWakeRemoved()
  203. {
  204. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  205. $count = $mail->countMessages();
  206. $content = $mail->getMessage(1)->getContent();
  207. $serialzed = serialize($mail);
  208. $mail = null;
  209. $stat = stat($this->_mboxFile);
  210. chmod($this->_mboxFile, 0);
  211. clearstatcache();
  212. $statcheck = stat($this->_mboxFile);
  213. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  214. chmod($this->_mboxFile, $stat['mode']);
  215. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  216. return;
  217. }
  218. $check = false;
  219. try {
  220. $mail = unserialize($serialzed);
  221. } catch (\Exception $e) {
  222. $check = true;
  223. // test ok
  224. }
  225. chmod($this->_mboxFile, $stat['mode']);
  226. if (!$check) {
  227. if (function_exists('posix_getuid') && posix_getuid() === 0) {
  228. $this->markTestSkipped('seems like you are root and we therefore cannot test the error handling');
  229. } elseif (!function_exists('posix_getuid')) {
  230. $this->markTestSkipped('Can\t test if you\'re root and we therefore cannot test the error handling');
  231. }
  232. $this->fail('no exception while waking with non readable file');
  233. }
  234. }
  235. public function testUniqueId()
  236. {
  237. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  238. $this->assertFalse($mail->hasUniqueId);
  239. $this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
  240. $ids = $mail->getUniqueId();
  241. foreach ($ids as $num => $id) {
  242. $this->assertEquals($num, $id);
  243. if ($mail->getNumberByUniqueId($id) != $num) {
  244. $this->fail('reverse lookup failed');
  245. }
  246. }
  247. }
  248. public function testShortMbox()
  249. {
  250. $fh = fopen($this->_mboxFile, 'w');
  251. fputs($fh, "From \r\nSubject: test\r\nFrom \r\nSubject: test2\r\n");
  252. fclose($fh);
  253. $mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
  254. $this->assertEquals($mail->countMessages(), 2);
  255. $this->assertEquals($mail->getMessage(1)->subject, 'test');
  256. $this->assertEquals($mail->getMessage(1)->getContent(), '');
  257. $this->assertEquals($mail->getMessage(2)->subject, 'test2');
  258. $this->assertEquals($mail->getMessage(2)->getContent(), '');
  259. }
  260. /**
  261. * @return string
  262. */
  263. private function getUnixMboxFile()
  264. {
  265. $this->_mboxFileUnix = $this->_tmpdir . 'INBOX.unix';
  266. copy(__DIR__ . '/../_files/test.mbox/INBOX.unix', $this->_mboxFileUnix);
  267. return $this->_mboxFileUnix;
  268. }
  269. }