From 3a4a49ef03c2ef60a9195acd508665067c27934c Mon Sep 17 00:00:00 2001 From: phpnut Date: Fri, 23 Mar 2007 21:59:08 +0000 Subject: [PATCH] Adding changes to test suite submitted by Mariano git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4667 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/tests/cases/libs/model/model.test.php | 240 +------------------ cake/tests/fixtures/article_fixture.php | 21 ++ cake/tests/fixtures/articles_tag_fixture.php | 18 ++ cake/tests/fixtures/attachment_fixture.php | 17 ++ cake/tests/fixtures/category_fixture.php | 22 ++ cake/tests/fixtures/comment_fixture.php | 24 ++ cake/tests/fixtures/tag_fixture.php | 18 ++ cake/tests/fixtures/user_fixture.php | 20 ++ cake/tests/lib/cake_test_case.php | 196 +++++++++++++++ cake/tests/lib/cake_test_fixture.php | 154 ++++++++++++ cake/tests/lib/cake_test_model.php | 9 + cake/tests/lib/test_manager.php | 3 +- 12 files changed, 513 insertions(+), 229 deletions(-) create mode 100644 cake/tests/fixtures/article_fixture.php create mode 100644 cake/tests/fixtures/articles_tag_fixture.php create mode 100644 cake/tests/fixtures/attachment_fixture.php create mode 100644 cake/tests/fixtures/category_fixture.php create mode 100644 cake/tests/fixtures/comment_fixture.php create mode 100644 cake/tests/fixtures/tag_fixture.php create mode 100644 cake/tests/fixtures/user_fixture.php create mode 100644 cake/tests/lib/cake_test_case.php create mode 100644 cake/tests/lib/cake_test_fixture.php create mode 100644 cake/tests/lib/cake_test_model.php diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 2bccc5b7a..1ee6df746 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -56,22 +56,13 @@ } } - /** - * 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. * * @package cake.tests * @subpackage cake.tests.cases.libs.model */ - class User extends TestSuiteModel { + class User extends CakeTestModel { var $name = 'User'; var $validate = array( 'user' => VALID_NOT_EMPTY, @@ -84,7 +75,7 @@ * @package cake.tests * @subpackage cake.tests.cases.libs.model */ - class Article extends TestSuiteModel { + class Article extends CakeTestModel { var $name = 'Article'; var $belongsTo = array('User'); var $hasMany = array( @@ -103,7 +94,7 @@ * @package cake.tests * @subpackage cake.tests.cases.libs.model */ - class Tag extends TestSuiteModel { + class Tag extends CakeTestModel { var $name = 'Tag'; } /** @@ -112,7 +103,7 @@ * @package cake.tests * @subpackage cake.tests.cases.libs.model */ - class Comment extends TestSuiteModel { + class Comment extends CakeTestModel { var $name = 'Comment'; var $belongsTo = array('Article', 'User'); var $hasOne = array( @@ -125,7 +116,7 @@ * @package cake.tests * @subpackage cake.tests.cases.libs.model */ - class Attachment extends TestSuiteModel { + class Attachment extends CakeTestModel { var $name = 'Attachment'; } /** @@ -134,7 +125,7 @@ * @package cake.tests * @subpackage cake.tests.cases.libs.model */ - class Category extends TestSuiteModel { + class Category extends CakeTestModel { var $name = 'Category'; } /** @@ -143,183 +134,16 @@ * @package cake.tests * @subpackage cake.tests.cases.libs.model */ -class ModelTest extends UnitTestCase { - function setUp() { - if (!isset($this->db) || !$this->db->isConnected()) { - restore_error_handler(); - @$db =& ConnectionManager::getDataSource('test'); - if (is_object($db) && isset($db->fullDebug)) { - $db->fullDebug = false; - } - set_error_handler('simpleTestErrorHandler'); - - if (!$db->isConnected()) { - $db =& ConnectionManager::getDataSource('default'); - } - - $config = $db->config; - $config['prefix'] .= 'test_suite_'; - - $this->db =& ConnectionManager::create('test_suite', $config); - $this->db->fullDebug = false; - } else { - $config = $this->db->config; - } - - $queries = array(); - - $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 `' . $config['prefix'] . '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(3, \'larry\', MD5(\'password\'), \'2007-03-17 01:20:23\', \'2007-03-17 01:22: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 `' . $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 `' . $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 `' . $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 `' . $config['prefix'] . 'tags` VALUES(1, \'tag1\', \'2007-03-18 12:22:23\', \'2007-03-18 12:24: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 `' . $config['prefix'] . 'tags` VALUES(3, \'tag3\', \'2007-03-18 12:26:23\', \'2007-03-18 12:28:31\')'; - - $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(1, 1)'; - $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(1, 2)'; - $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(2, 1)'; - $queries[] = 'INSERT INTO `' . $config['prefix'] . 'articles_tags` VALUES(2, 3)'; - - $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 `' . $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 `' . $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 `' . $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 `' . $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 `' . $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 `' . $config['prefix'] . 'attachments` VALUES(1, 5, \'attachment.zip\', \'2007-03-18 10:51:23\', \'2007-03-18 10:53: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 `' . $config['prefix'] . '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(3, 1, \'Category 1.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 `' . $config['prefix'] . '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(6, 5, \'Category 3.1\', \'2007-03-18 15:30:23\', \'2007-03-18 15:32:31\')'; - - foreach($queries as $query) { - $this->db->_execute($query); - } - } +class ModelTest extends CakeTestCase { + var $fixtures = array( 'core.category', 'core.user', 'core.article', 'core.tag', 'core.articles_tag', 'core.comment', 'core.attachment' ); - function tearDown() { - $config = $this->db->config; - - $queries = array(); - - $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'categories`'; - $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'attachments`'; - $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'comments`'; - $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'tags`'; - $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'articles`'; - $queries[] = 'TRUNCATE TABLE `' . $config['prefix'] . 'users`'; - - foreach($queries as $query) { - $this->db->_execute($query); - } - - if (isset($this->model)) { - unset($this->model); - } - } - - /** - * Leave as first test method, create tables. - */ - function testStartup() { - $config = $this->db->config; - - $queries = array(); - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'categories`( - `id` INT NOT NULL AUTO_INCREMENT, - `parent_id` INT NOT NULL, - `name` VARCHAR(255) NOT NULL, - `created` DATETIME, - `updated` DATETIME, - - PRIMARY KEY(`id`) - )'; - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'users`( - `id` INT NOT NULL AUTO_INCREMENT, - `user` VARCHAR(255) NOT NULL, - `password` VARCHAR(255) NOT NULL, - `created` DATETIME, - `updated` DATETIME, - - PRIMARY KEY(`id`) - )'; - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'articles`( - `id` INT NOT NULL AUTO_INCREMENT, - `user_id` INT NOT NULL, - `title` VARCHAR(255) NOT NULL, - `body` TEXT NOT NULL, - `published` CHAR(1) DEFAULT \'N\', - `created` DATETIME, - `updated` DATETIME, - - PRIMARY KEY(`id`) - )'; - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'tags`( - `id` INT NOT NULL AUTO_INCREMENT, - `tag` VARCHAR(255) NOT NULL, - `created` DATETIME, - `updated` DATETIME, - - PRIMARY KEY(`id`) - )'; - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'articles_tags`( - `article_id` INT NOT NULL, - `tag_id` INT NOT NULL, - - PRIMARY KEY(`article_id`, `tag_id`) - )'; - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'comments`( - `id` INT NOT NULL AUTO_INCREMENT, - `article_id` INT NOT NULL, - `user_id` INT NOT NULL, - `comment` TEXT NOT NULL, - `published` CHAR(1) DEFAULT \'N\', - `created` DATETIME, - `updated` DATETIME, - - PRIMARY KEY(`id`) - )'; - - $queries[] = 'CREATE TABLE IF NOT EXISTS `' . $config['prefix'] . 'attachments`( - `id` INT NOT NULL AUTO_INCREMENT, - `comment_id` INT NOT NULL, - `attachment` VARCHAR(255) NOT NULL, - `created` DATETIME, - `updated` DATETIME, - - PRIMARY KEY(`id`) - )'; - - foreach($queries as $query) { - $this->db->_execute($query); - } - - return parent::getTests(); - } - function testIdentity() { $this->model =& new Test(); $result = $this->model->name; $expected = 'Test'; $this->assertEqual($result, $expected); } - + function testCreation() { $this->model =& new Test(); $result = $this->model->create(); @@ -705,7 +529,7 @@ class ModelTest extends UnitTestCase { ) ); $this->assertEqual($result, $expected); - + $result = $this->model->findAll(array('Article.user_id' => 3), null, null, null, 1, 2); $expected = array ( array ( @@ -745,7 +569,7 @@ class ModelTest extends UnitTestCase { ) ) ); - $this->assertEqual($expected, $result); + $this->assertEqual($result, $expected); } function testSaveField() { @@ -1034,46 +858,6 @@ class ModelTest extends UnitTestCase { ); $this->assertEqual($result, $expected); } - - /** - * Leave as last test method, drop tables. - */ - function testFinish() { - $config = $this->db->config; - - $queries = array(); - - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'categories`'; - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'attachments`'; - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'comments`'; - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'articles_tags`'; - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'tags`'; - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'articles`'; - $queries[] = 'DROP TABLE `' . $config['prefix'] . 'users`'; - - foreach($queries as $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; -} - -?> \ No newline at end of file +?> diff --git a/cake/tests/fixtures/article_fixture.php b/cake/tests/fixtures/article_fixture.php new file mode 100644 index 000000000..efe5c2261 --- /dev/null +++ b/cake/tests/fixtures/article_fixture.php @@ -0,0 +1,21 @@ + array('type' => 'integer', 'key' => 'primary'), + 'user_id' => array('type' => 'integer', 'null' => false), + 'title' => array('type' => 'string', 'null' => false), + 'body' => 'text', + 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array ('id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'), + array ('id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'), + array ('id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/articles_tag_fixture.php b/cake/tests/fixtures/articles_tag_fixture.php new file mode 100644 index 000000000..d5cafec64 --- /dev/null +++ b/cake/tests/fixtures/articles_tag_fixture.php @@ -0,0 +1,18 @@ + array('type' => 'integer', 'null' => false), + 'tag_id' => array('type' => 'integer', 'null' => false), + ); + var $primaryKey = array('article_id', 'tag_id'); + var $records = array( + array('article_id' => 1, 'tag_id' => 1), + array('article_id' => 1, 'tag_id' => 2), + array('article_id' => 2, 'tag_id' => 1), + array('article_id' => 2, 'tag_id' => 3) + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/attachment_fixture.php b/cake/tests/fixtures/attachment_fixture.php new file mode 100644 index 000000000..11ceb6d2f --- /dev/null +++ b/cake/tests/fixtures/attachment_fixture.php @@ -0,0 +1,17 @@ + array('type' => 'integer', 'key' => 'primary'), + 'comment_id' => array('type' => 'integer', 'null' => false), + 'attachment' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array('id' => 1, 'comment_id' => 5, 'attachment' => 'attachment.zip', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/category_fixture.php b/cake/tests/fixtures/category_fixture.php new file mode 100644 index 000000000..53d67d120 --- /dev/null +++ b/cake/tests/fixtures/category_fixture.php @@ -0,0 +1,22 @@ + array('type' => 'integer', 'key' => 'primary'), + 'parent_id' => array('type' => 'integer', 'null' => false), + 'name' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array('id' => 1, 'parent_id' => 0, 'name' => 'Category 1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'), + array('id' => 2, 'parent_id' => 1, 'name' => 'Category 1.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'), + array('id' => 3, 'parent_id' => 1, 'name' => 'Category 1.2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'), + array('id' => 4, 'parent_id' => 0, 'name' => 'Category 2', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'), + array('id' => 5, 'parent_id' => 0, 'name' => 'Category 3', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31'), + array('id' => 6, 'parent_id' => 5, 'name' => 'Category 3.1', 'created' => '2007-03-18 15:30:23', 'updated' => '2007-03-18 15:32:31') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/comment_fixture.php b/cake/tests/fixtures/comment_fixture.php new file mode 100644 index 000000000..6f047529a --- /dev/null +++ b/cake/tests/fixtures/comment_fixture.php @@ -0,0 +1,24 @@ + array('type' => 'integer', 'key' => 'primary'), + 'article_id' => array('type' => 'integer', 'null'=>false), + 'user_id' => array('type' => 'integer', 'null'=>false), + 'comment' => 'text', + 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array('id' => 1, 'article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'), + array('id' => 2, 'article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'), + array('id' => 3, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'), + array('id' => 4, 'article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'), + array('id' => 5, 'article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'), + array('id' => 6, 'article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/tag_fixture.php b/cake/tests/fixtures/tag_fixture.php new file mode 100644 index 000000000..5dca2414b --- /dev/null +++ b/cake/tests/fixtures/tag_fixture.php @@ -0,0 +1,18 @@ + array('type' => 'integer', 'key' => 'primary'), + 'tag' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array('id' => 1, 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), + array('id' => 2, 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), + array('id' => 3, 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31') + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/user_fixture.php b/cake/tests/fixtures/user_fixture.php new file mode 100644 index 000000000..b0849456d --- /dev/null +++ b/cake/tests/fixtures/user_fixture.php @@ -0,0 +1,20 @@ + array('type' => 'integer', 'key' => 'primary'), + 'user' => array('type' => 'string', 'null' => false), + 'password' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array('id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'), + array('id' => 2, 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'), + array('id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'), + array('id' => 4, 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'), + ); +} + +?> \ No newline at end of file diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php new file mode 100644 index 000000000..25c40b28d --- /dev/null +++ b/cake/tests/lib/cake_test_case.php @@ -0,0 +1,196 @@ +fixtures) && (!is_array($this->fixtures) || empty($this->fixtures))) { + unset($this->fixtures); + } + + // Set up DB connection + if (isset($this->fixtures) && low($method) == 'start') { + // Try for test DB + restore_error_handler(); + @$db =& ConnectionManager::getDataSource('test'); + set_error_handler('simpleTestErrorHandler'); + + // Try for default DB + if (!$db->isConnected()) { + $db =& ConnectionManager::getDataSource('default'); + } + + // Add test prefix + $config = $db->config; + $config['prefix'] .= 'test_suite_'; + + // Set up db connection + ConnectionManager::create('test_suite', $config); + + // Get db connection + $this->db =& ConnectionManager::getDataSource('test_suite'); + $this->db->fullDebug = false; + + $this->_loadFixtures(); + } + + // Create records + if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end'))) { + foreach($this->_fixtures as $fixture) { + $inserts = $fixture->insert(); + + if (isset($inserts) && !empty($inserts)) { + foreach($inserts as $query) { + if (isset($query) && $query !== false) { + $this->db->_execute($query); + } + } + } + } + } + } + + /** + * Runs as first test to create tables. + * + * @access public + */ + function start() { + if (isset($this->_fixtures) && isset($this->db)) { + foreach($this->_fixtures as $fixture) { + $query = $fixture->create(); + + if (isset($query) && $query !== false) { + $this->db->_execute($query); + } + } + } + } + + /** + * Runs as last test to drop tables. + * + * @access public + */ + function end() { + if (isset($this->_fixtures) && isset($this->db)) { + foreach(array_reverse($this->_fixtures) as $fixture) { + $query = $fixture->drop(); + + if (isset($query) && $query !== false) { + $this->db->_execute($query); + } + } + } + } + + /** + * Announces the end of a test. + * + * @param string $method Test method just finished. + * + * @access public + */ + function after($method) { + if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end'))) { + foreach($this->_fixtures as $fixture) { + $query = $fixture->truncate(); + + if (isset($query) && $query !== false) { + $this->db->_execute($query); + } + } + } + + parent::after($method); + } + + /** + * Gets a list of test names. Normally that will be all internal methods that start with the + * name "test". This method should be overridden if you want a different rule. + * + * @return array List of test names. + * + * @access public + */ + function getTests() { + $methods = parent::getTests(); + + if (isset($this->fixtures)) { + $methods = am(am(array('start'), $methods), array('end')); + } + + return $methods; + } + + /** + * Load fixtures specified in var $fixtures. + * + * @access private + */ + function _loadFixtures() { + if (!isset($this->fixtures) || empty($this->fixtures)) { + return; + } + + if (!is_array($this->fixtures)) { + $this->fixtures = array( $this->fixtures ); + } + + $this->_fixtures = array(); + + foreach($this->fixtures as $index => $fixture) { + $fixtureFile = null; + + if (strpos($fixture, 'core.') === 0) { + $fixture = substr($fixture, strlen('core.')); + $fixturePaths = array( + CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures' + ); + } else if (strpos($fixture, 'app.') === 0) { + $fixture = substr($fixture, strlen('app.')); + $fixturePaths = array( + TESTS . 'fixtures' + ); + } else { + $fixturePaths = array( + TESTS . 'fixtures', + CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures' + ); + } + + foreach($fixturePaths as $path) { + if (is_readable($path . DS . $fixture . '_fixture.php')) { + $fixtureFile = $path . DS . $fixture . '_fixture.php'; + break; + } + } + + if (isset($fixtureFile)) { + require_once($fixtureFile); + + $fixtureClass = Inflector::camelize($fixture) . 'Fixture'; + + $this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db); + } + } + + if (empty($this->_fixtures)) { + unset($this->_fixtures); + } + } +} + +?> \ No newline at end of file diff --git a/cake/tests/lib/cake_test_fixture.php b/cake/tests/lib/cake_test_fixture.php new file mode 100644 index 000000000..e79819878 --- /dev/null +++ b/cake/tests/lib/cake_test_fixture.php @@ -0,0 +1,154 @@ +db =& $db; + + if (!isset($this->table)) { + $this->table = Inflector::underscore(Inflector::pluralize($this->name)); + } + + if (!isset($this->primaryKey) && isset($this->fields['id'])) { + $this->primaryKey = 'id'; + } + + if (isset($this->primaryKey) && !is_array($this->primaryKey)) { + $this->primaryKey = array( $this->primaryKey ); + } + } + + /** + * Run before all tests execute, should return SQL statement to create table for this fixture. + * + * @return string SQL CREATE TABLE statement, false if not applicable. + * + * @access public + */ + function create() { + if (!isset($this->_create)) { + if (!isset($this->fields) || empty($this->fields)) { + return null; + } + + $create = 'CREATE TABLE ' . $this->db->name($this->db->config['prefix'] . $this->table) . ' (' . "\n"; + + foreach($this->fields as $field => $attributes) { + if (!is_array($attributes)) { + $attributes = array('type' => $attributes); + } else if (isset($attributes['key']) && low($attributes['key']) == 'primary' && !isset($this->primaryKey)) { + $this->primaryKey = array ( $field ); + } + + $column = array($field, $attributes['type']); + unset($attributes['type']); + + if (!empty($attributes)) { + $column = array_merge($column, $attributes); + } + + $create .= $this->db->generateColumnSchema($column) . ',' . "\n"; + } + + if (isset($this->primaryKey)) { + foreach($this->primaryKey as $index => $field) { + $this->primaryKey[$index] = $this->db->name($field); + } + } + + if (!isset($this->primaryKey)) { + $create = substr($create, 0, -1); + } else { + $create .= 'PRIMARY KEY(' . implode(', ', $this->primaryKey) . ')' . "\n"; + } + + $create .= ')'; + + $this->_create = $create; + } + + return $this->_create; + } + + /** + * Run after all tests executed, should return SQL statement to drop table for this fixture. + * + * @return string SQL DROP TABLE statement, false if not applicable. + * + * @access public + */ + function drop() { + if (!isset($this->_drop)) { + $this->_drop = 'DROP TABLE ' . $this->db->name($this->db->config['prefix'] . $this->table); + } + + return $this->_drop; + } + + /** + * Run after each tests is executed, should return SQL statement to empty of records the table for this fixture. + * + * @return string SQL TRUNCATE TABLE statement, false if not applicable. + * + * @access public + */ + function truncate() { + if (!isset($this->_truncate)) { + $this->_truncate = 'TRUNCATE ' . $this->db->name($this->db->config['prefix'] . $this->table); + } + + return $this->_truncate; + } + + /** + * Run before each tests is executed, should return a set of SQL statements to insert records for the table of this fixture. + * + * @return array SQL INSERT statements, empty array if not applicable. + * + * @access public + */ + function insert() { + if (!isset($this->_insert)) { + $inserts = array(); + + if (isset($this->records) && !empty($this->records)) { + foreach($this->records as $record) { + $fields = array_keys($record); + $values = array_values($record); + + $insert = 'INSERT INTO ' . $this->db->name($this->db->config['prefix'] . $this->table) . '('; + + foreach($fields as $field) { + $insert .= $this->db->name($field) . ','; + } + $insert = substr($insert, 0, -1); + + $insert .= ') VALUES ('; + + foreach($values as $values) { + $insert .= $this->db->value($values) . ','; + } + $insert = substr($insert, 0, -1); + + $insert .= ')'; + + $inserts[] = $insert; + } + } + + $this->_insert = $inserts; + } + + return $this->_insert; + } +} + +?> \ No newline at end of file diff --git a/cake/tests/lib/cake_test_model.php b/cake/tests/lib/cake_test_model.php new file mode 100644 index 000000000..d95b93404 --- /dev/null +++ b/cake/tests/lib/cake_test_model.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/cake/tests/lib/test_manager.php b/cake/tests/lib/test_manager.php index f0e1ec86e..2cfa0de74 100644 --- a/cake/tests/lib/test_manager.php +++ b/cake/tests/lib/test_manager.php @@ -51,6 +51,7 @@ class TestManager { function _installSimpleTest() { vendor('simpletest'.DS.'unit_tester', 'simpletest'.DS.'web_tester', 'simpletest'.DS.'mock_objects'); require_once(LIB_TESTS . 'cake_web_test_case.php'); + require_once(LIB_TESTS . 'cake_test_case.php'); } function runAllTests(&$reporter) { @@ -364,4 +365,4 @@ class HtmlTestManager extends TestManager { return $buffer; } } -?> \ No newline at end of file +?>