Updating model test case

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4632 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-03-17 21:37:22 +00:00
parent 97396819cc
commit dba900eb30

View file

@ -55,14 +55,23 @@
)); ));
} }
} }
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model
*/
class TestSuiteModel extends Model {
var $useDbConfig = 'test_suite';
}
/** /**
* Short description for class. * Short description for class.
* *
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class User extends Model { class User extends TestSuiteModel {
var $name = 'User'; var $name = 'User';
var $validate = array( var $validate = array(
'user' => VALID_NOT_EMPTY, 'user' => VALID_NOT_EMPTY,
@ -75,7 +84,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Article extends Model { class Article extends TestSuiteModel {
var $name = 'Article'; var $name = 'Article';
var $belongsTo = array('User'); var $belongsTo = array('User');
var $hasMany = array( var $hasMany = array(
@ -94,7 +103,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Tag extends Model { class Tag extends TestSuiteModel {
var $name = 'Tag'; var $name = 'Tag';
} }
/** /**
@ -103,7 +112,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Comment extends Model { class Comment extends TestSuiteModel {
var $name = 'Comment'; var $name = 'Comment';
var $belongsTo = array('Article', 'User'); var $belongsTo = array('Article', 'User');
var $hasOne = array( var $hasOne = array(
@ -116,7 +125,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Attachment extends Model { class Attachment extends TestSuiteModel {
var $name = 'Attachment'; var $name = 'Attachment';
} }
/** /**
@ -125,7 +134,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Category extends Model { class Category extends TestSuiteModel {
var $name = 'Category'; var $name = 'Category';
} }
/** /**
@ -136,47 +145,61 @@
*/ */
class ModelTest extends UnitTestCase { class ModelTest extends UnitTestCase {
function setUp() { function setUp() {
require_once r('//', '/', APP) . 'config/database.php'; if (!isset($this->db) || !$this->db->isConnected()) {
restore_error_handler();
$config = new DATABASE_CONFIG(); @$db =& ConnectionManager::getDataSource('test');
$this->db =& new DboMysql($config->default); set_error_handler('simpleTestErrorHandler');
$this->db->fullDebug = false;
if (!$db->isConnected()) {
$db =& ConnectionManager::getDataSource('default');
}
$config = $db->config;
$config['prefix'] .= 'test_suite_';
ConnectionManager::create('test_suite', $config);
$this->db =& ConnectionManager::getDataSource('test_suite');
$this->db->fullDebug = false;
} else {
$config = $this->db->config;
}
$queries = array(); $queries = array();
$queries[] = 'INSERT INTO `users` VALUES(1, \'mariano\', MD5(\'password\'), \'2007-03-17 01:16:23\', \'2007-03-17 01:18:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'users` VALUES(1, \'mariano\', MD5(\'password\'), \'2007-03-17 01:16:23\', \'2007-03-17 01:18:31\')';
$queries[] = 'INSERT INTO `users` VALUES(2, \'nate\', MD5(\'password\'), \'2007-03-17 01:18:23\', \'2007-03-17 01:20:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'users` VALUES(2, \'nate\', MD5(\'password\'), \'2007-03-17 01:18:23\', \'2007-03-17 01:20:31\')';
$queries[] = 'INSERT INTO `users` VALUES(3, \'larry\', MD5(\'password\'), \'2007-03-17 01:20:23\', \'2007-03-17 01:22:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'users` VALUES(3, \'larry\', MD5(\'password\'), \'2007-03-17 01:20:23\', \'2007-03-17 01:22:31\')';
$queries[] = 'INSERT INTO `users` VALUES(4, \'garrett\', MD5(\'password\'), \'2007-03-17 01:22:23\', \'2007-03-17 01:24:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'users` VALUES(4, \'garrett\', MD5(\'password\'), \'2007-03-17 01:22:23\', \'2007-03-17 01:24:31\')';
$queries[] = 'INSERT INTO `articles` VALUES(1, 1, \'First Article\', \'First Article Body\', \'Y\', \'2007-03-18 10:39:23\', \'2007-03-18 10:41:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles` VALUES(1, 1, \'First Article\', \'First Article Body\', \'Y\', \'2007-03-18 10:39:23\', \'2007-03-18 10:41:31\')';
$queries[] = 'INSERT INTO `articles` VALUES(2, 3, \'Second Article\', \'Second Article Body\', \'Y\', \'2007-03-18 10:41:23\', \'2007-03-18 10:43:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles` VALUES(2, 3, \'Second Article\', \'Second Article Body\', \'Y\', \'2007-03-18 10:41:23\', \'2007-03-18 10:43:31\')';
$queries[] = 'INSERT INTO `articles` VALUES(3, 1, \'Third Article\', \'Third Article Body\', \'Y\', \'2007-03-18 10:43:23\', \'2007-03-18 10:45:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles` VALUES(3, 1, \'Third Article\', \'Third Article Body\', \'Y\', \'2007-03-18 10:43:23\', \'2007-03-18 10:45:31\')';
$queries[] = 'INSERT INTO `tags` VALUES(1, \'tag1\', \'2007-03-18 12:22:23\', \'2007-03-18 12:24:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'tags` VALUES(1, \'tag1\', \'2007-03-18 12:22:23\', \'2007-03-18 12:24:31\')';
$queries[] = 'INSERT INTO `tags` VALUES(2, \'tag2\', \'2007-03-18 12:24:23\', \'2007-03-18 12:26:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'tags` VALUES(2, \'tag2\', \'2007-03-18 12:24:23\', \'2007-03-18 12:26:31\')';
$queries[] = 'INSERT INTO `tags` VALUES(3, \'tag3\', \'2007-03-18 12:26:23\', \'2007-03-18 12:28:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'tags` VALUES(3, \'tag3\', \'2007-03-18 12:26:23\', \'2007-03-18 12:28:31\')';
$queries[] = 'INSERT INTO `articles_tags` VALUES(1, 1)'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(1, 1)';
$queries[] = 'INSERT INTO `articles_tags` VALUES(1, 2)'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(1, 2)';
$queries[] = 'INSERT INTO `articles_tags` VALUES(2, 1)'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(2, 1)';
$queries[] = 'INSERT INTO `articles_tags` VALUES(2, 3)'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(2, 3)';
$queries[] = 'INSERT INTO `comments` VALUES(1, 1, 2, \'First Comment for First Article\', \'Y\', \'2007-03-18 10:45:23\', \'2007-03-18 10:47:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'comments` VALUES(1, 1, 2, \'First Comment for First Article\', \'Y\', \'2007-03-18 10:45:23\', \'2007-03-18 10:47:31\')';
$queries[] = 'INSERT INTO `comments` VALUES(2, 1, 4, \'Second Comment for First Article\', \'Y\', \'2007-03-18 10:47:23\', \'2007-03-18 10:49:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'comments` VALUES(2, 1, 4, \'Second Comment for First Article\', \'Y\', \'2007-03-18 10:47:23\', \'2007-03-18 10:49:31\')';
$queries[] = 'INSERT INTO `comments` VALUES(3, 1, 1, \'Third Comment for First Article\', \'Y\', \'2007-03-18 10:49:23\', \'2007-03-18 10:51:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'comments` VALUES(3, 1, 1, \'Third Comment for First Article\', \'Y\', \'2007-03-18 10:49:23\', \'2007-03-18 10:51:31\')';
$queries[] = 'INSERT INTO `comments` VALUES(4, 1, 1, \'Fourth Comment for First Article\', \'N\', \'2007-03-18 10:51:23\', \'2007-03-18 10:53:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'comments` VALUES(4, 1, 1, \'Fourth Comment for First Article\', \'N\', \'2007-03-18 10:51:23\', \'2007-03-18 10:53:31\')';
$queries[] = 'INSERT INTO `comments` VALUES(5, 2, 1, \'First Comment for Second Article\', \'Y\', \'2007-03-18 10:53:23\', \'2007-03-18 10:55:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'comments` VALUES(5, 2, 1, \'First Comment for Second Article\', \'Y\', \'2007-03-18 10:53:23\', \'2007-03-18 10:55:31\')';
$queries[] = 'INSERT INTO `comments` VALUES(6, 2, 2, \'Second Comment for Second Article\', \'Y\', \'2007-03-18 10:55:23\', \'2007-03-18 10:57:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'comments` VALUES(6, 2, 2, \'Second Comment for Second Article\', \'Y\', \'2007-03-18 10:55:23\', \'2007-03-18 10:57:31\')';
$queries[] = 'INSERT INTO `attachments` VALUES(1, 5, \'attachment.zip\', \'2007-03-18 10:51:23\', \'2007-03-18 10:53:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'attachments` VALUES(1, 5, \'attachment.zip\', \'2007-03-18 10:51:23\', \'2007-03-18 10:53:31\')';
$queries[] = 'INSERT INTO `categories` VALUES(1, 0, \'Category 1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'categories` VALUES(1, 0, \'Category 1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')';
$queries[] = 'INSERT INTO `categories` VALUES(2, 1, \'Category 1.1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'categories` VALUES(2, 1, \'Category 1.1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')';
$queries[] = 'INSERT INTO `categories` VALUES(3, 1, \'Category 1.2\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'categories` VALUES(3, 1, \'Category 1.2\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')';
$queries[] = 'INSERT INTO `categories` VALUES(4, 0, \'Category 2\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'categories` VALUES(4, 0, \'Category 2\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')';
$queries[] = 'INSERT INTO `categories` VALUES(5, 0, \'Category 3\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'categories` VALUES(5, 0, \'Category 3\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')';
$queries[] = 'INSERT INTO `categories` VALUES(6, 5, \'Category 3.1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; $queries[] = 'INSERT INTO `' . $config['prefix'] . 'categories` VALUES(6, 5, \'Category 3.1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')';
foreach($queries as $query) { foreach($queries as $query) {
$this->db->_execute($query); $this->db->_execute($query);
@ -184,14 +207,16 @@ class ModelTest extends UnitTestCase {
} }
function tearDown() { function tearDown() {
$config = $this->db->config;
$queries = array(); $queries = array();
$queries[] = 'TRUNCATE TABLE `categories`'; $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'categories`';
$queries[] = 'TRUNCATE TABLE `attachments`'; $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'attachments`';
$queries[] = 'TRUNCATE TABLE `comments`'; $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'comments`';
$queries[] = 'TRUNCATE TABLE `tags`'; $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'tags`';
$queries[] = 'TRUNCATE TABLE `articles`'; $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'articles`';
$queries[] = 'TRUNCATE TABLE `users`'; $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'users`';
foreach($queries as $query) { foreach($queries as $query) {
$this->db->_execute($query); $this->db->_execute($query);
@ -200,16 +225,17 @@ class ModelTest extends UnitTestCase {
if (isset($this->model)) { if (isset($this->model)) {
unset($this->model); unset($this->model);
} }
unset($this->db);
} }
/** /**
* Leave as first test method, create tables. * Leave as first test method, create tables.
*/ */
function testStartup() { function testStartup() {
$config = $this->db->config;
$queries = array(); $queries = array();
$queries[] = 'CREATE TABLE IF NOT EXISTS `categories`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'categories`(
`id` INT NOT NULL AUTO_INCREMENT, `id` INT NOT NULL AUTO_INCREMENT,
`parent_id` INT NOT NULL, `parent_id` INT NOT NULL,
`name` VARCHAR(255) NOT NULL, `name` VARCHAR(255) NOT NULL,
@ -219,7 +245,7 @@ class ModelTest extends UnitTestCase {
PRIMARY KEY(`id`) PRIMARY KEY(`id`)
)'; )';
$queries[] = 'CREATE TABLE IF NOT EXISTS `users`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'users`(
`id` INT NOT NULL AUTO_INCREMENT, `id` INT NOT NULL AUTO_INCREMENT,
`user` VARCHAR(255) NOT NULL, `user` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL,
@ -229,7 +255,7 @@ class ModelTest extends UnitTestCase {
PRIMARY KEY(`id`) PRIMARY KEY(`id`)
)'; )';
$queries[] = 'CREATE TABLE IF NOT EXISTS `articles`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'articles`(
`id` INT NOT NULL AUTO_INCREMENT, `id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL, `user_id` INT NOT NULL,
`title` VARCHAR(255) NOT NULL, `title` VARCHAR(255) NOT NULL,
@ -241,7 +267,7 @@ class ModelTest extends UnitTestCase {
PRIMARY KEY(`id`) PRIMARY KEY(`id`)
)'; )';
$queries[] = 'CREATE TABLE IF NOT EXISTS `tags`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'tags`(
`id` INT NOT NULL AUTO_INCREMENT, `id` INT NOT NULL AUTO_INCREMENT,
`tag` VARCHAR(255) NOT NULL, `tag` VARCHAR(255) NOT NULL,
`created` DATETIME, `created` DATETIME,
@ -250,14 +276,14 @@ class ModelTest extends UnitTestCase {
PRIMARY KEY(`id`) PRIMARY KEY(`id`)
)'; )';
$queries[] = 'CREATE TABLE IF NOT EXISTS `articles_tags`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'articles_tags`(
`article_id` INT NOT NULL, `article_id` INT NOT NULL,
`tag_id` INT NOT NULL, `tag_id` INT NOT NULL,
PRIMARY KEY(`article_id`, `tag_id`) PRIMARY KEY(`article_id`, `tag_id`)
)'; )';
$queries[] = 'CREATE TABLE IF NOT EXISTS `comments`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'comments`(
`id` INT NOT NULL AUTO_INCREMENT, `id` INT NOT NULL AUTO_INCREMENT,
`article_id` INT NOT NULL, `article_id` INT NOT NULL,
`user_id` INT NOT NULL, `user_id` INT NOT NULL,
@ -269,7 +295,7 @@ class ModelTest extends UnitTestCase {
PRIMARY KEY(`id`) PRIMARY KEY(`id`)
)'; )';
$queries[] = 'CREATE TABLE IF NOT EXISTS `attachments`( $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'attachments`(
`id` INT NOT NULL AUTO_INCREMENT, `id` INT NOT NULL AUTO_INCREMENT,
`comment_id` INT NOT NULL, `comment_id` INT NOT NULL,
`attachment` VARCHAR(255) NOT NULL, `attachment` VARCHAR(255) NOT NULL,
@ -597,17 +623,17 @@ class ModelTest extends UnitTestCase {
function testRecursiveRead() { function testRecursiveRead() {
$this->model =& new User(); $this->model =& new User();
$result = $this->model->bindModel(array('hasMany' => array('Article')), false); $result = $this->model->bindModel(array('hasMany' => array('Article')), false);
$this->assertTrue($result); $this->assertTrue($result);
$this->model->recursive = 0; $this->model->recursive = 0;
$result = $this->model->read('id, user', 1); $result = $this->model->read('id, user', 1);
$expected = array( $expected = array(
'User' => array ( 'id' => '1', 'user' => 'mariano'), 'User' => array ( 'id' => '1', 'user' => 'mariano'),
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->model->recursive = 1; $this->model->recursive = 1;
$result = $this->model->read('id, user', 1); $result = $this->model->read('id, user', 1);
$expected = array( $expected = array(
@ -618,7 +644,10 @@ class ModelTest extends UnitTestCase {
) )
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$db =& ConnectionManager::getDataSource('test_suite');
$db->fullDebug = true;
$this->model->recursive = 2; $this->model->recursive = 2;
$result = $this->model->read('id, user', 3); $result = $this->model->read('id, user', 3);
$expected = array( $expected = array(
@ -1012,20 +1041,41 @@ class ModelTest extends UnitTestCase {
* Leave as last test method, drop tables. * Leave as last test method, drop tables.
*/ */
function testFinish() { function testFinish() {
$config = $this->db->config;
$queries = array(); $queries = array();
$queries[] = 'DROP TABLE `categories`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'categories`';
$queries[] = 'DROP TABLE `attachments`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'attachments`';
$queries[] = 'DROP TABLE `comments`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'comments`';
$queries[] = 'DROP TABLE `articles_tags`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'articles_tags`';
$queries[] = 'DROP TABLE `tags`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'tags`';
$queries[] = 'DROP TABLE `articles`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'articles`';
$queries[] = 'DROP TABLE `users`'; $queries[] = 'DROP TABLE `' . $config['prefix'] . 'users`';
foreach($queries as $query) { foreach($queries as $query) {
$this->db->_execute($query); $this->db->_execute($query);
} }
} }
}
function array_diff_recursive($array1, $array2) {
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (@!is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_recursive($value, $array2[$key]);
if ($new_diff != false) {
$difference[$key] = $new_diff;
}
}
} elseif (!isset($array2[$key]) || $array2[$key] != $value) {
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
} }
?> ?>