CodeIgniter v4.5 API

DatabaseTestTrait

DatabaseTestTrait

Provides functionality for refreshing/seeding the database during testing.

Tags
mixin

CIUnitTestCase

Table of Contents

Properties

$doneMigration  : bool
Is db migration done once or more than once?
$doneSeed  : bool
Is seeding done once or more than once?

Methods

dontSeeInDatabase()  : void
Asserts that records that match the conditions in $where do not exist in the database.
grabFromDatabase()  : bool
Fetches a single column from a database row with criteria matching $where.
hasInDatabase()  : bool
Inserts a row into to the database. This row will be removed after the test has run.
loadBuilder()  : BaseBuilder
Loads the Builder class appropriate for the current database.
loadDependencies()  : void
Load any database test dependencies.
resetMigrationSeedCount()  : void
Reset $doneMigration and $doneSeed
seed()  : void
Seeds that database with a specific seeder.
seeInDatabase()  : void
Asserts that records that match the conditions in $where DO exist in the database.
seeNumRecords()  : void
Asserts that the number of rows in the database that match $where is equal to $expected.
clearInsertCache()  : void
Removes any rows inserted via $this->hasInDatabase()
disableDBDebug()  : void
Sets $DBDebug to false.
enableDBDebug()  : void
Sets $DBDebug to true.
migrateDatabase()  : void
Run migrations as defined by the class
regressDatabase()  : void
Regress migrations as defined by the class
runSeeds()  : void
Run seeds as defined by the class
setUpDatabase()  : void
Runs the trait set up methods.
setUpMigrate()  : void
Migrate on setUp
setUpSeed()  : void
Seed on setUp
tearDownDatabase()  : void
Runs the trait set up methods.

Properties

$doneMigration

Is db migration done once or more than once?

private static bool $doneMigration = false

$doneSeed

Is seeding done once or more than once?

private static bool $doneSeed = false

Methods

dontSeeInDatabase()

Asserts that records that match the conditions in $where do not exist in the database.

public dontSeeInDatabase(string $table, array<string, mixed> $where) : void
Parameters
$table : string
$where : array<string, mixed>

grabFromDatabase()

Fetches a single column from a database row with criteria matching $where.

public grabFromDatabase(string $table, string $column, array<string, mixed> $where) : bool
Parameters
$table : string
$column : string
$where : array<string, mixed>
Tags
throws
DatabaseException
Return values
bool

hasInDatabase()

Inserts a row into to the database. This row will be removed after the test has run.

public hasInDatabase(string $table, array<string, mixed> $data) : bool
Parameters
$table : string
$data : array<string, mixed>
Return values
bool

loadBuilder()

Loads the Builder class appropriate for the current database.

public loadBuilder(string $tableName) : BaseBuilder
Parameters
$tableName : string
Return values
BaseBuilder

loadDependencies()

Load any database test dependencies.

public loadDependencies() : void

resetMigrationSeedCount()

Reset $doneMigration and $doneSeed

public static resetMigrationSeedCount() : void
Attributes
#[AfterClass]

seed()

Seeds that database with a specific seeder.

public seed(string $name) : void
Parameters
$name : string

seeInDatabase()

Asserts that records that match the conditions in $where DO exist in the database.

public seeInDatabase(string $table, array<string, mixed> $where) : void
Parameters
$table : string
$where : array<string, mixed>
Tags
throws
DatabaseException

seeNumRecords()

Asserts that the number of rows in the database that match $where is equal to $expected.

public seeNumRecords(int $expected, string $table, array<string, mixed> $where) : void
Parameters
$expected : int
$table : string
$where : array<string, mixed>
Tags
throws
DatabaseException

clearInsertCache()

Removes any rows inserted via $this->hasInDatabase()

protected clearInsertCache() : void

disableDBDebug()

Sets $DBDebug to false.

protected disableDBDebug() : void

WARNING: this value will persist! take care to roll it back.

migrateDatabase()

Run migrations as defined by the class

protected migrateDatabase() : void

regressDatabase()

Regress migrations as defined by the class

protected regressDatabase() : void

setUpDatabase()

Runs the trait set up methods.

protected setUpDatabase() : void

tearDownDatabase()

Runs the trait set up methods.

protected tearDownDatabase() : void

        
On this page

Search results