AttachmentTest.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. require_once 'Swift/Mime/MimeEntity.php';
  3. require_once 'Swift/Mime/Attachment.php';
  4. require_once 'Swift/Mime/AbstractMimeEntityTest.php';
  5. require_once 'Swift/FileStream.php';
  6. require_once 'Swift/Mime/Grammar.php';
  7. class Swift_Mime_AttachmentTest extends Swift_Mime_AbstractMimeEntityTest
  8. {
  9. public function testNestingLevelIsAttachment()
  10. {
  11. $attachment = $this->_createAttachment($this->_createHeaderSet(),
  12. $this->_createEncoder(), $this->_createCache()
  13. );
  14. $this->assertEqual(
  15. Swift_Mime_MimeEntity::LEVEL_MIXED, $attachment->getNestingLevel()
  16. );
  17. }
  18. public function testDispositionIsReturnedFromHeader()
  19. {
  20. /* -- RFC 2183, 2.1, 2.2.
  21. */
  22. $disposition = $this->_createHeader('Content-Disposition', 'attachment');
  23. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  24. 'Content-Disposition' => $disposition)),
  25. $this->_createEncoder(), $this->_createCache()
  26. );
  27. $this->assertEqual('attachment', $attachment->getDisposition());
  28. }
  29. public function testDispositionIsSetInHeader()
  30. {
  31. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  32. array(), false
  33. );
  34. $this->_checking(Expectations::create()
  35. -> one($disposition)->setFieldBodyModel('inline')
  36. -> ignoring($disposition)
  37. );
  38. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  39. 'Content-Disposition' => $disposition)),
  40. $this->_createEncoder(), $this->_createCache()
  41. );
  42. $attachment->setDisposition('inline');
  43. }
  44. public function testDispositionIsAddedIfNonePresent()
  45. {
  46. $headers = $this->_createHeaderSet(array(), false);
  47. $this->_checking(Expectations::create()
  48. -> one($headers)->addParameterizedHeader('Content-Disposition', 'inline')
  49. -> ignoring($headers)
  50. );
  51. $attachment = $this->_createAttachment($headers, $this->_createEncoder(),
  52. $this->_createCache()
  53. );
  54. $attachment->setDisposition('inline');
  55. }
  56. public function testDispositionIsAutoDefaultedToAttachment()
  57. {
  58. $headers = $this->_createHeaderSet(array(), false);
  59. $this->_checking(Expectations::create()
  60. -> one($headers)->addParameterizedHeader('Content-Disposition', 'attachment')
  61. -> ignoring($headers)
  62. );
  63. $attachment = $this->_createAttachment($headers, $this->_createEncoder(),
  64. $this->_createCache()
  65. );
  66. }
  67. public function testDefaultContentTypeInitializedToOctetStream()
  68. {
  69. $cType = $this->_createHeader('Content-Type', '',
  70. array(), false
  71. );
  72. $this->_checking(Expectations::create()
  73. -> one($cType)->setFieldBodyModel('application/octet-stream')
  74. -> ignoring($cType)
  75. );
  76. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  77. 'Content-Type' => $cType)),
  78. $this->_createEncoder(), $this->_createCache()
  79. );
  80. }
  81. public function testFilenameIsReturnedFromHeader()
  82. {
  83. /* -- RFC 2183, 2.3.
  84. */
  85. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  86. array('filename'=>'foo.txt')
  87. );
  88. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  89. 'Content-Disposition' => $disposition)),
  90. $this->_createEncoder(), $this->_createCache()
  91. );
  92. $this->assertEqual('foo.txt', $attachment->getFilename());
  93. }
  94. public function testFilenameIsSetInHeader()
  95. {
  96. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  97. array('filename'=>'foo.txt'), false
  98. );
  99. $this->_checking(Expectations::create()
  100. -> one($disposition)->setParameter('filename', 'bar.txt')
  101. -> ignoring($disposition)
  102. );
  103. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  104. 'Content-Disposition' => $disposition)),
  105. $this->_createEncoder(), $this->_createCache()
  106. );
  107. $attachment->setFilename('bar.txt');
  108. }
  109. public function testSettingFilenameSetsNameInContentType()
  110. {
  111. /*
  112. This is a legacy requirement which isn't covered by up-to-date RFCs.
  113. */
  114. $cType = $this->_createHeader('Content-Type', 'text/plain',
  115. array(), false
  116. );
  117. $this->_checking(Expectations::create()
  118. -> one($cType)->setParameter('name', 'bar.txt')
  119. -> ignoring($cType)
  120. );
  121. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  122. 'Content-Type' => $cType)),
  123. $this->_createEncoder(), $this->_createCache()
  124. );
  125. $attachment->setFilename('bar.txt');
  126. }
  127. public function testSizeIsReturnedFromHeader()
  128. {
  129. /* -- RFC 2183, 2.7.
  130. */
  131. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  132. array('size'=>1234)
  133. );
  134. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  135. 'Content-Disposition' => $disposition)),
  136. $this->_createEncoder(), $this->_createCache()
  137. );
  138. $this->assertEqual(1234, $attachment->getSize());
  139. }
  140. public function testSizeIsSetInHeader()
  141. {
  142. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  143. array(), false
  144. );
  145. $this->_checking(Expectations::create()
  146. -> one($disposition)->setParameter('size', 12345)
  147. -> ignoring($disposition)
  148. );
  149. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  150. 'Content-Disposition' => $disposition)),
  151. $this->_createEncoder(), $this->_createCache()
  152. );
  153. $attachment->setSize(12345);
  154. }
  155. public function testFilnameCanBeReadFromFileStream()
  156. {
  157. $file = $this->_createFileStream('/bar/file.ext', '');
  158. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  159. array('filename'=>'foo.txt'), false
  160. );
  161. $this->_checking(Expectations::create()
  162. -> one($disposition)->setParameter('filename', 'file.ext')
  163. -> ignoring($disposition)
  164. );
  165. $attachment = $this->_createAttachment($this->_createHeaderSet(array(
  166. 'Content-Disposition' => $disposition)),
  167. $this->_createEncoder(), $this->_createCache()
  168. );
  169. $attachment->setFile($file);
  170. }
  171. public function testContentTypeCanBeSetViaSetFile()
  172. {
  173. $file = $this->_createFileStream('/bar/file.ext', '');
  174. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  175. array('filename'=>'foo.txt'), false
  176. );
  177. $ctype = $this->_createHeader('Content-Type', 'text/plain', array(), false);
  178. $headers = $this->_createHeaderSet(array(
  179. 'Content-Disposition' => $disposition,
  180. 'Content-Type' => $ctype
  181. ));
  182. $this->_checking(Expectations::create()
  183. -> one($disposition)->setParameter('filename', 'file.ext')
  184. -> one($ctype)->setFieldBodyModel('text/html')
  185. -> ignoring($disposition)
  186. -> ignoring($ctype)
  187. );
  188. $attachment = $this->_createAttachment($headers, $this->_createEncoder(),
  189. $this->_createCache()
  190. );
  191. $attachment->setFile($file, 'text/html');
  192. }
  193. public function XtestContentTypeCanBeLookedUpFromCommonListIfNotProvided()
  194. {
  195. $file = $this->_createFileStream('/bar/file.zip', '');
  196. $disposition = $this->_createHeader('Content-Disposition', 'attachment',
  197. array('filename'=>'foo.zip'), false
  198. );
  199. $ctype = $this->_createHeader('Content-Type', 'text/plain', array(), false);
  200. $headers = $this->_createHeaderSet(array(
  201. 'Content-Disposition' => $disposition,
  202. 'Content-Type' => $ctype
  203. ));
  204. $this->_checking(Expectations::create()
  205. -> one($disposition)->setParameter('filename', 'file.zip')
  206. -> one($ctype)->setFieldBodyModel('application/zip')
  207. -> ignoring($disposition)
  208. -> ignoring($ctype)
  209. );
  210. $attachment = $this->_createAttachment($headers, $this->_createEncoder(),
  211. $this->_createCache(), array('zip'=>'application/zip', 'txt'=>'text/plain')
  212. );
  213. $attachment->setFile($file);
  214. }
  215. public function testDataCanBeReadFromFile()
  216. {
  217. $file = $this->_createFileStream('/foo/file.ext', '<some data>');
  218. $attachment = $this->_createAttachment($this->_createHeaderSet(),
  219. $this->_createEncoder(), $this->_createCache()
  220. );
  221. $attachment->setFile($file);
  222. $this->assertEqual('<some data>', $attachment->getBody());
  223. }
  224. public function testFluidInterface()
  225. {
  226. $attachment = $this->_createAttachment($this->_createHeaderSet(),
  227. $this->_createEncoder(), $this->_createCache()
  228. );
  229. $this->assertSame($attachment,
  230. $attachment
  231. ->setContentType('application/pdf')
  232. ->setEncoder($this->_createEncoder())
  233. ->setId('foo@bar')
  234. ->setDescription('my pdf')
  235. ->setMaxLineLength(998)
  236. ->setBody('xx')
  237. ->setBoundary('xyz')
  238. ->setChildren(array())
  239. ->setDisposition('inline')
  240. ->setFilename('afile.txt')
  241. ->setSize(123)
  242. ->setFile($this->_createFileStream('foo.txt', ''))
  243. );
  244. }
  245. // -- Private helpers
  246. protected function _createEntity($headers, $encoder, $cache)
  247. {
  248. return $this->_createAttachment($headers, $encoder, $cache);
  249. }
  250. protected function _createAttachment($headers, $encoder, $cache,
  251. $mimeTypes = array())
  252. {
  253. return new Swift_Mime_Attachment($headers, $encoder, $cache, new Swift_Mime_Grammar(), $mimeTypes);
  254. }
  255. protected function _createFileStream($path, $data, $stub = true)
  256. {
  257. $file = $this->_mock('Swift_FileStream');
  258. $pos = $this->_mockery()->states('position')->startsAs('at start');
  259. $this->_checking(Expectations::create()
  260. -> ignoring($file)->getPath() -> returns($path)
  261. -> ignoring($file)->read(optional()) -> returns($data)
  262. -> when($pos->isNot('at end')) -> then($pos->is('at end'))
  263. -> ignoring($file)->read(optional()) -> returns(false)
  264. );
  265. if ($stub)
  266. {
  267. $this->_checking(Expectations::create()
  268. -> ignoring($file)
  269. );
  270. }
  271. return $file;
  272. }
  273. }