diff --git a/docs/en/index.rst b/docs/en/index.rst index 97931d0f..1d6595cd 100644 --- a/docs/en/index.rst +++ b/docs/en/index.rst @@ -846,11 +846,13 @@ beginning:: // in tests/bootstrap.php use Migrations\TestSuite\Migrator; + $migrator = new Migrator(); + // Simple setup for with no plugins - Migrator::migrate(); + $migrator->run(); // Run migrations for multiple plugins - Migrator::migrate([ + $migrator->run([ ['plugin' => 'Contacts'], // Run the Documents migrations on the test_docs connection. ['plugin' => 'Documents', 'connection' => 'test_docs'], diff --git a/src/TestSuite/Migrator.php b/src/TestSuite/Migrator.php index dd70a8fa..712e43a5 100644 --- a/src/TestSuite/Migrator.php +++ b/src/TestSuite/Migrator.php @@ -14,37 +14,65 @@ namespace Migrations\TestSuite; use Cake\Console\ConsoleIo; +use Cake\Core\InstanceConfigTrait; use Cake\Datasource\ConnectionManager; use Cake\TestSuite\Fixture\SchemaCleaner; -use Cake\TestSuite\Fixture\SchemaLoader; use Migrations\Migrations; -class Migrator extends SchemaLoader +class Migrator { + use InstanceConfigTrait; + + /** + * @var \Cake\Console\ConsoleIo + */ + protected $io; + + /** + * @var \Cake\TestSuite\Fixture\SchemaCleaner + */ + protected $schemaCleaner; + + /** + * @var array + */ + protected $_defaultConfig = [ + 'outputLevel' => ConsoleIo::QUIET, + ]; + + /** + * Constructor. + * + * @param array $config Config settings + */ + public function __construct(array $config = []) + { + $this->setConfig($config); + + $this->io = new ConsoleIo(); + $this->io->level($this->getConfig('outputLevel')); + + $this->schemaCleaner = new SchemaCleaner($this->io); + } + /** * General command to run before your tests run * E.g. in tests/bootstrap.php * * @param array $config Configuration data - * @param bool $verbose Set to true to display verbose output - * @return self + * @return void */ - public static function migrate(array $config = [], $verbose = false): Migrator + public function run(array $config = []): void { - $migrator = new self([ - 'outputLevel' => $verbose ? ConsoleIo::VERBOSE : ConsoleIo::QUIET, - ]); // Don't recreate schema if we are in a phpunit separate process test. if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) { - return $migrator; + return; } $configReader = new ConfigReader(); $configReader->readMigrationsInDatasources(); $configReader->readConfig($config); - $migrator->handleMigrationsStatus($configReader->getConfig()); - - return $migrator; + $this->handleMigrationsStatus($configReader->getConfig()); } /** @@ -94,9 +122,8 @@ protected function handleMigrationsStatus(array $configs) return $this; } - $schemaCleaner = new SchemaCleaner($this->io); foreach ($connectionsToDrop as $connectionName) { - $schemaCleaner->dropTables($connectionName); + $this->schemaCleaner->dropTables($connectionName); } foreach ($configs as $migration) { @@ -108,7 +135,7 @@ protected function handleMigrationsStatus(array $configs) $schema = ConnectionManager::get($connectionName)->getSchemaCollection(); $allTables = $schema->listTables(); $tablesToTruncate = $this->unsetMigrationTables($allTables); - $schemaCleaner->truncateTables($connectionName, $tablesToTruncate); + $this->schemaCleaner->truncateTables($connectionName, $tablesToTruncate); } return $this; diff --git a/tests/TestCase/TestSuite/MigratorTest.php b/tests/TestCase/TestSuite/MigratorTest.php index fa9dc8a0..7e75bc58 100644 --- a/tests/TestCase/TestSuite/MigratorTest.php +++ b/tests/TestCase/TestSuite/MigratorTest.php @@ -49,7 +49,7 @@ private function fetchMigrationsInDB(string $dbTable): array public function testMigrate(): void { - Migrator::migrate(); + (new Migrator())->run(); $appMigrations = $this->fetchMigrationsInDB('phinxlog'); $fooPluginMigrations = $this->fetchMigrationsInDB('foo_plugin_phinxlog'); @@ -65,7 +65,8 @@ public function testMigrate(): void public function testDropTablesForMissingMigrations(): void { - Migrator::migrate(); + $migrator = new Migrator(); + $migrator->run(); $connection = ConnectionManager::get('test'); $connection->insert('phinxlog', ['version' => 1, 'migration_name' => 'foo',]); @@ -73,7 +74,7 @@ public function testDropTablesForMissingMigrations(): void $count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count(); $this->assertSame(2, $count); - Migrator::migrate(); + $migrator->run(); $count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count(); $this->assertSame(1, $count); }