MessageTest.php 4.1 KB

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