MimePartAcceptanceTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. require_once 'Swift/Mime/MimePart.php';
  3. require_once 'Swift/Mime/Headers/UnstructuredHeader.php';
  4. require_once 'Swift/Mime/Headers/ParameterizedHeader.php';
  5. require_once 'Swift/Encoder/Rfc2231Encoder.php';
  6. require_once 'Swift/Mime/ContentEncoder/QpContentEncoder.php';
  7. require_once 'Swift/Mime/HeaderEncoder/QpHeaderEncoder.php';
  8. require_once 'Swift/CharacterStream/ArrayCharacterStream.php';
  9. require_once 'Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php';
  10. require_once 'Swift/KeyCache/ArrayKeyCache.php';
  11. require_once 'Swift/KeyCache/SimpleKeyCacheInputStream.php';
  12. require_once 'Swift/Mime/SimpleHeaderSet.php';
  13. require_once 'Swift/Mime/SimpleHeaderFactory.php';
  14. require_once 'Swift/Mime/Grammar.php';
  15. class Swift_Mime_MimePartAcceptanceTest extends UnitTestCase
  16. {
  17. private $_contentEncoder;
  18. private $_cache;
  19. private $_grammar;
  20. private $_headers;
  21. public function setUp()
  22. {
  23. $this->_cache = new Swift_KeyCache_ArrayKeyCache(
  24. new Swift_KeyCache_SimpleKeyCacheInputStream()
  25. );
  26. $factory = new Swift_CharacterReaderFactory_SimpleCharacterReaderFactory();
  27. $this->_contentEncoder = new Swift_Mime_ContentEncoder_QpContentEncoder(
  28. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8'),
  29. new Swift_StreamFilters_ByteArrayReplacementFilter(
  30. array(array(0x0D, 0x0A), array(0x0D), array(0x0A)),
  31. array(array(0x0A), array(0x0A), array(0x0D, 0x0A))
  32. )
  33. );
  34. $headerEncoder = new Swift_Mime_HeaderEncoder_QpHeaderEncoder(
  35. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  36. );
  37. $paramEncoder = new Swift_Encoder_Rfc2231Encoder(
  38. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  39. );
  40. $this->_grammar = new Swift_Mime_Grammar();
  41. $this->_headers = new Swift_Mime_SimpleHeaderSet(
  42. new Swift_Mime_SimpleHeaderFactory($headerEncoder, $paramEncoder, $this->_grammar)
  43. );
  44. }
  45. public function testCharsetIsSetInHeader()
  46. {
  47. $part = $this->_createMimePart();
  48. $part->setContentType('text/plain');
  49. $part->setCharset('utf-8');
  50. $part->setBody('foobar');
  51. $this->assertEqual(
  52. 'Content-Type: text/plain; charset=utf-8' . "\r\n" .
  53. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  54. "\r\n" .
  55. 'foobar',
  56. $part->toString()
  57. );
  58. }
  59. public function testFormatIsSetInHeaders()
  60. {
  61. $part = $this->_createMimePart();
  62. $part->setContentType('text/plain');
  63. $part->setFormat('flowed');
  64. $part->setBody('> foobar');
  65. $this->assertEqual(
  66. 'Content-Type: text/plain; format=flowed' . "\r\n" .
  67. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  68. "\r\n" .
  69. '> foobar',
  70. $part->toString()
  71. );
  72. }
  73. public function testDelSpIsSetInHeaders()
  74. {
  75. $part = $this->_createMimePart();
  76. $part->setContentType('text/plain');
  77. $part->setDelSp(true);
  78. $part->setBody('foobar');
  79. $this->assertEqual(
  80. 'Content-Type: text/plain; delsp=yes' . "\r\n" .
  81. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  82. "\r\n" .
  83. 'foobar',
  84. $part->toString()
  85. );
  86. }
  87. public function testAll3ParamsInHeaders()
  88. {
  89. $part = $this->_createMimePart();
  90. $part->setContentType('text/plain');
  91. $part->setCharset('utf-8');
  92. $part->setFormat('fixed');
  93. $part->setDelSp(true);
  94. $part->setBody('foobar');
  95. $this->assertEqual(
  96. 'Content-Type: text/plain; charset=utf-8; format=fixed; delsp=yes' . "\r\n" .
  97. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  98. "\r\n" .
  99. 'foobar',
  100. $part->toString()
  101. );
  102. }
  103. public function testBodyIsCanonicalized()
  104. {
  105. $part = $this->_createMimePart();
  106. $part->setContentType('text/plain');
  107. $part->setCharset('utf-8');
  108. $part->setBody("foobar\r\rtest\ning\r");
  109. $this->assertEqual(
  110. 'Content-Type: text/plain; charset=utf-8' . "\r\n" .
  111. 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
  112. "\r\n" .
  113. "foobar\r\n" .
  114. "\r\n" .
  115. "test\r\n" .
  116. "ing\r\n",
  117. $part->toString()
  118. );
  119. }
  120. // -- Private helpers
  121. protected function _createMimePart()
  122. {
  123. $entity = new Swift_Mime_MimePart(
  124. $this->_headers,
  125. $this->_contentEncoder,
  126. $this->_cache,
  127. $this->_grammar
  128. );
  129. return $entity;
  130. }
  131. }