PumpStreamTest.php 2.3 KB

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