GuzzleStreamWrapperTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\GuzzleStreamWrapper;
  4. use GuzzleHttp\Stream\Stream;
  5. /**
  6. * @covers GuzzleHttp\Stream\GuzzleStreamWrapper
  7. */
  8. class GuzzleStreamWrapperTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function testResource()
  11. {
  12. $stream = Stream::factory('foo');
  13. $handle = GuzzleStreamWrapper::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->assertTrue(feof($handle));
  20. // This fails on HHVM for some reason
  21. if (!defined('HHVM_VERSION')) {
  22. $this->assertEquals([
  23. 'dev' => 0,
  24. 'ino' => 0,
  25. 'mode' => 33206,
  26. 'nlink' => 0,
  27. 'uid' => 0,
  28. 'gid' => 0,
  29. 'rdev' => 0,
  30. 'size' => 6,
  31. 'atime' => 0,
  32. 'mtime' => 0,
  33. 'ctime' => 0,
  34. 'blksize' => 0,
  35. 'blocks' => 0,
  36. 0 => 0,
  37. 1 => 0,
  38. 2 => 33206,
  39. 3 => 0,
  40. 4 => 0,
  41. 5 => 0,
  42. 6 => 0,
  43. 7 => 6,
  44. 8 => 0,
  45. 9 => 0,
  46. 10 => 0,
  47. 11 => 0,
  48. 12 => 0,
  49. ], fstat($handle));
  50. }
  51. $this->assertTrue(fclose($handle));
  52. $this->assertSame('foobar', (string) $stream);
  53. }
  54. /**
  55. * @expectedException \InvalidArgumentException
  56. */
  57. public function testValidatesStream()
  58. {
  59. $stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  60. ->setMethods(['isReadable', 'isWritable'])
  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. /**
  71. * @expectedException \PHPUnit_Framework_Error_Warning
  72. */
  73. public function testReturnsFalseWhenStreamDoesNotExist()
  74. {
  75. fopen('guzzle://foo', 'r');
  76. }
  77. public function testCanOpenReadonlyStream()
  78. {
  79. $stream = $this->getMockBuilder('GuzzleHttp\Stream\StreamInterface')
  80. ->setMethods(['isReadable', 'isWritable'])
  81. ->getMockForAbstractClass();
  82. $stream->expects($this->once())
  83. ->method('isReadable')
  84. ->will($this->returnValue(false));
  85. $stream->expects($this->once())
  86. ->method('isWritable')
  87. ->will($this->returnValue(true));
  88. $r = GuzzleStreamWrapper::getResource($stream);
  89. $this->assertInternalType('resource', $r);
  90. fclose($r);
  91. }
  92. }