Login   Register  
PHP Classes
elePHPant
Icontem

File: tests/SelectionKeyTest.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/SelectionKeyTest.php  >  Download  
File: tests/SelectionKeyTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test for SelectionKey class
Class: PHP Multiplexed I/O
Manage multiple simultaneous network connections
Author: By
Last change: Moved to tests directory
Date: 7 years ago
Size: 4,766 bytes
 

Contents

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

class 
MioBlankObject {
}

class 
MioSelectionKeyTest extends UnitTestCase
{
    private
        
$server,
        
$key;
    
    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]");
        }
        
$stream = new MioStreamfsockopen'127.0.0.1'8888$errno=null$errstr=null ), '127.0.0.1:8888' );
        
$this->key = new MioSelectionKey$stream);
    }

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

    public function 
testIllegalPropertyAccess()
    {
        try {
            
$this->key->stream;
            
$this->pass();
        } catch( 
InvalidArgumentException $e ) {
            
$this->fail();
        }
        try {
            
$this->key->interest_ops;
            
$this->fail();
        } catch( 
InvalidArgumentException $e ) {
            
$this->pass();
        }
    }

    public function 
testBlockingCreation()
    {
        
$stream = new MioStreamfsockopen'127.0.0.1'8888$errno=null$errstr=null ), '127.0.0.1:8888' );
        
$stream->setBlocking);
        try {
            
$key = new MioSelectionKey$stream);
            
$this->fail();
        } catch( 
MioBlockingException $e ) {
            
$this->pass();
        }
    }

    public function 
testClosedCreation()
    {
        
$stream = new MioStreamfsockopen'127.0.0.1'8888$errno=null$errstr=null ), '127.0.0.1:8888' );
        
$stream->close();
        try {
            
$key = new MioSelectionKey$stream);
            
$this->fail();
        } catch( 
MioClosedException $e ) {
            
$this->pass();
        }
    }

    public function 
testInterestOps()
    {
        
$this->assertTrue(
            
$this->key->interestedIn)
        );
        
$this->assertFalse(
            
$this->key->interestedInMioSelectionKey::OP_READ )
        );
    }

    public function 
testSetInterestOps()
    {
        
$this->key->setInterestOpsMioSelectionKey::OP_READ );
        
$this->assertTrue(
            
$this->key->interestedInMioSelectionKey::OP_READ )
        );
        
$this->assertFalse(
            
$this->key->interestedInMioSelectionKey::OP_WRITE )
        );
    }

    public function 
testSetInterestOpsFailure()
    {
        try {
            
$this->key->setInterestOps123 );
            
$this->fail();
        } catch( 
MioOpsException $e ) {
            
$this->pass();
        }
    }

    public function 
testAddReadableOp()
    {
        
$this->key->setInterestOpsMioSelectionKey::OP_READ );
        
$this->key->addReadyOpMioSelectionKey::OP_READ );
        
$this->assertTrue(
            
$this->key->isReadable()
        );
    }

    public function 
testAddWritableOp()
    {
        
$this->key->setInterestOpsMioSelectionKey::OP_WRITE );
        
$this->key->addReadyOpMioSelectionKey::OP_WRITE );
        
$this->assertTrue(
            
$this->key->isWritable()
        );
    }

    public function 
testAddAcceptableOp()
    {
        
$this->key->setInterestOpsMioSelectionKey::OP_ACCEPT );
        
$this->key->addReadyOpMioSelectionKey::OP_ACCEPT );
        
$this->assertTrue(
            
$this->key->isAcceptable()
        );
    }

    public function 
testAddBadReadyOp()
    {
        try {
            
$this->key->addReadyOp22 );
            
$this->fail();
        } catch( 
MioOpsException $e ) {
            
$this->pass();
        }
    }

    public function 
testResetReadyOps()
    {
        
$this->key->addReadyOpMioSelectionKey::OP_READ );
        
$this->key->resetReadyOps();
        
$this->assertFalse(
            
$this->key->isReadable()
        );
    }

    public function 
testAttach()
    {
        
$object = new MioBlankObject();
        
$this->key->attach$object );
        
$this->assert(
            new 
IdenticalExpectation$object ),
            
$this->key->attachment
        
);
    }

    public function 
testAttachNonObject()
    {
        
$array = array();
        try {
            
$this->key->attach$array );
            
$this->fail();
        } catch( 
MioException $e ) {
            
$this->pass();
        }
    }

    public function 
testTurnToString()
    {
        
$this->assertPattern(
            
'/SelectionKey \(\d:\d?\)/',
            
"".$this->key
        
);
    }
    
}