StreamTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @CreateTime: 2019/9/9 下午06:45
  4. * @Author: huizhang <tuzisir@163.com>
  5. * @Copyright: copyright(2019) Easyswoole all rights reserved
  6. * @Description: SplStream 单元测试
  7. */
  8. namespace EasySwoole\Spl\Test;
  9. use EasySwoole\Spl\SplStream;
  10. use EasySwoole\Spl\Test\Stream\TestStream;
  11. use PHPUnit\Framework\TestCase;
  12. class StreamTest extends TestCase {
  13. public function testConstruct() {
  14. $resource = fopen(getcwd().'/tmp.txt', 'a+');
  15. $stream = new SplStream($resource);
  16. $stream->truncate();
  17. $stream->seek(0);
  18. $stream->write('Easyswoole');
  19. $this->assertEquals(
  20. 'Easyswoole',
  21. $stream->__toString()
  22. );
  23. $stream = new SplStream(new TestStream());
  24. $this->assertEquals(
  25. 'EsObject',
  26. $stream->__toString()
  27. );
  28. $stream = new SplStream('Es');
  29. $this->assertEquals(
  30. 'Es',
  31. $stream->__toString()
  32. );
  33. }
  34. public function testToString() {
  35. $stream = new SplStream('Es');
  36. $this->assertEquals(
  37. 'Es',
  38. $stream->__toString()
  39. );
  40. }
  41. public function testClose() {
  42. $resource = fopen(getcwd().'/tmp.txt', 'ab+');
  43. $stream = new SplStream($resource);
  44. $stream->close();
  45. $this->assertEquals('', $stream->__toString());
  46. }
  47. public function testDetach() {
  48. $stream = new SplStream('Es');
  49. $stream->detach();
  50. // 抛异常,所以返回为''
  51. $this->assertEquals('', $stream->__toString());
  52. }
  53. public function testGetSize() {
  54. $stream = new SplStream('Es');
  55. $this->assertEquals(2, $stream->getSize());
  56. }
  57. public function testTell() {
  58. $stream = new SplStream('Es');
  59. $stream->seek(1);
  60. $this->assertEquals(1, $stream->tell());
  61. }
  62. public function testEof() {
  63. $stream = new SplStream('Es');
  64. $stream->seek(1);
  65. $this->assertNotTrue($stream->eof());
  66. }
  67. public function testIsSeekable() {
  68. $stream = new SplStream('Es');
  69. $this->assertTrue($stream->isSeekable());
  70. }
  71. public function testSeek() {
  72. $stream = new SplStream('Es');
  73. $stream->seek(1);
  74. $this->assertEquals(1, $stream->tell());
  75. }
  76. public function testRewind() {
  77. $stream = new SplStream('Es');
  78. $stream->rewind();
  79. $this->assertEquals(0, $stream->tell());
  80. }
  81. public function testIsWritable() {
  82. $stream = new SplStream('Es');
  83. $this->assertEquals(true, $stream->isWritable());
  84. }
  85. public function testWrite() {
  86. $stream = new SplStream('');
  87. $stream->write('Es');
  88. $this->assertEquals('Es', $stream->__toString());
  89. }
  90. public function testIsReadable() {
  91. $stream = new SplStream('Es');
  92. $this->assertTrue($stream->isReadable());
  93. }
  94. public function testRead() {
  95. $resource = fopen(getcwd().'/tmp.txt', 'a+');
  96. $stream = new SplStream($resource);
  97. $stream->truncate();
  98. $stream->seek(0);
  99. $stream->write('Es');
  100. $stream->seek(0);
  101. $this->assertEquals('E', $stream->read(1));
  102. }
  103. public function testGetContents() {
  104. $stream = new SplStream('Es');
  105. $stream->seek(0);
  106. $this->assertEquals('Es', $stream->getContents());
  107. }
  108. public function testGetMetadata() {
  109. $stream = new SplStream('Es');
  110. $this->assertEquals('MEMORY', $stream->getMetadata()['stream_type']);
  111. }
  112. public function testGetStreamResource() {
  113. $stream = new SplStream('Es');
  114. $source = $stream->getStreamResource();
  115. fseek($source, 0, SEEK_SET);
  116. $this->assertEquals('Es', stream_get_contents($source));
  117. }
  118. public function testTruncate() {
  119. $stream = new SplStream('Es');
  120. $stream->truncate(1);
  121. $this->assertEquals('E', $stream->__toString());
  122. }
  123. }