-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3993 from kenjis/feat-database-testing-setup
feat: make migration/seed settings flexible on database testing
- Loading branch information
Showing
4 changed files
with
246 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/system/Database/DatabaseTestCase/DatabaseTestCaseMigrationOnce1Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php namespace CodeIgniter\Database\DatabaseTestCase; | ||
|
||
use CodeIgniter\Test\CIDatabaseTestCase; | ||
use Config\Database; | ||
use Config\Services; | ||
|
||
/** | ||
* DatabaseTestCaseMigrationOnce1Test and DatabaseTestCaseMigrationOnce2Test | ||
* show $migrateOnce applies per test case file. | ||
* | ||
* @group DatabaseLive | ||
*/ | ||
class DatabaseTestCaseMigrationOnce1Test extends CIDatabaseTestCase | ||
{ | ||
/** | ||
* Should run db migration only once? | ||
* | ||
* @var boolean | ||
*/ | ||
protected $migrateOnce = true; | ||
|
||
/** | ||
* Should the db be refreshed before test? | ||
* | ||
* @var boolean | ||
*/ | ||
protected $refresh = true; | ||
|
||
/** | ||
* The namespace(s) to help us find the migration classes. | ||
* Empty is equivalent to running `spark migrate -all`. | ||
* Note that running "all" runs migrations in date order, | ||
* but specifying namespaces runs them in namespace order (then date) | ||
* | ||
* @var string|array|null | ||
*/ | ||
protected $namespace = [ | ||
'Tests\Support\MigrationTestMigrations', | ||
]; | ||
|
||
public function setUp(): void | ||
{ | ||
Services::autoloader()->addNamespace('Tests\Support\MigrationTestMigrations', SUPPORTPATH . 'MigrationTestMigrations'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testMigrationDone() | ||
{ | ||
$this->seeInDatabase('foo', ['key' => 'foobar']); | ||
|
||
// Drop table to make sure there is no foo table when | ||
// DatabaseTestCaseMigrationOnce2Test runs | ||
$this->dropTableFoo(); | ||
} | ||
|
||
private function dropTableFoo() | ||
{ | ||
$forge = Database::forge(); | ||
$forge->dropTable('foo', true); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
tests/system/Database/DatabaseTestCase/DatabaseTestCaseMigrationOnce2Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php namespace CodeIgniter\Database\DatabaseTestCase; | ||
|
||
use CodeIgniter\Test\CIDatabaseTestCase; | ||
use Config\Services; | ||
|
||
/** | ||
* DatabaseTestCaseMigrationOnce1Test and DatabaseTestCaseMigrationOnce2Test | ||
* show $migrateOnce applies per test case file. | ||
* | ||
* @group DatabaseLive | ||
*/ | ||
class DatabaseTestCaseMigrationOnce2Test extends CIDatabaseTestCase | ||
{ | ||
/** | ||
* Should run db migration only once? | ||
* | ||
* @var boolean | ||
*/ | ||
protected $migrateOnce = true; | ||
|
||
/** | ||
* Should the db be refreshed before test? | ||
* | ||
* @var boolean | ||
*/ | ||
protected $refresh = true; | ||
|
||
/** | ||
* The namespace(s) to help us find the migration classes. | ||
* Empty is equivalent to running `spark migrate -all`. | ||
* Note that running "all" runs migrations in date order, | ||
* but specifying namespaces runs them in namespace order (then date) | ||
* | ||
* @var string|array|null | ||
*/ | ||
protected $namespace = [ | ||
'Tests\Support\MigrationTestMigrations', | ||
]; | ||
|
||
public function setUp(): void | ||
{ | ||
Services::autoloader()->addNamespace('Tests\Support\MigrationTestMigrations', SUPPORTPATH . 'MigrationTestMigrations'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testMigrationDone() | ||
{ | ||
$this->seeInDatabase('foo', ['key' => 'foobar']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters