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