MimePartAcceptanceTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. use Egulias\EmailValidator\EmailValidator;
  3. class Swift_Mime_MimePartAcceptanceTest extends \PHPUnit\Framework\TestCase
  4. {
  5. private $contentEncoder;
  6. private $cache;
  7. private $headers;
  8. private $emailValidator;
  9. protected function setUp()
  10. {
  11. $this->cache = new Swift_KeyCache_ArrayKeyCache(
  12. new Swift_KeyCache_SimpleKeyCacheInputStream()
  13. );
  14. $factory = new Swift_CharacterReaderFactory_SimpleCharacterReaderFactory();
  15. $this->contentEncoder = new Swift_Mime_ContentEncoder_QpContentEncoder(
  16. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8'),
  17. new Swift_StreamFilters_ByteArrayReplacementFilter(
  18. [[0x0D, 0x0A], [0x0D], [0x0A]],
  19. [[0x0A], [0x0A], [0x0D, 0x0A]]
  20. )
  21. );
  22. $headerEncoder = new Swift_Mime_HeaderEncoder_QpHeaderEncoder(
  23. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  24. );
  25. $paramEncoder = new Swift_Encoder_Rfc2231Encoder(
  26. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  27. );
  28. $this->emailValidator = new EmailValidator();
  29. $this->idGenerator = new Swift_Mime_IdGenerator('example.com');
  30. $this->headers = new Swift_Mime_SimpleHeaderSet(
  31. new Swift_Mime_SimpleHeaderFactory($headerEncoder, $paramEncoder, $this->emailValidator)
  32. );
  33. }
  34. public function testCharsetIsSetInHeader()
  35. {
  36. $part = $this->createMimePart();
  37. $part->setContentType('text/plain');
  38. $part->setCharset('utf-8');
  39. $part->setBody('foobar');
  40. $this->assertEquals(
  41. 'Content-Type: text/plain; charset=utf-8'."\r\n".
  42. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  43. "\r\n".
  44. 'foobar',
  45. $part->toString()
  46. );
  47. }
  48. public function testFormatIsSetInHeaders()
  49. {
  50. $part = $this->createMimePart();
  51. $part->setContentType('text/plain');
  52. $part->setFormat('flowed');
  53. $part->setBody('> foobar');
  54. $this->assertEquals(
  55. 'Content-Type: text/plain; format=flowed'."\r\n".
  56. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  57. "\r\n".
  58. '> foobar',
  59. $part->toString()
  60. );
  61. }
  62. public function testDelSpIsSetInHeaders()
  63. {
  64. $part = $this->createMimePart();
  65. $part->setContentType('text/plain');
  66. $part->setDelSp(true);
  67. $part->setBody('foobar');
  68. $this->assertEquals(
  69. 'Content-Type: text/plain; delsp=yes'."\r\n".
  70. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  71. "\r\n".
  72. 'foobar',
  73. $part->toString()
  74. );
  75. }
  76. public function testAll3ParamsInHeaders()
  77. {
  78. $part = $this->createMimePart();
  79. $part->setContentType('text/plain');
  80. $part->setCharset('utf-8');
  81. $part->setFormat('fixed');
  82. $part->setDelSp(true);
  83. $part->setBody('foobar');
  84. $this->assertEquals(
  85. 'Content-Type: text/plain; charset=utf-8; format=fixed; delsp=yes'."\r\n".
  86. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  87. "\r\n".
  88. 'foobar',
  89. $part->toString()
  90. );
  91. }
  92. public function testBodyIsCanonicalized()
  93. {
  94. $part = $this->createMimePart();
  95. $part->setContentType('text/plain');
  96. $part->setCharset('utf-8');
  97. $part->setBody("foobar\r\rtest\ning\r");
  98. $this->assertEquals(
  99. 'Content-Type: text/plain; charset=utf-8'."\r\n".
  100. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  101. "\r\n".
  102. "foobar\r\n".
  103. "\r\n".
  104. "test\r\n".
  105. "ing\r\n",
  106. $part->toString()
  107. );
  108. }
  109. protected function createMimePart()
  110. {
  111. $entity = new Swift_Mime_MimePart(
  112. $this->headers,
  113. $this->contentEncoder,
  114. $this->cache,
  115. $this->idGenerator
  116. );
  117. return $entity;
  118. }
  119. }