Dennis Luitwieler

Using the atkMockDb

by Dennis Luitwieler |  No comments | February 11, 2008

If you develop an ATK application but do not have the luxury of a separate test-database, and can only test against actual databases, it is not safe to run test cases that actually add records to the database. Even though test cases can be built to clean-up test data afterward, this is not without risk. For instance, when a test case breaks halfway through the code with a PHP parse error, any test data that was already added will stay there. Besides the fact that this can break any future executions of your test case, the test record stays in the live database and possibly break the live site.

This is where the atkMockDb kicks in.

What is the atkMockDb?

The atkMockDb is a normal atkDb class but it doesn't actually execute any queries on the database. This prevents the problem of executing INSERT or UPDATE queries on a live environment, but it also prevents SELECT queries from returning any data. Since ATK performs quite some database queries in order to execute its functions, we need a way to simulate some results for any query that is executed. The atkMockDb provides just that!

Configuring the atkMockDb

Create the test case using the atkTestCase as its base class, so you can use the setMockDb() method to easily configure your test cases to use the atkMockDb.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
 
class test_mynode extends atkTestCase
{
    public function test_asimpletestcase()
 
    {<br />
       $this->setMockDb(); // tell ATK to use it
       // Some tests...
 
    }
}
 
?>

setResult

When you know the exact query that will be executed, you can set the records that will be returned by the mock database: you can use the setResult method for that purpose.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create a result record
$record = array('id'=>2, 'name'=>'test');
 
// The EXACT query on which we want the $record to be returned.
$sql= "SELECT * FROM mynodetablename WHERE id='2'"; // tell atkMockDb to return the mockRec on the given query.
 
$db = atkGetDb();
$db->setResult(array($record), $sql);
 
// rest of testcase ...
 
?>

Now whenever ATK tries to execute this exact query, the given record will be returned.

Final note

Note that using atkMockDb is not foolproof, it doesn't guarantee that the syntax of the queries is correct and that MySQL will properly execute the query, you only check if ATK tries to execute the queries, and simulate the result. Test cases for a certain function should only test that function. For example: constrain the tests that check if atkNode::updateDb() is executed properly, to the test cases for atkNode.

The atkMockDb has some more possibilities such as using regular expressions to determine on which queries certain records should be returned. A more complete howto on using the atkMockDb can be found on the ATK wiki.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Plus

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.