123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
-
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/Mime/ContentEncoder/QpContentEncoder.php';
- require_once 'Swift/CharacterStream/ArrayCharacterStream.php';
- require_once 'Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php';
- require_once 'Swift/ByteStream/ArrayByteStream.php';
-
- class Swift_Mime_ContentEncoder_QpContentEncoderAcceptanceTest
- extends Swift_Tests_SwiftUnitTestCase
- {
-
- private $_samplesDir;
- private $_factory;
-
- public function setUp()
- {
- $this->_samplesDir = realpath(dirname(__FILE__) . '/../../../../_samples/charsets');
- $this->_factory = new Swift_CharacterReaderFactory_SimpleCharacterReaderFactory();
- }
-
- public function testEncodingAndDecodingSamples()
- {
- $sampleFp = opendir($this->_samplesDir);
- while (false !== $encodingDir = readdir($sampleFp))
- {
- if (substr($encodingDir, 0, 1) == '.')
- {
- continue;
- }
-
- $encoding = $encodingDir;
- $charStream = new Swift_CharacterStream_NgCharacterStream(
- $this->_factory, $encoding);
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
-
- $sampleDir = $this->_samplesDir . '/' . $encodingDir;
-
- if (is_dir($sampleDir))
- {
-
- $fileFp = opendir($sampleDir);
- while (false !== $sampleFile = readdir($fileFp))
- {
- if (substr($sampleFile, 0, 1) == '.')
- {
- continue;
- }
-
- $text = file_get_contents($sampleDir . '/' . $sampleFile);
-
- $os = new Swift_ByteStream_ArrayByteStream();
- $os->write($text);
-
- $is = new Swift_ByteStream_ArrayByteStream();
- $encoder->encodeByteStream($os, $is);
-
- $encoded = '';
- while (false !== $bytes = $is->read(8192))
- {
- $encoded .= $bytes;
- }
-
- $this->assertEqual(
- quoted_printable_decode($encoded), $text,
- '%s: Encoded string should decode back to original string for sample ' .
- $sampleDir . '/' . $sampleFile
- );
- }
- closedir($fileFp);
- }
-
- }
- closedir($sampleFp);
- }
-
- public function testEncodingAndDecodingSamplesFromDiConfiguredInstance()
- {
- $sampleFp = opendir($this->_samplesDir);
- while (false !== $encodingDir = readdir($sampleFp))
- {
- if (substr($encodingDir, 0, 1) == '.')
- {
- continue;
- }
-
- $encoding = $encodingDir;
- $encoder = $this->_createEncoderFromContainer();
-
- $sampleDir = $this->_samplesDir . '/' . $encodingDir;
-
- if (is_dir($sampleDir))
- {
-
- $fileFp = opendir($sampleDir);
- while (false !== $sampleFile = readdir($fileFp))
- {
- if (substr($sampleFile, 0, 1) == '.')
- {
- continue;
- }
-
- $text = file_get_contents($sampleDir . '/' . $sampleFile);
-
- $os = new Swift_ByteStream_ArrayByteStream();
- $os->write($text);
-
- $is = new Swift_ByteStream_ArrayByteStream();
- $encoder->encodeByteStream($os, $is);
-
- $encoded = '';
- while (false !== $bytes = $is->read(8192))
- {
- $encoded .= $bytes;
- }
-
- $this->assertEqual(
- str_replace("\r\n", "\n", quoted_printable_decode($encoded)), str_replace("\r\n", "\n", $text),
- '%s: Encoded string should decode back to original string for sample ' .
- $sampleDir . '/' . $sampleFile
- );
- }
- closedir($fileFp);
- }
-
- }
- closedir($sampleFp);
- }
-
- public function testEncodingLFTextWithDiConfiguredInstance()
- {
- $encoder = $this->_createEncoderFromContainer();
- $this->assertEqual("a\r\nb\r\nc", $encoder->encodeString("a\nb\nc"));
- }
-
- public function testEncodingCRTextWithDiConfiguredInstance()
- {
- $encoder = $this->_createEncoderFromContainer();
- $this->assertEqual("a\r\nb\r\nc", $encoder->encodeString("a\rb\rc"));
- }
-
- public function testEncodingLFCRTextWithDiConfiguredInstance()
- {
- $encoder = $this->_createEncoderFromContainer();
- $this->assertEqual("a\r\n\r\nb\r\n\r\nc", $encoder->encodeString("a\n\rb\n\rc"));
- }
-
- public function testEncodingCRLFTextWithDiConfiguredInstance()
- {
- $encoder = $this->_createEncoderFromContainer();
- $this->assertEqual("a\r\nb\r\nc", $encoder->encodeString("a\r\nb\r\nc"));
- }
-
- public function testEncodingDotStuffingWithDiConfiguredInstance()
- {
- // Enable DotEscaping
- Swift_Preferences::getInstance()->setQPDotEscape(true);
- $encoder = $this->_createEncoderFromContainer();
- $this->assertEqual("a=2E\r\n=2E\r\n=2Eb\r\nc", $encoder->encodeString("a.\r\n.\r\n.b\r\nc"));
- // Return to default
- Swift_Preferences::getInstance()->setQPDotEscape(false);
- $encoder = $this->_createEncoderFromContainer();
- $this->assertEqual("a.\r\n.\r\n.b\r\nc", $encoder->encodeString("a.\r\n.\r\n.b\r\nc"));
- }
-
- public function testDotStuffingEncodingAndDecodingSamplesFromDiConfiguredInstance()
- {
- // Enable DotEscaping
- Swift_Preferences::getInstance()->setQPDotEscape(true);
- $this->testEncodingAndDecodingSamplesFromDiConfiguredInstance();
- // Disable DotStuffing to continue
- Swift_Preferences::getInstance()->setQPDotEscape(false);
- }
-
- // -- Private Methods
-
- private function _createEncoderFromContainer()
- {
- return Swift_DependencyContainer::getInstance()
- ->lookup('mime.qpcontentencoder')
- ;
- }
-
- }
|