1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
<?php
class Swift_Mime_ContentEncoder_QpContentEncoderAcceptanceTest extends \PHPUnit_Framework_TestCase
{
private $_samplesDir;
private $_factory;
public function setUp()
{
$this->_samplesDir = realpath(__DIR__.'/../../../../_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->assertEquals(
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->assertEquals(
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->assertEquals("a\r\nb\r\nc", $encoder->encodeString("a\nb\nc"));
}
public function testEncodingCRTextWithDiConfiguredInstance()
{
$encoder = $this->_createEncoderFromContainer();
$this->assertEquals("a\r\nb\r\nc", $encoder->encodeString("a\rb\rc"));
}
public function testEncodingLFCRTextWithDiConfiguredInstance()
{
$encoder = $this->_createEncoderFromContainer();
$this->assertEquals("a\r\n\r\nb\r\n\r\nc", $encoder->encodeString("a\n\rb\n\rc"));
}
public function testEncodingCRLFTextWithDiConfiguredInstance()
{
$encoder = $this->_createEncoderFromContainer();
$this->assertEquals("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->assertEquals("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->assertEquals("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 function _createEncoderFromContainer()
{
return Swift_DependencyContainer::getInstance()
->lookup('mime.qpcontentencoder')
;
}
}
|