1<?php 2namespace GuzzleHttp\Tests\Stream; 3 4use GuzzleHttp\Stream\BufferStream; 5use GuzzleHttp\Stream\Exception\CannotAttachException; 6use PHPUnit\Framework\TestCase; 7 8class BufferStreamTest extends TestCase 9{ 10 public function testHasMetadata() 11 { 12 $b = new BufferStream(10); 13 $this->assertTrue($b->isReadable()); 14 $this->assertTrue($b->isWritable()); 15 $this->assertFalse($b->isSeekable()); 16 $this->assertEquals(null, $b->getMetadata('foo')); 17 $this->assertEquals(10, $b->getMetadata('hwm')); 18 $this->assertEquals([], $b->getMetadata()); 19 } 20 21 public function testRemovesReadDataFromBuffer() 22 { 23 $b = new BufferStream(); 24 $this->assertEquals(3, $b->write('foo')); 25 $this->assertEquals(3, $b->getSize()); 26 $this->assertFalse($b->eof()); 27 $this->assertEquals('foo', $b->read(10)); 28 $this->assertTrue($b->eof()); 29 $this->assertEquals('', $b->read(10)); 30 } 31 32 public function testCanCastToStringOrGetContents() 33 { 34 $b = new BufferStream(); 35 $b->write('foo'); 36 $b->write('baz'); 37 $this->assertEquals('foo', $b->read(3)); 38 $b->write('bar'); 39 $this->assertEquals('bazbar', (string) $b); 40 $this->assertFalse($b->tell()); 41 } 42 43 public function testDetachClearsBuffer() 44 { 45 $b = new BufferStream(); 46 $b->write('foo'); 47 $b->detach(); 48 $this->assertEquals(0, $b->tell()); 49 $this->assertTrue($b->eof()); 50 $this->assertEquals(3, $b->write('abc')); 51 $this->assertEquals('abc', $b->read(10)); 52 } 53 54 public function testExceedingHighwaterMarkReturnsFalseButStillBuffers() 55 { 56 $b = new BufferStream(5); 57 $this->assertEquals(3, $b->write('hi ')); 58 $this->assertFalse($b->write('hello')); 59 $this->assertEquals('hi hello', (string) $b); 60 $this->assertEquals(4, $b->write('test')); 61 } 62 63 public function testCannotAttach() 64 { 65 $this->expectException(CannotAttachException::class); 66 $p = new BufferStream(); 67 $p->attach('a'); 68 } 69} 70