PumpStreamTest.php 2.3 KB

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