CakePHP Test Fixtures Drop My Tables Permanently After Running A Test Case
- by Frank
I'm not sure what I've done wrong in my CakePHP unit test configuration. Every time I run a test case, the model tables associated with my fixtures are missing form my test database.
After running an individual test case I have to re-import my database tables using phpMyAdmin.
Here are the relevant files:
This is the class I'm trying to test comment.php. This table is dropped after the test.
App::import('Sanitize');
class Comment extends AppModel{
public $name = 'Comment';
public $actsAs = array('Tree');
public $belongsTo = array('User' => array('fields'=>array('id', 'username')));
public $validate = array(
'text' = array(
'rule' =array('between', 1, 4000),
'required' ='true',
'allowEmpty'='false',
'message' = "You can't leave your comment text empty!")
);
database.php
class DATABASE_CONFIG {
var $default = array(
'driver' = 'mysql',
'persistent' = false,
'host' = 'project.db',
'login' = 'projectman',
'password' = 'projectpassword',
'database' = 'projectdb',
'prefix' = ''
);
var $test = array(
'driver' = 'mysql',
'persistent' = false,
'host' = 'project.db',
'login' = 'projectman',
'password' = 'projectpassword',
'database' = 'testprojectdb',
'prefix' = ''
);
}
My comment.test.php file. This is the table that keeps getting dropped.
<?php
App::import('Model', 'Comment');
class CommentTestCase extends CakeTestCase
{
public $fixtures = array('app.comment', 'app.user');
function start(){
$this-Comment =& ClassRegistry::init('Comment');
$this-Comment-useDbConfig = 'test_suite';
}
This is my comment_fixture.php class:
<?php
class CommentFixture extends CakeTestFixture
{
var $name = "Comment";
var $import = 'Comment';
}
And just in case, here is a typical test method in the CommentTestCase class
function testMsgNotificationUserComment(){
$user_id = '1';
$submission_id = '1';
$parent_id = $this-Comment-commentOnModel('Submission', $submission_id, '0', $user_id, "Says: A");
$other_user_id = '2';
$msg_id = $this-Comment-commentOnModel('Submission', $submission_id, $parent_id, $other_user_id, "Says: B");
$expected = array(array('Comment'=array('id'=$msg_id, 'text'="Says: B", 'submission_id'=$submission_id, 'topic_id'='0', 'ack'='0')));
$result = $this-Comment-getMessages($user_id);
$this-assertEqual($result, $expected);
}
I've been dealing with this for a day now and I'm starting to be put off by CakePHP's unit testing. In addition to this issue -- Servral times now I've had data inserted into by 'default' database configuration after running tests! What's going on with my configuration?!