123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
-
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/Mime/ContentEncoder/PlainContentEncoder.php';
- require_once 'Swift/InputByteStream.php';
- require_once 'Swift/OutputByteStream.php';
-
- class Swift_StreamCollector implements Yay_Action {
- public $content = '';
- public function &invoke(Yay_Invocation $inv) {
- $args = $inv->getArguments();
- $this->content .= current($args);
- }
- public function describeTo(Yay_Description $description) {
- $description->appendText(' gathers input;');
- }
- }
-
- class Swift_Mime_ContentEncoder_PlainContentEncoderTest
- extends Swift_Tests_SwiftUnitTestCase
- {
-
- public function testNameCanBeSpecifiedInConstructor()
- {
- $encoder = $this->_getEncoder('7bit');
- $this->assertEqual('7bit', $encoder->getName());
-
- $encoder = $this->_getEncoder('8bit');
- $this->assertEqual('8bit', $encoder->getName());
- }
-
- public function testNoOctetsAreModifiedInString()
- {
- $encoder = $this->_getEncoder('7bit');
- foreach (range(0x00, 0xFF) as $octet)
- {
- $byte = pack('C', $octet);
- $this->assertIdenticalBinary($byte, $encoder->encodeString($byte));
- }
- }
-
- public function testNoOctetsAreModifiedInByteStream()
- {
- $encoder = $this->_getEncoder('7bit');
- foreach (range(0x00, 0xFF) as $octet)
- {
- $byte = pack('C', $octet);
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> one($os)->read(optional()) -> returns($byte)
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $encoder->encodeByteStream($os, $is);
- $this->assertIdenticalBinary($byte, $collection->content);
- }
- }
-
- public function testLineLengthCanBeSpecified()
- {
- $encoder = $this->_getEncoder('7bit');
-
- $chars = array();
- for ($i = 0; $i < 50; $i++)
- {
- $chars[] = 'a';
- }
- $input = implode(' ', $chars); //99 chars long
-
- $this->assertEqual(
- '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 *
- '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
- $encoder->encodeString($input, 0, 50),
- '%s: Lines should be wrapped at 50 chars'
- );
- }
-
- public function testLineLengthCanBeSpecifiedInByteStream()
- {
- $encoder = $this->_getEncoder('7bit');
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
- );
-
- for ($i = 0; $i < 50; $i++)
- {
- $this->_checking(Expectations::create()
- -> one($os)->read(optional()) -> returns('a ')
- );
- }
-
- $this->_checking(Expectations::create()
- -> allowing($os)->read(optional()) -> returns(false)
- );
-
- $encoder->encodeByteStream($os, $is, 0, 50);
- $this->assertEqual(
- str_repeat('a ', 25) . "\r\n" . str_repeat('a ', 25),
- $collection->content
- );
- }
-
- public function testencodeStringGeneratesCorrectCrlf()
- {
- $encoder = $this->_getEncoder('7bit', true);
- $this->assertEqual("a\r\nb", $encoder->encodeString("a\rb"),
- '%s: Line endings should be standardized'
- );
- $this->assertEqual("a\r\nb", $encoder->encodeString("a\nb"),
- '%s: Line endings should be standardized'
- );
- $this->assertEqual("a\r\n\r\nb", $encoder->encodeString("a\n\rb"),
- '%s: Line endings should be standardized'
- );
- $this->assertEqual("a\r\n\r\nb", $encoder->encodeString("a\r\rb"),
- '%s: Line endings should be standardized'
- );
- $this->assertEqual("a\r\n\r\nb", $encoder->encodeString("a\n\nb"),
- '%s: Line endings should be standardized'
- );
- }
-
- public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_1()
- {
- $encoder = $this->_getEncoder('7bit', true);
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> one($os)->read(optional()) -> returns('a')
- -> one($os)->read(optional()) -> returns("\r")
- -> one($os)->read(optional()) -> returns('b')
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual("a\r\nb", $collection->content);
- }
-
- public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_2()
- {
- $encoder = $this->_getEncoder('7bit', true);
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> one($os)->read(optional()) -> returns('a')
- -> one($os)->read(optional()) -> returns("\n")
- -> one($os)->read(optional()) -> returns('b')
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual("a\r\nb", $collection->content);
- }
-
- public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_3()
- {
- $encoder = $this->_getEncoder('7bit', true);
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> one($os)->read(optional()) -> returns('a')
- -> one($os)->read(optional()) -> returns("\n\r")
- -> one($os)->read(optional()) -> returns('b')
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual("a\r\n\r\nb", $collection->content);
- }
-
- public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_4()
- {
- $encoder = $this->_getEncoder('7bit', true);
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> one($os)->read(optional()) -> returns('a')
- -> one($os)->read(optional()) -> returns("\n\n")
- -> one($os)->read(optional()) -> returns('b')
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual("a\r\n\r\nb", $collection->content);
- }
-
- public function testCanonicEncodeByteStreamGeneratesCorrectCrlf_5()
- {
- $encoder = $this->_getEncoder('7bit', true);
-
- $os = $this->_createOutputByteStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> one($os)->read(optional()) -> returns('a')
- -> one($os)->read(optional()) -> returns("\r\r")
- -> one($os)->read(optional()) -> returns('b')
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual("a\r\n\r\nb", $collection->content);
- }
-
- // -- Private helpers
-
- private function _getEncoder($name, $canonical = false)
- {
- return new Swift_Mime_ContentEncoder_PlainContentEncoder($name, $canonical);
- }
-
- private function _createOutputByteStream($stub = false)
- {
- return $stub
- ? $this->_stub('Swift_OutputByteStream')
- : $this->_mock('Swift_OutputByteStream')
- ;
- }
-
- private function _createInputByteStream($stub = false)
- {
- return $stub
- ? $this->_stub('Swift_InputByteStream')
- : $this->_mock('Swift_InputByteStream')
- ;
- }
-
- }
|