Browse Source

added setExpectedException and check type Config

master
Vyacheslav Agafonov 13 years ago
parent
commit
be83874392
  1. 7
      model/Db.php
  2. 12
      model/Model.php
  3. 12
      tests/model/DbTest.php
  4. 6
      tests/model/ModelTest.php

7
model/Db.php

@ -36,11 +36,14 @@ class Db
{ {
if (!isset(self::$connections[$name])) { if (!isset(self::$connections[$name])) {
if (!$config) { if (!$config) {
if (gettype(Config::get(__CLASS__)) != 'object') {
throw new InitializationException('config no object');
}
$config = Config::get(__CLASS__)->$name; $config = Config::get(__CLASS__)->$name;
} }
if (!is_array($config)) { if (!is_array($config)) {
throw new GeneralException('Connection parameters must be an array');
throw new InitializationException('Connection parameters must be an array');
} }
$driver = 'MySQLiDriver'; $driver = 'MySQLiDriver';
@ -52,7 +55,7 @@ class Db
$connection = new $driver($config); $connection = new $driver($config);
if (!$connection instanceof DbDriver) { if (!$connection instanceof DbDriver) {
throw new GeneralException('Database driver must extends DbDriver');
throw new InitializationException('Database driver must extends DbDriver');
} }
self::$connections[$name] = $connection; self::$connections[$name] = $connection;
} }

12
model/Model.php

@ -221,6 +221,18 @@ abstract class Model
$cache_key->set($result); $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; return $result;
} }

12
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/DbDriver.php';
require_once dirname(__FILE__) . '/../../model/Db.php'; require_once dirname(__FILE__) . '/../../model/Db.php';
require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
class DbTest extends PHPUnit_Framework_TestCase class DbTest extends PHPUnit_Framework_TestCase
{ {
/** /**
* @expectedException GeneralException
* @expectedExceptionMessage Trying to get property of non-object * @expectedExceptionMessage Trying to get property of non-object
*/ */
public function testConnectNoParams() public function testConnectNoParams()
{ {
Db::connect();
$this->setExpectedException('InitializationException');
Db::connect();
} }
/** /**
* @expectedException GeneralException
* @expectedExceptionMessage Connection parameters must be an array * @expectedExceptionMessage Connection parameters must be an array
*/ */
public function testConnectConfigNotArray() public function testConnectConfigNotArray()
{ {
$this->setExpectedException('InitializationException');
Db::connect('name', 'config'); 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'); $conf = array('hostname' => 'localhost', 'driver' => 'MySQLiDriverGlobalConfMock', 'database' => 'db', 'username' => 'test', 'password' => '1234');
$this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverGlobalConfMock', false); $this->getMockForAbstractClass('DbDriver', array(), 'MySQLiDriverGlobalConfMock', false);
Config::set('Db', array('global' =>$conf)); Config::set('Db', array('global' =>$conf));
Db::connect('global');
$driver = Db::connect('global');
$this->assertInstanceOf('DbDriver', $driver);
} }
public function testConnectWithConfigParam() public function testConnectWithConfigParam()
@ -52,12 +54,12 @@ class DbTest extends PHPUnit_Framework_TestCase
$this->assertInstanceOf('DbDriver', $driver); $this->assertInstanceOf('DbDriver', $driver);
} }
/** /**
* @expectedException GeneralException
* @expectedExceptionMessage Database driver must extends DbDriver * @expectedExceptionMessage Database driver must extends DbDriver
*/ */
public function testConnectWithWrongDriver() public function testConnectWithWrongDriver()
{ {
$this->getMock('NotDbDriver', array(), array(), 'NoDbDriverMock'); $this->getMock('NotDbDriver', array(), array(), 'NoDbDriverMock');
$this->setExpectedException('InitializationException');
$driver = Db::connect('nodb', array('hostname' => 'localhost', 'driver' => 'NoDbDriverMock')); $driver = Db::connect('nodb', array('hostname' => 'localhost', 'driver' => 'NoDbDriverMock'));
} }

6
tests/model/ModelTest.php

@ -113,6 +113,12 @@ class ModelTest extends PHPUnit_Framework_TestCase
$method->setAccessible(true); $method->setAccessible(true);
$key = $this->getCacheKeyMockGetSet(); $key = $this->getCacheKeyMockGetSet();
//$debug = print_r($this->model, true);
//throw new Exception($debug);
$this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key)); $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key));
} }

Loading…
Cancel
Save