StreamWrapperTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace GuzzleHttp\Tests\Psr7;
  3. use GuzzleHttp\Psr7\StreamWrapper;
  4. use GuzzleHttp\Psr7;
  5. /**
  6. * @covers GuzzleHttp\Psr7\StreamWrapper
  7. */
  8. class StreamWrapperTest extends BaseTest
  9. {
  10. public function testResource()
  11. {
  12. $stream = Psr7\stream_for('foo');
  13. $handle = StreamWrapper::getResource($stream);
  14. $this->assertSame('foo', fread($handle, 3));
  15. $this->assertSame(3, ftell($handle));
  16. $this->assertSame(3, fwrite($handle, 'bar'));
  17. $this->assertSame(0, fseek($handle, 0));
  18. $this->assertSame('foobar', fread($handle, 6));
  19. $this->assertSame('', fread($handle, 1));
  20. $this->assertTrue(feof($handle));
  21. $stBlksize = defined('PHP_WINDOWS_VERSION_BUILD') ? -1 : 0;
  22. // This fails on HHVM for some reason
  23. if (!defined('HHVM_VERSION')) {
  24. $this->assertEquals([
  25. 'dev' => 0,
  26. 'ino' => 0,
  27. 'mode' => 33206,
  28. 'nlink' => 0,
  29. 'uid' => 0,
  30. 'gid' => 0,
  31. 'rdev' => 0,
  32. 'size' => 6,
  33. 'atime' => 0,
  34. 'mtime' => 0,
  35. 'ctime' => 0,
  36. 'blksize' => $stBlksize,
  37. 'blocks' => $stBlksize,
  38. 0 => 0,
  39. 1 => 0,
  40. 2 => 33206,
  41. 3 => 0,
  42. 4 => 0,
  43. 5 => 0,
  44. 6 => 0,
  45. 7 => 6,
  46. 8 => 0,
  47. 9 => 0,
  48. 10 => 0,
  49. 11 => $stBlksize,
  50. 12 => $stBlksize,
  51. ], fstat($handle));
  52. }
  53. $this->assertTrue(fclose($handle));
  54. $this->assertSame('foobar', (string) $stream);
  55. }
  56. public function testStreamContext()
  57. {
  58. $stream = Psr7\stream_for('foo');
  59. $this->assertEquals('foo', file_get_contents('guzzle://stream', false, StreamWrapper::createStreamContext($stream)));
  60. }
  61. public function testStreamCast()
  62. {
  63. $streams = [
  64. StreamWrapper::getResource(Psr7\stream_for('foo')),
  65. StreamWrapper::getResource(Psr7\stream_for('bar'))
  66. ];
  67. $write = null;
  68. $except = null;
  69. $this->assertInternalType('integer', stream_select($streams, $write, $except, 0));
  70. }
  71. /**
  72. * @expectedException \InvalidArgumentException
  73. */
  74. public function testValidatesStream()
  75. {
  76. $stream = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  77. ->setMethods(['isReadable', 'isWritable'])
  78. ->getMockForAbstractClass();
  79. $stream->expects($this->once())
  80. ->method('isReadable')
  81. ->will($this->returnValue(false));
  82. $stream->expects($this->once())
  83. ->method('isWritable')
  84. ->will($this->returnValue(false));
  85. StreamWrapper::getResource($stream);
  86. }
  87. /**
  88. * @expectedException \PHPUnit\Framework\Error\Warning
  89. */
  90. public function testReturnsFalseWhenStreamDoesNotExist()
  91. {
  92. fopen('guzzle://foo', 'r');
  93. }
  94. public function testCanOpenReadonlyStream()
  95. {
  96. $stream = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
  97. ->setMethods(['isReadable', 'isWritable'])
  98. ->getMockForAbstractClass();
  99. $stream->expects($this->once())
  100. ->method('isReadable')
  101. ->will($this->returnValue(false));
  102. $stream->expects($this->once())
  103. ->method('isWritable')
  104. ->will($this->returnValue(true));
  105. $r = StreamWrapper::getResource($stream);
  106. $this->assertInternalType('resource', $r);
  107. fclose($r);
  108. }
  109. public function testUrlStat()
  110. {
  111. StreamWrapper::register();
  112. $this->assertEquals(
  113. [
  114. 'dev' => 0,
  115. 'ino' => 0,
  116. 'mode' => 0,
  117. 'nlink' => 0,
  118. 'uid' => 0,
  119. 'gid' => 0,
  120. 'rdev' => 0,
  121. 'size' => 0,
  122. 'atime' => 0,
  123. 'mtime' => 0,
  124. 'ctime' => 0,
  125. 'blksize' => 0,
  126. 'blocks' => 0,
  127. 0 => 0,
  128. 1 => 0,
  129. 2 => 0,
  130. 3 => 0,
  131. 4 => 0,
  132. 5 => 0,
  133. 6 => 0,
  134. 7 => 0,
  135. 8 => 0,
  136. 9 => 0,
  137. 10 => 0,
  138. 11 => 0,
  139. 12 => 0,
  140. ],
  141. stat('guzzle://stream')
  142. );
  143. }
  144. public function testXmlReaderWithStream()
  145. {
  146. if (!class_exists('XMLReader')) {
  147. $this->markTestSkipped('XML Reader is not available.');
  148. }
  149. if (defined('HHVM_VERSION')) {
  150. $this->markTestSkipped('This does not work on HHVM.');
  151. }
  152. $stream = Psr7\stream_for('<?xml version="1.0" encoding="utf-8"?><foo />');
  153. StreamWrapper::register();
  154. libxml_set_streams_context(StreamWrapper::createStreamContext($stream));
  155. $reader = new \XMLReader();
  156. $this->assertTrue($reader->open('guzzle://stream'));
  157. $this->assertTrue($reader->read());
  158. $this->assertEquals('foo', $reader->name);
  159. }
  160. public function testXmlWriterWithStream()
  161. {
  162. if (!class_exists('XMLWriter')) {
  163. $this->markTestSkipped('XML Writer is not available.');
  164. }
  165. if (defined('HHVM_VERSION')) {
  166. $this->markTestSkipped('This does not work on HHVM.');
  167. }
  168. $stream = Psr7\stream_for(fopen('php://memory', 'wb'));
  169. StreamWrapper::register();
  170. libxml_set_streams_context(StreamWrapper::createStreamContext($stream));
  171. $writer = new \XMLWriter();
  172. $this->assertTrue($writer->openURI('guzzle://stream'));
  173. $this->assertTrue($writer->startDocument());
  174. $this->assertTrue($writer->writeElement('foo'));
  175. $this->assertTrue($writer->endDocument());
  176. $stream->rewind();
  177. $this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><foo />', (string) $stream);
  178. }
  179. }