diff --git a/.gitignore b/.gitignore index f6a98c3..24bad3a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.project /.cache /tests/report +*~ diff --git a/model/DbDriver.php b/model/DbDriver.php index 444d2af..b5f2efd 100644 --- a/model/DbDriver.php +++ b/model/DbDriver.php @@ -12,8 +12,6 @@ abstract class DbDriver { - protected $identifier_quote = '`'; - /** * Database connection * @@ -44,173 +42,20 @@ abstract class DbDriver } } } - + public function getConnection() { $this->connect(); return $this->connection; } - - public function beginTransaction() - { - $this->connect(); - $this->driverBeginTransaction(); - return $this; - } - - public function commit() - { - $this->connect(); - $this->driverCommitTransaction(); - return $this; - } - - public function rollback() - { - $this->connect(); - $this->driverRollbackTransaction(); - return $this; - } - - /** - * @param string $sql - * @param mixed $params - * @return DbStatement - */ - public function query($sql, $params = array()) - { - $this->connect(); - if (!is_array($params)) { - $params = array($params); - } - $stmt = $this->prepare($sql); - $stmt->execute($params); - return $stmt; - } - - /** - * @param string $table - * @param mixed $bind - * @param mixed $on_duplicate - * @return int Affected rows count - */ - public function insert($table, $bind, $on_duplicate = array()) - { - $columns = array(); - foreach ($bind as $col => $val) { - $columns[] = $this->quoteIdentifier($col); - } - $values = array_values($bind); - - $sql = 'INSERT INTO ' . $this->quoteIdentifier($table) - . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')'; - return $this->query($sql)->affectedRows(); - } - - /** - * @param string $table - * @param array $bind - * @param mixed $where - * @return int - */ - public function update($table, $bind, $where = '') - { - $set = array(); - foreach ($bind as $col => $val) { - $set[] = $this->quoteIdentifier($col) . '=' . $this->quote($val); - } - $where = $this->whereExpr($where); - $sql = 'UPDATE ' . $this->quoteIdentifier($table) . ' SET ' . implode(', ', $set) - . (($where) ? (' WHERE ' . $where) : ''); - return $this->query($sql)->affectedRows(); - } - - /** - * @param string $table - * @param mixed $where - * @return int - */ - public function delete($table, $where = '') - { - $where = $this->whereExpr($where); - $sql = 'DELETE FROM ' . $this->quoteIdentifier($table) . (($where) ? (' WHERE ' . $where) : ''); - return $this->query($sql)->affectedRows(); - } - - /** - * @param mixed $value - * @return string - */ - public function quote($value) - { - if ($value instanceof DbExpr) { - return (string) $value; - } - if (is_array($value)) { - foreach ($value as &$val) { - $val = $this->quote($val); - } - return implode(', ', $value); - } - return $this->driverQuote($value); - } - - /** - * @param string $ident - * @return string - */ - public function quoteIdentifier($ident) - { - $ident = explode('.', $ident); - if (!is_array($ident)) { - $ident = array($ident); - } - foreach ($ident as &$segment) { - $segment = $this->identifier_quote . $segment . $this->identifier_quote; - } - return implode('.', $ident); - } - - public function quoteInto($text, $value) - { - $pos = mb_strpos($text, '?'); - if ($pos === false) { - return $text; - } - return mb_substr($text, 0, $pos) . $this->quote($value) . mb_substr($text, $pos + 1); - } - - /** - * @param mixed $where - * @return string - */ - protected function whereExpr($where) - { - if (empty($where)) { - return $where; - } - - if (!is_array($where)) { - $where = array($where); - } - foreach ($where as $cond => &$term) { - if (is_int($cond)) { - if ($term instanceof DbExpr) { - $term = (string) $term; - } - } else { - $term = $this->quoteInto($cond, $term); - } - } - return implode(' AND ', $where); - } - + /* Abstract methods */ + + abstract public function insert($table, $data); + + abstract public function update($table, $data, $condition); - /** - * @return DbStatement - */ - abstract public function prepare($sql); + abstract public function delete($table, $condition); abstract public function getInsertId($table = null, $key = null); @@ -219,12 +64,4 @@ abstract class DbDriver abstract public function disconnect(); abstract protected function connect(); - - abstract protected function driverQuote($value); - - abstract protected function driverBeginTransaction(); - - abstract protected function driverCommitTransaction(); - - abstract protected function driverRollbackTransaction(); } \ No newline at end of file diff --git a/model/Model.php b/model/Model.php index 53278c5..10e97f8 100644 --- a/model/Model.php +++ b/model/Model.php @@ -62,41 +62,13 @@ abstract class Model } /** - * @param string $ident - * @return string Quoted identifier. - */ - public function identify($ident) - { - return $this->db->quoteIdentifier($ident); - } - - /** - * @param mixed $value - * @return string Quoted value. - */ - public function quote($value) - { - return $this->db->quote($value); - } - - /** - * @param int $id - * @return object - */ - public function get($id) - { - $sql = 'SELECT * FROM :table WHERE :pk=?'; - return $this->fetch($sql, $id); - } - - /** * @param array $data * @param array $on_duplicate * @return int Id of inserted row */ - public function insert($data, $on_duplicate = array()) + public function insert($data) { - $affected = $this->db->insert($this->table(), $data, $on_duplicate); + $affected = $this->db->insert($this->table(), $data); return ($this->getInsertId()) ? $this->getInsertId() : $affected; } @@ -107,21 +79,8 @@ abstract class Model */ public function update($data, $where) { - if (is_int($where) || $where === (string) (int) $where) { - $where = $this->identify($this->key) . '=' . (int) $where; - } return $this->db->update($this->table(), $data, $where); } - - /** - * @param int $id Int id - * @return int Number of affected rows - */ - public function delete($id) - { - $where = $this->identify($this->key) . '=' . (int) $id; - return $this->db->delete($this->table(), $where); - } /** * @return string @@ -133,112 +92,7 @@ abstract class Model } return $this->table; } - - /** - * Creates order sql string - * - * @param array $params - * @param array $sortable - * @return string - */ - protected function order($params, $sortable = array('id')) - { - $sql = ''; - if (isset($params['sort'])) { - $order = (isset($params['order']) && $params['order'] == 'desc') ? 'DESC' : 'ASC'; - if (in_array($params['sort'], $sortable)) { - $sql = ' ORDER BY ' . $this->identify($params['sort']) . ' ' . $order; - } - } - return $sql; - } - - /** - * Searches using like - * - * @param array $params - * @param array $searchable - * @param string $table_prefix - * @return string - */ - protected function search($params, $searchable = array('id'), $table_prefix = '') - { - $sql = ''; - if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) { - if ($table_prefix) { - $sql = $table_prefix . '.'; - } - $sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%'); - } - return $sql; - } - /** - * This method appends to params table and primary key. - * So they can be accessed thru `:table` and `:pk` placeholders. - * - * @return DbStatement - */ - protected function query($sql, $params = array()) - { - if (!is_array($params)) { - $params = array($params); - } - $params = array( - 'table' => new DbExpr($this->identify($this->table())), - 'pk' => new DbExpr($this->identify($this->key)), - ) + $params; - return $this->db->query($sql, $params); - } - - /** - * @param string $sql - * @param array $params - * @param string $field - * @param CacheKey $cache_key - */ - protected function fetchField($sql, $params = array(), $field, $cache_key = null) - { - if (!$cache_key || !$result = $cache_key->get()) { - $result = $this->query($sql, $params)->fetchField($field); - if ($cache_key) { - $cache_key->set($result); - } - } - return $result; - } - - /** - * @param string $sql - * @param array $params - * @param CacheKey $cache_key - */ - protected function fetch($sql, $params = array(), $cache_key = null) - { - if (!$cache_key || !$result = $cache_key->get()) { - $result = $this->query($sql, $params)->fetch(); - if ($cache_key) { - $cache_key->set($result); - } - } - return $result; - } - - /** - * @param string $sql - * @param array $params - * @param CacheKey $cache_key - */ - protected function fetchAll($sql, $params = array(), $cache_key = null) - { - if (!$cache_key || !$result = $cache_key->get()) { - $result = $this->query($sql, $params)->fetchAll(); - if ($cache_key) { - $cache_key->set($result); - } - } - return $result; - } /* Cache workaround */ @@ -280,4 +134,51 @@ abstract class Model } $this->caches_clean = array(); } + + /** + * Abstract methods + */ + + /** + * @param int $id + * @return object + */ + abstract public function get($id); + + /** + * @param int $id Int id + * @return int Number of affected rows + */ + abstract public function delete($id); + + /** + * Creates order sql string + * + * @param array $params + * @param array $sortable + * @return string + */ + abstract protected function order($params, $sortable = array('id')); + + /** + * @param string $sql + * @param array $params + * @param string $field + * @param CacheKey $cache_key + */ + abstract protected function fetchField($data, $params = array(), $field, $cache_key = null); + + /** + * @param string $sql + * @param array $params + * @param CacheKey $cache_key + */ + abstract protected function fetch($data, $params = array(), $cache_key = null); + + /** + * @param string $sql + * @param array $params + * @param CacheKey $cache_key + */ + abstract protected function fetchAll($data, $params = array(), $cache_key = null); } \ No newline at end of file diff --git a/model/MongoDriver.php b/model/MongoDriver.php new file mode 100644 index 0000000..c1d48e0 --- /dev/null +++ b/model/MongoDriver.php @@ -0,0 +1,77 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage db + * @since 2011-11-10 + */ + +/** + * @property Mongo $connection + */ +class MongoDriver extends NoSqlDbDriver +{ + + public function find($data, $params = array(), $cache_key = null) + { + + } + + public function insert($table, $bind, $on_duplicate = array()) + { + return parent::insert($table, $bind, $on_duplicate); + } + + public function update($table, $bind, $where = '') + { + return parent::update($table, $bind, $where); + } + + public function delete($table, $where = '') + { + return parent::delete($table, $where); + } + + + public function getInsertId($table = null, $key = null) + { + // TODO: Implement getInsertId() method. + } + + public function isConnected() + { + if (!is_null($this->connection)) { + return $this->connection->connected; + } else { + return false; + } + } + + public function disconnect() + { + if ($this->isConnected()) { + $this->connection->close(); + } + $this->connection = null; + } + + protected function connect() + { + if ($this->connection) { + return; + } + + $host = $this->config['hostname']; + $port = isset($this->config['port']) ? ':' . (string) $this->config['port'] : ''; + + $this->config = array( + 'username' => $this->config['username'], + 'password' => $this->config['password'], + 'db' => $this->config['database'] + ); + + $this->connection = new Mongo('mongodb://' . $host . $port, $this->config); + } +} + diff --git a/model/MySQLiDriver.php b/model/MySQLiDriver.php index 4afa22c..2cae3f1 100644 --- a/model/MySQLiDriver.php +++ b/model/MySQLiDriver.php @@ -12,7 +12,7 @@ /** * @property MySQLi $connection */ -class MySQLiDriver extends DbDriver +class MySQLiDriver extends SqlDbDriver { public function insert($table, $bind, $on_duplicate = array()) diff --git a/model/NoSqlDbDriver.php b/model/NoSqlDbDriver.php new file mode 100644 index 0000000..66a1f8f --- /dev/null +++ b/model/NoSqlDbDriver.php @@ -0,0 +1,13 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage db + * @since 2011-11-11 + */ + +abstract class NoSqlDbDriver extends DbDriver +{ + abstract function find($data, $params = array(), $cache_key = null); +} \ No newline at end of file diff --git a/model/SqlDbDriver.php b/model/SqlDbDriver.php new file mode 100644 index 0000000..8d8b9af --- /dev/null +++ b/model/SqlDbDriver.php @@ -0,0 +1,185 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage db + * @since 2011-11-11 + */ + +abstract class SqlDbDriver extends DbDriver +{ + + protected $identifier_quote = '`'; + + + + public function beginTransaction() + { + $this->connect(); + $this->driverBeginTransaction(); + return $this; + } + + public function commit() + { + $this->connect(); + $this->driverCommitTransaction(); + return $this; + } + + public function rollback() + { + $this->connect(); + $this->driverRollbackTransaction(); + return $this; + } + + /** + * @param string $sql + * @param mixed $params + * @return DbStatement + */ + public function query($sql, $params = array()) + { + $this->connect(); + if (!is_array($params)) { + $params = array($params); + } + $stmt = $this->prepare($sql); + $stmt->execute($params); + return $stmt; + } + + /** + * @param string $table + * @param mixed $bind + * @param mixed $on_duplicate + * @return int Affected rows count + */ + public function insert($table, $data) + { + $columns = array(); + foreach ($data as $col => $val) { + $columns[] = $this->quoteIdentifier($col); + } + $values = array_values($data); + + $sql = 'INSERT INTO ' . $this->quoteIdentifier($table) + . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')'; + return $this->query($sql)->affectedRows(); + } + + /** + * @param string $table + * @param array $bind + * @param mixed $where + * @return int + */ + public function update($table, $data, $condition = '') + { + $set = array(); + foreach ($data as $col => $val) { + $set[] = $this->quoteIdentifier($col) . '=' . $this->quote($val); + } + $where = $this->whereExpr($condition); + $sql = 'UPDATE ' . $this->quoteIdentifier($table) . ' SET ' . implode(', ', $set) + . (($where) ? (' WHERE ' . $where) : ''); + return $this->query($sql)->affectedRows(); + } + + /** + * @param string $table + * @param mixed $where + * @return int + */ + public function delete($table, $condition = '') + { + $where = $this->whereExpr($condition); + $sql = 'DELETE FROM ' . $this->quoteIdentifier($table) . (($where) ? (' WHERE ' . $where) : ''); + return $this->query($sql)->affectedRows(); + } + + /** + * @param mixed $value + * @return string + */ + public function quote($value) + { + if ($value instanceof DbExpr) { + return (string) $value; + } + if (is_array($value)) { + foreach ($value as &$val) { + $val = $this->quote($val); + } + return implode(', ', $value); + } + return $this->driverQuote($value); + } + + /** + * @param string $ident + * @return string + */ + public function quoteIdentifier($ident) + { + $ident = explode('.', $ident); + if (!is_array($ident)) { + $ident = array($ident); + } + foreach ($ident as &$segment) { + $segment = $this->identifier_quote . $segment . $this->identifier_quote; + } + return implode('.', $ident); + } + + public function quoteInto($text, $value) + { + $pos = mb_strpos($text, '?'); + if ($pos === false) { + return $text; + } + return mb_substr($text, 0, $pos) . $this->quote($value) . mb_substr($text, $pos + 1); + } + + /** + * @param mixed $where + * @return string + */ + protected function whereExpr($where) + { + if (empty($where)) { + return $where; + } + + if (!is_array($where)) { + $where = array($where); + } + foreach ($where as $cond => &$term) { + if (is_int($cond)) { + if ($term instanceof DbExpr) { + $term = (string) $term; + } + } else { + $term = $this->quoteInto($cond, $term); + } + } + return implode(' AND ', $where); + } + + /** + * @return DbStatement + */ + abstract public function prepare($sql); + + abstract protected function driverQuote($value); + + abstract protected function driverBeginTransaction(); + + abstract protected function driverCommitTransaction(); + + abstract protected function driverRollbackTransaction(); + +} + \ No newline at end of file diff --git a/model/SqlModel.php b/model/SqlModel.php new file mode 100644 index 0000000..7724232 --- /dev/null +++ b/model/SqlModel.php @@ -0,0 +1,173 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Model + * @since 2011-11-11 + */ + +abstract class SqlModel extends Model +{ + + /** + * @param string $ident + * @return string Quoted identifier. + */ + public function identify($ident) + { + return $this->db->quoteIdentifier($ident); + } + + /** + * @param mixed $value + * @return string Quoted value. + */ + public function quote($value) + { + return $this->db->quote($value); + } + + /** + * @param int $id + * @return object + */ + public function get($id) + { + $sql = 'SELECT * FROM :table WHERE :pk=?'; + return $this->fetch($sql, $id); + } + + /** + * @param array $data + * @param mixed $where + * @return int Number of affected rows + */ + public function update($data, $where) + { + if (is_int($where) || $where === (string) (int) $where) { + $where = $this->identify($this->key) . '=' . (int) $where; + } + return parent::update($data, $where); + } + + /** + * @param int $id Int id + * @return int Number of affected rows + */ + public function delete($id) + { + $where = $this->identify($this->key) . '=' . (int) $id; + return $this->db->delete($this->table(), $where); + } + + /** + * Creates order sql string + * + * @param array $params + * @param array $sortable + * @return string + */ + protected function order($params, $sortable = array('id')) + { + $sql = ''; + if (isset($params['sort'])) { + $order = (isset($params['order']) && $params['order'] == 'desc') ? 'DESC' : 'ASC'; + if (in_array($params['sort'], $sortable)) { + $sql = ' ORDER BY ' . $this->identify($params['sort']) . ' ' . $order; + } + } + return $sql; + } + + /** + * Searches using like + * + * @param array $params + * @param array $searchable + * @param string $table_prefix + * @return string + */ + protected function search($params, $searchable = array('id'), $table_prefix = '') + { + $sql = ''; + if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) { + if ($table_prefix) { + $sql = $table_prefix . '.'; + } + $sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%'); + } + return $sql; + } + + /** + * This method appends to params table and primary key. + * So they can be accessed thru `:table` and `:pk` placeholders. + * + * @return DbStatement + */ + protected function query($sql, $params = array()) + { + if (!is_array($params)) { + $params = array($params); + } + $params = array( + 'table' => new DbExpr($this->identify($this->table())), + 'pk' => new DbExpr($this->identify($this->key)), + ) + $params; + return $this->db->query($sql, $params); + } + + /** + * @param string $sql + * @param array $params + * @param string $field + * @param CacheKey $cache_key + */ + protected function fetchField($data, $params = array(), $field, $cache_key = null) + { + if (!$cache_key || !$result = $cache_key->get()) { + $result = $this->query($data, $params)->fetchField($field); + if ($cache_key) { + $cache_key->set($result); + } + } + return $result; + } + + /** + * @param string $sql + * @param array $params + * @param CacheKey $cache_key + */ + protected function fetch($data, $params = array(), $cache_key = null) + { + if (!$cache_key || !$result = $cache_key->get()) { + $result = $this->query($data, $params)->fetch(); + if ($cache_key) { + $cache_key->set($result); + } + } + return $result; + } + + /** + * @param string $sql + * @param array $params + * @param CacheKey $cache_key + */ + protected function fetchAll($data, $params = array(), $cache_key = null) + { + if (!$cache_key || !$result = $cache_key->get()) { + $result = $this->query($data, $params)->fetchAll(); + if ($cache_key) { + $cache_key->set($result); + } + } + return $result; + } + +} \ No newline at end of file diff --git a/model_class_diadram.png b/model_class_diadram.png new file mode 100644 index 0000000..1c74e42 Binary files /dev/null and b/model_class_diadram.png differ diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php new file mode 100644 index 0000000..bcd0159 --- /dev/null +++ b/tests/model/MongoDriverTest.php @@ -0,0 +1,65 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-11-10 + * + * Unit tests for MongoDriver class + */ + +require_once dirname(__FILE__) . '/../../model/DbDriver.php'; +require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php'; +require_once dirname(__FILE__) . '/../../model/MongoDriver.php'; + +class MongoDriverTest extends PHPUnit_Framework_TestCase +{ + + private $conf = array(); + + public function setUp() + { + $this->conf = array( + 'hostname' => 'localhost', + 'database' => 'test', + 'username' => 'test', + 'password' => '1234', + 'port' => 27017 + ); + } + /** + * @expectedException Exception + * @expectedExceptionMessage Configuration must have a "hostname". + */ + public function testGetConnectionNoHostname() + { + unset($this->conf['hostname']); + $mongo = new MongoDriver($this->conf); + $mongo->getConnection(); + } + + /** + * @expectedException MongoConnectionException + * @expectedExceptionMessage Couldn't authenticate with database + */ + public function testGetConnectionWrongPassword() + { + $this->conf['password'] = 'nopass'; + $mongo = new MongoDriver($this->conf); + $this->assertInstanceOf('Mongo', $mongo->getConnection()); + } + + public function testGetConnection() + { + $mongo = new MongoDriver($this->conf); + + $this->assertFalse($mongo->isConnected()); + $this->assertInstanceOf('Mongo', $mongo->getConnection()); + $this->assertTrue($mongo->isConnected()); + $mongo->disconnect(); + $this->assertFalse($mongo->isConnected()); + } + +} \ No newline at end of file diff --git a/tests/model/MySQLiDriverTest.php b/tests/model/MySQLiDriverTest.php index 156cdad..a9a5747 100644 --- a/tests/model/MySQLiDriverTest.php +++ b/tests/model/MySQLiDriverTest.php @@ -14,6 +14,7 @@ require_once dirname(__FILE__) . '/../../model/Db.php'; require_once dirname(__FILE__) . '/../../model/DbStatement.php'; require_once dirname(__FILE__) . '/../../model/MySQLiStatement.php'; require_once dirname(__FILE__) . '/../../model/DbDriver.php'; +require_once dirname(__FILE__) . '/../../model/SqlDbDriver.php'; require_once dirname(__FILE__) . '/../../model/MySQLiDriver.php'; class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase diff --git a/tests/model/DbDriverTest.php b/tests/model/SqlDbDriverTest.php similarity index 93% rename from tests/model/DbDriverTest.php rename to tests/model/SqlDbDriverTest.php index 891b0a0..8fc8e89 100644 --- a/tests/model/DbDriverTest.php +++ b/tests/model/SqlDbDriverTest.php @@ -13,8 +13,9 @@ require_once dirname(__FILE__) . '/../../model/DbExpr.php'; require_once dirname(__FILE__) . '/../../model/Db.php'; require_once dirname(__FILE__) . '/../../model/DbDriver.php'; +require_once dirname(__FILE__) . '/../../model/SqlDbDriver.php'; -class DbDriverTest extends PHPUnit_Framework_TestCase +class SqlDbDriverTest extends PHPUnit_Framework_TestCase { private $driver; @@ -22,13 +23,13 @@ class DbDriverTest extends PHPUnit_Framework_TestCase public function setUp() { $conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'); - $this->driver = $this->getMockForAbstractClass('DbDriver', array($conf)); + $this->driver = $this->getMockForAbstractClass('SqlDbDriver', array($conf)); } public function testConstruct() { $conf = array('hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'); - $this->driver = $this->getMockForAbstractClass('DbDriver', array($conf)); + $this->driver = $this->getMockForAbstractClass('SqlDbDriver', array($conf)); } /** @@ -38,7 +39,7 @@ class DbDriverTest extends PHPUnit_Framework_TestCase public function testConstructWrongConfig() { $conf = array('hostname' => 'localhost', 'database' => 'db'); - $this->getMockForAbstractClass('DbDriver', array($conf)); + $this->getMockForAbstractClass('SqlDbDriver', array($conf)); } public function testGetConnection() diff --git a/tests/model/ModelTest.php b/tests/model/SqlModelTest.php similarity index 92% rename from tests/model/ModelTest.php rename to tests/model/SqlModelTest.php index e089eca..18de4ce 100644 --- a/tests/model/ModelTest.php +++ b/tests/model/SqlModelTest.php @@ -17,8 +17,9 @@ require_once dirname(__FILE__) . '/../../model/DbExpr.php'; require_once dirname(__FILE__) . '/../../model/Db.php'; require_once dirname(__FILE__) . '/../../model/DbDriver.php'; require_once dirname(__FILE__) . '/../../model/Model.php'; +require_once dirname(__FILE__) . '/../../model/SqlModel.php'; -class ModelTest extends PHPUnit_Framework_TestCase +class SqlModelTest extends PHPUnit_Framework_TestCase { private $model; @@ -32,7 +33,7 @@ class ModelTest extends PHPUnit_Framework_TestCase Config::set('Db', $conf); if (!class_exists('MockModel')) { - $this->model = $this->getMockForAbstractClass('Model', array(), 'MockModel'); + $this->model = $this->getMockForAbstractClass('SqlModel', array(), 'MockModel'); } else { $this->model = new MockModel(); } @@ -41,7 +42,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testModel() { - $this->assertInstanceOf('Model', $this->model); + $this->assertInstanceOf('SqlModel', $this->model); } public function testGetInsertId() @@ -83,7 +84,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testOrder() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('order'); $method->setAccessible(true); $this->assertEquals(' ORDER BY id DESC', $method->invoke($this->model, array('sort' => 'id', 'order' => 'desc'))); @@ -98,7 +99,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testSearch() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('search'); $method->setAccessible(true); $this->assertEmpty($method->invoke($this->model, array())); @@ -108,7 +109,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testFetch() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('fetch'); $method->setAccessible(true); @@ -118,7 +119,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testFetchField() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('fetchField'); $method->setAccessible(true); @@ -128,7 +129,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testFetchAll() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('fetchAll'); $method->setAccessible(true); @@ -167,7 +168,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testAddCleanCache() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('addCleanCache'); $method->setAccessible(true); @@ -180,7 +181,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCleanCaches() { - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('addCleanCache'); $method->setAccessible(true); @@ -190,7 +191,7 @@ class ModelTest extends PHPUnit_Framework_TestCase $method->invoke($this->model, $key); $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model); - $model = new ReflectionClass('Model'); + $model = new ReflectionClass('SqlModel'); $method = $model->getMethod('cleanCaches'); $method->setAccessible(true); diff --git a/uml_model.zargo b/uml_model.zargo new file mode 100644 index 0000000..a775ed3 Binary files /dev/null and b/uml_model.zargo differ