StreamWrapperTest.php 3.1 KB

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