tp6混合easyswoole DLL 多应用模式 修改版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.7 KiB

5 years ago
  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('./test.txt', 'ab+');
  15. $stream = new SplStream($resource);
  16. $this->assertEquals(
  17. 'Easyswoole',
  18. $stream->__toString()
  19. );
  20. $stream = new SplStream(new TestStream());
  21. $this->assertEquals(
  22. 'EsObject',
  23. $stream->__toString()
  24. );
  25. $stream = new SplStream('Es');
  26. $this->assertEquals(
  27. 'Es',
  28. $stream->__toString()
  29. );
  30. }
  31. public function testToString() {
  32. $stream = new SplStream('Es');
  33. $this->assertEquals(
  34. 'Es',
  35. $stream->__toString()
  36. );
  37. }
  38. public function testClose() {
  39. $resource = fopen('./test.txt', 'ab+');
  40. $stream = new SplStream($resource);
  41. $stream->close();
  42. $this->assertEquals('', $stream->__toString());
  43. }
  44. public function testDetach() {
  45. $stream = new SplStream('Es');
  46. $stream->detach();
  47. // 抛异常,所以返回为''
  48. $this->assertEquals('', $stream->__toString());
  49. }
  50. public function testGetSize() {
  51. $stream = new SplStream('Es');
  52. $this->assertEquals(2, $stream->getSize());
  53. }
  54. public function testTell() {
  55. $stream = new SplStream('Es');
  56. $stream->seek(1);
  57. $this->assertEquals(1, $stream->tell());
  58. }
  59. public function testEof() {
  60. $stream = new SplStream('Es');
  61. $stream->seek(1);
  62. $this->assertNotTrue($stream->eof());
  63. }
  64. public function testIsSeekable() {
  65. $stream = new SplStream('Es');
  66. $this->assertTrue($stream->isSeekable());
  67. }
  68. public function testSeek() {
  69. $stream = new SplStream('Es');
  70. $stream->seek(1);
  71. $this->assertEquals(1, $stream->tell());
  72. }
  73. public function testRewind() {
  74. $stream = new SplStream('Es');
  75. $stream->rewind();
  76. $this->assertEquals(0, $stream->tell());
  77. }
  78. public function testIsWritable() {
  79. $stream = new SplStream('Es');
  80. $this->assertEquals(true, $stream->isWritable());
  81. }
  82. public function testWrite() {
  83. $stream = new SplStream('');
  84. $stream->write('Es');
  85. $this->assertEquals('Es', $stream->__toString());
  86. }
  87. public function testIsReadable() {
  88. $stream = new SplStream('Es');
  89. $this->assertTrue($stream->isReadable());
  90. }
  91. public function testRead() {
  92. $resource = fopen('./test.txt', 'rb');
  93. $stream = new SplStream($resource);
  94. $this->assertEquals('E', $stream->read(1));
  95. }
  96. public function testGetContents() {
  97. $stream = new SplStream('Es');
  98. $stream->seek(0);
  99. $this->assertEquals('Es', $stream->getContents());
  100. }
  101. public function testGetMetadata() {
  102. $stream = new SplStream('Es');
  103. $this->assertEquals('MEMORY', $stream->getMetadata()['stream_type']);
  104. }
  105. public function testGetStreamResource() {
  106. $stream = new SplStream('Es');
  107. $source = $stream->getStreamResource();
  108. fseek($source, 0, SEEK_SET);
  109. $this->assertEquals('Es', stream_get_contents($source));
  110. }
  111. public function testTruncate() {
  112. $stream = new SplStream('Es');
  113. $stream->truncate(1);
  114. $this->assertEquals('E', $stream->__toString());
  115. }
  116. }