PartTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mime
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Mime_Part
  24. */
  25. require_once 'Zend/Mime/Part.php';
  26. /**
  27. * PHPUnit test case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Mime
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Mime
  37. */
  38. class Zend_Mime_PartTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * MIME part test object
  42. *
  43. * @var Zend_Mime_Part
  44. */
  45. protected $_part = null;
  46. protected $_testText;
  47. protected function setUp()
  48. {
  49. $this->_testText = 'safdsafsa�lg ��gd�� sd�jg�sdjg�ld�gksd�gj�sdfg�dsj�gjsd�gj�dfsjg�dsfj�djs�g kjhdkj '
  50. . 'fgaskjfdh gksjhgjkdh gjhfsdghdhgksdjhg';
  51. $this->part = new Zend_Mime_Part($this->_testText);
  52. $this->part->encoding = Zend_Mime::ENCODING_BASE64;
  53. $this->part->type = "text/plain";
  54. $this->part->filename = 'test.txt';
  55. $this->part->disposition = 'attachment';
  56. $this->part->charset = 'iso8859-1';
  57. $this->part->id = '4711';
  58. }
  59. public function testHeaders()
  60. {
  61. $expectedHeaders = array('Content-Type: text/plain',
  62. 'Content-Transfer-Encoding: ' . Zend_Mime::ENCODING_BASE64,
  63. 'Content-Disposition: attachment',
  64. 'filename="test.txt"',
  65. 'charset=iso8859-1',
  66. 'Content-ID: <4711>');
  67. $actual = $this->part->getHeaders();
  68. foreach ($expectedHeaders as $expected) {
  69. $this->assertContains($expected, $actual);
  70. }
  71. }
  72. public function testContentEncoding()
  73. {
  74. // Test with base64 encoding
  75. $content = $this->part->getContent();
  76. $this->assertEquals($this->_testText, base64_decode($content));
  77. // Test with quotedPrintable Encoding:
  78. $this->part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  79. $content = $this->part->getContent();
  80. $this->assertEquals($this->_testText, quoted_printable_decode($content));
  81. // Test with 8Bit encoding
  82. $this->part->encoding = Zend_Mime::ENCODING_8BIT;
  83. $content = $this->part->getContent();
  84. $this->assertEquals($this->_testText, $content);
  85. }
  86. public function testStreamEncoding()
  87. {
  88. $testfile = realpath(__FILE__);
  89. $original = file_get_contents($testfile);
  90. // Test Base64
  91. $fp = fopen($testfile,'rb');
  92. $this->assertTrue(is_resource($fp));
  93. $part = new Zend_Mime_Part($fp);
  94. $part->encoding = Zend_Mime::ENCODING_BASE64;
  95. $fp2 = $part->getEncodedStream();
  96. $this->assertTrue(is_resource($fp2));
  97. $encoded = stream_get_contents($fp2);
  98. fclose($fp);
  99. $this->assertEquals(base64_decode($encoded),$original);
  100. // test QuotedPrintable
  101. $fp = fopen($testfile,'rb');
  102. $this->assertTrue(is_resource($fp));
  103. $part = new Zend_Mime_Part($fp);
  104. $part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
  105. $fp2 = $part->getEncodedStream();
  106. $this->assertTrue(is_resource($fp2));
  107. $encoded = stream_get_contents($fp2);
  108. fclose($fp);
  109. $this->assertEquals(quoted_printable_decode($encoded),$original);
  110. }
  111. }