·

photo credit: Casino Island Sunset Bridge - HDR via photopin (license)

PHPUnit setup and teardown for tables

When you’re unit testing your code with PHPUnit, chances are that at some point you have to test some methods that involve manipulation on the database. Of course, you’ve written some test that validate the data in the database afterwards, and you might already have written a bootstrap that prepares the database for each testrun, but what to do when it comes to the individual tests?
In a proper testsuite, one test should not depend on another, and each test should start with the same data as any other test. But if you’re writing tests that manipulate the data inside the database, your other tests in the suite have to deal with that, unless you restore your entire test database before each individual test, but that can become very time- (and resource-) intensive.
So how can we overcome this problem in a simple way?

Temporary tables to the rescue

One nice feature of MySQL (or MariaDB or most other SQL engines) is the use of temporary tables. A temporary table is (as the Mysql documentation puts it):

A temporary table is visible only to the current session, and is dropped automatically when the session is closed.

When it comes to unit testing, we can use temporary tables as a ‘quick backup’ of our local table, and restore this table after our tests are complete. Take a look at the next 2 methods:

The methods are fairly simple:

  • The first method creates a copy of the table with the name $tableName .
  • The second method restores the original table by copying the content back from the temporary table.

With these two methods, we can restore our tables back to their original state after our tests have run, so that each test uses the same data as original provided in the test database.

Setting it up and tearing it down

In PHPUnit, there are various method to perform actions before and after tests (or testsuites). To prepare and restore the tables for a single class (with multiple tests, you can use the setUpBeforeClass()  and tearDownAfterClass() -methods:

If you need to setup and restore the tables before every test-method, you can use the setUp()  and tearDown() -methods:

Please note that it’s also (for performance sake) possible (or even advised) to combine the setUpBeforeClass() -method in combination with the tearDown() -method. That way, you only have to create the temporary table once, and you can restore the original table as much as you want. The only thing you need to change then is to remove the last line from the restoreFromTmpTable() -method, otherwise your temporary table gets dropped anyway…

Visitors give this article an average rating of 4.3 out of 5.

How would you rate this article?

One thought on “PHPUnit setup and teardown for tables”

  1. Elon Mallin says:

    setUpBeforeClass and tearDownAfterClass are static but they use the $this pointer which doesn’t work. What is the actual way to do this?
    The setUp and tearDown are not static functions.

Leave a Reply