Base64ContentEncoderAcceptanceTest.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once 'Swift/Mime/ContentEncoder/Base64ContentEncoder.php';
  3. require_once 'Swift/ByteStream/ArrayByteStream.php';
  4. class Swift_Mime_ContentEncoder_Base64ContentEncoderAcceptanceTest
  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_Base64ContentEncoder();
  13. }
  14. public function testEncodingAndDecodingSamples()
  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. $os = new Swift_ByteStream_ArrayByteStream();
  35. $os->write($text);
  36. $is = new Swift_ByteStream_ArrayByteStream();
  37. $this->_encoder->encodeByteStream($os, $is);
  38. $encoded = '';
  39. while (false !== $bytes = $is->read(8192))
  40. {
  41. $encoded .= $bytes;
  42. }
  43. $this->assertEqual(
  44. base64_decode($encoded), $text,
  45. '%s: Encoded string should decode back to original string for sample ' .
  46. $sampleDir . '/' . $sampleFile
  47. );
  48. }
  49. closedir($fileFp);
  50. }
  51. }
  52. closedir($sampleFp);
  53. }
  54. }