/vendor/guzzlehttp/psr7/tests/StreamTest.php

https://gitlab.com/ealexis.t/trends · PHP · 161 lines · 137 code · 17 blank · 7 comment · 0 complexity · 2627b4e94dcecbeaa34fc792f0796272 MD5 · raw file

  1. <?php
  2. namespace GuzzleHttp\Tests\Psr7;
  3. use GuzzleHttp\Psr7\NoSeekStream;
  4. use GuzzleHttp\Psr7\Stream;
  5. /**
  6. * @covers GuzzleHttp\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. $throws = function (callable $fn) use ($stream) {
  112. try {
  113. $fn($stream);
  114. $this->fail();
  115. } catch (\Exception $e) {}
  116. };
  117. $throws(function ($stream) { $stream->read(10); });
  118. $throws(function ($stream) { $stream->write('bar'); });
  119. $throws(function ($stream) { $stream->seek(10); });
  120. $throws(function ($stream) { $stream->tell(); });
  121. $throws(function ($stream) { $stream->eof(); });
  122. $throws(function ($stream) { $stream->getSize(); });
  123. $throws(function ($stream) { $stream->getContents(); });
  124. $this->assertSame('', (string) $stream);
  125. $stream->close();
  126. }
  127. public function testCloseClearProperties()
  128. {
  129. $handle = fopen('php://temp', 'r+');
  130. $stream = new Stream($handle);
  131. $stream->close();
  132. $this->assertFalse($stream->isSeekable());
  133. $this->assertFalse($stream->isReadable());
  134. $this->assertFalse($stream->isWritable());
  135. $this->assertNull($stream->getSize());
  136. $this->assertEmpty($stream->getMetadata());
  137. }
  138. public function testDoesNotThrowInToString()
  139. {
  140. $s = \GuzzleHttp\Psr7\stream_for('foo');
  141. $s = new NoSeekStream($s);
  142. $this->assertEquals('foo', (string) $s);
  143. }
  144. }