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:

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:

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.