AttachmentAcceptanceTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. require_once 'Swift/Mime/Attachment.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/Base64ContentEncoder.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/Grammar.php';
  13. class Swift_Mime_AttachmentAcceptanceTest extends UnitTestCase
  14. {
  15. private $_contentEncoder;
  16. private $_cache;
  17. private $_grammar;
  18. private $_headers;
  19. public function setUp()
  20. {
  21. $this->_cache = new Swift_KeyCache_ArrayKeyCache(
  22. new Swift_KeyCache_SimpleKeyCacheInputStream()
  23. );
  24. $factory = new Swift_CharacterReaderFactory_SimpleCharacterReaderFactory();
  25. $this->_contentEncoder = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
  26. $headerEncoder = new Swift_Mime_HeaderEncoder_QpHeaderEncoder(
  27. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  28. );
  29. $paramEncoder = new Swift_Encoder_Rfc2231Encoder(
  30. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  31. );
  32. $this->_grammar = new Swift_Mime_Grammar();
  33. $this->_headers = new Swift_Mime_SimpleHeaderSet(
  34. new Swift_Mime_SimpleHeaderFactory($headerEncoder, $paramEncoder, $this->_grammar)
  35. );
  36. }
  37. public function testDispositionIsSetInHeader()
  38. {
  39. $attachment = $this->_createAttachment();
  40. $attachment->setContentType('application/pdf');
  41. $attachment->setDisposition('inline');
  42. $this->assertEqual(
  43. 'Content-Type: application/pdf' . "\r\n" .
  44. 'Content-Transfer-Encoding: base64' . "\r\n" .
  45. 'Content-Disposition: inline' . "\r\n",
  46. $attachment->toString()
  47. );
  48. }
  49. public function testDispositionIsAttachmentByDefault()
  50. {
  51. $attachment = $this->_createAttachment();
  52. $attachment->setContentType('application/pdf');
  53. $this->assertEqual(
  54. 'Content-Type: application/pdf' . "\r\n" .
  55. 'Content-Transfer-Encoding: base64' . "\r\n" .
  56. 'Content-Disposition: attachment' . "\r\n",
  57. $attachment->toString()
  58. );
  59. }
  60. public function testFilenameIsSetInHeader()
  61. {
  62. $attachment = $this->_createAttachment();
  63. $attachment->setContentType('application/pdf');
  64. $attachment->setFilename('foo.pdf');
  65. $this->assertEqual(
  66. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  67. 'Content-Transfer-Encoding: base64' . "\r\n" .
  68. 'Content-Disposition: attachment; filename=foo.pdf' . "\r\n",
  69. $attachment->toString()
  70. );
  71. }
  72. public function testSizeIsSetInHeader()
  73. {
  74. $attachment = $this->_createAttachment();
  75. $attachment->setContentType('application/pdf');
  76. $attachment->setSize(12340);
  77. $this->assertEqual(
  78. 'Content-Type: application/pdf' . "\r\n" .
  79. 'Content-Transfer-Encoding: base64' . "\r\n" .
  80. 'Content-Disposition: attachment; size=12340' . "\r\n",
  81. $attachment->toString()
  82. );
  83. }
  84. public function testMultipleParametersInHeader()
  85. {
  86. $attachment = $this->_createAttachment();
  87. $attachment->setContentType('application/pdf');
  88. $attachment->setFilename('foo.pdf');
  89. $attachment->setSize(12340);
  90. $this->assertEqual(
  91. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  92. 'Content-Transfer-Encoding: base64' . "\r\n" .
  93. 'Content-Disposition: attachment; filename=foo.pdf; size=12340' . "\r\n",
  94. $attachment->toString()
  95. );
  96. }
  97. public function testEndToEnd()
  98. {
  99. $attachment = $this->_createAttachment();
  100. $attachment->setContentType('application/pdf');
  101. $attachment->setFilename('foo.pdf');
  102. $attachment->setSize(12340);
  103. $attachment->setBody('abcd');
  104. $this->assertEqual(
  105. 'Content-Type: application/pdf; name=foo.pdf' . "\r\n" .
  106. 'Content-Transfer-Encoding: base64' . "\r\n" .
  107. 'Content-Disposition: attachment; filename=foo.pdf; size=12340' . "\r\n" .
  108. "\r\n" .
  109. base64_encode('abcd'),
  110. $attachment->toString()
  111. );
  112. }
  113. // -- Private helpers
  114. protected function _createAttachment()
  115. {
  116. $entity = new Swift_Mime_Attachment(
  117. $this->_headers,
  118. $this->_contentEncoder,
  119. $this->_cache,
  120. $this->_grammar
  121. );
  122. return $entity;
  123. }
  124. }