PlainContentEncoderTest.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/ContentEncoder/PlainContentEncoder.php';
  4. require_once 'Swift/InputByteStream.php';
  5. require_once 'Swift/OutputByteStream.php';
  6. class Swift_StreamCollector implements Yay_Action {
  7. public $content = '';
  8. public function &invoke(Yay_Invocation $inv) {
  9. $args = $inv->getArguments();
  10. $this->content .= current($args);
  11. }
  12. public function describeTo(Yay_Description $description) {
  13. $description->appendText(' gathers input;');
  14. }
  15. }
  16. class Swift_Mime_ContentEncoder_PlainContentEncoderTest
  17. extends Swift_Tests_SwiftUnitTestCase
  18. {
  19. public function testNameCanBeSpecifiedInConstructor()
  20. {
  21. $encoder = $this->_getEncoder('7bit');
  22. $this->assertEqual('7bit', $encoder->getName());
  23. $encoder = $this->_getEncoder('8bit');
  24. $this->assertEqual('8bit', $encoder->getName());
  25. }
  26. public function testNoOctetsAreModifiedInString()
  27. {
  28. $encoder = $this->_getEncoder('7bit');
  29. foreach (range(0x00, 0xFF) as $octet)
  30. {
  31. $byte = pack('C', $octet);
  32. $this->assertIdenticalBinary($byte, $encoder->encodeString($byte));
  33. }
  34. }
  35. public function testNoOctetsAreModifiedInByteStream()
  36. {
  37. $encoder = $this->_getEncoder('7bit');
  38. foreach (range(0x00, 0xFF) as $octet)
  39. {
  40. $byte = pack('C', $octet);
  41. $os = $this->_createOutputByteStream();
  42. $is = $this->_createInputByteStream();
  43. $collection = new Swift_StreamCollector();
  44. $this->_checking(Expectations::create()
  45. -> allowing($is)->write(any(), optional()) -> will($collection)
  46. -> ignoring($is)
  47. -> one($os)->read(optional()) -> returns($byte)
  48. -> allowing($os)->read(optional()) -> returns(false)
  49. -> ignoring($os)
  50. );
  51. $encoder->encodeByteStream($os, $is);
  52. $this->assertIdenticalBinary($byte, $collection->content);
  53. }
  54. }
  55. public function testLineLengthCanBeSpecified()
  56. {
  57. $encoder = $this->_getEncoder('7bit');
  58. $chars = array();
  59. for ($i = 0; $i < 50; $i++)
  60. {
  61. $chars[] = 'a';
  62. }
  63. $input = implode(' ', $chars); //99 chars long
  64. $this->assertEqual(
  65. 'a a a a a a a a a a a a a a a a a a a a a a a a a ' . "\r\n" . //50 *
  66. 'a a a a a a a a a a a a a a a a a a a a a a a a a', //99
  67. $encoder->encodeString($input, 0, 50),
  68. '%s: Lines should be wrapped at 50 chars'
  69. );
  70. }
  71. public function testLineLengthCanBeSpecifiedInByteStream()
  72. {
  73. $encoder = $this->_getEncoder('7bit');
  74. $os = $this->_createOutputByteStream();
  75. $is = $this->_createInputByteStream();
  76. $collection = new Swift_StreamCollector();
  77. $this->_checking(Expectations::create()
  78. -> allowing($is)->write(any(), optional()) -> will($collection)
  79. -> ignoring($is)
  80. );
  81. for ($i = 0; $i < 50; $i++)
  82. {
  83. $this->_checking(Expectations::create()
  84. -> one($os)->read(optional()) -> returns('a ')
  85. );
  86. }
  87. $this->_checking(Expectations::create()
  88. -> allowing($os)->read(optional()) -> returns(false)
  89. );
  90. $encoder->encodeByteStream($os, $is, 0, 50);
  91. $this->assertEqual(
  92. str_repeat('a ', 25) . "\r\n" . str_repeat('a ', 25),
  93. $collection->content
  94. );
  95. }
  96. public function testencodeStringGeneratesCorrectCrlf()
  97. {
  98. $encoder = $this->_getEncoder('7bit', true);
  99. $this->assertEqual("a\r\nb", $encoder->encodeString("a\rb"),
  100. '%s: Line endings should be standardized'
  101. );
  102. $this->assertEqual("a\r\nb", $encoder->encodeString("a\nb"),
  103. '%s: Line endings should be standardized'
  104. );
  105. $this->assertEqual("a\r\n\r\nb", $encoder->encodeString("a\n\rb"),
  106. '%s: Line endings should be standardized'
  107. );
  108. $this->assertEqual("a\r\n\r\nb", $encoder->encodeString("a\r\rb"),
  109. '%s: Line endings should be standardized'
  110. );
  111. $this->assertEqual("a\r\n\r\nb", $encoder->encodeString("a\n\nb"),
  112. '%s: Line endings should be standardized'
  113. );
  114. }
  115. public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_1()
  116. {
  117. $encoder = $this->_getEncoder('7bit', true);
  118. $os = $this->_createOutputByteStream();
  119. $is = $this->_createInputByteStream();
  120. $collection = new Swift_StreamCollector();
  121. $this->_checking(Expectations::create()
  122. -> allowing($is)->write(any(), optional()) -> will($collection)
  123. -> ignoring($is)
  124. -> one($os)->read(optional()) -> returns('a')
  125. -> one($os)->read(optional()) -> returns("\r")
  126. -> one($os)->read(optional()) -> returns('b')
  127. -> allowing($os)->read(optional()) -> returns(false)
  128. -> ignoring($os)
  129. );
  130. $encoder->encodeByteStream($os, $is);
  131. $this->assertEqual("a\r\nb", $collection->content);
  132. }
  133. public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_2()
  134. {
  135. $encoder = $this->_getEncoder('7bit', true);
  136. $os = $this->_createOutputByteStream();
  137. $is = $this->_createInputByteStream();
  138. $collection = new Swift_StreamCollector();
  139. $this->_checking(Expectations::create()
  140. -> allowing($is)->write(any(), optional()) -> will($collection)
  141. -> ignoring($is)
  142. -> one($os)->read(optional()) -> returns('a')
  143. -> one($os)->read(optional()) -> returns("\n")
  144. -> one($os)->read(optional()) -> returns('b')
  145. -> allowing($os)->read(optional()) -> returns(false)
  146. -> ignoring($os)
  147. );
  148. $encoder->encodeByteStream($os, $is);
  149. $this->assertEqual("a\r\nb", $collection->content);
  150. }
  151. public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_3()
  152. {
  153. $encoder = $this->_getEncoder('7bit', true);
  154. $os = $this->_createOutputByteStream();
  155. $is = $this->_createInputByteStream();
  156. $collection = new Swift_StreamCollector();
  157. $this->_checking(Expectations::create()
  158. -> allowing($is)->write(any(), optional()) -> will($collection)
  159. -> ignoring($is)
  160. -> one($os)->read(optional()) -> returns('a')
  161. -> one($os)->read(optional()) -> returns("\n\r")
  162. -> one($os)->read(optional()) -> returns('b')
  163. -> allowing($os)->read(optional()) -> returns(false)
  164. -> ignoring($os)
  165. );
  166. $encoder->encodeByteStream($os, $is);
  167. $this->assertEqual("a\r\n\r\nb", $collection->content);
  168. }
  169. public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_4()
  170. {
  171. $encoder = $this->_getEncoder('7bit', true);
  172. $os = $this->_createOutputByteStream();
  173. $is = $this->_createInputByteStream();
  174. $collection = new Swift_StreamCollector();
  175. $this->_checking(Expectations::create()
  176. -> allowing($is)->write(any(), optional()) -> will($collection)
  177. -> ignoring($is)
  178. -> one($os)->read(optional()) -> returns('a')
  179. -> one($os)->read(optional()) -> returns("\n\n")
  180. -> one($os)->read(optional()) -> returns('b')
  181. -> allowing($os)->read(optional()) -> returns(false)
  182. -> ignoring($os)
  183. );
  184. $encoder->encodeByteStream($os, $is);
  185. $this->assertEqual("a\r\n\r\nb", $collection->content);
  186. }
  187. public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_5()
  188. {
  189. $encoder = $this->_getEncoder('7bit', true);
  190. $os = $this->_createOutputByteStream();
  191. $is = $this->_createInputByteStream();
  192. $collection = new Swift_StreamCollector();
  193. $this->_checking(Expectations::create()
  194. -> allowing($is)->write(any(), optional()) -> will($collection)
  195. -> ignoring($is)
  196. -> one($os)->read(optional()) -> returns('a')
  197. -> one($os)->read(optional()) -> returns("\r\r")
  198. -> one($os)->read(optional()) -> returns('b')
  199. -> allowing($os)->read(optional()) -> returns(false)
  200. -> ignoring($os)
  201. );
  202. $encoder->encodeByteStream($os, $is);
  203. $this->assertEqual("a\r\n\r\nb", $collection->content);
  204. }
  205. // -- Private helpers
  206. private function _getEncoder($name, $canonical = false)
  207. {
  208. return new Swift_Mime_ContentEncoder_PlainContentEncoder($name, $canonical);
  209. }
  210. private function _createOutputByteStream($stub = false)
  211. {
  212. return $stub
  213. ? $this->_stub('Swift_OutputByteStream')
  214. : $this->_mock('Swift_OutputByteStream')
  215. ;
  216. }
  217. private function _createInputByteStream($stub = false)
  218. {
  219. return $stub
  220. ? $this->_stub('Swift_InputByteStream')
  221. : $this->_mock('Swift_InputByteStream')
  222. ;
  223. }
  224. }