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