From be838743921f07c195ae97bed5a12943ca0343b0 Mon Sep 17 00:00:00 2001 From: Vyacheslav Agafonov Date: Wed, 30 Nov 2011 13:26:36 +0400 Subject: [PATCH] added setExpectedException and check type Config --- model/Db.php | 7 +++++-- model/Model.php | 12 ++++++++++++ tests/model/DbTest.php | 12 +++++++----- tests/model/ModelTest.php | 6 ++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/model/Db.php b/model/Db.php index 95e738c..62155ed 100644 --- a/model/Db.php +++ b/model/Db.php @@ -36,11 +36,14 @@ class Db { if (!isset(self::$connections[$name])) { if (!$config) { + if (gettype(Config::get(__CLASS__)) != 'object') { + throw new InitializationException('config no object'); + } $config = Config::get(__CLASS__)->$name; } if (!is_array($config)) { - throw new GeneralException('Connection parameters must be an array'); + throw new InitializationException('Connection parameters must be an array'); } $driver = 'MySQLiDriver'; @@ -52,7 +55,7 @@ class Db $connection = new $driver($config); if (!$connection instanceof DbDriver) { - throw new GeneralException('Database driver must extends DbDriver'); + throw new InitializationException('Database driver must extends DbDriver'); } self::$connections[$name] = $connection; } diff --git a/model/Model.php b/model/Model.php index 53278c5..8ef1eff 100644 --- a/model/Model.php +++ b/model/Model.php @@ -221,6 +221,18 @@ abstract class Model $cache_key->set($result); } } + + + + // $debug = __CLASS__; + $debug = get_class($this->query($sql, $params)); + + + // $debug = get_class_methods($this->query($sql, $params)); + + //$result = print_r($debug, true); + + return $result; } diff --git a/tests/model/DbTest.php b/tests/model/DbTest.php index 4357379..31a9730 100644 --- a/tests/model/DbTest.php +++ b/tests/model/DbTest.php @@ -15,24 +15,25 @@ require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../model/DbDriver.php'; require_once dirname(__FILE__) . '/../../model/Db.php'; require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; +require_once dirname(__FILE__) . '/../../exception/InitializationException.php'; class DbTest extends PHPUnit_Framework_TestCase { /** - * @expectedException GeneralException * @expectedExceptionMessage Trying to get property of non-object */ public function testConnectNoParams() { - Db::connect(); + $this->setExpectedException('InitializationException'); + Db::connect(); } /** - * @expectedException GeneralException * @expectedExceptionMessage Connection parameters must be an array */ public function testConnectConfigNotArray() { + $this->setExpectedException('InitializationException'); Db::connect('name', 'config'); } @@ -41,7 +42,8 @@ class DbTest extends PHPUnit_Framework_TestCase $conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverGlobalConfMock', 'database' => 'db', 'username' => 'test', 'password' => '1234'); $this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverGlobalConfMock', false); Config::set('Db', array('global' =>$conf)); - Db::connect('global'); + $driver = Db::connect('global'); + $this->assertInstanceOf('DbDriver', $driver); } public function testConnectWithConfigParam() @@ -52,12 +54,12 @@ class DbTest extends PHPUnit_Framework_TestCase $this->assertInstanceOf('DbDriver', $driver); } /** - * @expectedException GeneralException * @expectedExceptionMessage Database driver must extends DbDriver */ public function testConnectWithWrongDriver() { $this->getMock('NotDbDriver', array(), array(), 'NoDbDriverMock'); + $this->setExpectedException('InitializationException'); $driver = Db::connect('nodb', array('hostname' => 'localhost', 'driver' => 'NoDbDriverMock')); } diff --git a/tests/model/ModelTest.php b/tests/model/ModelTest.php index e089eca..cac61bd 100644 --- a/tests/model/ModelTest.php +++ b/tests/model/ModelTest.php @@ -113,6 +113,12 @@ class ModelTest extends PHPUnit_Framework_TestCase $method->setAccessible(true); $key = $this->getCacheKeyMockGetSet(); + + + //$debug = print_r($this->model, true); + //throw new Exception($debug); + + $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key)); }