diff --git a/base.php b/base.php index 09d312f..55e23fc 100644 --- a/base.php +++ b/base.php @@ -940,8 +940,10 @@ abstract class Base } private static function error () { - header("location: checkconfig.php?err=1"); - exit(); + if (php_sapi_name() != "cli") { + header("location: checkconfig.php?err=1"); + } + throw new Exception('Database not found.'); } public static function getDb ($database = NULL) { @@ -968,6 +970,7 @@ abstract class Base } else { self::getDb (); } + return true; } public static function clearDb () { diff --git a/test/baseTest.php b/test/baseTest.php index c678f5d..662107c 100644 --- a/test/baseTest.php +++ b/test/baseTest.php @@ -57,4 +57,45 @@ class BaseTest extends PHPUnit_Framework_TestCase $this->assertEquals ("One book", Base::getDbName (1)); $this->assertEquals ($config['calibre_directory'], Base::getDbList ()); } + + public function testCheckDatabaseAvailability_1 () { + global $config; + + $this->assertTrue (Base::checkDatabaseAvailability ()); + } + + public function testCheckDatabaseAvailability_2 () { + global $config; + + $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/", + "One book" => dirname(__FILE__) . "/BaseWithOneBook/"); + + $this->assertTrue (Base::checkDatabaseAvailability ()); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage not found + */ + public function testCheckDatabaseAvailability_Exception1 () { + global $config; + + $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/", + "One book" => dirname(__FILE__) . "/OneBook/"); + + $this->assertTrue (Base::checkDatabaseAvailability ()); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage not found + */ + public function testCheckDatabaseAvailability_Exception2 () { + global $config; + + $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/SomeBooks/", + "One book" => dirname(__FILE__) . "/BaseWithOneBook/"); + + $this->assertTrue (Base::checkDatabaseAvailability ()); + } } \ No newline at end of file