From 4a4e27e91d32db2a90a3a9856b42c1dd57de3df8 Mon Sep 17 00:00:00 2001 From: temp Date: Mon, 5 May 2008 21:15:32 +0000 Subject: [PATCH] added first test cases (phpunit) git-svn-id: svn://netflint.net/xmpphp@40 ef36c318-a008-4979-b6e8-6b496270793b --- tests/AllTests.php | 31 +++++++ tests/XMPPHP/LogTest.php | 162 ++++++++++++++++++++++++++++++++++++ tests/XMPPHP/XMLObjTest.php | 53 ++++++++++++ tests/XMPPHP/XMPPTest.php | 44 ++++++++++ 4 files changed, 290 insertions(+) create mode 100755 tests/AllTests.php create mode 100755 tests/XMPPHP/LogTest.php create mode 100755 tests/XMPPHP/XMLObjTest.php create mode 100755 tests/XMPPHP/XMPPTest.php diff --git a/tests/AllTests.php b/tests/AllTests.php new file mode 100755 index 0000000..049325f --- /dev/null +++ b/tests/AllTests.php @@ -0,0 +1,31 @@ +addTestSuite('XMPPHP_LogTest'); + $suite->addTestSuite('XMPPHP_XMLObjTest'); + $suite->addTestSuite('XMPPHP_XMPPTest'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'AllTests::main') { + AllTests::main(); +} diff --git a/tests/XMPPHP/LogTest.php b/tests/XMPPHP/LogTest.php new file mode 100755 index 0000000..9c1a236 --- /dev/null +++ b/tests/XMPPHP/LogTest.php @@ -0,0 +1,162 @@ +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); + } + + +} diff --git a/tests/XMPPHP/XMLObjTest.php b/tests/XMPPHP/XMLObjTest.php new file mode 100755 index 0000000..bb6fb41 --- /dev/null +++ b/tests/XMPPHP/XMLObjTest.php @@ -0,0 +1,53 @@ +"; + + $result = $xmlobj->toString(); + + $this->assertSame($expected, $result); + } + + public function testToStringNameNamespaceAttr() + { + $xmlobj = new XMPPHP_XMLObj('testName', 'testNameSpace', array('attr1'=>'valA', 'attr2'=>'valB')); + + $expected = ""; + + $result = $xmlobj->toString(); + + $this->assertSame($expected, $result); + } + + public function testToStringNameNamespaceData() + { + $xmlobj = new XMPPHP_XMLObj('testName', 'testNameSpace', array(), 'I am test data'); + + $expected = "I am test data"; + + $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 = ""; + + $result = $xmlobj->toString(); + + $this->assertSame($expected, $result); + } + +} diff --git a/tests/XMPPHP/XMPPTest.php b/tests/XMPPHP/XMPPTest.php new file mode 100755 index 0000000..ec21225 --- /dev/null +++ b/tests/XMPPHP/XMPPTest.php @@ -0,0 +1,44 @@ +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!'); + } +}