AppendStreamTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\AppendStream;
  4. use GuzzleHttp\Stream\Stream;
  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('GuzzleHttp\Stream\StreamInterface')
  15. ->setMethods(['isReadable'])
  16. ->getMockForAbstractClass();
  17. $s->expects($this->once())
  18. ->method('isReadable')
  19. ->will($this->returnValue(false));
  20. $a->addStream($s);
  21. }
  22. public function testValidatesSeekType()
  23. {
  24. $a = new AppendStream();
  25. $this->assertFalse($a->seek(100, SEEK_CUR));
  26. }
  27. public function testTriesToRewindOnSeek()
  28. {
  29. $a = new AppendStream();
  30. $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  31. ->setMethods(['isReadable', 'seek', 'isSeekable'])
  32. ->getMockForAbstractClass();
  33. $s->expects($this->once())
  34. ->method('isReadable')
  35. ->will($this->returnValue(true));
  36. $s->expects($this->once())
  37. ->method('isSeekable')
  38. ->will($this->returnValue(true));
  39. $s->expects($this->once())
  40. ->method('seek')
  41. ->will($this->returnValue(false));
  42. $a->addStream($s);
  43. $this->assertFalse($a->seek(10));
  44. }
  45. public function testSeeksToPositionByReading()
  46. {
  47. $a = new AppendStream([
  48. Stream::factory('foo'),
  49. Stream::factory('bar'),
  50. Stream::factory('baz'),
  51. ]);
  52. $this->assertTrue($a->seek(3));
  53. $this->assertEquals(3, $a->tell());
  54. $this->assertEquals('bar', $a->read(3));
  55. $a->seek(6);
  56. $this->assertEquals(6, $a->tell());
  57. $this->assertEquals('baz', $a->read(3));
  58. }
  59. public function testDetachesEachStream()
  60. {
  61. $s1 = Stream::factory('foo');
  62. $s2 = Stream::factory('foo');
  63. $a = new AppendStream([$s1, $s2]);
  64. $this->assertSame('foofoo', (string) $a);
  65. $a->detach();
  66. $this->assertSame('', (string) $a);
  67. $this->assertSame(0, $a->getSize());
  68. }
  69. public function testClosesEachStream()
  70. {
  71. $s1 = Stream::factory('foo');
  72. $a = new AppendStream([$s1]);
  73. $a->close();
  74. $this->assertSame('', (string) $a);
  75. }
  76. public function testIsNotWritable()
  77. {
  78. $a = new AppendStream([Stream::factory('foo')]);
  79. $this->assertFalse($a->isWritable());
  80. $this->assertTrue($a->isSeekable());
  81. $this->assertTrue($a->isReadable());
  82. $this->assertFalse($a->write('foo'));
  83. }
  84. public function testDoesNotNeedStreams()
  85. {
  86. $a = new AppendStream();
  87. $this->assertEquals('', (string) $a);
  88. }
  89. public function testCanReadFromMultipleStreams()
  90. {
  91. $a = new AppendStream([
  92. Stream::factory('foo'),
  93. Stream::factory('bar'),
  94. Stream::factory('baz'),
  95. ]);
  96. $this->assertFalse($a->eof());
  97. $this->assertSame(0, $a->tell());
  98. $this->assertEquals('foo', $a->read(3));
  99. $this->assertEquals('bar', $a->read(3));
  100. $this->assertEquals('baz', $a->read(3));
  101. $this->assertTrue($a->eof());
  102. $this->assertSame(9, $a->tell());
  103. $this->assertEquals('foobarbaz', (string) $a);
  104. }
  105. public function testCanDetermineSizeFromMultipleStreams()
  106. {
  107. $a = new AppendStream([
  108. Stream::factory('foo'),
  109. Stream::factory('bar')
  110. ]);
  111. $this->assertEquals(6, $a->getSize());
  112. $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  113. ->setMethods(['isSeekable', 'isReadable'])
  114. ->getMockForAbstractClass();
  115. $s->expects($this->once())
  116. ->method('isSeekable')
  117. ->will($this->returnValue(null));
  118. $s->expects($this->once())
  119. ->method('isReadable')
  120. ->will($this->returnValue(true));
  121. $a->addStream($s);
  122. $this->assertNull($a->getSize());
  123. }
  124. public function testCatchesExceptionsWhenCastingToString()
  125. {
  126. $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  127. ->setMethods(['read', 'isReadable', 'eof'])
  128. ->getMockForAbstractClass();
  129. $s->expects($this->once())
  130. ->method('read')
  131. ->will($this->throwException(new \RuntimeException('foo')));
  132. $s->expects($this->once())
  133. ->method('isReadable')
  134. ->will($this->returnValue(true));
  135. $s->expects($this->any())
  136. ->method('eof')
  137. ->will($this->returnValue(false));
  138. $a = new AppendStream([$s]);
  139. $this->assertFalse($a->eof());
  140. $this->assertSame('', (string) $a);
  141. }
  142. public function testCanDetach()
  143. {
  144. $s = new AppendStream();
  145. $s->detach();
  146. }
  147. public function testReturnsEmptyMetadata()
  148. {
  149. $s = new AppendStream();
  150. $this->assertEquals([], $s->getMetadata());
  151. $this->assertNull($s->getMetadata('foo'));
  152. }
  153. /**
  154. * @expectedException \GuzzleHttp\Stream\Exception\CannotAttachException
  155. */
  156. public function testCannotAttach()
  157. {
  158. $p = new AppendStream();
  159. $p->attach('a');
  160. }
  161. }