Open Journal Systems  3.3.0
CachingEntityBodyTest.php
1 <?php
2 
3 namespace Guzzle\Tests\Http;
4 
7 
12 {
14  protected $body;
15 
17  protected $decorated;
18 
19  public function setUp()
20  {
21  $this->decorated = EntityBody::factory('testing');
22  $this->body = new CachingEntityBody($this->decorated);
23  }
24 
25  public function testUsesRemoteSizeIfPossible()
26  {
27  $body = EntityBody::factory('test');
28  $caching = new CachingEntityBody($body);
29  $this->assertEquals(4, $caching->getSize());
30  $this->assertEquals(4, $caching->getContentLength());
31  }
32 
37  public function testDoesNotAllowRewindFunction()
38  {
39  $this->body->setRewindFunction(true);
40  }
41 
46  public function testCannotSeekPastWhatHasBeenRead()
47  {
48  $this->body->seek(10);
49  }
50 
55  public function testCannotUseSeekEnd()
56  {
57  $this->body->seek(2, SEEK_END);
58  }
59 
61  {
62  $size = filesize(__FILE__);
63  $s = fopen(__FILE__, 'r');
64  $this->body->setStream($s, $size);
65  $this->assertEquals($size, $this->body->getSize());
66  $this->assertEquals($size, $this->decorated->getSize());
67  $this->assertSame($s, $this->body->getStream());
68  $this->assertSame($s, $this->decorated->getStream());
69  }
70 
71  public function testRewindUsesSeek()
72  {
73  $a = EntityBody::factory('foo');
74  $d = $this->getMockBuilder('Guzzle\Http\CachingEntityBody')
75  ->setMethods(array('seek'))
76  ->setConstructorArgs(array($a))
77  ->getMock();
78  $d->expects($this->once())
79  ->method('seek')
80  ->with(0)
81  ->will($this->returnValue(true));
82  $d->rewind();
83  }
84 
85  public function testCanSeekToReadBytes()
86  {
87  $this->assertEquals('te', $this->body->read(2));
88  $this->body->seek(0);
89  $this->assertEquals('test', $this->body->read(4));
90  $this->assertEquals(4, $this->body->ftell());
91  $this->body->seek(2);
92  $this->assertEquals(2, $this->body->ftell());
93  $this->body->seek(2, SEEK_CUR);
94  $this->assertEquals(4, $this->body->ftell());
95  $this->assertEquals('ing', $this->body->read(3));
96  }
97 
98  public function testWritesToBufferStream()
99  {
100  $this->body->read(2);
101  $this->body->write('hi');
102  $this->body->rewind();
103  $this->assertEquals('tehiing', (string) $this->body);
104  }
105 
106  public function testReadLinesFromBothStreams()
107  {
108  $this->body->seek($this->body->ftell());
109  $this->body->write("test\n123\nhello\n1234567890\n");
110  $this->body->rewind();
111  $this->assertEquals("test\n", $this->body->readLine(7));
112  $this->assertEquals("123\n", $this->body->readLine(7));
113  $this->assertEquals("hello\n", $this->body->readLine(7));
114  $this->assertEquals("123456", $this->body->readLine(7));
115  $this->assertEquals("7890\n", $this->body->readLine(7));
116  // We overwrote the decorated stream, so no more data
117  $this->assertEquals('', $this->body->readLine(7));
118  }
119 
120  public function testSkipsOverwrittenBytes()
121  {
123  implode("\n", array_map(function ($n) {
124  return str_pad($n, 4, '0', STR_PAD_LEFT);
125  }, range(0, 25)))
126  );
127 
129 
130  $this->assertEquals("0000\n", $body->readLine());
131  $this->assertEquals("0001\n", $body->readLine());
132  // Write over part of the body yet to be read, so skip some bytes
133  $this->assertEquals(5, $body->write("TEST\n"));
134  $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
135  // Read, which skips bytes, then reads
136  $this->assertEquals("0003\n", $body->readLine());
137  $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
138  $this->assertEquals("0004\n", $body->readLine());
139  $this->assertEquals("0005\n", $body->readLine());
140 
141  // Overwrite part of the cached body (so don't skip any bytes)
142  $body->seek(5);
143  $this->assertEquals(5, $body->write("ABCD\n"));
144  $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
145  $this->assertEquals("TEST\n", $body->readLine());
146  $this->assertEquals("0003\n", $body->readLine());
147  $this->assertEquals("0004\n", $body->readLine());
148  $this->assertEquals("0005\n", $body->readLine());
149  $this->assertEquals("0006\n", $body->readLine());
150  $this->assertEquals(5, $body->write("1234\n"));
151  $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
152 
153  // Seek to 0 and ensure the overwritten bit is replaced
154  $body->rewind();
155  $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
156 
157  // Ensure that casting it to a string does not include the bit that was overwritten
158  $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
159  }
160 
161  public function testWrapsContentType()
162  {
163  $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
164  ->setMethods(array('getContentType'))
165  ->setConstructorArgs(array(fopen(__FILE__, 'r')))
166  ->getMock();
167  $a->expects($this->once())
168  ->method('getContentType')
169  ->will($this->returnValue('foo'));
170  $d = new CachingEntityBody($a);
171  $this->assertEquals('foo', $d->getContentType());
172  }
173 
174  public function testWrapsContentEncoding()
175  {
176  $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
177  ->setMethods(array('getContentEncoding'))
178  ->setConstructorArgs(array(fopen(__FILE__, 'r')))
179  ->getMock();
180  $a->expects($this->once())
181  ->method('getContentEncoding')
182  ->will($this->returnValue('foo'));
183  $d = new CachingEntityBody($a);
184  $this->assertEquals('foo', $d->getContentEncoding());
185  }
186 
187  public function testWrapsMetadata()
188  {
189  $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
190  ->setMethods(array('getMetadata', 'getWrapper', 'getWrapperData', 'getStreamType', 'getUri'))
191  ->setConstructorArgs(array(fopen(__FILE__, 'r')))
192  ->getMock();
193 
194  $a->expects($this->once())
195  ->method('getMetadata')
196  ->will($this->returnValue(array()));
197  // Called twice for getWrapper and getWrapperData
198  $a->expects($this->exactly(1))
199  ->method('getWrapper')
200  ->will($this->returnValue('wrapper'));
201  $a->expects($this->once())
202  ->method('getWrapperData')
203  ->will($this->returnValue(array()));
204  $a->expects($this->once())
205  ->method('getStreamType')
206  ->will($this->returnValue('baz'));
207  $a->expects($this->once())
208  ->method('getUri')
209  ->will($this->returnValue('path/to/foo'));
210 
211  $d = new CachingEntityBody($a);
212  $this->assertEquals(array(), $d->getMetaData());
213  $this->assertEquals('wrapper', $d->getWrapper());
214  $this->assertEquals(array(), $d->getWrapperData());
215  $this->assertEquals('baz', $d->getStreamType());
216  $this->assertEquals('path/to/foo', $d->getUri());
217  }
218 
219  public function testWrapsCustomData()
220  {
221  $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
222  ->setMethods(array('getCustomData', 'setCustomData'))
223  ->setConstructorArgs(array(fopen(__FILE__, 'r')))
224  ->getMock();
225 
226  $a->expects($this->exactly(1))
227  ->method('getCustomData')
228  ->with('foo')
229  ->will($this->returnValue('bar'));
230 
231  $a->expects($this->exactly(1))
232  ->method('setCustomData')
233  ->with('foo', 'bar')
234  ->will($this->returnSelf());
235 
236  $d = new CachingEntityBody($a);
237  $this->assertSame($d, $d->setCustomData('foo', 'bar'));
238  $this->assertEquals('bar', $d->getCustomData('foo'));
239  }
240 
241  public function testClosesBothStreams()
242  {
243  $s = fopen('php://temp', 'r');
244  $a = EntityBody::factory($s);
245  $d = new CachingEntityBody($a);
246  $d->close();
247  $this->assertFalse(is_resource($s));
248  }
249 }
Guzzle\Tests\Http\CachingEntityBodyTest\testWrapsMetadata
testWrapsMetadata()
Definition: CachingEntityBodyTest.php:193
Guzzle\Tests\Http\CachingEntityBodyTest\$body
$body
Definition: CachingEntityBodyTest.php:17
Guzzle\Tests\Http\CachingEntityBodyTest\testChangingUnderlyingStreamUpdatesSizeAndStream
testChangingUnderlyingStreamUpdatesSizeAndStream()
Definition: CachingEntityBodyTest.php:66
Guzzle\Tests\Http\CachingEntityBodyTest\testCannotUseSeekEnd
testCannotUseSeekEnd()
Definition: CachingEntityBodyTest.php:61
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Http\CachingEntityBodyTest\setUp
setUp()
Definition: CachingEntityBodyTest.php:25
Guzzle\Tests\Http\CachingEntityBodyTest\$decorated
$decorated
Definition: CachingEntityBodyTest.php:23
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Http\CachingEntityBody
Definition: CachingEntityBody.php:10
Guzzle\Tests\Http\CachingEntityBodyTest\testWritesToBufferStream
testWritesToBufferStream()
Definition: CachingEntityBodyTest.php:104
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Tests\Http\CachingEntityBodyTest\testCanSeekToReadBytes
testCanSeekToReadBytes()
Definition: CachingEntityBodyTest.php:91
Guzzle\Tests\Http\CachingEntityBodyTest\testClosesBothStreams
testClosesBothStreams()
Definition: CachingEntityBodyTest.php:247
Guzzle\Tests\Http\CachingEntityBodyTest\testUsesRemoteSizeIfPossible
testUsesRemoteSizeIfPossible()
Definition: CachingEntityBodyTest.php:31
Guzzle\Tests\Http\CachingEntityBodyTest\testWrapsCustomData
testWrapsCustomData()
Definition: CachingEntityBodyTest.php:225
Guzzle\Tests\Http\CachingEntityBodyTest\testWrapsContentEncoding
testWrapsContentEncoding()
Definition: CachingEntityBodyTest.php:180
Guzzle\Tests\Http\CachingEntityBodyTest\testDoesNotAllowRewindFunction
testDoesNotAllowRewindFunction()
Definition: CachingEntityBodyTest.php:43
Guzzle\Tests\Http\CachingEntityBodyTest\testReadLinesFromBothStreams
testReadLinesFromBothStreams()
Definition: CachingEntityBodyTest.php:112
Guzzle\Tests\Http\CachingEntityBodyTest\testWrapsContentType
testWrapsContentType()
Definition: CachingEntityBodyTest.php:167
Guzzle\Tests\Http\CachingEntityBodyTest\testRewindUsesSeek
testRewindUsesSeek()
Definition: CachingEntityBodyTest.php:77
Guzzle\Tests\Http\CachingEntityBodyTest
Definition: CachingEntityBodyTest.php:11
Guzzle\Tests\Http\CachingEntityBodyTest\testCannotSeekPastWhatHasBeenRead
testCannotSeekPastWhatHasBeenRead()
Definition: CachingEntityBodyTest.php:52
Guzzle\Tests\Http\CachingEntityBodyTest\testSkipsOverwrittenBytes
testSkipsOverwrittenBytes()
Definition: CachingEntityBodyTest.php:126
Guzzle\Tests\Http
Definition: AbstractEntityBodyDecoratorTest.php:3