BufferStreamTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7\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(array(), $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. /**
  27. * @expectedException \RuntimeException
  28. * @expectedExceptionMessage Cannot determine the position of a BufferStream
  29. */
  30. public function testCanCastToStringOrGetContents()
  31. {
  32. $b = new BufferStream();
  33. $b->write('foo');
  34. $b->write('baz');
  35. $this->assertEquals('foo', $b->read(3));
  36. $b->write('bar');
  37. $this->assertEquals('bazbar', (string) $b);
  38. $b->tell();
  39. }
  40. public function testDetachClearsBuffer()
  41. {
  42. $b = new BufferStream();
  43. $b->write('foo');
  44. $b->detach();
  45. $this->assertTrue($b->eof());
  46. $this->assertEquals(3, $b->write('abc'));
  47. $this->assertEquals('abc', $b->read(10));
  48. }
  49. public function testExceedingHighwaterMarkReturnsFalseButStillBuffers()
  50. {
  51. $b = new BufferStream(5);
  52. $this->assertEquals(3, $b->write('hi '));
  53. $this->assertFalse($b->write('hello'));
  54. $this->assertEquals('hi hello', (string) $b);
  55. $this->assertEquals(4, $b->write('test'));
  56. }
  57. }