Anton Grebnev
13 years ago
7 changed files with 536 additions and 0 deletions
-
1tests/model/DbTest.php
-
307tests/model/ModelTest.php
-
207tests/model/MySQLiDriverTest.php
-
1tests/model/MySQLiStatementTest.php
-
5tests/model/testData.xml
-
9tests/model/testDataAfterCommit.xml
-
6tests/phpunit.xml
@ -0,0 +1,307 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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; |
|||
} |
|||
} |
@ -0,0 +1,207 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @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); |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
<?xml version="1.0" ?> |
|||
<dataset> |
|||
<table id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" /> |
|||
<table id="2" content="I like it!" user="nancy" created="2010-04-26 12:14:20" /> |
|||
</dataset> |
@ -0,0 +1,9 @@ |
|||
<?xml version="1.0" ?> |
|||
<dataset> |
|||
<table id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" /> |
|||
<table id="2" content="I like it!" user="nancy" created="2010-04-26 12:14:20" /> |
|||
<table id="3" content="some test content" user="tony" created="2011-11-07 11:35:20" /> |
|||
<table id="4" content="some test content" user="tony" created="2011-11-07 11:35:20" /> |
|||
<table id="5" content="some test content" user="tony" created="2011-11-07 11:35:20" /> |
|||
<table id="6" content="some test content" user="tony" created="2011-11-07 11:35:20" /> |
|||
</dataset> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue