GuzzleStreamWrapperTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\GuzzleStreamWrapper;
  4. use GuzzleHttp\Stream\Stream;
  5. use InvalidArgumentException;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * @covers GuzzleHttp\Stream\GuzzleStreamWrapper
  9. */
  10. class GuzzleStreamWrapperTest extends TestCase
  11. {
  12. public function testResource()
  13. {
  14. $stream = Stream::factory('foo');
  15. $handle = GuzzleStreamWrapper::getResource($stream);
  16. $this->assertSame('foo', fread($handle, 3));
  17. $this->assertSame(3, ftell($handle));
  18. $this->assertSame(3, fwrite($handle, 'bar'));
  19. $this->assertSame(0, fseek($handle, 0));
  20. $this->assertSame('foobar', fread($handle, 6));
  21. $this->assertEmpty(fread($handle, 1));
  22. $this->assertTrue(feof($handle));
  23. // This fails on HHVM for some reason
  24. if (!defined('HHVM_VERSION')) {
  25. $this->assertEquals([
  26. 'dev' => 0,
  27. 'ino' => 0,
  28. 'mode' => 33206,
  29. 'nlink' => 0,
  30. 'uid' => 0,
  31. 'gid' => 0,
  32. 'rdev' => 0,
  33. 'size' => 6,
  34. 'atime' => 0,
  35. 'mtime' => 0,
  36. 'ctime' => 0,
  37. 'blksize' => 0,
  38. 'blocks' => 0,
  39. 0 => 0,
  40. 1 => 0,
  41. 2 => 33206,
  42. 3 => 0,
  43. 4 => 0,
  44. 5 => 0,
  45. 6 => 0,
  46. 7 => 6,
  47. 8 => 0,
  48. 9 => 0,
  49. 10 => 0,
  50. 11 => 0,
  51. 12 => 0,
  52. ], fstat($handle));
  53. }
  54. $this->assertTrue(fclose($handle));
  55. $this->assertSame('foobar', (string) $stream);
  56. }
  57. public function testValidatesStream()
  58. {
  59. $this->expectException(InvalidArgumentException::class);
  60. $stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  61. ->getMockForAbstractClass();
  62. $stream->expects($this->once())
  63. ->method('isReadable')
  64. ->will($this->returnValue(false));
  65. $stream->expects($this->once())
  66. ->method('isWritable')
  67. ->will($this->returnValue(false));
  68. GuzzleStreamWrapper::getResource($stream);
  69. }
  70. public function testReturnsFalseWhenStreamDoesNotExist()
  71. {
  72. $this->expectWarning();
  73. fopen('guzzle://foo', 'r');
  74. }
  75. public function testCanOpenReadonlyStream()
  76. {
  77. $stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  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(true));
  85. $r = GuzzleStreamWrapper::getResource($stream);
  86. $this->assertIsResource($r);
  87. fclose($r);
  88. }
  89. }