AppendStreamTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7\AppendStream;
  4. use RingCentral\Psr7;
  5. class AppendStreamTest extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @expectedException \InvalidArgumentException
  9. * @expectedExceptionMessage Each stream must be readable
  10. */
  11. public function testValidatesStreamsAreReadable()
  12. {
  13. $a = new AppendStream();
  14. $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  15. ->setMethods(array('isReadable'))
  16. ->getMockForAbstractClass();
  17. $s->expects($this->once())
  18. ->method('isReadable')
  19. ->will($this->returnValue(false));
  20. $a->addStream($s);
  21. }
  22. /**
  23. * @expectedException \RuntimeException
  24. * @expectedExceptionMessage The AppendStream can only seek with SEEK_SET
  25. */
  26. public function testValidatesSeekType()
  27. {
  28. $a = new AppendStream();
  29. $a->seek(100, SEEK_CUR);
  30. }
  31. /**
  32. * @expectedException \RuntimeException
  33. * @expectedExceptionMessage Unable to seek stream 0 of the AppendStream
  34. */
  35. public function testTriesToRewindOnSeek()
  36. {
  37. $a = new AppendStream();
  38. $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  39. ->setMethods(array('isReadable', 'rewind', 'isSeekable'))
  40. ->getMockForAbstractClass();
  41. $s->expects($this->once())
  42. ->method('isReadable')
  43. ->will($this->returnValue(true));
  44. $s->expects($this->once())
  45. ->method('isSeekable')
  46. ->will($this->returnValue(true));
  47. $s->expects($this->once())
  48. ->method('rewind')
  49. ->will($this->throwException(new \RuntimeException()));
  50. $a->addStream($s);
  51. $a->seek(10);
  52. }
  53. public function testSeeksToPositionByReading()
  54. {
  55. $a = new AppendStream(array(
  56. Psr7\stream_for('foo'),
  57. Psr7\stream_for('bar'),
  58. Psr7\stream_for('baz'),
  59. ));
  60. $a->seek(3);
  61. $this->assertEquals(3, $a->tell());
  62. $this->assertEquals('bar', $a->read(3));
  63. $a->seek(6);
  64. $this->assertEquals(6, $a->tell());
  65. $this->assertEquals('baz', $a->read(3));
  66. }
  67. public function testDetachesEachStream()
  68. {
  69. $s1 = Psr7\stream_for('foo');
  70. $s2 = Psr7\stream_for('bar');
  71. $a = new AppendStream(array($s1, $s2));
  72. $this->assertSame('foobar', (string) $a);
  73. $a->detach();
  74. $this->assertSame('', (string) $a);
  75. $this->assertSame(0, $a->getSize());
  76. }
  77. public function testClosesEachStream()
  78. {
  79. $s1 = Psr7\stream_for('foo');
  80. $a = new AppendStream(array($s1));
  81. $a->close();
  82. $this->assertSame('', (string) $a);
  83. }
  84. /**
  85. * @expectedExceptionMessage Cannot write to an AppendStream
  86. * @expectedException \RuntimeException
  87. */
  88. public function testIsNotWritable()
  89. {
  90. $a = new AppendStream(array(Psr7\stream_for('foo')));
  91. $this->assertFalse($a->isWritable());
  92. $this->assertTrue($a->isSeekable());
  93. $this->assertTrue($a->isReadable());
  94. $a->write('foo');
  95. }
  96. public function testDoesNotNeedStreams()
  97. {
  98. $a = new AppendStream();
  99. $this->assertEquals('', (string) $a);
  100. }
  101. public function testCanReadFromMultipleStreams()
  102. {
  103. $a = new AppendStream(array(
  104. Psr7\stream_for('foo'),
  105. Psr7\stream_for('bar'),
  106. Psr7\stream_for('baz'),
  107. ));
  108. $this->assertFalse($a->eof());
  109. $this->assertSame(0, $a->tell());
  110. $this->assertEquals('foo', $a->read(3));
  111. $this->assertEquals('bar', $a->read(3));
  112. $this->assertEquals('baz', $a->read(3));
  113. $this->assertSame('', $a->read(1));
  114. $this->assertTrue($a->eof());
  115. $this->assertSame(9, $a->tell());
  116. $this->assertEquals('foobarbaz', (string) $a);
  117. }
  118. public function testCanDetermineSizeFromMultipleStreams()
  119. {
  120. $a = new AppendStream(array(
  121. Psr7\stream_for('foo'),
  122. Psr7\stream_for('bar')
  123. ));
  124. $this->assertEquals(6, $a->getSize());
  125. $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  126. ->setMethods(array('isSeekable', 'isReadable'))
  127. ->getMockForAbstractClass();
  128. $s->expects($this->once())
  129. ->method('isSeekable')
  130. ->will($this->returnValue(null));
  131. $s->expects($this->once())
  132. ->method('isReadable')
  133. ->will($this->returnValue(true));
  134. $a->addStream($s);
  135. $this->assertNull($a->getSize());
  136. }
  137. public function testCatchesExceptionsWhenCastingToString()
  138. {
  139. $s = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  140. ->setMethods(array('isSeekable', 'read', 'isReadable', 'eof'))
  141. ->getMockForAbstractClass();
  142. $s->expects($this->once())
  143. ->method('isSeekable')
  144. ->will($this->returnValue(true));
  145. $s->expects($this->once())
  146. ->method('read')
  147. ->will($this->throwException(new \RuntimeException('foo')));
  148. $s->expects($this->once())
  149. ->method('isReadable')
  150. ->will($this->returnValue(true));
  151. $s->expects($this->any())
  152. ->method('eof')
  153. ->will($this->returnValue(false));
  154. $a = new AppendStream(array($s));
  155. $this->assertFalse($a->eof());
  156. $this->assertSame('', (string) $a);
  157. }
  158. public function testCanDetach()
  159. {
  160. $s = new AppendStream();
  161. $s->detach();
  162. }
  163. public function testReturnsEmptyMetadata()
  164. {
  165. $s = new AppendStream();
  166. $this->assertEquals(array(), $s->getMetadata());
  167. $this->assertNull($s->getMetadata('foo'));
  168. }
  169. }