PartTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @package Zend_Mime
  4. * @subpackage UnitTests
  5. */
  6. /**
  7. * Zend_Mime_Part
  8. */
  9. require_once 'Zend/Mime/Part.php';
  10. /**
  11. * PHPUnit test case
  12. */
  13. require_once 'PHPUnit/Framework/TestCase.php';
  14. /**
  15. * @package Zend_Mime
  16. * @subpackage UnitTests
  17. */
  18. class Zend_Mime_PartTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * MIME part test object
  22. *
  23. * @var Zend_Mime_Part
  24. */
  25. protected $_part = null;
  26. protected $_testText;
  27. protected function setUp()
  28. {
  29. $this->_testText = 'safdsafsa�lg ��gd�� sd�jg�sdjg�ld�gksd�gj�sdfg�dsj�gjsd�gj�dfsjg�dsfj�djs�g kjhdkj '
  30. . 'fgaskjfdh gksjhgjkdh gjhfsdghdhgksdjhg';
  31. $this->part = new Zend_Mime_Part($this->_testText);
  32. $this->part->encoding = Zend_Mime::ENCODING_BASE64;
  33. $this->part->type = "text/plain";
  34. $this->part->filename = 'test.txt';
  35. $this->part->disposition = 'attachment';
  36. $this->part->charset = 'iso8859-1';
  37. $this->part->id = '4711';
  38. }
  39. public function testHeaders()
  40. {
  41. $expectedHeaders = array('Content-Type: text/plain',
  42. 'Content-Transfer-Encoding: ' . Zend_Mime::ENCODING_BASE64,
  43. 'Content-Disposition: attachment',
  44. 'filename="test.txt"',
  45. 'charset=iso8859-1',
  46. 'Content-ID: <4711>');
  47. $actual = $this->part->getHeaders();
  48. foreach ($expectedHeaders as $expected) {
  49. $this->assertContains($expected, $actual);
  50. }
  51. }
  52. public function testContentEncoding()
  53. {
  54. // Test with base64 encoding
  55. $content = $this->part->getContent();
  56. $this->assertEquals($this->_testText, base64_decode($content));
  57. // Test with quotedPrintable Encoding:
  58. $this->part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  59. $content = $this->part->getContent();
  60. $this->assertEquals($this->_testText, quoted_printable_decode($content));
  61. // Test with 8Bit encoding
  62. $this->part->encoding = Zend_Mime::ENCODING_8BIT;
  63. $content = $this->part->getContent();
  64. $this->assertEquals($this->_testText, $content);
  65. }
  66. public function testStreamEncoding()
  67. {
  68. $testfile = realpath(__FILE__);
  69. $original = file_get_contents($testfile);
  70. // Test Base64
  71. $fp = fopen($testfile,'rb');
  72. $this->assertTrue(is_resource($fp));
  73. $part = new Zend_Mime_Part($fp);
  74. $part->encoding = Zend_Mime::ENCODING_BASE64;
  75. $fp2 = $part->getEncodedStream();
  76. $this->assertTrue(is_resource($fp2));
  77. $encoded = stream_get_contents($fp2);
  78. fclose($fp);
  79. $this->assertEquals(base64_decode($encoded),$original);
  80. // test QuotedPrintable
  81. $fp = fopen($testfile,'rb');
  82. $this->assertTrue(is_resource($fp));
  83. $part = new Zend_Mime_Part($fp);
  84. $part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  85. $fp2 = $part->getEncodedStream();
  86. $this->assertTrue(is_resource($fp2));
  87. $encoded = stream_get_contents($fp2);
  88. fclose($fp);
  89. $this->assertEquals(quoted_printable_decode($encoded),$original);
  90. }
  91. }