MediaMimeStreamTest.php 6.5 KB

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