StreamTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace RingCentral\Tests\Psr7;
  3. use RingCentral\Psr7\NoSeekStream;
  4. use RingCentral\Psr7\Stream;
  5. /**
  6. * @covers RingCentral\Psr7\Stream
  7. */
  8. class StreamTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @expectedException \InvalidArgumentException
  12. */
  13. public function testConstructorThrowsExceptionOnInvalidArgument()
  14. {
  15. new Stream(true);
  16. }
  17. public function testConstructorInitializesProperties()
  18. {
  19. $handle = fopen('php://temp', 'r+');
  20. fwrite($handle, 'data');
  21. $stream = new Stream($handle);
  22. $this->assertTrue($stream->isReadable());
  23. $this->assertTrue($stream->isWritable());
  24. $this->assertTrue($stream->isSeekable());
  25. $this->assertEquals('php://temp', $stream->getMetadata('uri'));
  26. $this->assertInternalType('array', $stream->getMetadata());
  27. $this->assertEquals(4, $stream->getSize());
  28. $this->assertFalse($stream->eof());
  29. $stream->close();
  30. }
  31. public function testStreamClosesHandleOnDestruct()
  32. {
  33. $handle = fopen('php://temp', 'r');
  34. $stream = new Stream($handle);
  35. unset($stream);
  36. $this->assertFalse(is_resource($handle));
  37. }
  38. public function testConvertsToString()
  39. {
  40. $handle = fopen('php://temp', 'w+');
  41. fwrite($handle, 'data');
  42. $stream = new Stream($handle);
  43. $this->assertEquals('data', (string) $stream);
  44. $this->assertEquals('data', (string) $stream);
  45. $stream->close();
  46. }
  47. public function testGetsContents()
  48. {
  49. $handle = fopen('php://temp', 'w+');
  50. fwrite($handle, 'data');
  51. $stream = new Stream($handle);
  52. $this->assertEquals('', $stream->getContents());
  53. $stream->seek(0);
  54. $this->assertEquals('data', $stream->getContents());
  55. $this->assertEquals('', $stream->getContents());
  56. }
  57. public function testChecksEof()
  58. {
  59. $handle = fopen('php://temp', 'w+');
  60. fwrite($handle, 'data');
  61. $stream = new Stream($handle);
  62. $this->assertFalse($stream->eof());
  63. $stream->read(4);
  64. $this->assertTrue($stream->eof());
  65. $stream->close();
  66. }
  67. public function testGetSize()
  68. {
  69. $size = filesize(__FILE__);
  70. $handle = fopen(__FILE__, 'r');
  71. $stream = new Stream($handle);
  72. $this->assertEquals($size, $stream->getSize());
  73. // Load from cache
  74. $this->assertEquals($size, $stream->getSize());
  75. $stream->close();
  76. }
  77. public function testEnsuresSizeIsConsistent()
  78. {
  79. $h = fopen('php://temp', 'w+');
  80. $this->assertEquals(3, fwrite($h, 'foo'));
  81. $stream = new Stream($h);
  82. $this->assertEquals(3, $stream->getSize());
  83. $this->assertEquals(4, $stream->write('test'));
  84. $this->assertEquals(7, $stream->getSize());
  85. $this->assertEquals(7, $stream->getSize());
  86. $stream->close();
  87. }
  88. public function testProvidesStreamPosition()
  89. {
  90. $handle = fopen('php://temp', 'w+');
  91. $stream = new Stream($handle);
  92. $this->assertEquals(0, $stream->tell());
  93. $stream->write('foo');
  94. $this->assertEquals(3, $stream->tell());
  95. $stream->seek(1);
  96. $this->assertEquals(1, $stream->tell());
  97. $this->assertSame(ftell($handle), $stream->tell());
  98. $stream->close();
  99. }
  100. public function testCanDetachStream()
  101. {
  102. $r = fopen('php://temp', 'w+');
  103. $stream = new Stream($r);
  104. $stream->write('foo');
  105. $this->assertTrue($stream->isReadable());
  106. $this->assertSame($r, $stream->detach());
  107. $stream->detach();
  108. $this->assertFalse($stream->isReadable());
  109. $this->assertFalse($stream->isWritable());
  110. $this->assertFalse($stream->isSeekable());
  111. $self = $this;
  112. $throws = function ($fn) use ($stream, $self) {
  113. try {
  114. $fn($stream);
  115. $self->fail();
  116. } catch (\Exception $e) {}
  117. };
  118. $throws(function ($stream) { $stream->read(10); });
  119. $throws(function ($stream) { $stream->write('bar'); });
  120. $throws(function ($stream) { $stream->seek(10); });
  121. $throws(function ($stream) { $stream->tell(); });
  122. $throws(function ($stream) { $stream->eof(); });
  123. $throws(function ($stream) { $stream->getSize(); });
  124. $throws(function ($stream) { $stream->getContents(); });
  125. $this->assertSame('', (string) $stream);
  126. $stream->close();
  127. }
  128. public function testCloseClearProperties()
  129. {
  130. $handle = fopen('php://temp', 'r+');
  131. $stream = new Stream($handle);
  132. $stream->close();
  133. $this->assertFalse($stream->isSeekable());
  134. $this->assertFalse($stream->isReadable());
  135. $this->assertFalse($stream->isWritable());
  136. $this->assertNull($stream->getSize());
  137. $this->assertEmpty($stream->getMetadata());
  138. }
  139. public function testDoesNotThrowInToString()
  140. {
  141. $s = \RingCentral\Psr7\stream_for('foo');
  142. $s = new NoSeekStream($s);
  143. $this->assertEquals('foo', (string) $s);
  144. }
  145. }