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:
<?php
namespace Guzzle\Tests\Stream;
use Guzzle\Stream\Stream;
class StreamTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testConstructorThrowsExceptionOnInvalidArgument()
{
$stream = new Stream(true);
}
public function testConstructor()
{
$handle = fopen('php://temp', 'r+');
fwrite($handle, 'data');
$stream = new Stream($handle);
$this->assertEquals($handle, $stream->getStream());
$this->assertTrue($stream->isReadable());
$this->assertTrue($stream->isWritable());
$this->assertTrue($stream->isLocal());
$this->assertTrue($stream->isSeekable());
$this->assertEquals('PHP', $stream->getWrapper());
$this->assertEquals('TEMP', $stream->getStreamType());
$this->assertEquals(4, $stream->getSize());
$this->assertEquals('php://temp', $stream->getUri());
$this->assertEquals(array(), $stream->getWrapperData());
$this->assertFalse($stream->isConsumed());
unset($stream);
}
public function testCanModifyStream()
{
$handle1 = fopen('php://temp', 'r+');
$handle2 = fopen('php://temp', 'r+');
$stream = new Stream($handle1);
$this->assertSame($handle1, $stream->getStream());
$stream->setStream($handle2, 10);
$this->assertEquals(10, $stream->getSize());
$this->assertSame($handle2, $stream->getStream());
}
public function testStreamClosesHandleOnDestruct()
{
$handle = fopen('php://temp', 'r');
$stream = new Stream($handle);
unset($stream);
$this->assertFalse(is_resource($handle));
}
public function testConvertsToString()
{
$handle = fopen('php://temp', 'w+');
fwrite($handle, 'data');
$stream = new Stream($handle);
$this->assertEquals('data', (string) $stream);
unset($stream);
$handle = fopen(__DIR__ . '/../TestData/FileBody.txt', 'r');
$stream = new Stream($handle);
$this->assertEquals('', (string) $stream);
unset($stream);
}
public function testConvertsToStringAndRestoresCursorPos()
{
$handle = fopen('php://temp', 'w+');
$stream = new Stream($handle);
$stream->write('foobazbar');
$stream->seek(3);
$this->assertEquals('foobazbar', (string) $stream);
$this->assertEquals(3, $stream->ftell());
}
public function testIsConsumed()
{
$handle = fopen('php://temp', 'w+');
fwrite($handle, 'data');
$stream = new Stream($handle);
$this->assertFalse($stream->isConsumed());
$stream->read(4);
$this->assertTrue($stream->isConsumed());
}
public function testAllowsSettingManualSize()
{
$handle = fopen('php://temp', 'w+');
fwrite($handle, 'data');
$stream = new Stream($handle);
$stream->setSize(10);
$this->assertEquals(10, $stream->getSize());
unset($stream);
}
public function testWrapsStream()
{
$handle = fopen('php://temp', 'w+');
fwrite($handle, 'data');
$stream = new Stream($handle);
$this->assertTrue($stream->isSeekable());
$this->assertTrue($stream->isReadable());
$this->assertTrue($stream->seek(0));
$this->assertEquals('da', $stream->read(2));
$this->assertEquals('ta', $stream->read(2));
$this->assertTrue($stream->seek(0));
$this->assertEquals('data', $stream->read(4));
$stream->write('_appended');
$stream->seek(0);
$this->assertEquals('data_appended', $stream->read(13));
}
public function testGetSize()
{
$size = filesize(__DIR__ . '/../../../bootstrap.php');
$handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
$stream = new Stream($handle);
$this->assertEquals($handle, $stream->getStream());
$this->assertEquals($size, $stream->getSize());
$this->assertEquals($size, $stream->getSize());
unset($stream);
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$handle = fopen('http://127.0.0.1:' . $this->getServer()->getPort(), 'r');
$stream = new Stream($handle);
$this->assertEquals(false, $stream->getSize());
unset($stream);
}
public function testEnsuresSizeIsConsistent()
{
$h = fopen('php://temp', 'r+');
fwrite($h, 'foo');
$stream = new Stream($h);
$this->assertEquals(3, $stream->getSize());
$stream->write('test');
$this->assertEquals(7, $stream->getSize());
fclose($h);
}
public function testAbstractsMetaData()
{
$handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
$stream = new Stream($handle);
$this->assertEquals('plainfile', $stream->getMetaData('wrapper_type'));
$this->assertEquals(null, $stream->getMetaData('wrapper_data'));
$this->assertInternalType('array', $stream->getMetaData());
}
public function testDoesNotAttemptToWriteToReadonlyStream()
{
$handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
$stream = new Stream($handle);
$this->assertEquals(0, $stream->write('foo'));
}
public function testProvidesStreamPosition()
{
$handle = fopen(__DIR__ . '/../../../bootstrap.php', 'r');
$stream = new Stream($handle);
$stream->read(2);
$this->assertSame(ftell($handle), $stream->ftell());
$this->assertEquals(2, $stream->ftell());
}
public function testRewindIsSeekZero()
{
$stream = new Stream(fopen('php://temp', 'w+'));
$stream->write('foobazbar');
$this->assertTrue($stream->rewind());
$this->assertEquals('foobazbar', $stream->read(9));
}
public function testCanDetachStream()
{
$r = fopen('php://temp', 'w+');
$stream = new Stream($r);
$stream->detachStream();
$this->assertNull($stream->getStream());
}
}