StreamTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\Stream;
  4. /**
  5. * @covers GuzzleHttp\Stream\Stream
  6. */
  7. class StreamTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @expectedException \InvalidArgumentException
  11. */
  12. public function testConstructorThrowsExceptionOnInvalidArgument()
  13. {
  14. new Stream(true);
  15. }
  16. public function testConstructorInitializesProperties()
  17. {
  18. $handle = fopen('php://temp', 'r+');
  19. fwrite($handle, 'data');
  20. $stream = new Stream($handle);
  21. $this->assertTrue($stream->isReadable());
  22. $this->assertTrue($stream->isWritable());
  23. $this->assertTrue($stream->isSeekable());
  24. $this->assertEquals('php://temp', $stream->getMetadata('uri'));
  25. $this->assertInternalType('array', $stream->getMetadata());
  26. $this->assertEquals(4, $stream->getSize());
  27. $this->assertFalse($stream->eof());
  28. $stream->close();
  29. }
  30. public function testStreamClosesHandleOnDestruct()
  31. {
  32. $handle = fopen('php://temp', 'r');
  33. $stream = new Stream($handle);
  34. unset($stream);
  35. $this->assertFalse(is_resource($handle));
  36. }
  37. public function testConvertsToString()
  38. {
  39. $handle = fopen('php://temp', 'w+');
  40. fwrite($handle, 'data');
  41. $stream = new Stream($handle);
  42. $this->assertEquals('data', (string) $stream);
  43. $this->assertEquals('data', (string) $stream);
  44. $stream->close();
  45. }
  46. public function testGetsContents()
  47. {
  48. $handle = fopen('php://temp', 'w+');
  49. fwrite($handle, 'data');
  50. $stream = new Stream($handle);
  51. $this->assertEquals('', $stream->getContents());
  52. $stream->seek(0);
  53. $this->assertEquals('data', $stream->getContents());
  54. $this->assertEquals('', $stream->getContents());
  55. }
  56. public function testChecksEof()
  57. {
  58. $handle = fopen('php://temp', 'w+');
  59. fwrite($handle, 'data');
  60. $stream = new Stream($handle);
  61. $this->assertFalse($stream->eof());
  62. $stream->read(4);
  63. $this->assertTrue($stream->eof());
  64. $stream->close();
  65. }
  66. public function testAllowsSettingManualSize()
  67. {
  68. $handle = fopen('php://temp', 'w+');
  69. fwrite($handle, 'data');
  70. $stream = new Stream($handle);
  71. $stream->setSize(10);
  72. $this->assertEquals(10, $stream->getSize());
  73. $stream->close();
  74. }
  75. public function testGetSize()
  76. {
  77. $size = filesize(__FILE__);
  78. $handle = fopen(__FILE__, 'r');
  79. $stream = new Stream($handle);
  80. $this->assertEquals($size, $stream->getSize());
  81. // Load from cache
  82. $this->assertEquals($size, $stream->getSize());
  83. $stream->close();
  84. }
  85. public function testEnsuresSizeIsConsistent()
  86. {
  87. $h = fopen('php://temp', 'w+');
  88. $this->assertEquals(3, fwrite($h, 'foo'));
  89. $stream = new Stream($h);
  90. $this->assertEquals(3, $stream->getSize());
  91. $this->assertEquals(4, $stream->write('test'));
  92. $this->assertEquals(7, $stream->getSize());
  93. $this->assertEquals(7, $stream->getSize());
  94. $stream->close();
  95. }
  96. public function testProvidesStreamPosition()
  97. {
  98. $handle = fopen('php://temp', 'w+');
  99. $stream = new Stream($handle);
  100. $this->assertEquals(0, $stream->tell());
  101. $stream->write('foo');
  102. $this->assertEquals(3, $stream->tell());
  103. $stream->seek(1);
  104. $this->assertEquals(1, $stream->tell());
  105. $this->assertSame(ftell($handle), $stream->tell());
  106. $stream->close();
  107. }
  108. public function testKeepsPositionOfResource()
  109. {
  110. $h = fopen(__FILE__, 'r');
  111. fseek($h, 10);
  112. $stream = Stream::factory($h);
  113. $this->assertEquals(10, $stream->tell());
  114. $stream->close();
  115. }
  116. public function testCanDetachAndAttachStream()
  117. {
  118. $r = fopen('php://temp', 'w+');
  119. $stream = new Stream($r);
  120. $stream->write('foo');
  121. $this->assertTrue($stream->isReadable());
  122. $this->assertSame($r, $stream->detach());
  123. $this->assertNull($stream->detach());
  124. $this->assertFalse($stream->isReadable());
  125. $this->assertFalse($stream->read(10));
  126. $this->assertFalse($stream->isWritable());
  127. $this->assertFalse($stream->write('bar'));
  128. $this->assertFalse($stream->isSeekable());
  129. $this->assertFalse($stream->seek(10));
  130. $this->assertFalse($stream->tell());
  131. $this->assertTrue($stream->eof());
  132. $this->assertNull($stream->getSize());
  133. $this->assertSame('', (string) $stream);
  134. $this->assertSame('', $stream->getContents());
  135. $stream->attach($r);
  136. $stream->seek(0);
  137. $this->assertEquals('foo', $stream->getContents());
  138. $this->assertTrue($stream->isReadable());
  139. $this->assertTrue($stream->isWritable());
  140. $this->assertTrue($stream->isSeekable());
  141. $stream->close();
  142. }
  143. public function testCloseClearProperties()
  144. {
  145. $handle = fopen('php://temp', 'r+');
  146. $stream = new Stream($handle);
  147. $stream->close();
  148. $this->assertEmpty($stream->getMetadata());
  149. $this->assertFalse($stream->isSeekable());
  150. $this->assertFalse($stream->isReadable());
  151. $this->assertFalse($stream->isWritable());
  152. $this->assertNull($stream->getSize());
  153. }
  154. public function testCreatesWithFactory()
  155. {
  156. $stream = Stream::factory('foo');
  157. $this->assertInstanceOf('GuzzleHttp\Stream\Stream', $stream);
  158. $this->assertEquals('foo', $stream->getContents());
  159. $stream->close();
  160. }
  161. public function testFactoryCreatesFromEmptyString()
  162. {
  163. $s = Stream::factory();
  164. $this->assertInstanceOf('GuzzleHttp\Stream\Stream', $s);
  165. }
  166. public function testFactoryCreatesFromResource()
  167. {
  168. $r = fopen(__FILE__, 'r');
  169. $s = Stream::factory($r);
  170. $this->assertInstanceOf('GuzzleHttp\Stream\Stream', $s);
  171. $this->assertSame(file_get_contents(__FILE__), (string) $s);
  172. }
  173. public function testFactoryCreatesFromObjectWithToString()
  174. {
  175. $r = new HasToString();
  176. $s = Stream::factory($r);
  177. $this->assertInstanceOf('GuzzleHttp\Stream\Stream', $s);
  178. $this->assertEquals('foo', (string) $s);
  179. }
  180. public function testCreatePassesThrough()
  181. {
  182. $s = Stream::factory('foo');
  183. $this->assertSame($s, Stream::factory($s));
  184. }
  185. /**
  186. * @expectedException \InvalidArgumentException
  187. */
  188. public function testThrowsExceptionForUnknown()
  189. {
  190. Stream::factory(new \stdClass());
  191. }
  192. public function testReturnsCustomMetadata()
  193. {
  194. $s = Stream::factory('foo', ['metadata' => ['hwm' => 3]]);
  195. $this->assertEquals(3, $s->getMetadata('hwm'));
  196. $this->assertArrayHasKey('hwm', $s->getMetadata());
  197. }
  198. public function testCanSetSize()
  199. {
  200. $s = Stream::factory('', ['size' => 10]);
  201. $this->assertEquals(10, $s->getSize());
  202. }
  203. public function testCanCreateIteratorBasedStream()
  204. {
  205. $a = new \ArrayIterator(['foo', 'bar', '123']);
  206. $p = Stream::factory($a);
  207. $this->assertInstanceOf('GuzzleHttp\Stream\PumpStream', $p);
  208. $this->assertEquals('foo', $p->read(3));
  209. $this->assertFalse($p->eof());
  210. $this->assertEquals('b', $p->read(1));
  211. $this->assertEquals('a', $p->read(1));
  212. $this->assertEquals('r12', $p->read(3));
  213. $this->assertFalse($p->eof());
  214. $this->assertEquals('3', $p->getContents());
  215. $this->assertTrue($p->eof());
  216. $this->assertEquals(9, $p->tell());
  217. }
  218. }
  219. class HasToString
  220. {
  221. public function __toString() {
  222. return 'foo';
  223. }
  224. }