123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
-
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/Mime/ContentEncoder/Base64ContentEncoder.php';
- require_once 'Swift/OutputByteStream.php';
- require_once 'Swift/InputByteStream.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_Base64ContentEncoderTest
- extends Swift_Tests_SwiftUnitTestCase
- {
-
- private $_encoder;
-
- public function setUp()
- {
- $this->_encoder = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
- }
-
- public function testNameIsBase64()
- {
- $this->assertEqual('base64', $this->_encoder->getName());
- }
-
-
-
-
- public function testInputOutputRatioIs3to4Bytes()
- {
-
-
-
- $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('123')
- -> allowing($os)->read(optional()) -> returns(false)
-
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is);
- $this->assertEqual('MTIz', $collection->content);
- }
-
- public function testPadLength()
- {
-
-
-
- for ($i = 0; $i < 30; ++$i)
- {
- $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(pack('C', rand(0, 255)))
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is);
- $this->assertPattern('~^[a-zA-Z0-9/\+]{2}==$~', $collection->content,
- '%s: A single byte should have 2 bytes of padding'
- );
- }
-
- for ($i = 0; $i < 30; ++$i)
- {
- $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(pack('C*', rand(0, 255), rand(0, 255)))
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is);
- $this->assertPattern('~^[a-zA-Z0-9/\+]{3}=$~', $collection->content,
- '%s: Two bytes should have 1 byte of padding'
- );
- }
-
- for ($i = 0; $i < 30; ++$i)
- {
- $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(pack('C*', rand(0, 255), rand(0, 255), rand(0, 255)))
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is);
- $this->assertPattern('~^[a-zA-Z0-9/\+]{4}$~', $collection->content,
- '%s: Three bytes should have no padding'
- );
- }
- }
-
- public function testMaximumLineLengthIs76Characters()
- {
-
-
-
- $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('abcdefghijkl')
- -> one($os)->read(optional()) -> returns('mnopqrstuvwx')
- -> one($os)->read(optional()) -> returns('yzabc1234567')
- -> one($os)->read(optional()) -> returns('890ABCDEFGHI')
- -> one($os)->read(optional()) -> returns('JKLMNOPQRSTU')
- -> one($os)->read(optional()) -> returns('VWXYZ1234567')
- -> one($os)->read(optional()) -> returns('abcdefghijkl')
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is);
- $this->assertEqual(
- "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmMxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFS\r\n" .
- "U1RVVldYWVoxMjM0NTY3YWJjZGVmZ2hpamts",
- $collection->content
- );
- }
-
- public function testMaximumLineLengthCanBeDifferent()
- {
- $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('abcdefghijkl')
- -> one($os)->read(optional()) -> returns('mnopqrstuvwx')
- -> one($os)->read(optional()) -> returns('yzabc1234567')
- -> one($os)->read(optional()) -> returns('890ABCDEFGHI')
- -> one($os)->read(optional()) -> returns('JKLMNOPQRSTU')
- -> one($os)->read(optional()) -> returns('VWXYZ1234567')
- -> one($os)->read(optional()) -> returns('abcdefghijkl')
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is, 0, 50);
- $this->assertEqual(
- "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmMxMjM0NTY3OD\r\n" .
- "kwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVoxMjM0NTY3YWJj\r\n" .
- "ZGVmZ2hpamts",
- $collection->content
- );
- }
-
- public function testMaximumLineLengthIsNeverMoreThan76Chars()
- {
- $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('abcdefghijkl')
- -> one($os)->read(optional()) -> returns('mnopqrstuvwx')
- -> one($os)->read(optional()) -> returns('yzabc1234567')
- -> one($os)->read(optional()) -> returns('890ABCDEFGHI')
- -> one($os)->read(optional()) -> returns('JKLMNOPQRSTU')
- -> one($os)->read(optional()) -> returns('VWXYZ1234567')
- -> one($os)->read(optional()) -> returns('abcdefghijkl')
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is, 0, 100);
- $this->assertEqual(
- "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmMxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFS\r\n" .
- "U1RVVldYWVoxMjM0NTY3YWJjZGVmZ2hpamts",
- $collection->content
- );
- }
-
- public function testFirstLineLengthCanBeDifferent()
- {
- $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('abcdefghijkl')
- -> one($os)->read(optional()) -> returns('mnopqrstuvwx')
- -> one($os)->read(optional()) -> returns('yzabc1234567')
- -> one($os)->read(optional()) -> returns('890ABCDEFGHI')
- -> one($os)->read(optional()) -> returns('JKLMNOPQRSTU')
- -> one($os)->read(optional()) -> returns('VWXYZ1234567')
- -> one($os)->read(optional()) -> returns('abcdefghijkl')
- -> allowing($os)->read(optional()) -> returns(false)
- -> ignoring($os)
- );
-
- $this->_encoder->encodeByteStream($os, $is, 19);
- $this->assertEqual(
- "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmMxMjM0NTY3ODkwQUJDR\r\n" .
- "EVGR0hJSktMTU5PUFFSU1RVVldYWVoxMjM0NTY3YWJjZGVmZ2hpamts",
- $collection->content
- );
- }
-
-
-
- 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')
- ;
- }
-
- }
|