Skip to content
This repository
tree: 3f653c98ff
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 187 lines (171 sloc) 6.909 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
<?php
namespace PEAR2\Net\Transmitter;
use PEAR2\Net\Transmitter\TcpClient as C;
use PEAR2\Net\Transmitter\TcpServerConnection as SC;

class UnconnectedTest extends \PHPUnit_Framework_TestCase
{
    
    /**
* @var int
*/
    public static $defaultSocketTimeout;

    public static function setUpBeforeClass()
    {
        self::$defaultSocketTimeout = ini_set('default_socket_timeout', 2);
    }

    public static function tearDownAfterClass()
    {
        ini_set('default_socket_timeout', self::$defaultSocketTimeout);
    }
    public function testDefaultStreamTransmitterException()
    {
        try {
            $trans = new Stream('invalid arg');
            $this->fail('Transmitter initialization had to fail.');
        } catch (StreamException $e) {
            $this->assertEquals(1, $e->getCode(), 'Improper exception code.');
        }
    }
    
    public function testEmptyFilterCollection()
    {
        $filters = new FilterCollection();
        $this->assertFalse($filters->rewind());
        $this->assertFalse($filters->end());
        $this->assertFalse($filters->key());
        $this->assertFalse($filters->current());
        $this->assertFalse($filters->valid());
        $this->assertEquals(0, count($filters));
    }
    
    public function testFilterCollection()
    {
        $filters = new FilterCollection();
        $this->assertEquals(0, count($filters));
        $filters->append('string.toupper');
        $this->assertEquals(1, count($filters));
        $filters->append('string.rot13');
        $this->assertEquals(2, count($filters));
        $filters->clear();
        $this->assertEquals(0, count($filters));
        
        $filters->append('string.toupper');
        $this->assertEquals(1, count($filters));
        $this->assertEquals('string.toupper', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $filters->insertBefore(-1, 'string.rot13');
        $filters->insertBefore(0, 'string.base64');
        $filters->insertBefore(1, 'string.tolower');
        $filters->insertBefore(count($filters) + 2, 'string.quoted-printable');
        
        $this->assertEquals('string.base64', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(0, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.tolower', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(1, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.rot13', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(2, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.toupper', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(3, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.quoted-printable', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(4, $filters->getCurrentPosition());
        
        $this->assertTrue($filters->prev());
        $this->assertEquals('string.toupper', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(3, $filters->getCurrentPosition());
        
        $this->assertTrue($filters->rewind());
        $filters->removeAt(2);
        
        $this->assertEquals('string.base64', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(0, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.tolower', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(1, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.toupper', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(2, $filters->getCurrentPosition());
        $this->assertTrue($filters->next());
        $this->assertEquals('string.quoted-printable', $filters->key());
        $this->assertEquals(array(), $filters->current());
        $this->assertEquals(3, $filters->getCurrentPosition());
    }
    
    public function testInvalidContext()
    {
        try {
            new C(
                REMOTE_HOSTNAME, REMOTE_PORT, false, null, '',
                fopen('php://input', 'r')
            );
            $this->fail('Client creation had to fail.');
        } catch(SocketException $e)
        {
            $this->assertEquals(6, $e->getCode(), 'Improper exception code.');
        }
    }
    
    public function testSilence()
    {
        try {
            new C(SILENT_HOSTNAME, REMOTE_PORT);
            $this->fail('Client creation had to fail.');
        } catch(SocketException $e)
        {
            $this->assertEquals(8, $e->getCode(), 'Improper exception code.');
            $this->assertEquals(
                10061, $e->getSocketErrorNumber(), 'Improper exception code.'
            );
        }
        try {
            new C(REMOTE_HOSTNAME, SILENT_PORT);
            $this->fail('Client creation had to fail.');
        } catch(SocketException $e)
        {
            $this->assertEquals(8, $e->getCode(), 'Improper exception code.');
            $this->assertEquals(
                10061, $e->getSocketErrorNumber(), 'Improper exception code.'
            );
        }
    }
    
    public function testInvalidClient()
    {
        try {
            new C('@', REMOTE_PORT);
            $this->fail('Client creation had to fail.');
        } catch(SocketException $e)
        {
            $this->assertEquals(7, $e->getCode(), 'Improper exception code.');
            $this->assertEquals(
                0, $e->getSocketErrorNumber(), 'Improper exception code.'
            );
        }
    }
    
    public function testInvalidServer()
    {
        try {
            new SC('not a server', 1/*h*/ * 60/*m*/ * 60/*s*/);
            $this->fail('Server creation had to fail.');
        } catch(SocketException $e)
        {
            $this->assertEquals(9, $e->getCode(), 'Improper exception code.');
        }
    }
    
    public function testServerConnectionTimeout()
    {
        try {
            new SC(
                stream_socket_server(
                    'tcp://' . LOCAL_HOSTNAME . ':' . LOCAL_PORT
                ),
                2
            );
            $this->fail('Server creation had to fail.');
        } catch(SocketException $e)
        {
            $this->assertEquals(10, $e->getCode(), 'Improper exception code.');
        }
    }
}
Something went wrong with that request. Please try again.