CakePHP web testing - deleting database test data

CakePHP delete database test data FAQ: How can I delete CakePHP test data from my database in automated CakePHP unit and web tests (integration tests)?

I recently created a large set of CakePHP unit tests and CakePHP web tests (essentially integration tests), and as part of thorough testing, I needed to be able to create some user accounts and insert some test data into the database.

This is great for complete CakePHP integration testing, but for the purposes of automating my tests, I also needed to be able to delete my test data from the database after creating it. For instance, I was creating a test user named "bubba", and if I didn't also delete this user account from the database after every test run, my tests would fail the next time I tried to run them. In short, I wouldn't be able to automate my CakePHP unit tests and web tests unless I could access my database and delete data like the user "bubba" after every test run.

Deleting CakePHP test data from the database

In short, what I did for my purposes was to create a function in my web integration tests to delete the user named "bubba" from the database. Here's the function I created:

function delete_bubba_from_db() {
  $link = mysql_connect('localhost', 'root', 'root');
  if (!$link) {
    die('Could not connect: ' . mysql_error());
  mysql_select_db ('appmojo');

  $query = "DELETE FROM users WHERE username='bubba'";
  $mysql_result = @ mysql_query ($query)
    or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

As you can see, that function essentially just runs a SQL DELETE query against my CakePHP database to delete my test user account. (It might be possible to remove some of that code, especially the "die" tests, but I haven't tried yet. As I think about it, if this function can't connect to the database, I'm going to find out about it pretty soon anyway.)

I then call this function whenever I need it, like when I create a new user account with my application's registration form, like this:

# test that a valid registration attempt takes me to the
# next registration page
function testRegistrationValid1() {
  $this->setField('data[User][username]', 'bubba');
  $this->setField('data[User][email_address]', '');
  $this->setField('data[User][password]', 'aaaaaa');
  $this->setField('data[User][password_confirm]', 'aaaaaa');
  # test: should see the second registration form
  # now clean up the database

As you can see, in the last line of this web test I call my function to delete the user account from my CakePHP database. (For the record, this code is created to work with SimpleTest, specifically with the CakePHP SimpleTest integration, where I have extended the CakeWebTestCase class to create my tests.

CakePHP unit/web tests and deleting test data from the database

I hope this tip on how I delete test data from my database in my automated CakePHP unit and web tests (integration tests) has been helpful.