From 5ded6d764f0ea0621a6660c5b1c61e40ba8b9e00 Mon Sep 17 00:00:00 2001 From: MGatner Date: Sat, 4 Jul 2020 15:04:15 +0000 Subject: [PATCH] Run migrations by default; expose migration methods --- system/Test/CIDatabaseTestCase.php | 79 ++++++++++++++-------- user_guide_src/source/testing/database.rst | 14 +++- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/system/Test/CIDatabaseTestCase.php b/system/Test/CIDatabaseTestCase.php index bf4b03561aae..3d11ce3b72fa 100644 --- a/system/Test/CIDatabaseTestCase.php +++ b/system/Test/CIDatabaseTestCase.php @@ -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)) @@ -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. * diff --git a/user_guide_src/source/testing/database.rst b/user_guide_src/source/testing/database.rst index 071b509fdc26..df697bd134ce 100644 --- a/user_guide_src/source/testing/database.rst +++ b/user_guide_src/source/testing/database.rst @@ -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** @@ -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