MediaMimeStreamTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Gdata/MediaMimeStream.php';
  22. /**
  23. * @package Zend_Gdata
  24. * @subpackage UnitTests
  25. */
  26. class Zend_Gdata_MediaMimeStreamTest extends PHPUnit_Framework_TestCase
  27. {
  28. public function setUp()
  29. {
  30. $this->locationOfFakeBinary =
  31. 'Zend/Gdata/_files/MediaMimeStreamSample1.txt';
  32. $this->smallXMLString = '<xml><entry><title>foo</title></entry>';
  33. $this->testMediaType = 'video/mpeg';
  34. $this->mediaMimeStream = new Zend_Gdata_MediaMimeStream(
  35. $this->smallXMLString, $this->locationOfFakeBinary,
  36. $this->testMediaType);
  37. $this->exceptedLenOfMimeMessage = 283;
  38. }
  39. public function testExceptionOnUnreadableFile()
  40. {
  41. $exceptionThrown = false;
  42. try {
  43. $mediaMimeStream = new Zend_Gdata_MediaMimeStream(
  44. $this->smallXMLString, '/non/existant/path/to/nowhere');
  45. } catch (Zend_Gdata_App_IOException $e) {
  46. $exceptionThrown = true;
  47. }
  48. $this->assertTrue($exceptionThrown, 'Was expecting an exception on ' .
  49. 'attempting to read an unreadable or non-existant file');
  50. }
  51. public function testGetTotalSize()
  52. {
  53. $this->assertEquals($this->exceptedLenOfMimeMessage,
  54. $this->mediaMimeStream->getTotalSize());
  55. }
  56. public function testHasData()
  57. {
  58. $this->assertTrue($this->mediaMimeStream->hasData());
  59. }
  60. public function testGetContentType()
  61. {
  62. $pattern =
  63. '/multipart\/related;\sboundary=\"=_[a-z0-9]{32,}.*\"/';
  64. $this->assertEquals(1, preg_match($pattern,
  65. $this->mediaMimeStream->getContentType()));
  66. }
  67. /**
  68. * Ensure that nothing breaks if we read past the end of the messsage in a
  69. * single read.
  70. *
  71. * Note: The test message has the following part sizes in length:
  72. * 211, 22, 39 for a total size of 272. This test performs a single read
  73. * for 400 bytes.
  74. */
  75. public function testReadAll()
  76. {
  77. $this->assertEquals($this->exceptedLenOfMimeMessage,
  78. $this->mediaMimeStream->getTotalSize());
  79. $outputArray = array();
  80. while ($this->mediaMimeStream->hasData()) {
  81. $outputArray = explode("\r\n", $this->mediaMimeStream->read(400));
  82. }
  83. $mimeBoundaryPattern = '/--=_[a-z0-9]{32,}/';
  84. $mimeClosingBoundaryPattern = '/--=_[a-z0-9]{32,}--/';
  85. $this->assertEquals('', $outputArray[0]);
  86. $this->assertEquals(1,
  87. preg_match($mimeBoundaryPattern, $outputArray[1]));
  88. $this->assertEquals('Content-Type: application/atom+xml',
  89. $outputArray[2]);
  90. $this->assertEquals('', $outputArray[3]);
  91. $this->assertEquals($this->smallXMLString, $outputArray[4]);
  92. $this->assertEquals('', $outputArray[5]);
  93. $this->assertEquals(1,
  94. preg_match($mimeBoundaryPattern, $outputArray[6]));
  95. $this->assertEquals('Content-Type: video/mpeg', $outputArray[7]);
  96. $this->assertEquals('Content-Transfer-Encoding: binary',
  97. $outputArray[8]);
  98. $this->assertEquals('', $outputArray[9]);
  99. $this->assertEquals(file_get_contents($this->locationOfFakeBinary),
  100. $outputArray[10]);
  101. $this->assertEquals(1,
  102. preg_match($mimeClosingBoundaryPattern, $outputArray[11]));
  103. }
  104. /**
  105. * Ensure that a variety of different stream sizes work.
  106. *
  107. * Note: The test message has the following part sizes in length:
  108. * 211, 22, 39 for a total size of 287.
  109. */
  110. public function testReadVariousBufferSizes()
  111. {
  112. $bufferSizesToTest = array(2, 20, 33, 44, 88, 100, 201);
  113. foreach($bufferSizesToTest as $sizeToTest) {
  114. $mediaMimeStream = new Zend_Gdata_MediaMimeStream(
  115. $this->smallXMLString, $this->locationOfFakeBinary,
  116. $this->testMediaType);
  117. $this->assertEquals($sizeToTest,
  118. strlen($mediaMimeStream->read($sizeToTest)));
  119. }
  120. }
  121. /**
  122. * Ensure that nothing breaks if we read a message 1 byte at time.
  123. */
  124. public function testReadWithoutCrossingSections()
  125. {
  126. $outputString = '';
  127. while ($this->mediaMimeStream->hasData()) {
  128. $outputString .= $this->mediaMimeStream->read(1);
  129. }
  130. $this->assertEquals($this->exceptedLenOfMimeMessage,
  131. strlen($outputString));
  132. }
  133. /**
  134. * Ensure that nothing breaks if we read past at least two sections of
  135. * the message.
  136. *
  137. * Note: The test message has the following part sizes in length:
  138. * 211, 22, 39 for a total size of 272. This test reads 250 bytes at a time
  139. * to make sure that we cross sections 1 and 2 and then read part of
  140. * section 3.
  141. */
  142. public function testReadCrossing2Sections()
  143. {
  144. $outputString = '';
  145. while ($this->mediaMimeStream->hasData()) {
  146. $outputString .= $this->mediaMimeStream->read(250);
  147. }
  148. $this->assertEquals($this->exceptedLenOfMimeMessage,
  149. strlen($outputString));
  150. }
  151. /**
  152. * Ensure that nothing breaks if we read past at least one section of
  153. * the message.
  154. *
  155. * Note: The test message has the following part sizes in length:
  156. * 211, 22, 39 for a total size of 272. This test reads 230 bytes at a time
  157. * to make sure that we cross section 1 and then read sections 2 and 3.
  158. */
  159. public function testReadCrossing1Section()
  160. {
  161. $outputString = '';
  162. while ($this->mediaMimeStream->hasData()) {
  163. $outputString .= $this->mediaMimeStream->read(230);
  164. }
  165. $this->assertEquals($this->exceptedLenOfMimeMessage,
  166. strlen($outputString));
  167. }
  168. }