BufferStreamTest.php 2.0 KB

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