PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/bug/Swift/Bug38Test.php

http://github.com/swiftmailer/swiftmailer
PHP | 192 lines | 165 code | 27 blank | 0 comment | 1 complexity | a7a5b934f6789a193a5a8e510c26a104 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. class Swift_Bug38Test extends \PHPUnit\Framework\TestCase
  3. {
  4. private $attFile;
  5. private $attFileName;
  6. private $attFileType;
  7. protected function setUp(): void
  8. {
  9. $this->attFileName = 'data.txt';
  10. $this->attFileType = 'text/plain';
  11. $this->attFile = __DIR__.'/../../_samples/files/data.txt';
  12. Swift_Preferences::getInstance()->setCharset('utf-8');
  13. }
  14. public function testWritingMessageToByteStreamProducesCorrectStructure()
  15. {
  16. $message = new Swift_Message();
  17. $message->setSubject('test subject');
  18. $message->setTo('user@domain.tld');
  19. $message->setCc('other@domain.tld');
  20. $message->setFrom('user@domain.tld');
  21. $image = new Swift_Image('<data>', 'image.gif', 'image/gif');
  22. $cid = $message->embed($image);
  23. $message->setBody('HTML part', 'text/html');
  24. $id = $message->getId();
  25. $date = preg_quote($message->getDate()->format('r'), '~');
  26. $boundary = $message->getBoundary();
  27. $imgId = $image->getId();
  28. $stream = new Swift_ByteStream_ArrayByteStream();
  29. $message->toByteStream($stream);
  30. $this->assertPatternInStream(
  31. '~^'.
  32. 'Message-ID: <'.$id.'>'."\r\n".
  33. 'Date: '.$date."\r\n".
  34. 'Subject: test subject'."\r\n".
  35. 'From: user@domain.tld'."\r\n".
  36. 'To: user@domain.tld'."\r\n".
  37. 'Cc: other@domain.tld'."\r\n".
  38. 'MIME-Version: 1.0'."\r\n".
  39. 'Content-Type: multipart/related;'."\r\n".
  40. ' boundary="'.$boundary.'"'."\r\n".
  41. "\r\n\r\n".
  42. '--'.$boundary."\r\n".
  43. 'Content-Type: text/html; charset=utf-8'."\r\n".
  44. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  45. "\r\n".
  46. 'HTML part'.
  47. "\r\n\r\n".
  48. '--'.$boundary."\r\n".
  49. 'Content-Type: image/gif; name=image.gif'."\r\n".
  50. 'Content-Transfer-Encoding: base64'."\r\n".
  51. 'Content-ID: <'.preg_quote($imgId, '~').'>'."\r\n".
  52. 'Content-Disposition: inline; filename=image.gif'."\r\n".
  53. "\r\n".
  54. preg_quote(base64_encode('<data>'), '~').
  55. "\r\n\r\n".
  56. '--'.$boundary.'--'."\r\n".
  57. '$~D',
  58. $stream
  59. );
  60. }
  61. public function testWritingMessageToByteStreamTwiceProducesCorrectStructure()
  62. {
  63. $message = new Swift_Message();
  64. $message->setSubject('test subject');
  65. $message->setTo('user@domain.tld');
  66. $message->setCc('other@domain.tld');
  67. $message->setFrom('user@domain.tld');
  68. $image = new Swift_Image('<data>', 'image.gif', 'image/gif');
  69. $cid = $message->embed($image);
  70. $message->setBody('HTML part', 'text/html');
  71. $id = $message->getId();
  72. $date = preg_quote($message->getDate()->format('r'), '~');
  73. $boundary = $message->getBoundary();
  74. $imgId = $image->getId();
  75. $pattern = '~^'.
  76. 'Message-ID: <'.$id.'>'."\r\n".
  77. 'Date: '.$date."\r\n".
  78. 'Subject: test subject'."\r\n".
  79. 'From: user@domain.tld'."\r\n".
  80. 'To: user@domain.tld'."\r\n".
  81. 'Cc: other@domain.tld'."\r\n".
  82. 'MIME-Version: 1.0'."\r\n".
  83. 'Content-Type: multipart/related;'."\r\n".
  84. ' boundary="'.$boundary.'"'."\r\n".
  85. "\r\n\r\n".
  86. '--'.$boundary."\r\n".
  87. 'Content-Type: text/html; charset=utf-8'."\r\n".
  88. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  89. "\r\n".
  90. 'HTML part'.
  91. "\r\n\r\n".
  92. '--'.$boundary."\r\n".
  93. 'Content-Type: image/gif; name=image.gif'."\r\n".
  94. 'Content-Transfer-Encoding: base64'."\r\n".
  95. 'Content-ID: <'.preg_quote($imgId, '~').'>'."\r\n".
  96. 'Content-Disposition: inline; filename=image.gif'."\r\n".
  97. "\r\n".
  98. preg_quote(base64_encode('<data>'), '~').
  99. "\r\n\r\n".
  100. '--'.$boundary.'--'."\r\n".
  101. '$~D'
  102. ;
  103. $streamA = new Swift_ByteStream_ArrayByteStream();
  104. $streamB = new Swift_ByteStream_ArrayByteStream();
  105. $message->toByteStream($streamA);
  106. $message->toByteStream($streamB);
  107. $this->assertPatternInStream($pattern, $streamA);
  108. $this->assertPatternInStream($pattern, $streamB);
  109. }
  110. public function testWritingMessageToByteStreamTwiceUsingAFileAttachment()
  111. {
  112. $message = new Swift_Message();
  113. $message->setSubject('test subject');
  114. $message->setTo('user@domain.tld');
  115. $message->setCc('other@domain.tld');
  116. $message->setFrom('user@domain.tld');
  117. $attachment = Swift_Attachment::fromPath($this->attFile);
  118. $message->attach($attachment);
  119. $message->setBody('HTML part', 'text/html');
  120. $id = $message->getId();
  121. $date = preg_quote($message->getDate()->format('r'), '~');
  122. $boundary = $message->getBoundary();
  123. $streamA = new Swift_ByteStream_ArrayByteStream();
  124. $streamB = new Swift_ByteStream_ArrayByteStream();
  125. $pattern = '~^'.
  126. 'Message-ID: <'.$id.'>'."\r\n".
  127. 'Date: '.$date."\r\n".
  128. 'Subject: test subject'."\r\n".
  129. 'From: user@domain.tld'."\r\n".
  130. 'To: user@domain.tld'."\r\n".
  131. 'Cc: other@domain.tld'."\r\n".
  132. 'MIME-Version: 1.0'."\r\n".
  133. 'Content-Type: multipart/mixed;'."\r\n".
  134. ' boundary="'.$boundary.'"'."\r\n".
  135. "\r\n\r\n".
  136. '--'.$boundary."\r\n".
  137. 'Content-Type: text/html; charset=utf-8'."\r\n".
  138. 'Content-Transfer-Encoding: quoted-printable'."\r\n".
  139. "\r\n".
  140. 'HTML part'.
  141. "\r\n\r\n".
  142. '--'.$boundary."\r\n".
  143. 'Content-Type: '.$this->attFileType.'; name='.$this->attFileName."\r\n".
  144. 'Content-Transfer-Encoding: base64'."\r\n".
  145. 'Content-Disposition: attachment; filename='.$this->attFileName."\r\n".
  146. "\r\n".
  147. preg_quote(base64_encode(file_get_contents($this->attFile)), '~').
  148. "\r\n\r\n".
  149. '--'.$boundary.'--'."\r\n".
  150. '$~D'
  151. ;
  152. $message->toByteStream($streamA);
  153. $message->toByteStream($streamB);
  154. $this->assertPatternInStream($pattern, $streamA);
  155. $this->assertPatternInStream($pattern, $streamB);
  156. }
  157. public function assertPatternInStream($pattern, $stream, $message = '%s')
  158. {
  159. $string = '';
  160. while (false !== $bytes = $stream->read(8192)) {
  161. $string .= $bytes;
  162. }
  163. $this->assertRegExp($pattern, $string, $message);
  164. }
  165. }