Location: PHPKode > projects > TeamSpeak3-Library > devMX-TeamSpeak3-Library-e93460b/test/src/devmx/Teamspeak3/Query/ServerQueryTest.php
<?php

namespace devmx\Teamspeak3\Query;


require_once dirname( __FILE__ ) . '/../../../../../src/devmx/Teamspeak3/Query/ServerQuery.php';

/**
 * Test class for ServerQuery.
 * Generated by PHPUnit on 2012-01-26 at 19:03:35.
 */
class ServerQueryTest extends \PHPUnit_Framework_TestCase
{

    /**
     * @var \devmx\Teamspeak3\Query\ServerQuery
     */
    protected $query;
    
    /**
     *@var \devmx\Teamspeak3\Query\Transport\QueryTransportStub
     */
    protected $stub;
    
    /**
     *@var type \ðevmx\Teamspeak3\Query\Transport\Decorator\DebuggingDecorator
     */
    protected $transport;

    /**
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->stub = new Transport\QueryTransportStub;
        $this->transport = new Transport\Decorator\DebuggingDecorator($this->stub);
        $this->query = new ServerQuery($this->transport);
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::__construct
     * @covers devmx\Teamspeak3\Query\ServerQuery::getTransport
     */
    public function testConstruct() {
        $this->assertEquals($this->transport,$this->query->getTransport());
    }
    
        
    /**
     * @dataProvider whoamiProvider
     * @covers devmx\Teamspeak3\Query\ServerQuery::refreshWhoAmI
     * @covers devmx\Teamspeak3\Query\ServerQuery::isOnVirtualServer
     * @covers devmx\Teamspeak3\Query\ServerQuery::isLoggedIn
     * @covers devmx\Teamspeak3\Query\ServerQuery::getLoginName
     * @covers devmx\Teamspeak3\Query\ServerQuery::getVirtualServerPort
     * @covers devmx\Teamspeak3\Query\ServerQuery::getVirtualServerID
     * @covers devmx\Teamspeak3\Query\ServerQuery::getVirtualServerIdentifyer
     * @covers devmx\Teamspeak3\Query\ServerQuery::getChannelID
     * @covers devmx\Teamspeak3\Query\ServerQuery::getVirtualServerStatus
     * @covers devmx\Teamspeak3\Query\ServerQuery::getUniqueID
     * @covers devmx\Teamspeak3\Query\ServerQuery::getNickname
     * @covers devmx\Teamspeak3\Query\ServerQuery::getDataBaseID
     * @covers devmx\Teamspeak3\Query\ServerQuery::getUniqueVirtualServerID
     * @covers devmx\Teamspeak3\Query\ServerQuery::getClientID
     */
    public function testRefreshWhoAmI($items, $values)
    {
        $cmd = new Command('whoami');
        $this->stub->addResponse(new CommandResponse($cmd, $items));
        $this->query->connect();
        $this->query->refreshWhoAmI();
        $run = 0;
        foreach($values as $method => $expected) {
            $run++;
            $this->assertEquals($expected, $this->query->$method(), "Testing $method in run $run");
        }
    }
    
    public function whoamiProvider() {
        $items1 = array(
          'virtualserver_status' => 'online',
          'virtualserver_id' => 1,
          'virtualserver_unique_identifyer' => 'foo',
          'virtualserver_port' => 9987,
          'client_id' => 11,
          'client_channel_id' => 123,
          'client_nickname' => 'foobar',
          'client_database_id' => 0,
          'client_login_name' => 'asdf',
          'client_unique_identifyer' => 'sdfsdf',
        );
        $expected1 = array(
            'isOnVirtualServer' => true,
            'isLoggedIn' => true,
            'getLoginName' => 'asdf',
            'getVirtualServerPort' => 9987,
            'getVirtualServerID' => 1,
            'getVirtualServerIdentifyer' => array('id'=>1),
            'getChannelID' => 123,
            'getVirtualServerStatus' => 'online',
            'getUniqueID' => 'sdfsdf',
            'getNickname' => 'foobar',
            'getDataBaseID' => 0,
            'getUniqueVirtualServerID' => 'foo',
            'getClientID' => 11,
        );
        
        $items2 = array(
          'virtualserver_status' => 'unknown',
          'virtualserver_id' => 0,
          'virtualserver_unique_identifyer' => '',
          'virtualserver_port' => 0,
          'client_id' => 0,
          'client_channel_id' => 0,
          'client_nickname' => '',
          'client_database_id' => 0,
          'client_login_name' => '',
          'client_unique_identifyer' => '',
        );
        $expected2 = array(
            'isOnVirtualServer' => false,
            'isLoggedIn' => false,
            'getLoginName' => '',
            'getVirtualServerPort' => 0,
            'getVirtualServerID' => 0,
            'getVirtualServerIdentifyer' => array(),
            'getChannelID' => 0,
            'getVirtualServerStatus' => 'unknown',
            'getUniqueID' => '',
            'getNickname' => '',
            'getDataBaseID' => 0,
            'getUniqueVirtualServerID' => '',
            'getClientID' => 0,
        );
        
        return array(
            array($items1, $expected1),
            array($items2, $expected2),
        );
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::refreshWhoAmI
     * @expectedException \RuntimeException
     */
    public function testRefreshWhoAmI_failedResponse() {
        $r = new CommandResponse(new Command('whoami'), array(), 123, 'failed');
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->query->refreshWhoAmI();
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::login
     * @covers devmx\Teamspeak3\Query\ServerQuery::isLoggedIn
     */
    public function testLogin()
    {
        $this->login();
        $this->assertTrue($this->query->isLoggedIn());
    }
    
    /**
     * @expectedException \RunTimeException
     * @covers devmx\Teamspeak3\Query\ServerQuery::login
     * @covers devmx\Teamspeak3\Query\ServerQuery::isLoggedIn
     */
    public function testLogin_failed()
    {
        $cmd = new Command('login', array('client_login_name'=>'foo', 'client_login_password'=>'bar'));
        $r = new CommandResponse($cmd, array(), 123, 'there was an error');
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->query->login('foo','bar');
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::logout
     * @covers devmx\Teamspeak3\Query\ServerQuery::isLoggedIn
     */
    public function testLogout()
    {
        $this->login();
        $cmd = new Command('logout');
        $r = new CommandResponse($cmd);
        $this->stub->addResponse($r);
        $this->query->logout();
        $this->assertFalse($this->query->isLoggedIn());
    }
    
    protected function login() {
        $cmd = new Command('login', array('client_login_name'=>'foo', 'client_login_password'=>'bar'));
        $r = new CommandResponse($cmd, array());
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->query->login('foo','bar');
    }
    
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::useByPort
     * @todo Implement testUseByPort().
     */
    public function testUseByPort()
    {
       $cmd = new Command('use', array('port'=>9987), array('virtual'));
       $r = new CommandResponse($cmd);
       $this->stub->addResponse($r);
       $this->query->connect();
       $this->query->useByPort(9987);
       $this->assertTrue($this->query->isOnVirtualServer());
       $this->assertEquals(array('port'=>9987), $this->query->getVirtualServerIdentifyer());
       $this->assertEquals(array($r), $this->transport->getReceivedResponses());
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::useByID
     * @todo Implement testUseByID().
     */
    public function testUseByID()
    {
       $r = $this->useByID(15);
       $this->assertTrue($this->query->isOnVirtualServer());
       $this->assertEquals(array('id'=>15), $this->query->getVirtualServerIdentifyer());
       $this->assertEquals(array($r), $this->transport->getReceivedResponses());
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::useByID
     * @expectedException \InvalidArgumentException
     */
    public function testUseByID_invalidID()
    {
        $this->useByID(0);
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::deselect
     * @todo Implement testDeselect().
     */
    public function testDeselect()
    {
       $this->useByID(32);
       $this->stub->addResponse(new CommandResponse(new Command('use')));
       $this->query->deselect();
       $this->assertFalse($this->query->isOnVirtualServer());
       $this->assertEquals(array(), $this->query->getVirtualServerIdentifyer());
    }
    
    protected function useByID($id) {
       $cmd = new Command('use', array('sid'=>$id), array('virtual'));
       $r = new CommandResponse($cmd);
       $this->stub->addResponse($r);
       $this->query->connect();
       $this->query->useByID($id);
       return $r;
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::moveToChannel
     * @todo Implement testMoveToChannel().
     */
    public function testMoveToChannel()
    {
        $cmd = new Command('clientmove', array('clid'=>15, 'cid'=>12));
        $whoamiCmd = new Command('whoami');
        $whoamiItems = array(
          'virtualserverstatus' => 'online',
          'virtualserver_id' => 1,
          'virtualserver_unique_identifyer' => 'foo',
          'virtualserver_port' => 9987,
          'client_id' => 15,
          'client_channel_id' => 123,
          'client_nickname' => 'foobar',
          'client_database_id' => 0,
          'client_login_name' => 'asdf',
          'client_unique_identifyer' => 'sdfsdf',
        );
        $this->stub->addResponse(new CommandResponse(new Command('use', array('port'=>123), array('virtual'))));
        $this->stub->addResponse(new CommandResponse($whoamiCmd, $whoamiItems));
        $this->stub->addResponse(new CommandResponse($cmd));
        $this->query->connect();
        $this->query->useByPort(123);
        $this->query->refreshWhoAmI();
        $this->query->moveToChannel(12);
        $this->assertEquals(12, $this->query->getChannelID());
    }
    
    /**
     * @expectedException \BadMethodCallException
     * @covers devmx\Teamspeak3\Query\ServerQuery::moveToChannel
     */
    public function testMoveToChannel_notOnVirtualServer()
    {
        $cmd = new Command('clientmove', array('clid'=>15, 'cid'=>12));
        $this->stub->addResponse(new CommandResponse($cmd));
        $this->query->connect();
        $this->query->moveToChannel(12);
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::registerForEvent
     * @covers devmx\Teamspeak3\Query\ServerQuery::getRegisterCommands
     */
    public function testRegisterForEvent()
    {
        $cmd = new Command('servernotifyregister', array('event'=>'foobar'));
        $r = new CommandResponse($cmd);
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->query->registerForEvent('foobar');
        $this->assertEquals(array($cmd), $this->query->getRegisterCommands());
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::registerForEvent
     * @covers devmx\Teamspeak3\Query\ServerQuery::getRegisterCommands
     */
    public function testRegisterForEvent_cid()
    {
        $cmd = new Command('servernotifyregister', array('event'=>'foobar', 'cid'=>123));
        $r = new CommandResponse($cmd);
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->query->registerForEvent('foobar', 123);
        $this->assertEquals(array($cmd), $this->query->getRegisterCommands());
        $this->stub->assertAllResponsesReceived();
    }
    
    /**
     * @expectedException \RuntimeException
     * @covers devmx\Teamspeak3\Query\ServerQuery::registerForEvent
     */
    public function testRegisterForEvent_error()
    {
        $cmd = new Command('servernotifyregister', array('event'=>'foobar', 'cid'=>123));
        $r = new CommandResponse($cmd, array(), 123, 'error');
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->query->registerForEvent('foobar', 123);
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::unregisterEvents
     * @todo Implement testUnregisterEvents().
     */
    public function testUnregisterEvents()
    {
        $cmd = new Command('servernotifyunregister');
        $cmd2 = new Command('servernotifyregister', array('event'=>'foo'));
        $r = new CommandResponse($cmd);
        $r2 = new CommandResponse($cmd2);
        $this->stub->addResponse($r);
        $this->stub->addResponse($r2);
        $this->query->connect();
        $this->query->registerForEvent('foo');
        $this->query->unregisterEvents();
        $this->stub->assertAllResponsesReceived();
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::quit
     */
    public function testQuit()
    {
        $this->query->connect();
        $this->query->quit();
        $this->assertFalse($this->query->isConnected());
    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::__clone
     * @todo Implement test__clone().
     */
    public function test__clone()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
                'This test has not been implemented yet.'
        );

    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::__sleep
     * @covers devmx\Teamspeak3\Query\ServerQuery::__wakeup
     * @covers devmx\Teamspeak3\Query\ServerQuery::recoverState
     */
    public function testSerializeUnserialize()
    {
        $r1 = new CommandResponse(new Command('use', array('sid'=>12), array('virtual')));
        $r2 = new CommandResponse(new Command('servernotifyregister', array('event'=>'foo')));
        $r3 = new CommandResponse(new Command('login', array('client_login_name'=>'foo', 'client_login_password'=>'bar')));
        $this->stub->addResponses($r1, $r2, $r3);
        $this->query->connect();
        $this->query->useByID(12);
        $this->query->registerForEvent('foo');
        $this->query->login('foo', 'bar');
        $this->stub->assertAllResponsesReceived();
        $this->stub->addResponses($r1, $r2, $r3);
        $serialized = serialize($this->query);
        $this->assertFalse($this->transport->isConnected());
        $unserialized = unserialize($serialized);
        $this->assertTrue($unserialized->isOnVirtualServer());
        $this->assertEquals(12, $unserialized->getVirtualServerID());
        $this->assertEquals(array($r2->getCommand()), $unserialized->getRegisterCommands());
        $this->assertTrue($unserialized->isLoggedIn());
    }

    

    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::connect
     * @todo Implement testConnect().
     */
    public function testConnect()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
                'This test has not been implemented yet.'
        );

    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::disconnect
     * @todo Implement testDisconnect().
     */
    public function testDisconnect()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
                'This test has not been implemented yet.'
        );

    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::getAllEvents
     * @todo Implement testGetAllEvents().
     */
    public function testGetAllEvents()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
                'This test has not been implemented yet.'
        );

    }

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::isConnected
     * @todo Implement testIsConnected().
     */
    public function testIsConnected()
    {
        // Remove the following lines when you implement this test.
        $this->markTestIncomplete(
                'This test has not been implemented yet.'
        );

    }

    

    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::waitForEvent
     * @covers devmx\Teamspeak3\Query\ServerQuery::hasRegisteredForEvents
     */
    public function testWaitForEvent()
    {
        $e = new Event('notifyfoo', array());
        $this->stub->addEvent($e);
        $this->stub->addResponse(new CommandResponse(new Command('servernotifyregister', array('event'=>'channel'))));
        
        $this->query->connect();
        $this->query->registerForEvent('channel');
        $this->assertEquals(array($e), $this->query->waitForEvent());
    }
    
    /**
     * @expectedException \LogicException
     * @covers devmx\Teamspeak3\Query\ServerQuery::waitForEvent
     * @covers devmx\Teamspeak3\Query\ServerQuery::hasRegisteredForEvents
     */
    public function testWaitForEvent_notRegistered()
    {
        $e = new Event('notifyfoo', array());
        $this->stub->addEvent($e);
        
        $this->query->connect();
        $this->assertEquals(array($e), $this->query->waitForEvent());
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::sendCommand
     */
    public function testSendCommand()
    {
        $cmd = new Command('foo');
        $r = new CommandResponse($cmd);
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->assertEquals($r, $this->query->sendCommand($cmd));
    }
    
    /**
     * @dataProvider whoamiProvider
     * @covers devmx\Teamspeak3\Query\ServerQuery::sendCommand
     */
    public function testSendCommand_recognizeWhoAmI($items, $values) {
        $cmd = new Command('whoami');
        $r = new CommandResponse($cmd, $items);
        $this->stub->addResponse($r);
        $run = 0;
        
        $this->query->connect();
        $this->query->query('whoami');
        foreach($values as $method => $expected) {
            $run++;
            $this->assertEquals($expected, $this->query->$method(), "Testing $method in run $run");
        }
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::sendCommand
     * @covers devmx\Teamspeak3\Query\ServerQuery::useVirtualServer
     */
    public function testSendCommand_recognizeUse() {
        $cmd = new Command('use', array('port'=>9987), array('virtual'));
        $this->stub->addResponse(new CommandResponse($cmd));
        $this->query->connect();
        $this->query->sendCommand($cmd);
        $this->assertEquals(true, $this->query->isOnVirtualServer());
        $this->stub->assertAllResponsesReceived();
    }
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::sendCommand
     * @covers devmx\Teamspeak3\Query\ServerQuery::getLoginPass
     */
    public function testSendCommand_recognizeLoginAndLogout() {
        $cmd = new Command('login', array('client_login_name'=>'foo', 'client_login_password'=>'bar'));
        $this->stub->addResponse(new CommandResponse($cmd));
        $this->query->connect();
        $this->query->sendCommand($cmd);
        $this->assertEquals(true, $this->query->isLoggedIn());
        $this->assertEquals('foo', $this->query->getLoginName());
        $this->assertEquals('bar', $this->query->getLoginPass());
        $this->stub->assertAllResponsesReceived();
        $this->stub->addResponse(new CommandResponse(new Command('logout')));
        $this->query->query('logout');
        $this->assertFalse($this->query->isLoggedIn());
    }
    
    
    /**
     * @covers devmx\Teamspeak3\Query\ServerQuery::query
     */
    public function testQuery()
    {
        $cmd = new Command('foo');
        $r = new CommandResponse($cmd);
        $this->stub->addResponse($r);
        $this->query->connect();
        $this->assertEquals($r, $this->query->query('foo'));
    }

}

?>
Return current item: TeamSpeak3-Library