Add tests for the current exception class.
authorGunnar Wrobel <wrobel@temple.(none)>
Wed, 10 Feb 2010 10:49:17 +0000 (11:49 +0100)
committerGunnar Wrobel <wrobel@temple.(none)>
Wed, 10 Feb 2010 10:49:17 +0000 (11:49 +0100)
framework/Exception/test/Horde/Exception/AllTests.php [new file with mode: 0644]
framework/Exception/test/Horde/Exception/ExceptionTest.php [new file with mode: 0644]
framework/Exception/test/Horde/Exception/Stub/PearError.php [new file with mode: 0644]
framework/Exception/test/Horde/Exception/phpunit.xml [new file with mode: 0644]

diff --git a/framework/Exception/test/Horde/Exception/AllTests.php b/framework/Exception/test/Horde/Exception/AllTests.php
new file mode 100644 (file)
index 0000000..c297c35
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * All tests for the Horde_Exception:: package.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Exception
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 (file)
index 0000000..40dfae8
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+/**
+ * Test for the Horde_Exception:: class.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Exception
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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 (file)
index 0000000..f91f1d8
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Stub replacement for PEAR Errors
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Exception
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 (file)
index 0000000..502d3c9
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit>
+  <filter>
+    <whitelist>
+      <directory suffix=".php">../../../lib</directory>
+    </whitelist>
+  </filter>
+</phpunit>