BufferStreamTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\BufferStream;
  4. use GuzzleHttp\Stream\Exception\CannotAttachException;
  5. use PHPUnit\Framework\TestCase;
  6. class BufferStreamTest extends 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. public function testRemovesReadDataFromBuffer()
  19. {
  20. $b = new BufferStream();
  21. $this->assertEquals(3, $b->write('foo'));
  22. $this->assertEquals(3, $b->getSize());
  23. $this->assertFalse($b->eof());
  24. $this->assertEquals('foo', $b->read(10));
  25. $this->assertTrue($b->eof());
  26. $this->assertEquals('', $b->read(10));
  27. }
  28. public function testCanCastToStringOrGetContents()
  29. {
  30. $b = new BufferStream();
  31. $b->write('foo');
  32. $b->write('baz');
  33. $this->assertEquals('foo', $b->read(3));
  34. $b->write('bar');
  35. $this->assertEquals('bazbar', (string) $b);
  36. $this->assertFalse($b->tell());
  37. }
  38. public function testDetachClearsBuffer()
  39. {
  40. $b = new BufferStream();
  41. $b->write('foo');
  42. $b->detach();
  43. $this->assertEquals(0, $b->tell());
  44. $this->assertTrue($b->eof());
  45. $this->assertEquals(3, $b->write('abc'));
  46. $this->assertEquals('abc', $b->read(10));
  47. }
  48. public function testExceedingHighwaterMarkReturnsFalseButStillBuffers()
  49. {
  50. $b = new BufferStream(5);
  51. $this->assertEquals(3, $b->write('hi '));
  52. $this->assertFalse($b->write('hello'));
  53. $this->assertEquals('hi hello', (string) $b);
  54. $this->assertEquals(4, $b->write('test'));
  55. }
  56. public function testCannotAttach()
  57. {
  58. $this->expectException(CannotAttachException::class);
  59. $p = new BufferStream();
  60. $p->attach('a');
  61. }
  62. }