Testing Your Database

The Test Class

In order to take advantage of the built-in database tools that CodeIgniter provides for testing, yourtests must extend CIDatabaseTestCase:

  1. <?php namespace App\Database;
  2.  
  3. use CodeIgniter\Test\CIDatabaseTestCase;
  4.  
  5. class MyTests extends CIDatabaseTestCase
  6. {
  7. . . .
  8. }

Because special functionality executed during the setUp() and tearDown() phases, you must ensurethat you call the parent’s methods if you need to use those methods, otherwise you will lose muchof the functionality described here:

  1. <?php namespace App\Database;
  2.  
  3. use CodeIgniter\Test\CIDatabaseTestCase;
  4.  
  5. class MyTests extends CIDatabaseTestCase
  6. {
  7. public function setUp()
  8. {
  9. parent::setUp();
  10.  
  11. // Do something here....
  12. }
  13.  
  14. public function tearDown()
  15. {
  16. parent::tearDown();
  17.  
  18. // Do something here....
  19. }
  20. }

Setting Up a Test Database

When running database tests, you need to provide a database that can be used during testing. Instead ofusing the PHPUnit built-in database features, the framework provides tools specific to CodeIgniter. The firststep is to ensure that you have set up a tests database group in app/Config/Database.php.This specifies a database connection that is only used while running tests, to keep your other data safe.

If you have multiple developers on your team, you will likely want to keep your credentials stored inthe .env file. To do so, edit the file to ensure the following lines are present and have thecorrect information:

  1. database.tests.dbdriver = 'MySQLi';
  2. database.tests.username = 'root';
  3. database.tests.password = '';
  4. database.tests.database = '';

Migrations and Seeds

When running tests, you need to ensure that your database has the correct schema set up and thatit is in a known state for every test. You can use migrations and seeds to set up your database,by adding a couple of class properties to your test.

  1. <?php namespace App\Database;
  2.  
  3. use CodeIgniter\Test\CIDatabaseTestCase;
  4.  
  5. class MyTests extends\CIDatabaseTestCase
  6. {
  7. protected $refresh = true;
  8. protected $seed = 'TestSeeder';
  9. protected $basePath = 'path/to/database/files';
  10. }

$refresh

This boolean value determines whether the database is completely refreshed before every test. If true,all migrations are rolled back to version 0, then the database is migrated to the latest available migration.

$seed

If present and not empty, this specifies the name of a Seed file that is used to populate the database withtest data prior to every test running.

$basePath

By default, CodeIgniter will look in tests/_support/Database/Seeds to locate the seeds that it should run during testing.You can change this directores by specifying the $basePath property. This should not include the seeds directory,but the path to the single directory that holds the sub-directory.

$namespace

By default, CodeIgniter will look in tests/_support/DatabaseTestMigrations/Database/Migrations to locate the migrationsthat it should run during testing. You can change this location by specifying a new namespace in the $namespace properties.This should not include the Database/Migrations path, just the base namespace.

Helper Methods

The CIDatabaseTestCase class provides several helper methods to aid in testing your database.

seed($name)

Allows you to manually load a Seed into the database. The only parameter is the name of the seed to run. The seedmust be present within the path specified in $basePath.

dontSeeInDatabase($table, $criteria)

Asserts that a row with criteria matching the key/value pairs in $criteria DOES NOT exist in the database.

  1. $criteria = [
  2. 'email' => 'joe@example.com',
  3. 'active' => 1
  4. ];
  5. $this->dontSeeInDatabase('users', $criteria);

seeInDatabase($table, $criteria)

Asserts that a row with criteria matching the key/value pairs in $criteria DOES exist in the database.

  1. $criteria = [
  2. 'email' => 'joe@example.com',
  3. 'active' => 1
  4. ];
  5. $this->seeInDatabase('users', $criteria);

grabFromDatabase($table, $column, $criteria)

Returns the value of $column from the specified table where the row matches $criteria. If more than onerow is found, it will only test against the first one.

  1. $username = $this->grabFromDatabase('users', 'username', ['email' => 'joe@example.com']);

hasInDatabase($table, $data)

Inserts a new row into the database. This row is removed after the current test runs. $data is an associativearray with the data to insert into the table.

  1. $data = [
  2. 'email' => 'joe@example.com',
  3. 'name' => 'Joe Cool'
  4. ];
  5. $this->hasInDatabase('users', $data);

seeNumRecords($expected, $table, $criteria)

Asserts that a number of matching rows are found in the database that match $criteria.

  1. $criteria = [
  2. 'active' => 1
  3. ];
  4. $this->seeNumRecords(2, 'users', $criteria);