PumpStreamTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7\LimitStream;
  4. use RingCentral\Psr7\PumpStream;
  5. use RingCentral\Psr7;
  6. class PumpStreamTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testHasMetadataAndSize()
  9. {
  10. $p = new PumpStream(function () {}, array(
  11. 'metadata' => array('foo' => 'bar'),
  12. 'size' => 100
  13. ));
  14. $this->assertEquals('bar', $p->getMetadata('foo'));
  15. $this->assertEquals(array('foo' => 'bar'), $p->getMetadata());
  16. $this->assertEquals(100, $p->getSize());
  17. }
  18. public function testCanReadFromCallable()
  19. {
  20. $p = Psr7\stream_for(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 = array();
  31. $p = Psr7\stream_for(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(array(1, 9, 3), $called);
  40. }
  41. public function testInifiniteStreamWrappedInLimitStream()
  42. {
  43. $p = Psr7\stream_for(function () { return 'a'; });
  44. $s = new LimitStream($p, 5);
  45. $this->assertEquals('aaaaa', (string) $s);
  46. }
  47. public function testDescribesCapabilities()
  48. {
  49. $p = Psr7\stream_for(function () {});
  50. $this->assertTrue($p->isReadable());
  51. $this->assertFalse($p->isSeekable());
  52. $this->assertFalse($p->isWritable());
  53. $this->assertNull($p->getSize());
  54. $this->assertEquals('', $p->getContents());
  55. $this->assertEquals('', (string) $p);
  56. $p->close();
  57. $this->assertEquals('', $p->read(10));
  58. $this->assertTrue($p->eof());
  59. try {
  60. $this->assertFalse($p->write('aa'));
  61. $this->fail();
  62. } catch (\RuntimeException $e) {}
  63. }
  64. }