PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/libraries/phpmio/tests/StreamTest.php

https://github.com/ibnoe/Microweber
PHP | 176 lines | 152 code | 23 blank | 1 comment | 2 complexity | 70cd600761a8ba7b04beefc900fd5df1 MD5 | raw file
  1. <?php
  2. require_once MIO_PATH . 'Stream.php';
  3. require_once MIO_PATH . 'Exception.php';
  4. class MioStreamTest extends UnitTestCase
  5. {
  6. private
  7. $server,
  8. $stream;
  9. public function setUp()
  10. {
  11. $this->server = stream_socket_server( '127.0.0.1:8888', $errno=null, $errstr=null );
  12. if( !$this->server ) {
  13. throw new Exception("Could not start test server [$errno:$errstr]");
  14. }
  15. $this->stream = new MioStream( fsockopen( '127.0.0.1', 8888, $errno=null, $errstr=null ), '127.0.0.1:8888' );
  16. }
  17. public function tearDown()
  18. {
  19. if( is_resource( $this->server ) ) {
  20. fclose( $this->server );
  21. }
  22. $this->stream->close();
  23. unset( $this->server, $this->stream );
  24. }
  25. public function accept()
  26. {
  27. return stream_socket_accept( $this->server );
  28. }
  29. public function testCreateWithBadStream()
  30. {
  31. try {
  32. new MioStream( null, 'fail me' );
  33. $this->fail();
  34. } catch( MioException $e ) {
  35. $this->pass();
  36. }
  37. }
  38. public function testBlocking()
  39. {
  40. $this->assertFalse(
  41. $this->stream->isBlocking(),
  42. 'Stream should start off in non-blocking mode'
  43. );
  44. $this->stream->setBlocking( 1 );
  45. $this->assertTrue(
  46. $this->stream->isBlocking()
  47. );
  48. }
  49. public function testBadBlocking()
  50. {
  51. try {
  52. $this->stream->setBlocking( 2 );
  53. $this->fail();
  54. } catch( MioBlockingException $e ) {
  55. $this->pass();
  56. }
  57. }
  58. public function testOpen()
  59. {
  60. $this->assertTrue(
  61. $this->stream->isOpen(),
  62. 'Stream should start off open'
  63. );
  64. }
  65. public function testClose()
  66. {
  67. $this->stream->close();
  68. $this->assertFalse(
  69. $this->stream->isOpen(),
  70. 'Stream should be marked as closed when it is closed'
  71. );
  72. }
  73. public function testRead()
  74. {
  75. $con = $this->accept();
  76. fwrite( $con, "hello" );
  77. $this->assertEqual(
  78. $this->stream->read( 1024 ),
  79. 'hello'
  80. );
  81. }
  82. public function testReadClosed()
  83. {
  84. $con = $this->accept();
  85. fwrite( $con, "hello" );
  86. $this->stream->close();
  87. try {
  88. $this->stream->read( 1024 );
  89. $this->fail();
  90. } catch( MioClosedException $e ) {
  91. $this->pass();
  92. }
  93. }
  94. public function testWrite()
  95. {
  96. $con = $this->accept();
  97. $string = "hello";
  98. $written = $this->stream->write( $string );
  99. $this->assertEqual(
  100. fread( $con, 1024 ),
  101. $string
  102. );
  103. $this->assertEqual(
  104. $written,
  105. strlen( $string )
  106. );
  107. }
  108. public function testWriteClosed()
  109. {
  110. $this->stream->close();
  111. try {
  112. $this->stream->write( 'hello' );
  113. $this->fail();
  114. } catch( MioClosedException $e ) {
  115. $this->pass();
  116. }
  117. }
  118. public function testWriteOveriteFwriteBuffer()
  119. {
  120. $string = 'hello';
  121. $string = str_repeat( $string, 13464 );
  122. $string_len = strlen( $string );
  123. $con = $this->accept();
  124. $this->assertFalse(
  125. $this->stream->write( $string )
  126. );
  127. // Clear out the stream buffer
  128. fread( $con, $string_len );
  129. $this->assertTrue(
  130. $this->stream->write()
  131. );
  132. }
  133. public function testWriteExceedStreamBuffer()
  134. {
  135. $string = 'hello';
  136. $string = str_repeat( $string, 1000000 );
  137. $string_len = strlen( $string );
  138. $con = $this->accept();
  139. try {
  140. $this->stream->write( $string );
  141. $this->fail();
  142. } catch( MioException $e ) {
  143. $this->pass();
  144. }
  145. }
  146. public function testAccept()
  147. {
  148. $stream = new MioStream( $this->server, '127.0.0.1:8888' );
  149. $this->assertTrue(
  150. $stream->accept() instanceof MioStream
  151. );
  152. }
  153. }