diff --git a/tests/model/DbTest.php b/tests/model/DbTest.php index fa61e24..f1e9847 100644 --- a/tests/model/DbTest.php +++ b/tests/model/DbTest.php @@ -20,6 +20,7 @@ class DbTest extends PHPUnit_Framework_TestCase /** * @expectedException Exception + * @expectedExceptionMessage Trying to get property of non-object */ public function testConnectNoParams() { diff --git a/tests/model/ModelTest.php b/tests/model/ModelTest.php new file mode 100644 index 0000000..d9a3e62 --- /dev/null +++ b/tests/model/ModelTest.php @@ -0,0 +1,307 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-11-7 + * + * Unit tests for Model class + */ + +require_once dirname(__FILE__) . '/../../Registry.php'; +require_once dirname(__FILE__) . '/../../Config.php'; +require_once dirname(__FILE__) . '/../../cache/Cacher.php'; +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'; + +class ModelTest extends PHPUnit_Framework_TestCase +{ + + private $model; + + public function setUp() + { + $conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234')); + if (!class_exists('MockDbDriver')) { + $this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false); + } + + Config::set('Db', $conf); + if (!class_exists('MockModel')) { + $this->model = $this->getMockForAbstractClass('Model', array(), 'MockModel'); + } else { + $this->model = new MockModel(); + } + set_new_overload(array($this, 'newCallback')); + } + + public function testModel() + { + $this->assertInstanceOf('Model', $this->model); + } + + public function testGetInsertId() + { + $this->assertTrue($this->model->getInsertId()); + } + + public function testIdentify() + { + $param = 'param'; + $this->assertEquals($param, $this->model->identify($param)); + } + + public function testQuote() + { + $param = 'param'; + $this->assertEquals($param, $this->model->quote($param)); + } + + public function testGet() + { + $this->assertTrue($this->model->get(1)); + } + + public function testInsert() + { + $this->assertTrue($this->model->insert(array('data'))); + } + + public function testUpdate() + { + $this->assertEquals('mock', $this->model->update(array('var' => 'val'), 1)); + } + + public function testDelete() + { + $this->assertEquals('mock', $this->model->delete(1)); + } + + public function testOrder() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('order'); + $method->setAccessible(true); + $this->assertEquals(' ORDER BY id DESC', $method->invoke($this->model, array('sort' => 'id', 'order' => 'desc'))); + $this->assertEquals(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name'), array('id', 'name'))); + $this->assertEmpty($method->invoke($this->model, array())); + + /** + * @TODO: Model::order - check DESC condition - make case insensitive + */ + $this->assertEquals(' ORDER BY name ASC', $method->invoke($this->model, array('sort' => 'name', 'order' => 'DESC'), array('id', 'name'))); + } + + public function testSearch() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('search'); + $method->setAccessible(true); + $this->assertEmpty($method->invoke($this->model, array())); + $this->assertEmpty($method->invoke($this->model, array('q' => 'in', 'qt' => 'name'))); + $this->assertEquals('test.name LIKE %on%', $method->invoke($this->model, array('qt' => 'name', 'q' => 'on'), array('name'), 'test')); + } + + public function testFetch() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('fetch'); + $method->setAccessible(true); + + $key = $this->getCacheKeyMockGetSet(); + $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key)); + } + + public function testFetchField() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('fetchField'); + $method->setAccessible(true); + + $key = $this->getCacheKeyMockGetSet(); + $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), 'field', $key)); + } + + public function testFetchAll() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('fetchAll'); + $method->setAccessible(true); + + $key = $this->getCacheKeyMockGetSet(); + $this->assertEquals('field', $method->invoke($this->model, 'SELECT', array(), $key)); + } + + public function testGetCache() + { + $model = new ReflectionClass('MockModel'); + $method = $model->getMethod('getCache'); + $method->setAccessible(true); + + Config::set('Model', 'MockCache'); + if (!class_exists('MockCache')) { + $this->getMock('Cache', array(), array(), 'MockCache'); + } + $this->assertInstanceOf('MockCache', $method->invoke($this->model)); + } + + public function testCacheKey() + { + $model = new ReflectionClass('MockModel'); + $method = $model->getMethod('cacheKey'); + $method->setAccessible(true); + + Config::set('Model', 'MockCache'); + if (!class_exists('MockCache')) { + $this->getMock('Cache', array(), array(), 'MockCache', false); + } + if (!class_exists('MockCacheKey')) { + $this->getMock('CacheKey', array(), array(), 'MockCacheKey', false); + } + $this->assertInstanceOf('MockCacheKey', $method->invoke($this->model, 'name')); + } + + public function testAddCleanCache() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('addCleanCache'); + $method->setAccessible(true); + + $key = $this->getMock('Key', array('set', 'get')); + $method->invoke($this->model, $key); + $method->invoke($this->model, $key); + $method->invoke($this->model, $key); + $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model); + } + + public function testCleanCaches() + { + $model = new ReflectionClass('Model'); + $method = $model->getMethod('addCleanCache'); + $method->setAccessible(true); + + $key = $this->getCacheKeyMockDel(3); + $method->invoke($this->model, $key); + $method->invoke($this->model, $key); + $method->invoke($this->model, $key); + $this->assertAttributeEquals(array($key, $key, $key), 'caches_clean', $this->model); + + $model = new ReflectionClass('Model'); + $method = $model->getMethod('cleanCaches'); + $method->setAccessible(true); + + $method->invoke($this->model, $key); + $this->assertAttributeEquals(array(), 'caches_clean', $this->model); + } + + protected function getCacheKeyMockGetSet() + { + $key = $this->getMock('Key', array('set', 'get')); + $key + ->expects($this->any()) + ->method('set') + ->with($this->anything()) + ->will($this->returnValue(true)); + $key + ->expects($this->any()) + ->method('get') + ->will($this->returnValue(false)); + return $key; + } + + protected function getCacheKeyMockDel($count) + { + $key = $this->getMock('Key', array('del')); + $key + ->expects($this->exactly($count)) + ->method('del') + ->will($this->returnValue(true)); + return $key; + } + + public function tearDown() + { + Config::getInstance()->offsetUnset('Db'); + $config = new ReflectionClass('Db'); + $registry = $config->getProperty('connections'); + $registry->setAccessible(true); + $registry->setValue('Db', array()); + unset_new_overload(); + } + + + protected function newCallback($className) + { + switch ($className) { + case 'MockDbDriver': + return 'MockDbDriver'; + case 'CacheKey': + return 'MockCacheKey'; + default: + return $className; + } + } +} + +abstract class MyDbDriver extends DbDriver +{ + public function getInsertId($table = null, $key = null) + { + return true; + } + + public function quoteIdentifier($param) + { + return $param; + } + + public function quote($param) + { + return $param; + } + + public function insert($table, $bind, $on_duplicate = array()) + { + return $table; + } + + public function update($table, $bind, $where = '') + { + return $table; + } + + public function delete($table, $where = '') + { + return $table; + } + + public function query($sql, $params) + { + $conf = array('driver' => 'MockDbDriver', 'hostname' => 'localhost', 'database' => 'db', 'username' => 'test', 'password' => '1234'); + return new MockDbDriver($conf); + } + + public function execute() + { + return true; + } + + public function fetch() + { + return true; + } + + public function fetchField($field) + { + return $field; + } + + public function fetchAll() + { + return true; + } +} \ No newline at end of file diff --git a/tests/model/MySQLiDriverTest.php b/tests/model/MySQLiDriverTest.php new file mode 100644 index 0000000..878ded8 --- /dev/null +++ b/tests/model/MySQLiDriverTest.php @@ -0,0 +1,207 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-11-7 + * + * Unit tests for MySQLiDriver class + */ + +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/MySQLiDriver.php'; + +class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase +{ + static private $pdo = null; + + private $conn = null; + + protected function getConnection() + { + if ($this->conn === null) { + if (self::$pdo == null) { + self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']); + } + $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']); + } + + return $this->conn; + } + + protected function getDataSet() + { + return $this->createFlatXMLDataSet(dirname(__FILE__) . '/testData.xml'); + } + + public function run(PHPUnit_Framework_TestResult $result = NULL) + { + $this->setPreserveGlobalState(false); + return parent::run($result); + } + + public function testDriver() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + + $queryTable = $this->getConnection()->createQueryTable( + 'table', 'SELECT * FROM `table`' + ); + $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml') + ->getTable("table"); + $this->assertTablesEqual($expectedTable, $queryTable); + + $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'))); + $this->assertEquals(3, $this->getConnection()->getRowCount('table')); + } + + public function testGetConnection() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + $this->assertInstanceOf('mysqli', $driver->getConnection()); + $this->assertTrue($driver->isConnected()); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Unknown database + */ + public function testGetConnectionWrongConfig() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'nodb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + $this->assertNull('mysqli', $driver->getConnection()); + } + + public function testDisconnect() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + + $driver->disconnect(); + $this->assertAttributeEquals(null, 'connection', $driver); + + $this->assertInstanceOf('mysqli', $driver->getConnection()); + $this->assertAttributeInstanceOf('mysqli', 'connection', $driver); + $driver->disconnect(); + $this->assertAttributeEquals(null, 'connection', $driver); + } + + public function testInsert() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'))); + $this->assertEquals(3, $driver->getInsertId()); + $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'))); + $this->assertEquals(4, $driver->getInsertId()); + $this->assertEquals(1, $driver->insert('table', array('id' => null, 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'))); + $this->assertEquals(5, $driver->getInsertId()); + $this->assertEquals(2, $driver->insert('table', array('id' => '5', 'user' => true, 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'), array('id' => 8))); + $this->assertEquals(8, $driver->getInsertId()); + } + + /** + * @TODO: DbDriver::getInsertId($table = null, $key = null) - params not used + */ + public function testGetInsertId() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + + $this->assertEquals(1, $driver->insert('table', array('id' => 3, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20'))); + $this->assertEquals(3, $driver->getInsertId()); + } + + public function testTransaction() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + + $queryTable = $this->getConnection()->createQueryTable( + 'table', 'SELECT * FROM `table`' + ); + $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml') + ->getTable("table"); + $this->assertTablesEqual($expectedTable, $queryTable); + + $driver->getConnection(); + $driver->beginTransaction(); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + + $queryTable = $this->getConnection()->createQueryTable( + 'table', 'SELECT * FROM `table`' + ); + $driver->commit(); + $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testDataAfterCommit.xml') + ->getTable("table"); + $this->assertTablesEqual($expectedTable, $queryTable); + } + + public function testRollback() + { + if (!defined('DEBUG')) { + define('DEBUG', false); + } + + $conf = array('hostname' => 'localhost', 'database' => 'testdb', 'username' => 'root', 'password' => '1234'); + $driver = new MySQLiDriver($conf); + + $queryTable = $this->getConnection()->createQueryTable( + 'table', 'SELECT * FROM `table`' + ); + $expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__) . '/testData.xml') + ->getTable("table"); + $this->assertTablesEqual($expectedTable, $queryTable); + + $driver->getConnection(); + $driver->beginTransaction(); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->insert('table', array('id' => null, 'user' => 'tony', 'content' => 'some test content', 'created' => '2011-11-07 11:35:20')); + $driver->rollback(); + $queryTable = $this->getConnection()->createQueryTable( + 'table', 'SELECT * FROM `table`' + ); + $this->assertTablesEqual($expectedTable, $queryTable); + } +} diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index 1e87b52..1e695c6 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -154,6 +154,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase /** * @runInSeparateProcess * @expectedException Exception + * @TODO: exception just for code coverage - could not mock mysqli properties */ public function testNumRows() { diff --git a/tests/model/testData.xml b/tests/model/testData.xml new file mode 100644 index 0000000..bb2bb43 --- /dev/null +++ b/tests/model/testData.xml @@ -0,0 +1,5 @@ + + + +
+ \ No newline at end of file diff --git a/tests/model/testDataAfterCommit.xml b/tests/model/testDataAfterCommit.xml new file mode 100644 index 0000000..4e95b21 --- /dev/null +++ b/tests/model/testDataAfterCommit.xml @@ -0,0 +1,9 @@ + + +
+
+
+
+
+
+ \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 603da83..5cdf41b 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -23,4 +23,10 @@ . + + + + + +