added first test cases (phpunit)
git-svn-id: svn://netflint.net/xmpphp@40 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
		
							
								
								
									
										31
									
								
								tests/AllTests.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										31
									
								
								tests/AllTests.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
<?php
 | 
			
		||||
if (!defined('PHPUnit_MAIN_METHOD')) {
 | 
			
		||||
    define('PHPUnit_MAIN_METHOD', 'AllTests::main');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
require_once 'XMPPHP/LogTest.php';
 | 
			
		||||
require_once 'XMPPHP/XMLObjTest.php';
 | 
			
		||||
require_once 'XMPPHP/XMPPTest.php';
 | 
			
		||||
 | 
			
		||||
class AllTests
 | 
			
		||||
{
 | 
			
		||||
    public static function main()
 | 
			
		||||
    {
 | 
			
		||||
        PHPUnit_TextUI_TestRunner::run(self::suite());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function suite()
 | 
			
		||||
    {
 | 
			
		||||
        $suite = new PHPUnit_Framework_TestSuite();
 | 
			
		||||
 | 
			
		||||
        $suite->addTestSuite('XMPPHP_LogTest');
 | 
			
		||||
        $suite->addTestSuite('XMPPHP_XMLObjTest');
 | 
			
		||||
        $suite->addTestSuite('XMPPHP_XMPPTest');
 | 
			
		||||
 | 
			
		||||
        return $suite;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if (PHPUnit_MAIN_METHOD == 'AllTests::main') {
 | 
			
		||||
    AllTests::main();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										162
									
								
								tests/XMPPHP/LogTest.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										162
									
								
								tests/XMPPHP/LogTest.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,162 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once dirname(dirname(dirname(__FILE__))) . '/Log.php';
 | 
			
		||||
 | 
			
		||||
class XMPPHP_LogTest extends PHPUnit_Framework_TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testPrintoutNoOutput()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log();
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log('test');
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertEquals('', $result);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function testPrintoutOutput()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(true);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertContains($msg, $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testPrintoutNoOutputWithDefaultLevel()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(true, XMPPHP_Log::LEVEL_ERROR);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testPrintoutOutputWithDefaultLevel()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(true, XMPPHP_Log::LEVEL_INFO);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertContains($msg, $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testPrintoutNoOutputWithCustomLevel()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(true, XMPPHP_Log::LEVEL_INFO);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg, XMPPHP_Log::LEVEL_DEBUG);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function testPrintoutOutputWithCustomLevel()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(true, XMPPHP_Log::LEVEL_INFO);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg, XMPPHP_Log::LEVEL_INFO);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertContains($msg, $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testExplicitPrintout()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(false);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testExplicitPrintoutResult()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(false);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->printout();
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertContains($msg, $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testExplicitPrintoutClear()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(false);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->printout();
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertContains($msg, $result);
 | 
			
		||||
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->printout();
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function testExplicitPrintoutLevel()
 | 
			
		||||
    {
 | 
			
		||||
        $log = new XMPPHP_Log(false, XMPPHP_Log::LEVEL_ERROR);
 | 
			
		||||
        
 | 
			
		||||
        $msg = 'I am a test log message';
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->log($msg);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
        
 | 
			
		||||
        ob_start();
 | 
			
		||||
        $log->printout(true, XMPPHP_Log::LEVEL_INFO);
 | 
			
		||||
        $result = ob_get_clean();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame('', $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								tests/XMPPHP/XMLObjTest.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										53
									
								
								tests/XMPPHP/XMLObjTest.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once dirname(dirname(dirname(__FILE__))) . '/XMLObj.php';
 | 
			
		||||
 | 
			
		||||
class XMPPHP_XMLObjTest extends PHPUnit_Framework_TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testToStringNameNamespace()
 | 
			
		||||
    {
 | 
			
		||||
        $xmlobj = new XMPPHP_XMLObj('testname', 'testNameSpace');
 | 
			
		||||
        
 | 
			
		||||
        $expected = "<testname xmlns='testNameSpace' ></testname>";
 | 
			
		||||
        
 | 
			
		||||
        $result = $xmlobj->toString();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame($expected, $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testToStringNameNamespaceAttr()
 | 
			
		||||
    {
 | 
			
		||||
        $xmlobj = new XMPPHP_XMLObj('testName', 'testNameSpace', array('attr1'=>'valA', 'attr2'=>'valB'));
 | 
			
		||||
        
 | 
			
		||||
        $expected = "<testname xmlns='testNameSpace' attr1='valA' attr2='valB' ></testname>";
 | 
			
		||||
        
 | 
			
		||||
        $result = $xmlobj->toString();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame($expected, $result);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function testToStringNameNamespaceData()
 | 
			
		||||
    {
 | 
			
		||||
        $xmlobj = new XMPPHP_XMLObj('testName', 'testNameSpace', array(), 'I am test data');
 | 
			
		||||
        
 | 
			
		||||
        $expected = "<testname xmlns='testNameSpace' >I am test data</testname>";
 | 
			
		||||
        
 | 
			
		||||
        $result = $xmlobj->toString();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame($expected, $result);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function testToStringNameNamespaceSub()
 | 
			
		||||
    {
 | 
			
		||||
        $xmlobj = new XMPPHP_XMLObj('testName', 'testNameSpace');
 | 
			
		||||
        $sub1 = new XMPPHP_XMLObj('subName', 'subNameSpace');
 | 
			
		||||
        $xmlobj->subs = array($sub1);
 | 
			
		||||
        
 | 
			
		||||
        $expected = "<testname xmlns='testNameSpace' ><subname xmlns='subNameSpace' ></subname></testname>";
 | 
			
		||||
        
 | 
			
		||||
        $result = $xmlobj->toString();
 | 
			
		||||
        
 | 
			
		||||
        $this->assertSame($expected, $result);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								tests/XMPPHP/XMPPTest.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										44
									
								
								tests/XMPPHP/XMPPTest.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once dirname(dirname(dirname(__FILE__))) . '/XMPP.php';
 | 
			
		||||
 | 
			
		||||
class XMPPHP_XMPPTest extends PHPUnit_Framework_TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testConnectException()
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $xmpp = new XMPPHP_XMPP('talk.google.com', 1234, 'invalidusername', 'invalidpassword', 'xmpphp', 'talk.google.com', false, XMPPHP_Log::LEVEL_VERBOSE);
 | 
			
		||||
            $xmpp->useEncryption(false);
 | 
			
		||||
            $xmpp->connect(10);
 | 
			
		||||
            $xmpp->processUntil('session_start');
 | 
			
		||||
            $xmpp->presence();
 | 
			
		||||
            $xmpp->message('stephan@jabber.wentz.it', 'This is a test message!');
 | 
			
		||||
            $xmpp->disconnect();
 | 
			
		||||
        } catch(XMPPHP_Exception $e) {
 | 
			
		||||
            return;
 | 
			
		||||
        } catch(Exception $e) {
 | 
			
		||||
            $this->fail('Unexpected Exception thrown: '.$e->getMessage());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $this->fail('Expected XMPPHP_Exception not thrown!');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testAuthException()
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $xmpp = new XMPPHP_XMPP('jabber.wentz.it', 5222, 'invalidusername', 'invalidpassword', 'xmpphp', 'jabber.wentz.it', true, XMPPHP_Log::LEVEL_VERBOSE);
 | 
			
		||||
            $xmpp->useEncryption(false);
 | 
			
		||||
            $xmpp->connect(10);
 | 
			
		||||
            $xmpp->processUntil('session_start');
 | 
			
		||||
            $xmpp->presence();
 | 
			
		||||
            $xmpp->message('stephan@jabber.wentz.it', 'This is a test message!');
 | 
			
		||||
            $xmpp->disconnect();
 | 
			
		||||
        } catch(XMPPHP_Exception $e) {
 | 
			
		||||
            return;
 | 
			
		||||
        } catch(Exception $e) {
 | 
			
		||||
            $this->fail('Unexpected Exception thrown: '.$e->getMessage());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $this->fail('Expected XMPPHP_Exception not thrown!');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user