1<?php 2namespace GuzzleHttp\Tests\Stream; 3 4use GuzzleHttp\Stream\Exception\CannotAttachException; 5use GuzzleHttp\Stream\LimitStream; 6use GuzzleHttp\Stream\PumpStream; 7use GuzzleHttp\Stream\Stream; 8use PHPUnit\Framework\TestCase; 9 10class PumpStreamTest extends TestCase 11{ 12 public function testHasMetadataAndSize() 13 { 14 $p = new PumpStream(function () {}, [ 15 'metadata' => ['foo' => 'bar'], 16 'size' => 100 17 ]); 18 19 $this->assertEquals('bar', $p->getMetadata('foo')); 20 $this->assertEquals(['foo' => 'bar'], $p->getMetadata()); 21 $this->assertEquals(100, $p->getSize()); 22 } 23 24 public function testCanReadFromCallable() 25 { 26 $p = Stream::factory(function ($size) { 27 return 'a'; 28 }); 29 $this->assertEquals('a', $p->read(1)); 30 $this->assertEquals(1, $p->tell()); 31 $this->assertEquals('aaaaa', $p->read(5)); 32 $this->assertEquals(6, $p->tell()); 33 } 34 35 public function testStoresExcessDataInBuffer() 36 { 37 $called = []; 38 $p = Stream::factory(function ($size) use (&$called) { 39 $called[] = $size; 40 return 'abcdef'; 41 }); 42 $this->assertEquals('a', $p->read(1)); 43 $this->assertEquals('b', $p->read(1)); 44 $this->assertEquals('cdef', $p->read(4)); 45 $this->assertEquals('abcdefabc', $p->read(9)); 46 $this->assertEquals([1, 9, 3], $called); 47 } 48 49 public function testInifiniteStreamWrappedInLimitStream() 50 { 51 $p = Stream::factory(function () { return 'a'; }); 52 $s = new LimitStream($p, 5); 53 $this->assertEquals('aaaaa', (string) $s); 54 } 55 56 public function testDescribesCapabilities() 57 { 58 $p = Stream::factory(function () {}); 59 $this->assertTrue($p->isReadable()); 60 $this->assertFalse($p->isSeekable()); 61 $this->assertFalse($p->isWritable()); 62 $this->assertNull($p->getSize()); 63 $this->assertFalse($p->write('aa')); 64 $this->assertEquals('', $p->getContents()); 65 $this->assertEquals('', (string) $p); 66 $p->close(); 67 $this->assertEquals('', $p->read(10)); 68 $this->assertTrue($p->eof()); 69 } 70 71 public function testCannotAttach() 72 { 73 $this->expectException(CannotAttachException::class); 74 $p = Stream::factory(function () {}); 75 $p->attach('a'); 76 } 77} 78