/vendor/swiftmailer/swiftmailer/tests/unit/Swift/Mime/ContentEncoder/PlainContentEncoderTest.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 173 lines · 143 code · 26 blank · 4 comment · 2 complexity · bb477a0c61b4473df46af3ec2de701c4 MD5 · raw file

  1. <?php
  2. class Swift_Mime_ContentEncoder_PlainContentEncoderTest extends \SwiftMailerTestCase
  3. {
  4. public function testNameCanBeSpecifiedInConstructor()
  5. {
  6. $encoder = $this->_getEncoder('7bit');
  7. $this->assertEquals('7bit', $encoder->getName());
  8. $encoder = $this->_getEncoder('8bit');
  9. $this->assertEquals('8bit', $encoder->getName());
  10. }
  11. public function testNoOctetsAreModifiedInString()
  12. {
  13. $encoder = $this->_getEncoder('7bit');
  14. foreach (range(0x00, 0xFF) as $octet) {
  15. $byte = pack('C', $octet);
  16. $this->assertIdenticalBinary($byte, $encoder->encodeString($byte));
  17. }
  18. }
  19. public function testNoOctetsAreModifiedInByteStream()
  20. {
  21. $encoder = $this->_getEncoder('7bit');
  22. foreach (range(0x00, 0xFF) as $octet) {
  23. $byte = pack('C', $octet);
  24. $os = $this->_createOutputByteStream();
  25. $is = $this->_createInputByteStream();
  26. $collection = new Swift_StreamCollector();
  27. $is->shouldReceive('write')
  28. ->zeroOrMoreTimes()
  29. ->andReturnUsing($collection);
  30. $os->shouldReceive('read')
  31. ->once()
  32. ->andReturn($byte);
  33. $os->shouldReceive('read')
  34. ->zeroOrMoreTimes()
  35. ->andReturn(false);
  36. $encoder->encodeByteStream($os, $is);
  37. $this->assertIdenticalBinary($byte, $collection->content);
  38. }
  39. }
  40. public function testLineLengthCanBeSpecified()
  41. {
  42. $encoder = $this->_getEncoder('7bit');
  43. $chars = array();
  44. for ($i = 0; $i < 50; ++$i) {
  45. $chars[] = 'a';
  46. }
  47. $input = implode(' ', $chars); //99 chars long
  48. $this->assertEquals(
  49. '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 *
  50. '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
  51. $encoder->encodeString($input, 0, 50),
  52. '%s: Lines should be wrapped at 50 chars'
  53. );
  54. }
  55. public function testLineLengthCanBeSpecifiedInByteStream()
  56. {
  57. $encoder = $this->_getEncoder('7bit');
  58. $os = $this->_createOutputByteStream();
  59. $is = $this->_createInputByteStream();
  60. $collection = new Swift_StreamCollector();
  61. $is->shouldReceive('write')
  62. ->zeroOrMoreTimes()
  63. ->andReturnUsing($collection);
  64. for ($i = 0; $i < 50; ++$i) {
  65. $os->shouldReceive('read')
  66. ->once()
  67. ->andReturn('a ');
  68. }
  69. $os->shouldReceive('read')
  70. ->zeroOrMoreTimes()
  71. ->andReturn(false);
  72. $encoder->encodeByteStream($os, $is, 0, 50);
  73. $this->assertEquals(
  74. str_repeat('a ', 25)."\r\n".str_repeat('a ', 25),
  75. $collection->content
  76. );
  77. }
  78. public function testencodeStringGeneratesCorrectCrlf()
  79. {
  80. $encoder = $this->_getEncoder('7bit', true);
  81. $this->assertEquals("a\r\nb", $encoder->encodeString("a\rb"),
  82. '%s: Line endings should be standardized'
  83. );
  84. $this->assertEquals("a\r\nb", $encoder->encodeString("a\nb"),
  85. '%s: Line endings should be standardized'
  86. );
  87. $this->assertEquals("a\r\n\r\nb", $encoder->encodeString("a\n\rb"),
  88. '%s: Line endings should be standardized'
  89. );
  90. $this->assertEquals("a\r\n\r\nb", $encoder->encodeString("a\r\rb"),
  91. '%s: Line endings should be standardized'
  92. );
  93. $this->assertEquals("a\r\n\r\nb", $encoder->encodeString("a\n\nb"),
  94. '%s: Line endings should be standardized'
  95. );
  96. }
  97. public function crlfProvider()
  98. {
  99. return array(
  100. array("\r", "a\r\nb"),
  101. array("\n", "a\r\nb"),
  102. array("\n\r", "a\r\n\r\nb"),
  103. array("\n\n", "a\r\n\r\nb"),
  104. array("\r\r", "a\r\n\r\nb"),
  105. );
  106. }
  107. /**
  108. * @dataProvider crlfProvider
  109. */
  110. public function testCanonicEncodeByteStreamGeneratesCorrectCrlf($test, $expected)
  111. {
  112. $encoder = $this->_getEncoder('7bit', true);
  113. $os = $this->_createOutputByteStream();
  114. $is = $this->_createInputByteStream();
  115. $collection = new Swift_StreamCollector();
  116. $is->shouldReceive('write')
  117. ->zeroOrMoreTimes()
  118. ->andReturnUsing($collection);
  119. $os->shouldReceive('read')
  120. ->once()
  121. ->andReturn('a');
  122. $os->shouldReceive('read')
  123. ->once()
  124. ->andReturn($test);
  125. $os->shouldReceive('read')
  126. ->once()
  127. ->andReturn('b');
  128. $os->shouldReceive('read')
  129. ->zeroOrMoreTimes()
  130. ->andReturn(false);
  131. $encoder->encodeByteStream($os, $is);
  132. $this->assertEquals($expected, $collection->content);
  133. }
  134. // -- Private helpers
  135. private function _getEncoder($name, $canonical = false)
  136. {
  137. return new Swift_Mime_ContentEncoder_PlainContentEncoder($name, $canonical);
  138. }
  139. private function _createOutputByteStream($stub = false)
  140. {
  141. return $this->getMockery('Swift_OutputByteStream')->shouldIgnoreMissing();
  142. }
  143. private function _createInputByteStream($stub = false)
  144. {
  145. return $this->getMockery('Swift_InputByteStream')->shouldIgnoreMissing();
  146. }
  147. }