PlainContentEncoderAcceptanceTest.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. require_once 'Swift/Mime/ContentEncoder/PlainContentEncoder.php';
  3. require_once 'Swift/ByteStream/ArrayByteStream.php';
  4. class Swift_Mime_ContentEncoder_PlainContentEncoderAcceptanceTest
  5. extends UnitTestCase
  6. {
  7. private $_samplesDir;
  8. private $_encoder;
  9. public function setUp()
  10. {
  11. $this->_samplesDir = realpath(dirname(__FILE__) . '/../../../../_samples/charsets');
  12. $this->_encoder = new Swift_Mime_ContentEncoder_PlainContentEncoder('8bit');
  13. }
  14. public function testEncodingAndDecodingSamplesString()
  15. {
  16. $sampleFp = opendir($this->_samplesDir);
  17. while (false !== $encodingDir = readdir($sampleFp))
  18. {
  19. if (substr($encodingDir, 0, 1) == '.')
  20. {
  21. continue;
  22. }
  23. $sampleDir = $this->_samplesDir . '/' . $encodingDir;
  24. if (is_dir($sampleDir))
  25. {
  26. $fileFp = opendir($sampleDir);
  27. while (false !== $sampleFile = readdir($fileFp))
  28. {
  29. if (substr($sampleFile, 0, 1) == '.')
  30. {
  31. continue;
  32. }
  33. $text = file_get_contents($sampleDir . '/' . $sampleFile);
  34. $encodedText = $this->_encoder->encodeString($text);
  35. $this->assertEqual(
  36. $encodedText, $text,
  37. '%s: Encoded string should be identical to original string for sample ' .
  38. $sampleDir . '/' . $sampleFile
  39. );
  40. }
  41. closedir($fileFp);
  42. }
  43. }
  44. closedir($sampleFp);
  45. }
  46. public function testEncodingAndDecodingSamplesByteStream()
  47. {
  48. $sampleFp = opendir($this->_samplesDir);
  49. while (false !== $encodingDir = readdir($sampleFp))
  50. {
  51. if (substr($encodingDir, 0, 1) == '.')
  52. {
  53. continue;
  54. }
  55. $sampleDir = $this->_samplesDir . '/' . $encodingDir;
  56. if (is_dir($sampleDir))
  57. {
  58. $fileFp = opendir($sampleDir);
  59. while (false !== $sampleFile = readdir($fileFp))
  60. {
  61. if (substr($sampleFile, 0, 1) == '.')
  62. {
  63. continue;
  64. }
  65. $text = file_get_contents($sampleDir . '/' . $sampleFile);
  66. $os = new Swift_ByteStream_ArrayByteStream();
  67. $os->write($text);
  68. $is = new Swift_ByteStream_ArrayByteStream();
  69. $this->_encoder->encodeByteStream($os, $is);
  70. $encoded = '';
  71. while (false !== $bytes = $is->read(8192))
  72. {
  73. $encoded .= $bytes;
  74. }
  75. $this->assertEqual(
  76. $encoded, $text,
  77. '%s: Encoded string should be identical to original string for sample ' .
  78. $sampleDir . '/' . $sampleFile
  79. );
  80. }
  81. closedir($fileFp);
  82. }
  83. }
  84. closedir($sampleFp);
  85. }
  86. }