MessageTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_Mime
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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. /**
  23. * Zend_Mime_Message
  24. */
  25. require_once 'Zend/Mime/Message.php';
  26. /**
  27. * PHPUnit test case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Mime
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Mime
  37. */
  38. class Zend_Mime_MessageTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testMultiPart()
  41. {
  42. $msg = new Zend_Mime_Message(); // No Parts
  43. $this->assertFalse($msg->isMultiPart());
  44. }
  45. public function testSetGetParts()
  46. {
  47. $msg = new Zend_Mime_Message(); // No Parts
  48. $p = $msg->getParts();
  49. $this->assertTrue(is_array($p));
  50. $this->assertTrue(count($p) == 0);
  51. $p2 = array();
  52. $p2[] = new Zend_Mime_Part('This is a test');
  53. $p2[] = new Zend_Mime_Part('This is another test');
  54. $msg->setParts($p2);
  55. $p = $msg->getParts();
  56. $this->assertTrue(is_array($p));
  57. $this->assertTrue(count($p) == 2);
  58. }
  59. public function testGetMime()
  60. {
  61. $msg = new Zend_Mime_Message(); // No Parts
  62. $m = $msg->getMime();
  63. $this->assertType('Zend_Mime', $m);
  64. $msg = new Zend_Mime_Message(); // No Parts
  65. $mime = new Zend_Mime('1234');
  66. $msg->setMime($mime);
  67. $m2 = $msg->getMime();
  68. $this->assertType('Zend_Mime', $m2);
  69. $this->assertEquals('1234', $m2->boundary());
  70. }
  71. public function testGenerate()
  72. {
  73. $msg = new Zend_Mime_Message(); // No Parts
  74. $p1 = new Zend_Mime_Part('This is a test');
  75. $p2 = new Zend_Mime_Part('This is another test');
  76. $msg->addPart($p1);
  77. $msg->addPart($p2);
  78. $res = $msg->generateMessage();
  79. $mime = $msg->getMime();
  80. $boundary = $mime->boundary();
  81. $p1 = strpos($res, $boundary);
  82. // $boundary must appear once for every mime part
  83. $this->assertTrue($p1 !== false);
  84. if ($p1) {
  85. $p2 = strpos($res, $boundary, $p1 + strlen($boundary));
  86. $this->assertTrue($p2 !== false);
  87. }
  88. // check if the two test messages appear:
  89. $this->assertTrue(strpos($res, 'This is a test') !== false);
  90. $this->assertTrue(strpos($res, 'This is another test') !== false);
  91. // ... more in ZMailTest
  92. }
  93. /**
  94. * check if decoding a string into a Zend_Mime_Message object works
  95. *
  96. */
  97. public function testDecodeMimeMessage()
  98. {
  99. $text = <<<EOD
  100. This is a message in Mime Format. If you see this, your mail reader does not support this format.
  101. --=_af4357ef34b786aae1491b0a2d14399f
  102. Content-Type: application/octet-stream
  103. Content-Transfer-Encoding: 8bit
  104. This is a test
  105. --=_af4357ef34b786aae1491b0a2d14399f
  106. Content-Type: image/gif
  107. Content-Transfer-Encoding: base64
  108. Content-ID: <12>
  109. This is another test
  110. --=_af4357ef34b786aae1491b0a2d14399f--
  111. EOD;
  112. $res = Zend_Mime_Message::createFromMessage($text, '=_af4357ef34b786aae1491b0a2d14399f');
  113. $parts = $res->getParts();
  114. $this->assertEquals(2, count($parts));
  115. $part1 = $parts[0];
  116. $this->assertEquals('application/octet-stream', $part1->type);
  117. $this->assertEquals('8bit', $part1->encoding);
  118. $part2 = $parts[1];
  119. $this->assertEquals('image/gif', $part2->type);
  120. $this->assertEquals('base64', $part2->encoding);
  121. $this->assertEquals('12', $part2->id);
  122. }
  123. }