Skip to content

Commit

Permalink
Merge pull request #3221 from MGatner/tests-refresh
Browse files Browse the repository at this point in the history
DatabaseTestCase migrations
  • Loading branch information
lonnieezell authored Jul 5, 2020
2 parents dbdafec + 5ded6d7 commit 76816f2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
79 changes: 52 additions & 27 deletions system/Test/CIDatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,38 +167,14 @@ protected function setUp(): void

if ($this->refresh === true)
{
// If no namespace was specified then rollback/migrate all
if (empty($this->namespace))
{
$this->migrations->setNamespace(null);

$this->migrations->regress(0, 'tests');

$this->migrations->latest('tests');
}

// Run migrations for each specified namespace
else
{
$namespaces = is_array($this->namespace) ? $this->namespace : [$this->namespace];

foreach ($namespaces as $namespace)
{
$this->migrations->setNamespace($namespace);
$this->migrations->regress(0, 'tests');
}

foreach ($namespaces as $namespace)
{
$this->migrations->setNamespace($namespace);
$this->migrations->latest('tests');
}
}
$this->regressDatabase();

// Reset counts on faked items
Fabricator::resetCounts();
}

$this->migrateDatabase();

if (! empty($this->seed))
{
if (! empty($this->basePath))
Expand Down Expand Up @@ -237,6 +213,55 @@ protected function tearDown(): void

//--------------------------------------------------------------------

/**
* Regress migrations as defined by the class
*/
protected function regressDatabase()
{
// If no namespace was specified then rollback all
if (empty($this->namespace))
{
$this->migrations->setNamespace(null);
$this->migrations->regress(0, 'tests');
}

// Regress each specified namespace
else
{
$namespaces = is_array($this->namespace) ? $this->namespace : [$this->namespace];

foreach ($namespaces as $namespace)
{
$this->migrations->setNamespace($namespace);
$this->migrations->regress(0, 'tests');
}
}
}

/**
* Run migrations as defined by the class
*/
protected function migrateDatabase()
{
// If no namespace was specified then migrate all
if (empty($this->namespace))
{
$this->migrations->setNamespace(null);
$this->migrations->latest('tests');
}
// Run migrations for each specified namespace
else
{
$namespaces = is_array($this->namespace) ? $this->namespace : [$this->namespace];

foreach ($namespaces as $namespace)
{
$this->migrations->setNamespace($namespace);
$this->migrations->latest('tests');
}
}
}

/**
* Seeds that database with a specific seeder.
*
Expand Down
14 changes: 12 additions & 2 deletions user_guide_src/source/testing/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ by adding a couple of class properties to your test.
**$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.
all migrations are rolled back to version 0. The database is always migrated to the latest available
state as defined by ``$namespace``.

**$seed**

Expand All @@ -100,15 +101,24 @@ 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 migrations
By default, CodeIgniter will look in **tests/_support/Database/Migrations** to locate the migrations
that 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.
To run migrations from all available namespaces set this property to ``null``.

Helper Methods
==============

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

**regressDatabase()**

Called during ``$refresh`` described above, this method is available if you need to reset the database manually.

**migrateDatabase()**

Called during ``setUp``, this method is available if you need to run migrations manually.

**seed($name)**

Allows you to manually load a Seed into the database. The only parameter is the name of the seed to run. The seed
Expand Down

0 comments on commit 76816f2

Please sign in to comment.