PHP Classes
elePHPant
Icontem

File: tests/StreamTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Robert Young  >  PHP Multiplexed I/O  >  tests/StreamTest.php  >  Download  
File: tests/StreamTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test for StreamTest
Class: PHP Multiplexed I/O
Manage multiple simultaneous network connections
Author: By
Last change: moved to tests directory
Date: 7 years ago
Size: 3,832 bytes
 

Contents

Class file image Download
<?php
require_once MIO_PATH . 'Stream.php';
require_once
MIO_PATH . 'Exception.php';

class
MioStreamTest extends UnitTestCase
{
    private
       
$server,
       
$stream;

    public function
setUp()
    {
       
$this->server = stream_socket_server( '127.0.0.1:8888', $errno=null, $errstr=null );
        if( !
$this->server ) {
            throw new
Exception("Could not start test server [$errno:$errstr]");
        }
       
$this->stream = new MioStream( fsockopen( '127.0.0.1', 8888, $errno=null, $errstr=null ), '127.0.0.1:8888' );
    }

    public function
tearDown()
    {
        if(
is_resource( $this->server ) ) {
           
fclose( $this->server );
        }
       
$this->stream->close();
        unset(
$this->server, $this->stream );
    }

    public function
accept()
    {
        return
stream_socket_accept( $this->server );
    }

    public function
testCreateWithBadStream()
    {
        try {
            new
MioStream( null, 'fail me' );
           
$this->fail();
        } catch(
MioException $e ) {
           
$this->pass();
        }
    }

    public function
testBlocking()
    {
       
$this->assertFalse(
           
$this->stream->isBlocking(),
           
'Stream should start off in non-blocking mode'
       
);
       
$this->stream->setBlocking( 1 );
       
$this->assertTrue(
           
$this->stream->isBlocking()
        );
    }

    public function
testBadBlocking()
    {
        try {
           
$this->stream->setBlocking( 2 );
           
$this->fail();
        } catch(
MioBlockingException $e ) {
           
$this->pass();
        }
    }

    public function
testOpen()
    {
       
$this->assertTrue(
           
$this->stream->isOpen(),
           
'Stream should start off open'
       
);
    }

    public function
testClose()
    {
       
$this->stream->close();
       
$this->assertFalse(
           
$this->stream->isOpen(),
           
'Stream should be marked as closed when it is closed'
       
);
    }

    public function
testRead()
    {
       
$con = $this->accept();
       
fwrite( $con, "hello" );
       
$this->assertEqual(
           
$this->stream->read( 1024 ),
           
'hello'
       
);
    }

    public function
testReadClosed()
    {
       
$con = $this->accept();
       
fwrite( $con, "hello" );
       
$this->stream->close();
        try {
           
$this->stream->read( 1024 );
           
$this->fail();
        } catch(
MioClosedException $e ) {
           
$this->pass();
        }
    }

    public function
testWrite()
    {
       
$con = $this->accept();
       
$string = "hello";
       
$written = $this->stream->write( $string );
       
$this->assertEqual(
           
fread( $con, 1024 ),
           
$string
       
);
       
$this->assertEqual(
           
$written,
           
strlen( $string )
        );
    }

    public function
testWriteClosed()
    {
       
$this->stream->close();
        try {
           
$this->stream->write( 'hello' );
           
$this->fail();
        } catch(
MioClosedException $e ) {
           
$this->pass();
        }
    }

    public function
testWriteExceedStreamBuffer()
    {
       
$string = 'hello';
       
$string = str_repeat( $string, 1000000 );
       
$string_len = strlen( $string );

       
$con = $this->accept();
        try {
           
$this->stream->write( $string );
           
$this->fail();
        } catch(
MioException $e ) {
           
$this->pass();
        }
    }

    public function
testAccept()
    {
       
$stream = new MioStream( $this->server, '127.0.0.1:8888' );
       
$this->assertTrue(
           
$stream->accept() instanceof MioStream
       
);
    }

}