Skip to content

Commit

Permalink
feat: on database testing, you can disable migration/seed and run the…
Browse files Browse the repository at this point in the history
…m only once
  • Loading branch information
kenjis committed Dec 16, 2020
1 parent af0e087 commit 68eab49
Showing 1 changed file with 93 additions and 17 deletions.
110 changes: 93 additions & 17 deletions system/Test/CIDatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@
*/
abstract class CIDatabaseTestCase extends CIUnitTestCase
{
/**
* Should run db migration?
*
* @var boolean
*/
protected $migrate = true;

/**
* Should run db migration only once?
*
* @var boolean
*/
protected $migrateOnce = false;

/**
* Is db migration done once or more than once?
*
* @var boolean
*/
private static $doneMigration = false;

/**
* Should run seeding only once?
*
* @var boolean
*/
protected $seedOnce = false;

/**
* Is seeding done once or more than once?
*
* @var boolean
*/
private static $doneSeed = false;

/**
* Should the db be refreshed before
* each test?
Expand Down Expand Up @@ -139,28 +174,22 @@ protected function setUp(): void

$this->loadDependencies();

if ($this->refresh === true)
if ($this->migrateOnce === false || self::$doneMigration === false)
{
$this->regressDatabase();
if ($this->refresh === true)
{
$this->regressDatabase();

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

$this->migrateDatabase();
$this->migrateDatabase();
}

if (! empty($this->seed))
if ($this->seedOnce === false || self::$doneSeed === false)
{
if (! empty($this->basePath))
{
$this->seeder->setPath(rtrim($this->basePath, '/') . '/Seeds');
}

$seeds = is_array($this->seed) ? $this->seed : [$this->seed];
foreach ($seeds as $seed)
{
$this->seed($seed);
}
$this->runSeeds();
}
}

Expand All @@ -187,11 +216,40 @@ protected function tearDown(): void

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

/**
* Run seeds as defined by the class
*/
protected function runSeeds()
{
if (! empty($this->seed))
{
if (! empty($this->basePath))
{
$this->seeder->setPath(rtrim($this->basePath, '/') . '/Seeds');
}

$seeds = is_array($this->seed) ? $this->seed : [$this->seed];
foreach ($seeds as $seed)
{
$this->seed($seed);
}
}

self::$doneSeed = true;
}

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

/**
* Regress migrations as defined by the class
*/
protected function regressDatabase()
{
if ($this->migrate === false)
{
return;
}

// If no namespace was specified then rollback all
if (empty($this->namespace))
{
Expand All @@ -217,11 +275,17 @@ protected function regressDatabase()
*/
protected function migrateDatabase()
{
if ($this->migrate === false)
{
return;
}

// If no namespace was specified then migrate all
if (empty($this->namespace))
{
$this->migrations->setNamespace(null);
$this->migrations->latest('tests');
self::$doneMigration = true;
}
// Run migrations for each specified namespace
else
Expand All @@ -232,6 +296,7 @@ protected function migrateDatabase()
{
$this->migrations->setNamespace($namespace);
$this->migrations->latest('tests');
self::$doneMigration = true;
}
}
}
Expand Down Expand Up @@ -361,4 +426,15 @@ public function seeNumRecords(int $expected, string $table, array $where)
}

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

/**
* Reset $doneMigration and $doneSeed
*
* @afterClass
*/
public static function resetMigrationSeedCount()
{
self::$doneMigration = false;
self::$doneSeed = false;
}
}

0 comments on commit 68eab49

Please sign in to comment.