AttachmentAcceptanceTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. use Egulias\EmailValidator\EmailValidator;
  3. class Swift_Mime_AttachmentAcceptanceTest 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_Base64ContentEncoder();
  16. $headerEncoder = new Swift_Mime_HeaderEncoder_QpHeaderEncoder(
  17. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  18. );
  19. $paramEncoder = new Swift_Encoder_Rfc2231Encoder(
  20. new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8')
  21. );
  22. $this->emailValidator = new EmailValidator();
  23. $this->idGenerator = new Swift_Mime_IdGenerator('example.com');
  24. $this->headers = new Swift_Mime_SimpleHeaderSet(
  25. new Swift_Mime_SimpleHeaderFactory($headerEncoder, $paramEncoder, $this->emailValidator)
  26. );
  27. }
  28. public function testDispositionIsSetInHeader()
  29. {
  30. $attachment = $this->createAttachment();
  31. $attachment->setContentType('application/pdf');
  32. $attachment->setDisposition('inline');
  33. $this->assertEquals(
  34. 'Content-Type: application/pdf'."\r\n".
  35. 'Content-Transfer-Encoding: base64'."\r\n".
  36. 'Content-Disposition: inline'."\r\n",
  37. $attachment->toString()
  38. );
  39. }
  40. public function testDispositionIsAttachmentByDefault()
  41. {
  42. $attachment = $this->createAttachment();
  43. $attachment->setContentType('application/pdf');
  44. $this->assertEquals(
  45. 'Content-Type: application/pdf'."\r\n".
  46. 'Content-Transfer-Encoding: base64'."\r\n".
  47. 'Content-Disposition: attachment'."\r\n",
  48. $attachment->toString()
  49. );
  50. }
  51. public function testFilenameIsSetInHeader()
  52. {
  53. $attachment = $this->createAttachment();
  54. $attachment->setContentType('application/pdf');
  55. $attachment->setFilename('foo.pdf');
  56. $this->assertEquals(
  57. 'Content-Type: application/pdf; name=foo.pdf'."\r\n".
  58. 'Content-Transfer-Encoding: base64'."\r\n".
  59. 'Content-Disposition: attachment; filename=foo.pdf'."\r\n",
  60. $attachment->toString()
  61. );
  62. }
  63. public function testSizeIsSetInHeader()
  64. {
  65. $attachment = $this->createAttachment();
  66. $attachment->setContentType('application/pdf');
  67. $attachment->setSize(12340);
  68. $this->assertEquals(
  69. 'Content-Type: application/pdf'."\r\n".
  70. 'Content-Transfer-Encoding: base64'."\r\n".
  71. 'Content-Disposition: attachment; size=12340'."\r\n",
  72. $attachment->toString()
  73. );
  74. }
  75. public function testMultipleParametersInHeader()
  76. {
  77. $attachment = $this->createAttachment();
  78. $attachment->setContentType('application/pdf');
  79. $attachment->setFilename('foo.pdf');
  80. $attachment->setSize(12340);
  81. $this->assertEquals(
  82. 'Content-Type: application/pdf; name=foo.pdf'."\r\n".
  83. 'Content-Transfer-Encoding: base64'."\r\n".
  84. 'Content-Disposition: attachment; filename=foo.pdf; size=12340'."\r\n",
  85. $attachment->toString()
  86. );
  87. }
  88. public function testEndToEnd()
  89. {
  90. $attachment = $this->createAttachment();
  91. $attachment->setContentType('application/pdf');
  92. $attachment->setFilename('foo.pdf');
  93. $attachment->setSize(12340);
  94. $attachment->setBody('abcd');
  95. $this->assertEquals(
  96. 'Content-Type: application/pdf; name=foo.pdf'."\r\n".
  97. 'Content-Transfer-Encoding: base64'."\r\n".
  98. 'Content-Disposition: attachment; filename=foo.pdf; size=12340'."\r\n".
  99. "\r\n".
  100. base64_encode('abcd'),
  101. $attachment->toString()
  102. );
  103. }
  104. protected function createAttachment()
  105. {
  106. $entity = new Swift_Mime_Attachment(
  107. $this->headers,
  108. $this->contentEncoder,
  109. $this->cache,
  110. $this->idGenerator
  111. );
  112. return $entity;
  113. }
  114. }