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
This commit is contained in:
phpnut 2007-03-23 21:59:08 +00:00
parent 719d704360
commit 3a4a49ef03
12 changed files with 513 additions and 229 deletions

View file

@ -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. * 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 TestSuiteModel { class User extends CakeTestModel {
var $name = 'User'; var $name = 'User';
var $validate = array( var $validate = array(
'user' => VALID_NOT_EMPTY, 'user' => VALID_NOT_EMPTY,
@ -84,7 +75,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Article extends TestSuiteModel { class Article extends CakeTestModel {
var $name = 'Article'; var $name = 'Article';
var $belongsTo = array('User'); var $belongsTo = array('User');
var $hasMany = array( var $hasMany = array(
@ -103,7 +94,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Tag extends TestSuiteModel { class Tag extends CakeTestModel {
var $name = 'Tag'; var $name = 'Tag';
} }
/** /**
@ -112,7 +103,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Comment extends TestSuiteModel { class Comment extends CakeTestModel {
var $name = 'Comment'; var $name = 'Comment';
var $belongsTo = array('Article', 'User'); var $belongsTo = array('Article', 'User');
var $hasOne = array( var $hasOne = array(
@ -125,7 +116,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Attachment extends TestSuiteModel { class Attachment extends CakeTestModel {
var $name = 'Attachment'; var $name = 'Attachment';
} }
/** /**
@ -134,7 +125,7 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class Category extends TestSuiteModel { class Category extends CakeTestModel {
var $name = 'Category'; var $name = 'Category';
} }
/** /**
@ -143,183 +134,16 @@
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.model * @subpackage cake.tests.cases.libs.model
*/ */
class ModelTest extends UnitTestCase { class ModelTest extends CakeTestCase {
function setUp() { var $fixtures = array( 'core.category', 'core.user', 'core.article', 'core.tag', 'core.articles_tag', 'core.comment', 'core.attachment' );
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);
}
}
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() { function testIdentity() {
$this->model =& new Test(); $this->model =& new Test();
$result = $this->model->name; $result = $this->model->name;
$expected = 'Test'; $expected = 'Test';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
function testCreation() { function testCreation() {
$this->model =& new Test(); $this->model =& new Test();
$result = $this->model->create(); $result = $this->model->create();
@ -705,7 +529,7 @@ class ModelTest extends UnitTestCase {
) )
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->model->findAll(array('Article.user_id' => 3), null, null, null, 1, 2); $result = $this->model->findAll(array('Article.user_id' => 3), null, null, null, 1, 2);
$expected = array ( $expected = array (
array ( array (
@ -745,7 +569,7 @@ class ModelTest extends UnitTestCase {
) )
) )
); );
$this->assertEqual($expected, $result); $this->assertEqual($result, $expected);
} }
function testSaveField() { function testSaveField() {
@ -1034,46 +858,6 @@ class ModelTest extends UnitTestCase {
); );
$this->assertEqual($result, $expected); $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;
}
?>

21
cake/tests/fixtures/article_fixture.php vendored Normal file
View file

@ -0,0 +1,21 @@
<?php
class ArticleFixture extends CakeTestFixture {
var $name = 'Article';
var $fields = array(
'id' => 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')
);
}
?>

View file

@ -0,0 +1,18 @@
<?php
class ArticlesTagFixture extends CakeTestFixture {
var $name = 'ArticlesTag';
var $fields = array(
'article_id' => 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)
);
}
?>

View file

@ -0,0 +1,17 @@
<?php
class AttachmentFixture extends CakeTestFixture {
var $name = 'Attachment';
var $fields = array(
'id' => 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')
);
}
?>

View file

@ -0,0 +1,22 @@
<?php
class CategoryFixture extends CakeTestFixture {
var $name = 'Category';
var $fields = array(
'id' => 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')
);
}
?>

24
cake/tests/fixtures/comment_fixture.php vendored Normal file
View file

@ -0,0 +1,24 @@
<?php
class CommentFixture extends CakeTestFixture {
var $name = 'Comment';
var $fields = array(
'id' => 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')
);
}
?>

18
cake/tests/fixtures/tag_fixture.php vendored Normal file
View file

@ -0,0 +1,18 @@
<?php
class TagFixture extends CakeTestFixture {
var $name = 'Tag';
var $fields = array(
'id' => 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')
);
}
?>

20
cake/tests/fixtures/user_fixture.php vendored Normal file
View file

@ -0,0 +1,20 @@
<?php
class UserFixture extends CakeTestFixture {
var $name = 'User';
var $fields = array(
'id' => 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'),
);
}
?>

View file

@ -0,0 +1,196 @@
<?php
require_once CAKE . 'tests' . DS . 'lib' . DS . 'cake_test_model.php';
require_once CAKE . 'tests' . DS . 'lib' . DS . 'cake_test_fixture.php';
vendor('simpletest'.DS.'unit_tester');
class CakeTestCase extends UnitTestCase {
/**
* Announces the start of a test.
*
* @param string $method Test method just started.
*
* @access public
*/
function before($method) {
parent::before($method);
if (isset($this->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);
}
}
}
?>

View file

@ -0,0 +1,154 @@
<?php
class CakeTestFixture extends Object {
var $db = null;
/**
* Instantiate the fixture.
*
* @param object Cake's DBO driver (e.g: DboMysql).
*
* @access public
*/
function __construct(&$db) {
$this->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;
}
}
?>

View file

@ -0,0 +1,9 @@
<?php
require_once LIBS.'model'.DS.'model.php';
class CakeTestModel extends Model {
var $useDbConfig = 'test_suite';
}
?>

View file

@ -51,6 +51,7 @@ class TestManager {
function _installSimpleTest() { function _installSimpleTest() {
vendor('simpletest'.DS.'unit_tester', 'simpletest'.DS.'web_tester', 'simpletest'.DS.'mock_objects'); 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_web_test_case.php');
require_once(LIB_TESTS . 'cake_test_case.php');
} }
function runAllTests(&$reporter) { function runAllTests(&$reporter) {
@ -364,4 +365,4 @@ class HtmlTestManager extends TestManager {
return $buffer; return $buffer;
} }
} }
?> ?>