From: Gunnar Wrobel Date: Wed, 10 Feb 2010 10:49:17 +0000 (+0100) Subject: Add tests for the current exception class. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=e52a1e3d69d8098d0fac64ecb9b08a3e51f3febb;p=horde.git Add tests for the current exception class. --- diff --git a/framework/Exception/test/Horde/Exception/AllTests.php b/framework/Exception/test/Horde/Exception/AllTests.php new file mode 100644 index 000000000..c297c359e --- /dev/null +++ b/framework/Exception/test/Horde/Exception/AllTests.php @@ -0,0 +1,38 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Exception + */ + +/** + * Define the main method + */ +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Horde_Exception_AllTests::main'); +} + +/** + * Prepare the test setup. + */ +require_once 'Horde/Test/AllTests.php'; + +/** + * @package Horde_Exception + * @subpackage UnitTests + */ +class Horde_Exception_AllTests extends Horde_Test_AllTests +{ +} + +Horde_Exception_AllTests::init('Horde_Exception', __FILE__); + +if (PHPUnit_MAIN_METHOD == 'Horde_Exception_AllTests::main') { + Horde_Exception_AllTests::main(); +} diff --git a/framework/Exception/test/Horde/Exception/ExceptionTest.php b/framework/Exception/test/Horde/Exception/ExceptionTest.php new file mode 100644 index 000000000..40dfae816 --- /dev/null +++ b/framework/Exception/test/Horde/Exception/ExceptionTest.php @@ -0,0 +1,109 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Exception + */ + +/** + * Require the tested class. + */ +require_once 'Horde/Exception.php'; + +/** + * Test for the Horde_Exception:: class. + * + * Copyright 2009 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Horde + * @package Exception + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Exception + */ +class Horde_Exception_ExceptionTest extends PHPUnit_Framework_TestCase +{ + + public function testEmptyConstructionYieldsEmptyMessage() + { + $e = new Horde_Exception(); + $this->assertSame('', $e->getMessage()); + } + + public function testEmptyConstructionYieldsCodeZero() + { + $e = new Horde_Exception(); + $this->assertSame(0, $e->getCode()); + } + + public function testConstructionWithPearErrorYieldsMessageFromPearError() + { + require_once dirname(__FILE__) . '/Stub/PearError.php'; + $p = new Horde_Exception_Stub_PearError('pear'); + $e = new Horde_Exception($p); + $this->assertSame('pear', $e->getMessage()); + } + + public function testConstructionWithPearErrorYieldsCodeFromPearError() + { + require_once dirname(__FILE__) . '/Stub/PearError.php'; + $p = new Horde_Exception_Stub_PearError('pear', 666); + $e = new Horde_Exception($p); + $this->assertSame(666, $e->getCode()); + } + + public function testConstructionWithGetlasterrorarrayYieldsMessageFromArray() + { + $e = new Horde_Exception(null, $this->_getLastError()); + $this->assertSame('get_last_error', $e->getMessage()); + } + + public function testConstructionWithGetlasterrorarrayYieldsCodeFromArray() + { + $e = new Horde_Exception(null, $this->_getLastError()); + $this->assertSame(666, $e->getCode()); + } + + public function testConstructionWithGetlasterrorarrayYieldsFileFromArray() + { + $e = new Horde_Exception(null, $this->_getLastError()); + $this->assertSame('/some/file.php', $e->getFile()); + } + + public function testConstructionWithGetlasterrorarrayYieldsLineFromArray() + { + $e = new Horde_Exception(null, $this->_getLastError()); + $this->assertSame(99, $e->getLine()); + } + + public function testConstructionWithGetlasterrorarrayConcatenatesMessagesFromConstructorAndErrorarray() + { + $e = new Horde_Exception('An error occurred: ', $this->_getLastError()); + $this->assertSame('An error occurred: get_last_error', $e->getMessage()); + } + + public function testStringCodesAreSetToNull() + { + $e = new Horde_Exception('test', 'some code'); + $this->assertSame(0, $e->getCode()); + } + + private function _getLastError() + { + return array( + 'message' => 'get_last_error', + 'type' => 666, + 'file' => '/some/file.php', + 'line' => 99 + ); + } +} \ No newline at end of file diff --git a/framework/Exception/test/Horde/Exception/Stub/PearError.php b/framework/Exception/test/Horde/Exception/Stub/PearError.php new file mode 100644 index 000000000..f91f1d819 --- /dev/null +++ b/framework/Exception/test/Horde/Exception/Stub/PearError.php @@ -0,0 +1,33 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Exception + */ +class Horde_Exception_Stub_PearError +{ + private $_message; + private $_code; + + public function __construct($message = null, $code = null) + { + $this->_message = $message; + $this->_code = $code; + } + + public function getMessage() + { + return $this->_message; + } + + public function getCode() + { + return $this->_code; + } +} \ No newline at end of file diff --git a/framework/Exception/test/Horde/Exception/phpunit.xml b/framework/Exception/test/Horde/Exception/phpunit.xml new file mode 100644 index 000000000..502d3c9b8 --- /dev/null +++ b/framework/Exception/test/Horde/Exception/phpunit.xml @@ -0,0 +1,8 @@ + + + + + ../../../lib + + +