AppendStreamTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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->assertEmpty($a->read(1));
  102. $this->assertTrue($a->eof());
  103. $this->assertSame(9, $a->tell());
  104. $this->assertEquals('foobarbaz', (string) $a);
  105. }
  106. public function testCanDetermineSizeFromMultipleStreams()
  107. {
  108. $a = new AppendStream([
  109. Stream::factory('foo'),
  110. Stream::factory('bar'),
  111. ]);
  112. $this->assertEquals(6, $a->getSize());
  113. $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  114. ->setMethods(['isSeekable', 'isReadable'])
  115. ->getMockForAbstractClass();
  116. $s->expects($this->once())
  117. ->method('isSeekable')
  118. ->will($this->returnValue(null));
  119. $s->expects($this->once())
  120. ->method('isReadable')
  121. ->will($this->returnValue(true));
  122. $a->addStream($s);
  123. $this->assertNull($a->getSize());
  124. }
  125. public function testCatchesExceptionsWhenCastingToString()
  126. {
  127. $s = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  128. ->setMethods(['read', 'isReadable', 'eof'])
  129. ->getMockForAbstractClass();
  130. $s->expects($this->once())
  131. ->method('read')
  132. ->will($this->throwException(new \RuntimeException('foo')));
  133. $s->expects($this->once())
  134. ->method('isReadable')
  135. ->will($this->returnValue(true));
  136. $s->expects($this->any())
  137. ->method('eof')
  138. ->will($this->returnValue(false));
  139. $a = new AppendStream([$s]);
  140. $this->assertFalse($a->eof());
  141. $this->assertSame('', (string) $a);
  142. }
  143. public function testCanDetach()
  144. {
  145. $s = new AppendStream();
  146. $s->detach();
  147. }
  148. public function testReturnsEmptyMetadata()
  149. {
  150. $s = new AppendStream();
  151. $this->assertEquals([], $s->getMetadata());
  152. $this->assertNull($s->getMetadata('foo'));
  153. }
  154. /**
  155. * @expectedException \GuzzleHttp\Stream\Exception\CannotAttachException
  156. */
  157. public function testCannotAttach()
  158. {
  159. $p = new AppendStream();
  160. $p->attach('a');
  161. }
  162. }