| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Tests\Psr7;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Psr7\BufferStream;
|
|---|
| 5 |
|
|---|
| 6 | class BufferStreamTest extends \PHPUnit_Framework_TestCase
|
|---|
| 7 | {
|
|---|
| 8 | public function testHasMetadata()
|
|---|
| 9 | {
|
|---|
| 10 | $b = new BufferStream(10);
|
|---|
| 11 | $this->assertTrue($b->isReadable());
|
|---|
| 12 | $this->assertTrue($b->isWritable());
|
|---|
| 13 | $this->assertFalse($b->isSeekable());
|
|---|
| 14 | $this->assertEquals(null, $b->getMetadata('foo'));
|
|---|
| 15 | $this->assertEquals(10, $b->getMetadata('hwm'));
|
|---|
| 16 | $this->assertEquals([], $b->getMetadata());
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | public function testRemovesReadDataFromBuffer()
|
|---|
| 20 | {
|
|---|
| 21 | $b = new BufferStream();
|
|---|
| 22 | $this->assertEquals(3, $b->write('foo'));
|
|---|
| 23 | $this->assertEquals(3, $b->getSize());
|
|---|
| 24 | $this->assertFalse($b->eof());
|
|---|
| 25 | $this->assertEquals('foo', $b->read(10));
|
|---|
| 26 | $this->assertTrue($b->eof());
|
|---|
| 27 | $this->assertEquals('', $b->read(10));
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * @expectedException \RuntimeException
|
|---|
| 32 | * @expectedExceptionMessage Cannot determine the position of a BufferStream
|
|---|
| 33 | */
|
|---|
| 34 | public function testCanCastToStringOrGetContents()
|
|---|
| 35 | {
|
|---|
| 36 | $b = new BufferStream();
|
|---|
| 37 | $b->write('foo');
|
|---|
| 38 | $b->write('baz');
|
|---|
| 39 | $this->assertEquals('foo', $b->read(3));
|
|---|
| 40 | $b->write('bar');
|
|---|
| 41 | $this->assertEquals('bazbar', (string) $b);
|
|---|
| 42 | $b->tell();
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public function testDetachClearsBuffer()
|
|---|
| 46 | {
|
|---|
| 47 | $b = new BufferStream();
|
|---|
| 48 | $b->write('foo');
|
|---|
| 49 | $b->detach();
|
|---|
| 50 | $this->assertTrue($b->eof());
|
|---|
| 51 | $this->assertEquals(3, $b->write('abc'));
|
|---|
| 52 | $this->assertEquals('abc', $b->read(10));
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public function testExceedingHighwaterMarkReturnsFalseButStillBuffers()
|
|---|
| 56 | {
|
|---|
| 57 | $b = new BufferStream(5);
|
|---|
| 58 | $this->assertEquals(3, $b->write('hi '));
|
|---|
| 59 | $this->assertFalse($b->write('hello'));
|
|---|
| 60 | $this->assertEquals('hi hello', (string) $b);
|
|---|
| 61 | $this->assertEquals(4, $b->write('test'));
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|